]> code.delx.au - gnu-emacs/blob - lisp/erc/erc-dcc.el
Add 2009 to copyright years.
[gnu-emacs] / lisp / erc / erc-dcc.el
1 ;;; erc-dcc.el --- CTCP DCC module for ERC
2
3 ;; Copyright (C) 1993, 1994, 1995, 1998, 2002, 2003, 2004, 2006, 2007, 2008, 2009
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Ben A. Mesander <ben@gnu.ai.mit.edu>
7 ;; Noah Friedman <friedman@prep.ai.mit.edu>
8 ;; Per Persson <pp@sno.pp.se>
9 ;; Maintainer: mlang@delysid.org
10 ;; Keywords: comm, processes
11 ;; Created: 1994-01-23
12
13 ;; This file is part of GNU Emacs.
14
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
19
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
24
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27
28 ;;; Commentary:
29
30 ;; This file provides Direct Client-to-Client support for ERC.
31 ;;
32 ;; The original code was taken from zenirc-dcc.el, heavily mangled and
33 ;; rewritten to support the way how ERC operates. Server socket support
34 ;; was added for DCC CHAT and SEND afterwards. Thanks
35 ;; to the original authors for their work.
36
37 ;;; Usage:
38
39 ;; To use this file, put
40 ;; (require 'erc-dcc)
41 ;; in your .emacs.
42 ;;
43 ;; Provided commands
44 ;; /dcc chat nick - Either accept pending chat offer from nick, or offer
45 ;; DCC chat to nick
46 ;; /dcc close type [nick] - Close DCC connection (SEND/GET/CHAT) with nick
47 ;; /dcc get nick [file] - Accept DCC offer from nick
48 ;; /dcc list - List all DCC offers/connections
49 ;; /dcc send nick file - Offer DCC SEND to nick
50 ;;
51 ;; Please note that offering DCC connections (offering chats and sending
52 ;; files) is only supported with Emacs 22.
53
54 ;;; Code:
55
56 (require 'erc)
57 (eval-when-compile
58 (require 'cl)
59 (require 'pcomplete))
60
61 ;;;###autoload (autoload 'erc-dcc-mode "erc-dcc")
62 (define-erc-module dcc nil
63 "Provide Direct Client-to-Client support for ERC."
64 ((add-hook 'erc-server-401-functions 'erc-dcc-no-such-nick))
65 ((remove-hook 'erc-server-401-functions 'erc-dcc-no-such-nick)))
66
67 (defgroup erc-dcc nil
68 "DCC stands for Direct Client Communication, where you and your
69 friend's client programs connect directly to each other,
70 bypassing IRC servers and their occasional \"lag\" or \"split\"
71 problems. Like /MSG, the DCC chat is completely private.
72
73 Using DCC get and send, you can transfer files directly from and to other
74 IRC users."
75 :group 'erc)
76
77 (defcustom erc-dcc-verbose nil
78 "*If non-nil, be verbose about DCC activity reporting."
79 :group 'erc-dcc
80 :type 'boolean)
81
82 (defconst erc-dcc-connection-types
83 '("CHAT" "GET" "SEND")
84 "List of valid DCC connection types.
85 All values of the list must be uppercase strings.")
86
87 (defvar erc-dcc-list nil
88 "List of DCC connections. Looks like:
89 ((:nick \"nick!user@host\" :type GET :peer proc :parent proc :size size :file file)
90 (:nick \"nick!user@host\" :type CHAT :peer proc :parent proc)
91 (:nick \"nick\" :type SEND :peer server-proc :parent parent-proc :file
92 file :sent <marker> :confirmed <marker>))
93
94 :nick - a user or userhost for the peer. combine with :parent to reach them
95
96 :type - the type of DCC connection - SEND for outgoing files, GET for
97 incoming, and CHAT for both directions. To tell which end started
98 the DCC chat, look at :peer
99
100 :peer - the other end of the DCC connection. In the case of outgoing DCCs,
101 this represents a server process until a connection is established
102
103 :parent - the server process where the dcc connection was established.
104 Note that this can be nil or an invalid process since a DCC
105 connection is in general independent from a particular server
106 connection after it was established.
107
108 :file - for outgoing sends, the full path to the file. for incoming sends,
109 the suggested filename or vetted filename
110
111 :size - size of the file, may be nil on incoming DCCs")
112
113 (defun erc-dcc-list-add (type nick peer parent &rest args)
114 "Add a new entry of type TYPE to `erc-dcc-list' and return it."
115 (car
116 (setq erc-dcc-list
117 (cons
118 (append (list :nick nick :type type :peer peer :parent parent) args)
119 erc-dcc-list))))
120
121 ;; This function takes all the usual args as open-network-stream, plus one
122 ;; more: the entry data from erc-dcc-list for this particular process.
123 (defvar erc-dcc-connect-function 'erc-dcc-open-network-stream)
124
125 (defun erc-dcc-open-network-stream (procname buffer addr port entry)
126 (if nil; (fboundp 'open-network-stream-nowait) ;; this currently crashes
127 ;; cvs emacs
128 (open-network-stream-nowait procname buffer addr port)
129 (open-network-stream procname buffer addr port)))
130
131 (erc-define-catalog
132 'english
133 '((dcc-chat-discarded
134 . "DCC: previous chat request from %n (%u@%h) discarded")
135 (dcc-chat-ended . "DCC: chat with %n ended %t: %e")
136 (dcc-chat-no-request . "DCC: chat request from %n not found")
137 (dcc-chat-offered . "DCC: chat offered by %n (%u@%h:%p)")
138 (dcc-chat-offer . "DCC: offering chat to %n")
139 (dcc-chat-accept . "DCC: accepting chat from %n")
140 (dcc-chat-privmsg . "=%n= %m")
141 (dcc-closed . "DCC: Closed %T from %n")
142 (dcc-command-undefined
143 . "DCC: %c undefined subcommand. GET, CHAT and LIST are defined.")
144 (dcc-ctcp-errmsg . "DCC: `%s' is not a DCC subcommand known to this client")
145 (dcc-ctcp-unknown . "DCC: unknown dcc command `%q' from %n (%u@%h)")
146 (dcc-get-bytes-received . "DCC: %f: %b bytes received")
147 (dcc-get-complete
148 . "DCC: file %f transfer complete (%s bytes in %t seconds)")
149 (dcc-get-cmd-aborted . "DCC: Aborted getting %f from %n")
150 (dcc-get-file-too-long
151 . "DCC: %f: File longer than sender claimed; aborting transfer")
152 (dcc-get-notfound . "DCC: %n hasn't offered %f for DCC transfer")
153 (dcc-list-head . "DCC: From Type Active Size Filename")
154 (dcc-list-line . "DCC: -------- ---- ------ -------------- --------")
155 (dcc-list-item . "DCC: %-8n %-4t %-6a %-14s %f")
156 (dcc-list-end . "DCC: End of list.")
157 (dcc-malformed . "DCC: error: %n (%u@%h) sent malformed request: %q")
158 (dcc-privileged-port
159 . "DCC: possibly bogus request: %p is a privileged port.")
160 (dcc-request-bogus . "DCC: bogus dcc `%r' from %n (%u@%h)")
161 (dcc-send-finished . "DCC: SEND of %f to %n finished (size %s)")
162 (dcc-send-offered . "DCC: file %f offered by %n (%u@%h) (size %s)")
163 (dcc-send-offer . "DCC: offering %f to %n")))
164
165 ;;; Misc macros and utility functions
166
167 (defun erc-dcc-member (&rest args)
168 "Return the first matching entry in `erc-dcc-list' which satisfies the
169 constraints given as a plist in ARGS. Returns nil on no match.
170
171 The property :nick is treated specially, if it contains a '!' character,
172 it is treated as a nick!user@host string, and compared with the :nick property
173 value of the individual elements using string-equal. Otherwise it is
174 compared with `erc-nick-equal-p' which is IRC case-insensitive."
175 (let ((list erc-dcc-list)
176 result test)
177 ;; for each element in erc-dcc-list
178 (while (and list (not result))
179 (let ((elt (car list))
180 (prem args)
181 (cont t))
182 ;; loop through the constraints
183 (while (and prem cont)
184 (let ((prop (car prem))
185 (val (cadr prem)))
186 (setq prem (cddr prem)
187 ;; plist-member is a predicate in xemacs
188 test (and (plist-member elt prop)
189 (plist-get elt prop)))
190 ;; if the property exists and is equal, we continue, else, try the
191 ;; next element of the list
192 (or (and (eq prop :nick) (string-match "!" val)
193 test (string-equal test val))
194 (and (eq prop :nick)
195 test val
196 (erc-nick-equal-p
197 (erc-extract-nick test)
198 (erc-extract-nick val)))
199 ;; not a nick
200 (eq test val)
201 (setq cont nil))))
202 (if cont
203 (setq result elt)
204 (setq list (cdr list)))))
205 result))
206
207 (defun erc-pack-int (value)
208 "Convert an integer into a packed string in network byte order,
209 which is big-endian."
210 ;; make sure value is not negative
211 (when (< value 0)
212 (error "ERC-DCC (erc-pack-int): packet size is negative"))
213 ;; make sure size is not larger than 4 bytes
214 (let ((len (if (= value 0) 0
215 (ceiling (/ (ceiling (/ (log value) (log 2))) 8.0)))))
216 (when (> len 4)
217 (error "ERC-DCC (erc-pack-int): packet too large")))
218 ;; pack
219 (let ((str (make-string 4 0))
220 (i 3))
221 (while (and (>= i 0) (> value 0))
222 (aset str i (% value 256))
223 (setq value (/ value 256))
224 (setq i (1- i)))
225 str))
226
227 (defconst erc-most-positive-int-bytes
228 (ceiling (/ (ceiling (/ (log most-positive-fixnum) (log 2))) 8.0))
229 "Maximum number of bytes for a fixnum.")
230
231 (defconst erc-most-positive-int-msb
232 (lsh most-positive-fixnum (- 0 (* 8 (1- erc-most-positive-int-bytes))))
233 "Content of the most significant byte of most-positive-fixnum.")
234
235 (defun erc-unpack-int (str)
236 "Unpack a packed string into an integer."
237 (let ((len (length str)))
238 ;; strip leading 0-bytes
239 (let ((start 0))
240 (while (and (> len start) (eq (aref str start) 0))
241 (setq start (1+ start)))
242 (when (> start 0)
243 (setq str (substring str start))
244 (setq len (- len start))))
245 ;; make sure size is not larger than Emacs can handle
246 (when (or (> len (min 4 erc-most-positive-int-bytes))
247 (and (eq len erc-most-positive-int-bytes)
248 (> (aref str 0) erc-most-positive-int-msb)))
249 (error "ERC-DCC (erc-unpack-int): packet to send is too large"))
250 ;; unpack
251 (let ((num 0)
252 (count 0))
253 (while (< count len)
254 (setq num (+ num (lsh (aref str (- len count 1)) (* 8 count))))
255 (setq count (1+ count)))
256 num)))
257
258 (defconst erc-dcc-ipv4-regexp
259 (concat "^"
260 (mapconcat #'identity (make-list 4 "\\([0-9]\\{1,3\\}\\)") "\\.")
261 "$"))
262
263 (defun erc-ip-to-decimal (ip)
264 "Convert IP address to its decimal representation.
265 Argument IP is the address as a string. The result is also a string."
266 (interactive "sIP Address: ")
267 (if (not (string-match erc-dcc-ipv4-regexp ip))
268 (error "Not an IP address")
269 (let* ((ips (mapcar
270 (lambda (str)
271 (let ((n (string-to-number str)))
272 (if (and (>= n 0) (< n 256))
273 n
274 (error "%d out of range" n))))
275 (split-string ip "\\.")))
276 (res (+ (* (car ips) 16777216.0)
277 (* (nth 1 ips) 65536.0)
278 (* (nth 2 ips) 256.0)
279 (nth 3 ips))))
280 (if (interactive-p)
281 (message "%s is %.0f" ip res)
282 (format "%.0f" res)))))
283
284 (defun erc-decimal-to-ip (dec)
285 "Convert a decimal representation DEC to an IP address.
286 The result is also a string."
287 (when (stringp dec)
288 (setq dec (string-to-number (concat dec ".0"))))
289 (let* ((first (floor (/ dec 16777216.0)))
290 (first-rest (- dec (* first 16777216.0)))
291 (second (floor (/ first-rest 65536.0)))
292 (second-rest (- first-rest (* second 65536.0)))
293 (third (floor (/ second-rest 256.0)))
294 (third-rest (- second-rest (* third 256.0)))
295 (fourth (floor third-rest)))
296 (format "%s.%s.%s.%s" first second third fourth)))
297
298 ;;; Server code
299
300 (defcustom erc-dcc-listen-host nil
301 "IP address to listen on when offering files.
302 Should be set to a string or nil. If nil, automatic detection of
303 the host interface to use will be attempted."
304 :group 'erc-dcc
305 :type (list 'choice (list 'const :tag "Auto-detect" nil)
306 (list 'string :tag "IP-address"
307 :valid-regexp erc-dcc-ipv4-regexp)))
308
309 (defcustom erc-dcc-public-host nil
310 "IP address to use for outgoing DCC offers.
311 Should be set to a string or nil. If nil, use the value of
312 `erc-dcc-listen-host'."
313 :group 'erc-dcc
314 :type (list 'choice (list 'const :tag "Same as erc-dcc-listen-host" nil)
315 (list 'string :tag "IP-address"
316 :valid-regexp erc-dcc-ipv4-regexp)))
317
318 (defcustom erc-dcc-send-request 'ask
319 "*How to treat incoming DCC Send requests.
320 'ask - Report the Send request, and wait for the user to manually accept it
321 You might want to set `erc-dcc-auto-masks' for this.
322 'auto - Automatically accept the request and begin downloading the file
323 'ignore - Ignore incoming DCC Send requests completely."
324 :group 'erc-dcc
325 :type '(choice (const ask) (const auto) (const ignore)))
326
327 (defun erc-dcc-get-host (proc)
328 "Returns the local IP address used for an open PROCess."
329 (format-network-address (process-contact proc :local) t))
330
331 (defun erc-dcc-host ()
332 "Determine the IP address we are using.
333 If variable `erc-dcc-host' is non-nil, use it. Otherwise call
334 `erc-dcc-get-host' on the erc-server-process."
335 (or erc-dcc-listen-host (erc-dcc-get-host erc-server-process)
336 (error "Unable to determine local address")))
337
338 (defcustom erc-dcc-port-range nil
339 "If nil, any available user port is used for outgoing DCC connections.
340 If set to a cons, it specifies a range of ports to use in the form (min . max)"
341 :group 'erc-dcc
342 :type '(choice
343 (const :tag "Any port" nil)
344 (cons :tag "Port range"
345 (integer :tag "Lower port")
346 (integer :tag "Upper port"))))
347
348 (defcustom erc-dcc-auto-masks nil
349 "List of regexps matching user identifiers whose DCC send offers should be
350 accepted automatically. A user identifier has the form \"nick!login@host\".
351 For instance, to accept all incoming DCC send offers automatically, add the
352 string \".*!.*@.*\" to this list."
353 :group 'erc-dcc
354 :type '(repeat regexp))
355
356 (defun erc-dcc-server (name filter sentinel)
357 "Start listening on a port for an incoming DCC connection. Returns the newly
358 created subprocess, or nil."
359 (let ((port (or (and erc-dcc-port-range (car erc-dcc-port-range)) t))
360 (upper (and erc-dcc-port-range (cdr erc-dcc-port-range)))
361 process)
362 (while (not process)
363 (condition-case err
364 (progn
365 (setq process
366 (make-network-process :name name
367 :buffer nil
368 :host (erc-dcc-host)
369 :service port
370 :nowait t
371 :noquery nil
372 :filter filter
373 :sentinel sentinel
374 :log #'erc-dcc-server-accept
375 :server t))
376 (when (processp process)
377 (when (fboundp 'set-process-coding-system)
378 (set-process-coding-system process 'binary 'binary))
379 (when (fboundp 'set-process-filter-multibyte)
380 (set-process-filter-multibyte process nil))))
381 (file-error
382 (unless (and (string= "Cannot bind server socket" (cadr err))
383 (string= "address already in use" (caddr err)))
384 (signal (car err) (cdr err)))
385 (setq port (1+ port))
386 (unless (< port upper)
387 (error "No available ports in erc-dcc-port-range")))))
388 process))
389
390 (defun erc-dcc-server-accept (server client message)
391 "Log an accepted DCC offer, then terminate the listening process and set up
392 the accepted connection."
393 (erc-log (format "(erc-dcc-server-accept): server %s client %s message %s"
394 server client message))
395 (when (and (string-match "^accept from " message)
396 (processp server) (processp client))
397 (let ((elt (erc-dcc-member :peer server)))
398 ;; change the entry in erc-dcc-list from the listening process to the
399 ;; accepted process
400 (setq elt (plist-put elt :peer client))
401 ;; delete the listening process, as we've accepted the connection
402 (delete-process server))))
403
404 ;;; Interactive command handling
405
406 (defcustom erc-dcc-get-default-directory nil
407 "*Default directory for incoming DCC file transfers.
408 If this is nil, then the current value of `default-directory' is used."
409 :group 'erc-dcc
410 :type '(choice (const nil :tag "Default directory") directory))
411
412 ;;;###autoload
413 (defun erc-cmd-DCC (cmd &rest args)
414 "Parser for /dcc command.
415 This figures out the dcc subcommand and calls the appropriate routine to
416 handle it. The function dispatched should be named \"erc-dcc-do-FOO-command\",
417 where FOO is one of CLOSE, GET, SEND, LIST, CHAT, etc."
418 (when cmd
419 (let ((fn (intern-soft (concat "erc-dcc-do-" (upcase cmd) "-command"))))
420 (if fn
421 (apply fn erc-server-process args)
422 (erc-display-message
423 nil 'notice 'active
424 'dcc-command-undefined ?c cmd)
425 (apropos "erc-dcc-do-.*-command")
426 t))))
427
428 ;;;###autoload
429 (defun pcomplete/erc-mode/DCC ()
430 "Provides completion for the /DCC command."
431 (pcomplete-here (append '("chat" "close" "get" "list")
432 (when (fboundp 'make-network-process) '("send"))))
433 (pcomplete-here
434 (case (intern (downcase (pcomplete-arg 1)))
435 (chat (mapcar (lambda (elt) (plist-get elt :nick))
436 (erc-remove-if-not
437 #'(lambda (elt)
438 (eq (plist-get elt :type) 'CHAT))
439 erc-dcc-list)))
440 (close (remove-duplicates
441 (mapcar (lambda (elt) (symbol-name (plist-get elt :type)))
442 erc-dcc-list) :test 'string=))
443 (get (mapcar #'erc-dcc-nick
444 (erc-remove-if-not
445 #'(lambda (elt)
446 (eq (plist-get elt :type) 'GET))
447 erc-dcc-list)))
448 (send (pcomplete-erc-all-nicks))))
449 (pcomplete-here
450 (case (intern (downcase (pcomplete-arg 2)))
451 (get (mapcar (lambda (elt) (plist-get elt :file))
452 (erc-remove-if-not
453 #'(lambda (elt)
454 (and (eq (plist-get elt :type) 'GET)
455 (erc-nick-equal-p (erc-extract-nick
456 (plist-get elt :nick))
457 (pcomplete-arg 1))))
458 erc-dcc-list)))
459 (close (mapcar #'erc-dcc-nick
460 (erc-remove-if-not
461 #'(lambda (elt)
462 (eq (plist-get elt :type)
463 (intern (upcase (pcomplete-arg 1)))))
464 erc-dcc-list)))
465 (send (pcomplete-entries)))))
466
467 (defun erc-dcc-do-CHAT-command (proc &optional nick)
468 (when nick
469 (let ((elt (erc-dcc-member :nick nick :type 'CHAT :parent proc)))
470 (if (and elt (not (processp (plist-get elt :peer))))
471 ;; accept an existing chat offer
472 ;; FIXME: perhaps /dcc accept like other clients?
473 (progn (erc-dcc-chat-accept elt erc-server-process)
474 (erc-display-message
475 nil 'notice 'active
476 'dcc-chat-accept ?n nick)
477 t)
478 (erc-dcc-chat nick erc-server-process)
479 (erc-display-message
480 nil 'notice 'active
481 'dcc-chat-offer ?n nick)
482 t))))
483
484 (defun erc-dcc-do-CLOSE-command (proc &optional type nick)
485 "Close a connection. Usage: /dcc close type nick.
486 At least one of TYPE and NICK must be provided."
487 ;; disambiguate type and nick if only one is provided
488 (when (and type (null nick)
489 (not (member (upcase type) erc-dcc-connection-types)))
490 (setq nick type)
491 (setq type nil))
492 ;; validate nick argument
493 (unless (and nick (string-match (concat "\\`" erc-valid-nick-regexp "\\'")
494 nick))
495 (setq nick nil))
496 ;; validate type argument
497 (if (and type (member (upcase type) erc-dcc-connection-types))
498 (setq type (intern (upcase type)))
499 (setq type nil))
500 (when (or nick type)
501 (let ((ret t))
502 (while ret
503 (cond ((and nick type)
504 (setq ret (erc-dcc-member :type type :nick nick)))
505 (nick
506 (setq ret (erc-dcc-member :nick nick)))
507 (type
508 (setq ret (erc-dcc-member :type type)))
509 (t
510 (setq ret nil)))
511 (when ret
512 ;; found a match - delete process if it exists.
513 (and (processp (plist-get ret :peer))
514 (delete-process (plist-get ret :peer)))
515 (setq erc-dcc-list (delq ret erc-dcc-list))
516 (erc-display-message
517 nil 'notice 'active
518 'dcc-closed
519 ?T (plist-get ret :type)
520 ?n (erc-extract-nick (plist-get ret :nick))))))
521 t))
522
523 (defun erc-dcc-do-GET-command (proc nick &rest file)
524 "Do a DCC GET command. NICK is the person who is sending the file.
525 FILE is the filename. If FILE is split into multiple arguments,
526 re-join the arguments, separated by a space.
527 PROC is the server process."
528 (setq file (and file (mapconcat #'identity file " ")))
529 (let* ((elt (erc-dcc-member :nick nick :type 'GET))
530 (filename (or file (plist-get elt :file) "unknown")))
531 (if elt
532 (let* ((file (read-file-name
533 (format "Local filename (default %s): "
534 (file-name-nondirectory filename))
535 (or erc-dcc-get-default-directory
536 default-directory)
537 (expand-file-name (file-name-nondirectory filename)
538 (or erc-dcc-get-default-directory
539 default-directory)))))
540 (cond ((file-exists-p file)
541 (if (yes-or-no-p (format "File %s exists. Overwrite? "
542 file))
543 (erc-dcc-get-file elt file proc)
544 (erc-display-message
545 nil '(notice error) proc
546 'dcc-get-cmd-aborted
547 ?n nick ?f filename)))
548 (t
549 (erc-dcc-get-file elt file proc))))
550 (erc-display-message
551 nil '(notice error) 'active
552 'dcc-get-notfound ?n nick ?f filename))))
553
554 (defvar erc-dcc-byte-count nil)
555 (make-variable-buffer-local 'erc-dcc-byte-count)
556
557 (defun erc-dcc-do-LIST-command (proc)
558 "This is the handler for the /dcc list command.
559 It lists the current state of `erc-dcc-list' in an easy to read manner."
560 (let ((alist erc-dcc-list)
561 size elt)
562 (erc-display-message
563 nil 'notice 'active
564 'dcc-list-head)
565 (erc-display-message
566 nil 'notice 'active
567 'dcc-list-line)
568 (while alist
569 (setq elt (car alist)
570 alist (cdr alist))
571
572 (setq size (or (and (plist-member elt :size)
573 (plist-get elt :size))
574 ""))
575 (setq size
576 (cond ((null size) "")
577 ((numberp size) (number-to-string size))
578 ((string= size "") "unknown")))
579 (erc-display-message
580 nil 'notice 'active
581 'dcc-list-item
582 ?n (erc-dcc-nick elt)
583 ?t (plist-get elt :type)
584 ?a (if (processp (plist-get elt :peer))
585 (process-status (plist-get elt :peer))
586 "no")
587 ?s (concat size
588 (if (and (eq 'GET (plist-get elt :type))
589 (plist-member elt :file)
590 (buffer-live-p (get-buffer (plist-get elt :file)))
591 (plist-member elt :size))
592 (let ((byte-count (with-current-buffer
593 (get-buffer (plist-get elt :file))
594 (+ (buffer-size) 0.0
595 erc-dcc-byte-count))))
596 (concat " ("
597 (if (= byte-count 0)
598 "0"
599 (number-to-string
600 (truncate
601 (* 100
602 (/ byte-count (plist-get elt :size))))))
603 "%)"))))
604 ?f (or (and (plist-member elt :file) (plist-get elt :file)) "")))
605 (erc-display-message
606 nil 'notice 'active
607 'dcc-list-end)
608 t))
609
610 (defun erc-dcc-do-SEND-command (proc nick &rest file)
611 "Offer FILE to NICK by sending a ctcp dcc send message.
612 If FILE is split into multiple arguments, re-join the arguments,
613 separated by a space."
614 (setq file (and file (mapconcat #'identity file " ")))
615 (if (file-exists-p file)
616 (progn
617 (erc-display-message
618 nil 'notice 'active
619 'dcc-send-offer ?n nick ?f file)
620 (erc-dcc-send-file nick file) t)
621 (erc-display-message nil '(notice error) proc "File not found") t))
622
623 ;;; Server message handling (i.e. messages from remote users)
624
625 ;;;###autoload
626 (defvar erc-ctcp-query-DCC-hook '(erc-ctcp-query-DCC)
627 "Hook variable for CTCP DCC queries")
628
629 (defvar erc-dcc-query-handler-alist
630 '(("SEND" . erc-dcc-handle-ctcp-send)
631 ("CHAT" . erc-dcc-handle-ctcp-chat)))
632
633 ;;;###autoload
634 (defun erc-ctcp-query-DCC (proc nick login host to query)
635 "The function called when a CTCP DCC request is detected by the client.
636 It examines the DCC subcommand, and calls the appropriate routine for
637 that subcommand."
638 (let* ((cmd (cadr (split-string query " ")))
639 (handler (cdr (assoc cmd erc-dcc-query-handler-alist))))
640 (if handler
641 (funcall handler proc query nick login host to)
642 ;; FIXME: Send a ctcp error notice to the remote end?
643 (erc-display-message
644 nil '(notice error) proc
645 'dcc-ctcp-unknown
646 ?q query ?n nick ?u login ?h host))))
647
648 (defconst erc-dcc-ctcp-query-send-regexp
649 "^DCC SEND \\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) *\\([0-9]*\\)")
650
651 (defun erc-dcc-handle-ctcp-send (proc query nick login host to)
652 "This is called if a CTCP DCC SEND subcommand is sent to the client.
653 It extracts the information about the dcc request and adds it to
654 `erc-dcc-list'."
655 (unless (eq erc-dcc-send-request 'ignore)
656 (cond
657 ((not (erc-current-nick-p to))
658 ;; DCC SEND requests must be sent to you, and you alone.
659 (erc-display-message
660 nil 'notice proc
661 'dcc-request-bogus
662 ?r "SEND" ?n nick ?u login ?h host))
663 ((string-match erc-dcc-ctcp-query-send-regexp query)
664 (let ((filename (match-string 1 query))
665 (ip (erc-decimal-to-ip (match-string 2 query)))
666 (port (match-string 3 query))
667 (size (match-string 4 query)))
668 ;; FIXME: a warning really should also be sent
669 ;; if the ip address != the host the dcc sender is on.
670 (erc-display-message
671 nil 'notice proc
672 'dcc-send-offered
673 ?f filename ?n nick ?u login ?h host
674 ?s (if (string= size "") "unknown" size))
675 (and (< (string-to-number port) 1025)
676 (erc-display-message
677 nil 'notice proc
678 'dcc-privileged-port
679 ?p port))
680 (erc-dcc-list-add
681 'GET (format "%s!%s@%s" nick login host)
682 nil proc
683 :ip ip :port port :file filename
684 :size (string-to-number size))
685 (if (and (eq erc-dcc-send-request 'auto)
686 (erc-dcc-auto-mask-p (format "\"%s!%s@%s\"" nick login host)))
687 (erc-dcc-get-file (car erc-dcc-list) filename proc))))
688 (t
689 (erc-display-message
690 nil 'notice proc
691 'dcc-malformed
692 ?n nick ?u login ?h host ?q query)))))
693
694 (defun erc-dcc-auto-mask-p (spec)
695 "Takes a full SPEC of a user in the form \"nick!login@host\" and
696 matches against all the regexp's in `erc-dcc-auto-masks'. If any
697 match, returns that regexp and nil otherwise."
698 (let ((lst erc-dcc-auto-masks))
699 (while (and lst
700 (not (string-match (car lst) spec)))
701 (setq lst (cdr lst)))
702 (and lst (car lst))))
703
704 (defconst erc-dcc-ctcp-query-chat-regexp
705 "^DCC CHAT +chat +\\([0-9]+\\) +\\([0-9]+\\)")
706
707 (defcustom erc-dcc-chat-request 'ask
708 "*How to treat incoming DCC Chat requests.
709 'ask - Report the Chat request, and wait for the user to manually accept it
710 'auto - Automatically accept the request and open a new chat window
711 'ignore - Ignore incoming DCC chat requests completely."
712 :group 'erc-dcc
713 :type '(choice (const ask) (const auto) (const ignore)))
714
715 (defun erc-dcc-handle-ctcp-chat (proc query nick login host to)
716 (unless (eq erc-dcc-chat-request 'ignore)
717 (cond
718 (;; DCC CHAT requests must be sent to you, and you alone.
719 (not (erc-current-nick-p to))
720 (erc-display-message
721 nil '(notice error) proc
722 'dcc-request-bogus ?r "CHAT" ?n nick ?u login ?h host))
723 ((string-match erc-dcc-ctcp-query-chat-regexp query)
724 ;; We need to use let* here, since erc-dcc-member might clutter
725 ;; the match value.
726 (let* ((ip (erc-decimal-to-ip (match-string 1 query)))
727 (port (match-string 2 query))
728 (elt (erc-dcc-member :nick nick :type 'CHAT)))
729 ;; FIXME: A warning really should also be sent if the ip
730 ;; address != the host the dcc sender is on.
731 (erc-display-message
732 nil 'notice proc
733 'dcc-chat-offered
734 ?n nick ?u login ?h host ?p port)
735 (and (< (string-to-number port) 1025)
736 (erc-display-message
737 nil 'notice proc
738 'dcc-privileged-port ?p port))
739 (cond (elt
740 ;; XXX: why are we updating ip/port on the existing connection?
741 (setq elt (plist-put (plist-put elt :port port) :ip ip))
742 (erc-display-message
743 nil 'notice proc
744 'dcc-chat-discarded ?n nick ?u login ?h host))
745 (t
746 (erc-dcc-list-add
747 'CHAT (format "%s!%s@%s" nick login host)
748 nil proc
749 :ip ip :port port)))
750 (if (eq erc-dcc-chat-request 'auto)
751 (erc-dcc-chat-accept (erc-dcc-member :nick nick :type 'CHAT)
752 proc))))
753 (t
754 (erc-display-message
755 nil '(notice error) proc
756 'dcc-malformed ?n nick ?u login ?h host ?q query)))))
757
758
759 (defvar erc-dcc-entry-data nil
760 "Holds the `erc-dcc-list' entry for this DCC connection.")
761 (make-variable-buffer-local 'erc-dcc-entry-data)
762
763 ;;; SEND handling
764
765 (defcustom erc-dcc-block-size 1024
766 "*Block size to use for DCC SEND sessions."
767 :group 'erc-dcc
768 :type 'integer)
769
770 (defcustom erc-dcc-pump-bytes nil
771 "*If set to an integer, keep sending until that number of bytes are
772 unconfirmed."
773 :group 'erc-dcc
774 :type '(choice (const nil) integer))
775
776 (defsubst erc-dcc-get-parent (proc)
777 (plist-get (erc-dcc-member :peer proc) :parent))
778
779 (defun erc-dcc-send-block (proc)
780 "Send one block of data.
781 PROC is the process-object of the DCC connection. Returns the number of
782 bytes sent."
783 (let* ((elt (erc-dcc-member :peer proc))
784 (confirmed-marker (plist-get elt :sent))
785 (sent-marker (plist-get elt :sent)))
786 (with-current-buffer (process-buffer proc)
787 (when erc-dcc-verbose
788 (erc-display-message
789 nil 'notice (erc-dcc-get-parent proc)
790 (format "DCC: Confirmed %d, sent %d, sending block now"
791 (- confirmed-marker (point-min))
792 (- sent-marker (point-min)))))
793 (let* ((end (min (+ sent-marker erc-dcc-block-size)
794 (point-max)))
795 (string (buffer-substring-no-properties sent-marker end)))
796 (when (< sent-marker end)
797 (set-marker sent-marker end)
798 (process-send-string proc string))
799 (length string)))))
800
801 (defun erc-dcc-send-filter (proc string)
802 (let* ((size (erc-unpack-int string))
803 (elt (erc-dcc-member :peer proc))
804 (parent (plist-get elt :parent))
805 (sent-marker (plist-get elt :sent))
806 (confirmed-marker (plist-get elt :confirmed)))
807 (with-current-buffer (process-buffer proc)
808 (set-marker confirmed-marker (+ (point-min) size))
809 (cond
810 ((and (= confirmed-marker sent-marker)
811 (= confirmed-marker (point-max)))
812 (erc-display-message
813 nil 'notice parent
814 'dcc-send-finished
815 ?n (plist-get elt :nick)
816 ?f buffer-file-name
817 ?s (number-to-string (- sent-marker (point-min))))
818 (setq erc-dcc-list (delete elt erc-dcc-list))
819 (set-buffer-modified-p nil)
820 (kill-buffer (current-buffer))
821 (delete-process proc))
822 ((<= confirmed-marker sent-marker)
823 (while (and (< (- sent-marker confirmed-marker)
824 (or erc-dcc-pump-bytes
825 erc-dcc-block-size))
826 (> (erc-dcc-send-block proc) 0))))
827 ((> confirmed-marker sent-marker)
828 (erc-display-message
829 nil 'notice parent
830 (format "DCC: Client confirmed too much (%s vs %s)!"
831 (marker-position confirmed-marker)
832 (marker-position sent-marker)))
833 (set-buffer-modified-p nil)
834 (kill-buffer (current-buffer))
835 (delete-process proc))))))
836
837 (defun erc-dcc-display-send (proc)
838 (erc-display-message
839 nil 'notice (erc-dcc-get-parent proc)
840 (format "DCC: SEND connect from %s"
841 (format-network-address (process-contact proc :remote)))))
842
843 (defcustom erc-dcc-send-connect-hook
844 '(erc-dcc-display-send erc-dcc-send-block)
845 "*Hook run whenever the remote end of a DCC SEND offer connected to your
846 listening port."
847 :group 'erc-dcc
848 :type 'hook)
849
850 (defun erc-dcc-nick (plist)
851 "Extract the nickname portion of the :nick property value in PLIST."
852 (erc-extract-nick (plist-get plist :nick)))
853
854 (defun erc-dcc-send-sentinel (proc event)
855 (let* ((elt (erc-dcc-member :peer proc)))
856 (cond
857 ((string-match "^open from " event)
858 (when elt
859 (let ((buf (marker-buffer (plist-get elt :sent))))
860 (with-current-buffer buf
861 (set-process-buffer proc buf)
862 (setq erc-dcc-entry-data elt)))
863 (run-hook-with-args 'erc-dcc-send-connect-hook proc))))))
864
865 (defun erc-dcc-find-file (file)
866 (with-current-buffer (generate-new-buffer (file-name-nondirectory file))
867 (insert-file-contents-literally file)
868 (setq buffer-file-name file)
869 (current-buffer)))
870
871 (defun erc-dcc-file-to-name (file)
872 (with-temp-buffer
873 (insert (file-name-nondirectory file))
874 (subst-char-in-region (point-min) (point-max) ? ?_ t)
875 (buffer-string)))
876
877 (defun erc-dcc-send-file (nick file &optional pproc)
878 "Open a socket for incoming connections, and send a CTCP send request to the
879 other client."
880 (interactive "sNick: \nfFile: ")
881 (when (null pproc) (if (processp erc-server-process)
882 (setq pproc erc-server-process)
883 (error "Can not find parent process")))
884 (if (featurep 'make-network-process)
885 (let* ((buffer (erc-dcc-find-file file))
886 (size (buffer-size buffer))
887 (start (with-current-buffer buffer
888 (set-marker (make-marker) (point-min))))
889 (sproc (erc-dcc-server "dcc-send"
890 'erc-dcc-send-filter
891 'erc-dcc-send-sentinel))
892 (contact (process-contact sproc)))
893 (erc-dcc-list-add
894 'SEND nick sproc pproc
895 :file file :size size
896 :sent start :confirmed (copy-marker start))
897 (process-send-string
898 pproc (format "PRIVMSG %s :\C-aDCC SEND %s %s %d %d\C-a\n"
899 nick (erc-dcc-file-to-name file)
900 (erc-ip-to-decimal (or erc-dcc-public-host
901 (nth 0 contact)))
902 (nth 1 contact)
903 size)))
904 (error "`make-network-process' not supported by your Emacs")))
905
906 ;;; GET handling
907
908 (defcustom erc-dcc-receive-cache (* 1024 512)
909 "Number of bytes to let the receive buffer grow before flushing it."
910 :group 'erc-dcc
911 :type 'integer)
912
913 (defvar erc-dcc-file-name nil)
914 (make-variable-buffer-local 'erc-dcc-file-name)
915
916 (defun erc-dcc-get-file (entry file parent-proc)
917 "This function does the work of setting up a transfer from the remote client
918 to the local one over a tcp connection. This involves setting up a process
919 filter and a process sentinel, and making the connection."
920 (let* ((buffer (generate-new-buffer (file-name-nondirectory file)))
921 proc)
922 (with-current-buffer buffer
923 (fundamental-mode)
924 (buffer-disable-undo (current-buffer))
925 ;; This is necessary to have the buffer saved as-is in GNU
926 ;; Emacs.
927 ;; XEmacs change: We don't have `set-buffer-multibyte', setting
928 ;; coding system to 'binary below takes care of us.
929 (when (fboundp 'set-buffer-multibyte)
930 (set-buffer-multibyte nil))
931
932 (setq mode-line-process '(":%s")
933 buffer-file-type t
934 buffer-read-only t)
935 (setq erc-dcc-file-name file)
936
937 ;; Truncate the given file to size 0 before appending to it.
938 (let ((inhibit-file-name-handlers
939 (append '(jka-compr-handler image-file-handler)
940 inhibit-file-name-handlers))
941 (inhibit-file-name-operation 'write-region))
942 (write-region (point) (point) erc-dcc-file-name nil 'nomessage))
943
944 (setq erc-server-process parent-proc
945 erc-dcc-entry-data entry)
946 (setq erc-dcc-byte-count 0)
947 (setq proc
948 (funcall erc-dcc-connect-function
949 "dcc-get" buffer
950 (plist-get entry :ip)
951 (string-to-number (plist-get entry :port))
952 entry))
953 (set-process-buffer proc buffer)
954 (set-process-coding-system proc 'binary 'binary)
955 (set-buffer-file-coding-system 'binary t)
956
957 (set-process-filter proc 'erc-dcc-get-filter)
958 (set-process-sentinel proc 'erc-dcc-get-sentinel)
959 (setq entry (plist-put entry :start-time (erc-current-time)))
960 (setq entry (plist-put entry :peer proc)))))
961
962 (defun erc-dcc-append-contents (buffer file)
963 "Append the contents of BUFFER to FILE.
964 The contents of the BUFFER will then be erased."
965 (with-current-buffer buffer
966 (let ((coding-system-for-write 'binary)
967 (inhibit-read-only t)
968 (inhibit-file-name-handlers
969 (append '(jka-compr-handler image-file-handler)
970 inhibit-file-name-handlers))
971 (inhibit-file-name-operation 'write-region))
972 (write-region (point-min) (point-max) erc-dcc-file-name t 'nomessage)
973 (setq erc-dcc-byte-count (+ (buffer-size) erc-dcc-byte-count))
974 (erase-buffer))))
975
976 (defun erc-dcc-get-filter (proc str)
977 "This is the process filter for transfers from other clients to this one.
978 It reads incoming bytes from the network and stores them in the DCC
979 buffer, and sends back the replies after each block of data per the DCC
980 protocol spec. Well not really. We write back a reply after each read,
981 rather than every 1024 byte block, but nobody seems to care."
982 (with-current-buffer (process-buffer proc)
983 (let ((inhibit-read-only t)
984 received-bytes)
985 (goto-char (point-max))
986 (insert (string-make-unibyte str))
987
988 (when (> (point-max) erc-dcc-receive-cache)
989 (erc-dcc-append-contents (current-buffer) erc-dcc-file-name))
990 (setq received-bytes (+ (buffer-size) erc-dcc-byte-count))
991
992 (and erc-dcc-verbose
993 (erc-display-message
994 nil 'notice erc-server-process
995 'dcc-get-bytes-received
996 ?f (file-name-nondirectory buffer-file-name)
997 ?b (number-to-string received-bytes)))
998 (cond
999 ((and (> (plist-get erc-dcc-entry-data :size) 0)
1000 (> received-bytes (plist-get erc-dcc-entry-data :size)))
1001 (erc-display-message
1002 nil '(error notice) 'active
1003 'dcc-get-file-too-long
1004 ?f (file-name-nondirectory buffer-file-name))
1005 (delete-process proc))
1006 (t
1007 (process-send-string
1008 proc (erc-pack-int received-bytes)))))))
1009
1010
1011 (defun erc-dcc-get-sentinel (proc event)
1012 "This is the process sentinel for CTCP DCC SEND connections.
1013 It shuts down the connection and notifies the user that the
1014 transfer is complete."
1015 ;; FIXME, we should look at EVENT, and also check size.
1016 (with-current-buffer (process-buffer proc)
1017 (delete-process proc)
1018 (setq erc-dcc-list (delete erc-dcc-entry-data erc-dcc-list))
1019 (unless (= (point-min) (point-max))
1020 (erc-dcc-append-contents (current-buffer) erc-dcc-file-name))
1021 (erc-display-message
1022 nil 'notice erc-server-process
1023 'dcc-get-complete
1024 ?f erc-dcc-file-name
1025 ?s (number-to-string erc-dcc-byte-count)
1026 ?t (format "%.0f"
1027 (erc-time-diff (plist-get erc-dcc-entry-data :start-time)
1028 (erc-current-time)))))
1029 (kill-buffer (process-buffer proc))
1030 (delete-process proc))
1031
1032 ;;; CHAT handling
1033
1034 (defcustom erc-dcc-chat-buffer-name-format "DCC-CHAT-%s"
1035 "*Format to use for DCC Chat buffer names."
1036 :group 'erc-dcc
1037 :type 'string)
1038
1039 (defcustom erc-dcc-chat-mode-hook nil
1040 "*Hook calls when `erc-dcc-chat-mode' finished setting up the buffer."
1041 :group 'erc-dcc
1042 :type 'hook)
1043
1044 (defcustom erc-dcc-chat-connect-hook nil
1045 ""
1046 :group 'erc-dcc
1047 :type 'hook)
1048
1049 (defcustom erc-dcc-chat-exit-hook nil
1050 ""
1051 :group 'erc-dcc
1052 :type 'hook)
1053
1054 (defun erc-cmd-CREQ (line &optional force)
1055 "Set or get the DCC chat request flag.
1056 Possible values are: ask, auto, ignore."
1057 (when (string-match "^\\s-*\\(auto\\|ask\\|ignore\\)?$" line)
1058 (let ((cmd (match-string 1 line)))
1059 (if (stringp cmd)
1060 (erc-display-message
1061 nil 'notice 'active
1062 (format "Set DCC Chat requests to %S"
1063 (setq erc-dcc-chat-request (intern cmd))))
1064 (erc-display-message nil 'notice 'active
1065 (format "DCC Chat requests are set to %S"
1066 erc-dcc-chat-request)))
1067 t)))
1068
1069 (defun erc-cmd-SREQ (line &optional force)
1070 "Set or get the DCC send request flag.
1071 Possible values are: ask, auto, ignore."
1072 (when (string-match "^\\s-*\\(auto\\|ask\\|ignore\\)?$" line)
1073 (let ((cmd (match-string 1 line)))
1074 (if (stringp cmd)
1075 (erc-display-message
1076 nil 'notice 'active
1077 (format "Set DCC Send requests to %S"
1078 (setq erc-dcc-send-request (intern cmd))))
1079 (erc-display-message nil 'notice 'active
1080 (format "DCC Send requests are set to %S"
1081 erc-dcc-send-request)))
1082 t)))
1083
1084 (defun pcomplete/erc-mode/CREQ ()
1085 (pcomplete-here '("auto" "ask" "ignore")))
1086 (defalias 'pcomplete/erc-mode/SREQ 'pcomplete/erc-mode/CREQ)
1087
1088 (defvar erc-dcc-chat-filter-hook '(erc-dcc-chat-parse-output)
1089 "*Hook to run after doing parsing (and possible insertion) of DCC messages.")
1090
1091 (defvar erc-dcc-chat-mode-map
1092 (let ((map (make-sparse-keymap)))
1093 (define-key map (kbd "RET") 'erc-send-current-line)
1094 (define-key map "\t" 'erc-complete-word)
1095 map)
1096 "Keymap for `erc-dcc-mode'.")
1097
1098 (defun erc-dcc-chat-mode ()
1099 "Major mode for wasting time via DCC chat."
1100 (interactive)
1101 (kill-all-local-variables)
1102 (setq mode-line-process '(":%s")
1103 mode-name "DCC-Chat"
1104 major-mode 'erc-dcc-chat-mode
1105 erc-send-input-line-function 'erc-dcc-chat-send-input-line
1106 erc-default-recipients '(dcc))
1107 (use-local-map erc-dcc-chat-mode-map)
1108 (run-hooks 'erc-dcc-chat-mode-hook))
1109
1110 (defun erc-dcc-chat-send-input-line (recipient line &optional force)
1111 "Send LINE to the remote end.
1112 Argument RECIPIENT should always be the symbol dcc, and force
1113 is ignored."
1114 ;; FIXME: We need to get rid of all force arguments one day!
1115 (if (eq recipient 'dcc)
1116 (process-send-string
1117 (get-buffer-process (current-buffer)) line)
1118 (error "erc-dcc-chat-send-input-line in %s" (current-buffer))))
1119
1120 (defun erc-dcc-chat (nick &optional pproc)
1121 "Open a socket for incoming connections, and send a chat request to the
1122 other client."
1123 (interactive "sNick: ")
1124 (when (null pproc) (if (processp erc-server-process)
1125 (setq pproc erc-server-process)
1126 (error "Can not find parent process")))
1127 (let* ((sproc (erc-dcc-server "dcc-chat-out"
1128 'erc-dcc-chat-filter
1129 'erc-dcc-chat-sentinel))
1130 (contact (process-contact sproc)))
1131 (erc-dcc-list-add 'OCHAT nick sproc pproc)
1132 (process-send-string pproc
1133 (format "PRIVMSG %s :\C-aDCC CHAT chat %s %d\C-a\n"
1134 nick
1135 (erc-ip-to-decimal (nth 0 contact)) (nth 1 contact)))))
1136
1137 (defvar erc-dcc-from)
1138 (make-variable-buffer-local 'erc-dcc-from)
1139
1140 (defvar erc-dcc-unprocessed-output)
1141 (make-variable-buffer-local 'erc-dcc-unprocessed-output)
1142
1143 (defun erc-dcc-chat-setup (entry)
1144 "Setup a DCC chat buffer, returning the buffer."
1145 (let* ((nick (erc-extract-nick (plist-get entry :nick)))
1146 (buffer (generate-new-buffer
1147 (format erc-dcc-chat-buffer-name-format nick)))
1148 (proc (plist-get entry :peer))
1149 (parent-proc (plist-get entry :parent)))
1150 (erc-setup-buffer buffer)
1151 ;; buffer is now the current buffer.
1152 (erc-dcc-chat-mode)
1153 (setq erc-server-process parent-proc)
1154 (setq erc-dcc-from nick)
1155 (setq erc-dcc-entry-data entry)
1156 (setq erc-dcc-unprocessed-output "")
1157 (setq erc-insert-marker (set-marker (make-marker) (point-max)))
1158 (setq erc-input-marker (make-marker))
1159 (erc-display-prompt buffer (point-max))
1160 (set-process-buffer proc buffer)
1161 (add-hook 'kill-buffer-hook 'erc-dcc-chat-buffer-killed nil t)
1162 (run-hook-with-args 'erc-dcc-chat-connect-hook proc)
1163 buffer))
1164
1165 (defun erc-dcc-chat-accept (entry parent-proc)
1166 "Accept an incoming DCC connection and open a DCC window"
1167 (let* ((nick (erc-extract-nick (plist-get entry :nick)))
1168 buffer proc)
1169 (setq proc
1170 (funcall erc-dcc-connect-function
1171 "dcc-chat" nil
1172 (plist-get entry :ip)
1173 (string-to-number (plist-get entry :port))
1174 entry))
1175 ;; XXX: connected, should we kill the ip/port properties?
1176 (setq entry (plist-put entry :peer proc))
1177 (setq entry (plist-put entry :parent parent-proc))
1178 (set-process-filter proc 'erc-dcc-chat-filter)
1179 (set-process-sentinel proc 'erc-dcc-chat-sentinel)
1180 (setq buffer (erc-dcc-chat-setup entry))))
1181
1182 (defun erc-dcc-chat-filter (proc str)
1183 (let ((orig-buffer (current-buffer)))
1184 (unwind-protect
1185 (progn
1186 (set-buffer (process-buffer proc))
1187 (setq erc-dcc-unprocessed-output
1188 (concat erc-dcc-unprocessed-output str))
1189 (run-hook-with-args 'erc-dcc-chat-filter-hook proc
1190 erc-dcc-unprocessed-output))
1191 (set-buffer orig-buffer))))
1192
1193 (defun erc-dcc-chat-parse-output (proc str)
1194 (save-match-data
1195 (let ((posn 0)
1196 line)
1197 (while (string-match "\n" str posn)
1198 (setq line (substring str posn (match-beginning 0)))
1199 (setq posn (match-end 0))
1200 (erc-display-message
1201 nil nil proc
1202 'dcc-chat-privmsg ?n (erc-propertize erc-dcc-from 'face
1203 'erc-nick-default-face) ?m line))
1204 (setq erc-dcc-unprocessed-output (substring str posn)))))
1205
1206 (defun erc-dcc-chat-buffer-killed ()
1207 (erc-dcc-chat-close "killed buffer"))
1208
1209 (defun erc-dcc-chat-close (&optional event)
1210 "Close a DCC chat, removing any associated processes and tidying up
1211 `erc-dcc-list'"
1212 (let ((proc (plist-get erc-dcc-entry-data :peer))
1213 (evt (or event "")))
1214 (when proc
1215 (setq erc-dcc-list (delq erc-dcc-entry-data erc-dcc-list))
1216 (run-hook-with-args 'erc-dcc-chat-exit-hook proc)
1217 (delete-process proc)
1218 (erc-display-message
1219 nil 'notice erc-server-process
1220 'dcc-chat-ended ?n erc-dcc-from ?t (current-time-string) ?e evt)
1221 (setq erc-dcc-entry-data (plist-put erc-dcc-entry-data :peer nil)))))
1222
1223 (defun erc-dcc-chat-sentinel (proc event)
1224 (let ((buf (current-buffer))
1225 (elt (erc-dcc-member :peer proc)))
1226 ;; the sentinel is also notified when the connection is opened, so don't
1227 ;; immediately kill it again
1228 ;(message "buf %s elt %S evt %S" buf elt event)
1229 (unwind-protect
1230 (if (string-match "^open from" event)
1231 (erc-dcc-chat-setup elt)
1232 (erc-dcc-chat-close event))
1233 (set-buffer buf))))
1234
1235 (defun erc-dcc-no-such-nick (proc parsed)
1236 "Detect and handle no-such-nick replies from the IRC server."
1237 (let* ((elt (erc-dcc-member :nick (second (erc-response.command-args parsed))
1238 :parent proc))
1239 (peer (plist-get elt :peer)))
1240 (when (or (and (processp peer) (not (eq (process-status peer) 'open)))
1241 elt)
1242 ;; Since we already created an entry before sending the CTCP
1243 ;; message, we now remove it, if it doesn't point to a process
1244 ;; which is already open.
1245 (setq erc-dcc-list (delq elt erc-dcc-list))
1246 (if (processp peer) (delete-process peer)))
1247 nil))
1248
1249 (provide 'erc-dcc)
1250
1251 ;;; erc-dcc.el ends here
1252 ;;
1253 ;; Local Variables:
1254 ;; indent-tabs-mode: nil
1255 ;; End:
1256
1257 ;; arch-tag: cda5a6b3-c510-4dbe-b699-84cccfa04edb