Fix last changelog entry.
[emacs.git] / lisp / erc / erc-dcc.el
blob4dda869315c836a172834af93da3e4f9220ede4f
1 ;;; erc-dcc.el --- CTCP DCC module for ERC
3 ;; Copyright (C) 1993, 1994, 1995, 1998, 2002, 2003, 2004, 2006, 2007,
4 ;; 2008, 2009 Free Software Foundation, Inc.
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
13 ;; This file is part of GNU Emacs.
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.
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.
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/>.
28 ;;; Commentary:
30 ;; This file provides Direct Client-to-Client support for ERC.
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.
37 ;;; Usage:
39 ;; To use this file, put
40 ;; (require 'erc-dcc)
41 ;; in your .emacs.
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
51 ;; Please note that offering DCC connections (offering chats and sending
52 ;; files) is only supported with Emacs 22.
54 ;;; Code:
56 (require 'erc)
57 (eval-when-compile
58 (require 'cl)
59 (require 'pcomplete))
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)))
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.
73 Using DCC get and send, you can transfer files directly from and to other
74 IRC users."
75 :group 'erc)
77 (defcustom erc-dcc-verbose nil
78 "*If non-nil, be verbose about DCC activity reporting."
79 :group 'erc-dcc
80 :type 'boolean)
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.")
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>))
94 :nick - a user or userhost for the peer. combine with :parent to reach them
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
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
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.
108 :file - for outgoing sends, the full path to the file. for incoming sends,
109 the suggested filename or vetted filename
111 :size - size of the file, may be nil on incoming DCCs")
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))))
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)
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)))
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")))
165 ;;; Misc macros and utility functions
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.
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))
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))
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.")
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.")
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)))
258 (defconst erc-dcc-ipv4-regexp
259 (concat "^"
260 (mapconcat #'identity (make-list 4 "\\([0-9]\\{1,3\\}\\)") "\\.")
261 "$"))
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))
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)))))
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)))
298 ;;; Server code
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)))
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)))
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)))
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))
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")))
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"))))
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))
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 (with-no-warnings ; obsolete since 23.1
381 (set-process-filter-multibyte process nil)))))
382 (file-error
383 (unless (and (string= "Cannot bind server socket" (cadr err))
384 (string= "address already in use" (caddr err)))
385 (signal (car err) (cdr err)))
386 (setq port (1+ port))
387 (unless (< port upper)
388 (error "No available ports in erc-dcc-port-range")))))
389 process))
391 (defun erc-dcc-server-accept (server client message)
392 "Log an accepted DCC offer, then terminate the listening process and set up
393 the accepted connection."
394 (erc-log (format "(erc-dcc-server-accept): server %s client %s message %s"
395 server client message))
396 (when (and (string-match "^accept from " message)
397 (processp server) (processp client))
398 (let ((elt (erc-dcc-member :peer server)))
399 ;; change the entry in erc-dcc-list from the listening process to the
400 ;; accepted process
401 (setq elt (plist-put elt :peer client))
402 ;; delete the listening process, as we've accepted the connection
403 (delete-process server))))
405 ;;; Interactive command handling
407 (defcustom erc-dcc-get-default-directory nil
408 "*Default directory for incoming DCC file transfers.
409 If this is nil, then the current value of `default-directory' is used."
410 :group 'erc-dcc
411 :type '(choice (const nil :tag "Default directory") directory))
413 ;;;###autoload
414 (defun erc-cmd-DCC (cmd &rest args)
415 "Parser for /dcc command.
416 This figures out the dcc subcommand and calls the appropriate routine to
417 handle it. The function dispatched should be named \"erc-dcc-do-FOO-command\",
418 where FOO is one of CLOSE, GET, SEND, LIST, CHAT, etc."
419 (when cmd
420 (let ((fn (intern-soft (concat "erc-dcc-do-" (upcase cmd) "-command"))))
421 (if fn
422 (apply fn erc-server-process args)
423 (erc-display-message
424 nil 'notice 'active
425 'dcc-command-undefined ?c cmd)
426 (apropos "erc-dcc-do-.*-command")
427 t))))
429 ;;;###autoload
430 (defun pcomplete/erc-mode/DCC ()
431 "Provides completion for the /DCC command."
432 (pcomplete-here (append '("chat" "close" "get" "list")
433 (when (fboundp 'make-network-process) '("send"))))
434 (pcomplete-here
435 (case (intern (downcase (pcomplete-arg 1)))
436 (chat (mapcar (lambda (elt) (plist-get elt :nick))
437 (erc-remove-if-not
438 #'(lambda (elt)
439 (eq (plist-get elt :type) 'CHAT))
440 erc-dcc-list)))
441 (close (remove-duplicates
442 (mapcar (lambda (elt) (symbol-name (plist-get elt :type)))
443 erc-dcc-list) :test 'string=))
444 (get (mapcar #'erc-dcc-nick
445 (erc-remove-if-not
446 #'(lambda (elt)
447 (eq (plist-get elt :type) 'GET))
448 erc-dcc-list)))
449 (send (pcomplete-erc-all-nicks))))
450 (pcomplete-here
451 (case (intern (downcase (pcomplete-arg 2)))
452 (get (mapcar (lambda (elt) (plist-get elt :file))
453 (erc-remove-if-not
454 #'(lambda (elt)
455 (and (eq (plist-get elt :type) 'GET)
456 (erc-nick-equal-p (erc-extract-nick
457 (plist-get elt :nick))
458 (pcomplete-arg 1))))
459 erc-dcc-list)))
460 (close (mapcar #'erc-dcc-nick
461 (erc-remove-if-not
462 #'(lambda (elt)
463 (eq (plist-get elt :type)
464 (intern (upcase (pcomplete-arg 1)))))
465 erc-dcc-list)))
466 (send (pcomplete-entries)))))
468 (defun erc-dcc-do-CHAT-command (proc &optional nick)
469 (when nick
470 (let ((elt (erc-dcc-member :nick nick :type 'CHAT :parent proc)))
471 (if (and elt (not (processp (plist-get elt :peer))))
472 ;; accept an existing chat offer
473 ;; FIXME: perhaps /dcc accept like other clients?
474 (progn (erc-dcc-chat-accept elt erc-server-process)
475 (erc-display-message
476 nil 'notice 'active
477 'dcc-chat-accept ?n nick)
479 (erc-dcc-chat nick erc-server-process)
480 (erc-display-message
481 nil 'notice 'active
482 'dcc-chat-offer ?n nick)
483 t))))
485 (defun erc-dcc-do-CLOSE-command (proc &optional type nick)
486 "Close a connection. Usage: /dcc close type nick.
487 At least one of TYPE and NICK must be provided."
488 ;; disambiguate type and nick if only one is provided
489 (when (and type (null nick)
490 (not (member (upcase type) erc-dcc-connection-types)))
491 (setq nick type)
492 (setq type nil))
493 ;; validate nick argument
494 (unless (and nick (string-match (concat "\\`" erc-valid-nick-regexp "\\'")
495 nick))
496 (setq nick nil))
497 ;; validate type argument
498 (if (and type (member (upcase type) erc-dcc-connection-types))
499 (setq type (intern (upcase type)))
500 (setq type nil))
501 (when (or nick type)
502 (let ((ret t))
503 (while ret
504 (cond ((and nick type)
505 (setq ret (erc-dcc-member :type type :nick nick)))
506 (nick
507 (setq ret (erc-dcc-member :nick nick)))
508 (type
509 (setq ret (erc-dcc-member :type type)))
511 (setq ret nil)))
512 (when ret
513 ;; found a match - delete process if it exists.
514 (and (processp (plist-get ret :peer))
515 (delete-process (plist-get ret :peer)))
516 (setq erc-dcc-list (delq ret erc-dcc-list))
517 (erc-display-message
518 nil 'notice 'active
519 'dcc-closed
520 ?T (plist-get ret :type)
521 ?n (erc-extract-nick (plist-get ret :nick))))))
524 (defun erc-dcc-do-GET-command (proc nick &rest file)
525 "Do a DCC GET command. NICK is the person who is sending the file.
526 FILE is the filename. If FILE is split into multiple arguments,
527 re-join the arguments, separated by a space.
528 PROC is the server process."
529 (setq file (and file (mapconcat #'identity file " ")))
530 (let* ((elt (erc-dcc-member :nick nick :type 'GET))
531 (filename (or file (plist-get elt :file) "unknown")))
532 (if elt
533 (let* ((file (read-file-name
534 (format "Local filename (default %s): "
535 (file-name-nondirectory filename))
536 (or erc-dcc-get-default-directory
537 default-directory)
538 (expand-file-name (file-name-nondirectory filename)
539 (or erc-dcc-get-default-directory
540 default-directory)))))
541 (cond ((file-exists-p file)
542 (if (yes-or-no-p (format "File %s exists. Overwrite? "
543 file))
544 (erc-dcc-get-file elt file proc)
545 (erc-display-message
546 nil '(notice error) proc
547 'dcc-get-cmd-aborted
548 ?n nick ?f filename)))
550 (erc-dcc-get-file elt file proc))))
551 (erc-display-message
552 nil '(notice error) 'active
553 'dcc-get-notfound ?n nick ?f filename))))
555 (defvar erc-dcc-byte-count nil)
556 (make-variable-buffer-local 'erc-dcc-byte-count)
558 (defun erc-dcc-do-LIST-command (proc)
559 "This is the handler for the /dcc list command.
560 It lists the current state of `erc-dcc-list' in an easy to read manner."
561 (let ((alist erc-dcc-list)
562 size elt)
563 (erc-display-message
564 nil 'notice 'active
565 'dcc-list-head)
566 (erc-display-message
567 nil 'notice 'active
568 'dcc-list-line)
569 (while alist
570 (setq elt (car alist)
571 alist (cdr alist))
573 (setq size (or (and (plist-member elt :size)
574 (plist-get elt :size))
575 ""))
576 (setq size
577 (cond ((null size) "")
578 ((numberp size) (number-to-string size))
579 ((string= size "") "unknown")))
580 (erc-display-message
581 nil 'notice 'active
582 'dcc-list-item
583 ?n (erc-dcc-nick elt)
584 ?t (plist-get elt :type)
585 ?a (if (processp (plist-get elt :peer))
586 (process-status (plist-get elt :peer))
587 "no")
588 ?s (concat size
589 (if (and (eq 'GET (plist-get elt :type))
590 (plist-member elt :file)
591 (buffer-live-p (get-buffer (plist-get elt :file)))
592 (plist-member elt :size))
593 (let ((byte-count (with-current-buffer
594 (get-buffer (plist-get elt :file))
595 (+ (buffer-size) 0.0
596 erc-dcc-byte-count))))
597 (concat " ("
598 (if (= byte-count 0)
600 (number-to-string
601 (truncate
602 (* 100
603 (/ byte-count (plist-get elt :size))))))
604 "%)"))))
605 ?f (or (and (plist-member elt :file) (plist-get elt :file)) "")))
606 (erc-display-message
607 nil 'notice 'active
608 'dcc-list-end)
611 (defun erc-dcc-do-SEND-command (proc nick &rest file)
612 "Offer FILE to NICK by sending a ctcp dcc send message.
613 If FILE is split into multiple arguments, re-join the arguments,
614 separated by a space."
615 (setq file (and file (mapconcat #'identity file " ")))
616 (if (file-exists-p file)
617 (progn
618 (erc-display-message
619 nil 'notice 'active
620 'dcc-send-offer ?n nick ?f file)
621 (erc-dcc-send-file nick file) t)
622 (erc-display-message nil '(notice error) proc "File not found") t))
624 ;;; Server message handling (i.e. messages from remote users)
626 ;;;###autoload
627 (defvar erc-ctcp-query-DCC-hook '(erc-ctcp-query-DCC)
628 "Hook variable for CTCP DCC queries")
630 (defvar erc-dcc-query-handler-alist
631 '(("SEND" . erc-dcc-handle-ctcp-send)
632 ("CHAT" . erc-dcc-handle-ctcp-chat)))
634 ;;;###autoload
635 (defun erc-ctcp-query-DCC (proc nick login host to query)
636 "The function called when a CTCP DCC request is detected by the client.
637 It examines the DCC subcommand, and calls the appropriate routine for
638 that subcommand."
639 (let* ((cmd (cadr (split-string query " ")))
640 (handler (cdr (assoc cmd erc-dcc-query-handler-alist))))
641 (if handler
642 (funcall handler proc query nick login host to)
643 ;; FIXME: Send a ctcp error notice to the remote end?
644 (erc-display-message
645 nil '(notice error) proc
646 'dcc-ctcp-unknown
647 ?q query ?n nick ?u login ?h host))))
649 (defconst erc-dcc-ctcp-query-send-regexp
650 "^DCC SEND \\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) *\\([0-9]*\\)")
652 (defun erc-dcc-handle-ctcp-send (proc query nick login host to)
653 "This is called if a CTCP DCC SEND subcommand is sent to the client.
654 It extracts the information about the dcc request and adds it to
655 `erc-dcc-list'."
656 (unless (eq erc-dcc-send-request 'ignore)
657 (cond
658 ((not (erc-current-nick-p to))
659 ;; DCC SEND requests must be sent to you, and you alone.
660 (erc-display-message
661 nil 'notice proc
662 'dcc-request-bogus
663 ?r "SEND" ?n nick ?u login ?h host))
664 ((string-match erc-dcc-ctcp-query-send-regexp query)
665 (let ((filename (match-string 1 query))
666 (ip (erc-decimal-to-ip (match-string 2 query)))
667 (port (match-string 3 query))
668 (size (match-string 4 query)))
669 ;; FIXME: a warning really should also be sent
670 ;; if the ip address != the host the dcc sender is on.
671 (erc-display-message
672 nil 'notice proc
673 'dcc-send-offered
674 ?f filename ?n nick ?u login ?h host
675 ?s (if (string= size "") "unknown" size))
676 (and (< (string-to-number port) 1025)
677 (erc-display-message
678 nil 'notice proc
679 'dcc-privileged-port
680 ?p port))
681 (erc-dcc-list-add
682 'GET (format "%s!%s@%s" nick login host)
683 nil proc
684 :ip ip :port port :file filename
685 :size (string-to-number size))
686 (if (and (eq erc-dcc-send-request 'auto)
687 (erc-dcc-auto-mask-p (format "\"%s!%s@%s\"" nick login host)))
688 (erc-dcc-get-file (car erc-dcc-list) filename proc))))
690 (erc-display-message
691 nil 'notice proc
692 'dcc-malformed
693 ?n nick ?u login ?h host ?q query)))))
695 (defun erc-dcc-auto-mask-p (spec)
696 "Takes a full SPEC of a user in the form \"nick!login@host\" and
697 matches against all the regexp's in `erc-dcc-auto-masks'. If any
698 match, returns that regexp and nil otherwise."
699 (let ((lst erc-dcc-auto-masks))
700 (while (and lst
701 (not (string-match (car lst) spec)))
702 (setq lst (cdr lst)))
703 (and lst (car lst))))
705 (defconst erc-dcc-ctcp-query-chat-regexp
706 "^DCC CHAT +chat +\\([0-9]+\\) +\\([0-9]+\\)")
708 (defcustom erc-dcc-chat-request 'ask
709 "*How to treat incoming DCC Chat requests.
710 'ask - Report the Chat request, and wait for the user to manually accept it
711 'auto - Automatically accept the request and open a new chat window
712 'ignore - Ignore incoming DCC chat requests completely."
713 :group 'erc-dcc
714 :type '(choice (const ask) (const auto) (const ignore)))
716 (defun erc-dcc-handle-ctcp-chat (proc query nick login host to)
717 (unless (eq erc-dcc-chat-request 'ignore)
718 (cond
719 (;; DCC CHAT requests must be sent to you, and you alone.
720 (not (erc-current-nick-p to))
721 (erc-display-message
722 nil '(notice error) proc
723 'dcc-request-bogus ?r "CHAT" ?n nick ?u login ?h host))
724 ((string-match erc-dcc-ctcp-query-chat-regexp query)
725 ;; We need to use let* here, since erc-dcc-member might clutter
726 ;; the match value.
727 (let* ((ip (erc-decimal-to-ip (match-string 1 query)))
728 (port (match-string 2 query))
729 (elt (erc-dcc-member :nick nick :type 'CHAT)))
730 ;; FIXME: A warning really should also be sent if the ip
731 ;; address != the host the dcc sender is on.
732 (erc-display-message
733 nil 'notice proc
734 'dcc-chat-offered
735 ?n nick ?u login ?h host ?p port)
736 (and (< (string-to-number port) 1025)
737 (erc-display-message
738 nil 'notice proc
739 'dcc-privileged-port ?p port))
740 (cond (elt
741 ;; XXX: why are we updating ip/port on the existing connection?
742 (setq elt (plist-put (plist-put elt :port port) :ip ip))
743 (erc-display-message
744 nil 'notice proc
745 'dcc-chat-discarded ?n nick ?u login ?h host))
747 (erc-dcc-list-add
748 'CHAT (format "%s!%s@%s" nick login host)
749 nil proc
750 :ip ip :port port)))
751 (if (eq erc-dcc-chat-request 'auto)
752 (erc-dcc-chat-accept (erc-dcc-member :nick nick :type 'CHAT)
753 proc))))
755 (erc-display-message
756 nil '(notice error) proc
757 'dcc-malformed ?n nick ?u login ?h host ?q query)))))
760 (defvar erc-dcc-entry-data nil
761 "Holds the `erc-dcc-list' entry for this DCC connection.")
762 (make-variable-buffer-local 'erc-dcc-entry-data)
764 ;;; SEND handling
766 (defcustom erc-dcc-block-size 1024
767 "*Block size to use for DCC SEND sessions."
768 :group 'erc-dcc
769 :type 'integer)
771 (defcustom erc-dcc-pump-bytes nil
772 "*If set to an integer, keep sending until that number of bytes are
773 unconfirmed."
774 :group 'erc-dcc
775 :type '(choice (const nil) integer))
777 (defsubst erc-dcc-get-parent (proc)
778 (plist-get (erc-dcc-member :peer proc) :parent))
780 (defun erc-dcc-send-block (proc)
781 "Send one block of data.
782 PROC is the process-object of the DCC connection. Returns the number of
783 bytes sent."
784 (let* ((elt (erc-dcc-member :peer proc))
785 (confirmed-marker (plist-get elt :sent))
786 (sent-marker (plist-get elt :sent)))
787 (with-current-buffer (process-buffer proc)
788 (when erc-dcc-verbose
789 (erc-display-message
790 nil 'notice (erc-dcc-get-parent proc)
791 (format "DCC: Confirmed %d, sent %d, sending block now"
792 (- confirmed-marker (point-min))
793 (- sent-marker (point-min)))))
794 (let* ((end (min (+ sent-marker erc-dcc-block-size)
795 (point-max)))
796 (string (buffer-substring-no-properties sent-marker end)))
797 (when (< sent-marker end)
798 (set-marker sent-marker end)
799 (process-send-string proc string))
800 (length string)))))
802 (defun erc-dcc-send-filter (proc string)
803 (let* ((size (erc-unpack-int string))
804 (elt (erc-dcc-member :peer proc))
805 (parent (plist-get elt :parent))
806 (sent-marker (plist-get elt :sent))
807 (confirmed-marker (plist-get elt :confirmed)))
808 (with-current-buffer (process-buffer proc)
809 (set-marker confirmed-marker (+ (point-min) size))
810 (cond
811 ((and (= confirmed-marker sent-marker)
812 (= confirmed-marker (point-max)))
813 (erc-display-message
814 nil 'notice parent
815 'dcc-send-finished
816 ?n (plist-get elt :nick)
817 ?f buffer-file-name
818 ?s (number-to-string (- sent-marker (point-min))))
819 (setq erc-dcc-list (delete elt erc-dcc-list))
820 (set-buffer-modified-p nil)
821 (kill-buffer (current-buffer))
822 (delete-process proc))
823 ((<= confirmed-marker sent-marker)
824 (while (and (< (- sent-marker confirmed-marker)
825 (or erc-dcc-pump-bytes
826 erc-dcc-block-size))
827 (> (erc-dcc-send-block proc) 0))))
828 ((> confirmed-marker sent-marker)
829 (erc-display-message
830 nil 'notice parent
831 (format "DCC: Client confirmed too much (%s vs %s)!"
832 (marker-position confirmed-marker)
833 (marker-position sent-marker)))
834 (set-buffer-modified-p nil)
835 (kill-buffer (current-buffer))
836 (delete-process proc))))))
838 (defun erc-dcc-display-send (proc)
839 (erc-display-message
840 nil 'notice (erc-dcc-get-parent proc)
841 (format "DCC: SEND connect from %s"
842 (format-network-address (process-contact proc :remote)))))
844 (defcustom erc-dcc-send-connect-hook
845 '(erc-dcc-display-send erc-dcc-send-block)
846 "*Hook run whenever the remote end of a DCC SEND offer connected to your
847 listening port."
848 :group 'erc-dcc
849 :type 'hook)
851 (defun erc-dcc-nick (plist)
852 "Extract the nickname portion of the :nick property value in PLIST."
853 (erc-extract-nick (plist-get plist :nick)))
855 (defun erc-dcc-send-sentinel (proc event)
856 (let* ((elt (erc-dcc-member :peer proc)))
857 (cond
858 ((string-match "^open from " event)
859 (when elt
860 (let ((buf (marker-buffer (plist-get elt :sent))))
861 (with-current-buffer buf
862 (set-process-buffer proc buf)
863 (setq erc-dcc-entry-data elt)))
864 (run-hook-with-args 'erc-dcc-send-connect-hook proc))))))
866 (defun erc-dcc-find-file (file)
867 (with-current-buffer (generate-new-buffer (file-name-nondirectory file))
868 (insert-file-contents-literally file)
869 (setq buffer-file-name file)
870 (current-buffer)))
872 (defun erc-dcc-file-to-name (file)
873 (with-temp-buffer
874 (insert (file-name-nondirectory file))
875 (subst-char-in-region (point-min) (point-max) ? ?_ t)
876 (buffer-string)))
878 (defun erc-dcc-send-file (nick file &optional pproc)
879 "Open a socket for incoming connections, and send a CTCP send request to the
880 other client."
881 (interactive "sNick: \nfFile: ")
882 (when (null pproc) (if (processp erc-server-process)
883 (setq pproc erc-server-process)
884 (error "Can not find parent process")))
885 (if (featurep 'make-network-process)
886 (let* ((buffer (erc-dcc-find-file file))
887 (size (buffer-size buffer))
888 (start (with-current-buffer buffer
889 (set-marker (make-marker) (point-min))))
890 (sproc (erc-dcc-server "dcc-send"
891 'erc-dcc-send-filter
892 'erc-dcc-send-sentinel))
893 (contact (process-contact sproc)))
894 (erc-dcc-list-add
895 'SEND nick sproc pproc
896 :file file :size size
897 :sent start :confirmed (copy-marker start))
898 (process-send-string
899 pproc (format "PRIVMSG %s :\C-aDCC SEND %s %s %d %d\C-a\n"
900 nick (erc-dcc-file-to-name file)
901 (erc-ip-to-decimal (or erc-dcc-public-host
902 (nth 0 contact)))
903 (nth 1 contact)
904 size)))
905 (error "`make-network-process' not supported by your Emacs")))
907 ;;; GET handling
909 (defcustom erc-dcc-receive-cache (* 1024 512)
910 "Number of bytes to let the receive buffer grow before flushing it."
911 :group 'erc-dcc
912 :type 'integer)
914 (defvar erc-dcc-file-name nil)
915 (make-variable-buffer-local 'erc-dcc-file-name)
917 (defun erc-dcc-get-file (entry file parent-proc)
918 "This function does the work of setting up a transfer from the remote client
919 to the local one over a tcp connection. This involves setting up a process
920 filter and a process sentinel, and making the connection."
921 (let* ((buffer (generate-new-buffer (file-name-nondirectory file)))
922 proc)
923 (with-current-buffer buffer
924 (fundamental-mode)
925 (buffer-disable-undo (current-buffer))
926 ;; This is necessary to have the buffer saved as-is in GNU
927 ;; Emacs.
928 ;; XEmacs change: We don't have `set-buffer-multibyte', setting
929 ;; coding system to 'binary below takes care of us.
930 (when (fboundp 'set-buffer-multibyte)
931 (set-buffer-multibyte nil))
933 (setq mode-line-process '(":%s")
934 buffer-file-type t
935 buffer-read-only t)
936 (setq erc-dcc-file-name file)
938 ;; Truncate the given file to size 0 before appending to it.
939 (let ((inhibit-file-name-handlers
940 (append '(jka-compr-handler image-file-handler)
941 inhibit-file-name-handlers))
942 (inhibit-file-name-operation 'write-region))
943 (write-region (point) (point) erc-dcc-file-name nil 'nomessage))
945 (setq erc-server-process parent-proc
946 erc-dcc-entry-data entry)
947 (setq erc-dcc-byte-count 0)
948 (setq proc
949 (funcall erc-dcc-connect-function
950 "dcc-get" buffer
951 (plist-get entry :ip)
952 (string-to-number (plist-get entry :port))
953 entry))
954 (set-process-buffer proc buffer)
955 (set-process-coding-system proc 'binary 'binary)
956 (set-buffer-file-coding-system 'binary t)
958 (set-process-filter proc 'erc-dcc-get-filter)
959 (set-process-sentinel proc 'erc-dcc-get-sentinel)
960 (setq entry (plist-put entry :start-time (erc-current-time)))
961 (setq entry (plist-put entry :peer proc)))))
963 (defun erc-dcc-append-contents (buffer file)
964 "Append the contents of BUFFER to FILE.
965 The contents of the BUFFER will then be erased."
966 (with-current-buffer buffer
967 (let ((coding-system-for-write 'binary)
968 (inhibit-read-only t)
969 (inhibit-file-name-handlers
970 (append '(jka-compr-handler image-file-handler)
971 inhibit-file-name-handlers))
972 (inhibit-file-name-operation 'write-region))
973 (write-region (point-min) (point-max) erc-dcc-file-name t 'nomessage)
974 (setq erc-dcc-byte-count (+ (buffer-size) erc-dcc-byte-count))
975 (erase-buffer))))
977 (defun erc-dcc-get-filter (proc str)
978 "This is the process filter for transfers from other clients to this one.
979 It reads incoming bytes from the network and stores them in the DCC
980 buffer, and sends back the replies after each block of data per the DCC
981 protocol spec. Well not really. We write back a reply after each read,
982 rather than every 1024 byte block, but nobody seems to care."
983 (with-current-buffer (process-buffer proc)
984 (let ((inhibit-read-only t)
985 received-bytes)
986 (goto-char (point-max))
987 (insert (string-make-unibyte str))
989 (when (> (point-max) erc-dcc-receive-cache)
990 (erc-dcc-append-contents (current-buffer) erc-dcc-file-name))
991 (setq received-bytes (+ (buffer-size) erc-dcc-byte-count))
993 (and erc-dcc-verbose
994 (erc-display-message
995 nil 'notice erc-server-process
996 'dcc-get-bytes-received
997 ?f (file-name-nondirectory buffer-file-name)
998 ?b (number-to-string received-bytes)))
999 (cond
1000 ((and (> (plist-get erc-dcc-entry-data :size) 0)
1001 (> received-bytes (plist-get erc-dcc-entry-data :size)))
1002 (erc-display-message
1003 nil '(error notice) 'active
1004 'dcc-get-file-too-long
1005 ?f (file-name-nondirectory buffer-file-name))
1006 (delete-process proc))
1008 (process-send-string
1009 proc (erc-pack-int received-bytes)))))))
1012 (defun erc-dcc-get-sentinel (proc event)
1013 "This is the process sentinel for CTCP DCC SEND connections.
1014 It shuts down the connection and notifies the user that the
1015 transfer is complete."
1016 ;; FIXME, we should look at EVENT, and also check size.
1017 (with-current-buffer (process-buffer proc)
1018 (delete-process proc)
1019 (setq erc-dcc-list (delete erc-dcc-entry-data erc-dcc-list))
1020 (unless (= (point-min) (point-max))
1021 (erc-dcc-append-contents (current-buffer) erc-dcc-file-name))
1022 (erc-display-message
1023 nil 'notice erc-server-process
1024 'dcc-get-complete
1025 ?f erc-dcc-file-name
1026 ?s (number-to-string erc-dcc-byte-count)
1027 ?t (format "%.0f"
1028 (erc-time-diff (plist-get erc-dcc-entry-data :start-time)
1029 (erc-current-time)))))
1030 (kill-buffer (process-buffer proc))
1031 (delete-process proc))
1033 ;;; CHAT handling
1035 (defcustom erc-dcc-chat-buffer-name-format "DCC-CHAT-%s"
1036 "*Format to use for DCC Chat buffer names."
1037 :group 'erc-dcc
1038 :type 'string)
1040 (defcustom erc-dcc-chat-mode-hook nil
1041 "*Hook calls when `erc-dcc-chat-mode' finished setting up the buffer."
1042 :group 'erc-dcc
1043 :type 'hook)
1045 (defcustom erc-dcc-chat-connect-hook nil
1047 :group 'erc-dcc
1048 :type 'hook)
1050 (defcustom erc-dcc-chat-exit-hook nil
1052 :group 'erc-dcc
1053 :type 'hook)
1055 (defun erc-cmd-CREQ (line &optional force)
1056 "Set or get the DCC chat request flag.
1057 Possible values are: ask, auto, ignore."
1058 (when (string-match "^\\s-*\\(auto\\|ask\\|ignore\\)?$" line)
1059 (let ((cmd (match-string 1 line)))
1060 (if (stringp cmd)
1061 (erc-display-message
1062 nil 'notice 'active
1063 (format "Set DCC Chat requests to %S"
1064 (setq erc-dcc-chat-request (intern cmd))))
1065 (erc-display-message nil 'notice 'active
1066 (format "DCC Chat requests are set to %S"
1067 erc-dcc-chat-request)))
1068 t)))
1070 (defun erc-cmd-SREQ (line &optional force)
1071 "Set or get the DCC send request flag.
1072 Possible values are: ask, auto, ignore."
1073 (when (string-match "^\\s-*\\(auto\\|ask\\|ignore\\)?$" line)
1074 (let ((cmd (match-string 1 line)))
1075 (if (stringp cmd)
1076 (erc-display-message
1077 nil 'notice 'active
1078 (format "Set DCC Send requests to %S"
1079 (setq erc-dcc-send-request (intern cmd))))
1080 (erc-display-message nil 'notice 'active
1081 (format "DCC Send requests are set to %S"
1082 erc-dcc-send-request)))
1083 t)))
1085 (defun pcomplete/erc-mode/CREQ ()
1086 (pcomplete-here '("auto" "ask" "ignore")))
1087 (defalias 'pcomplete/erc-mode/SREQ 'pcomplete/erc-mode/CREQ)
1089 (defvar erc-dcc-chat-filter-hook '(erc-dcc-chat-parse-output)
1090 "*Hook to run after doing parsing (and possible insertion) of DCC messages.")
1092 (defvar erc-dcc-chat-mode-map
1093 (let ((map (make-sparse-keymap)))
1094 (define-key map (kbd "RET") 'erc-send-current-line)
1095 (define-key map "\t" 'erc-complete-word)
1096 map)
1097 "Keymap for `erc-dcc-mode'.")
1099 (defun erc-dcc-chat-mode ()
1100 "Major mode for wasting time via DCC chat."
1101 (interactive)
1102 (kill-all-local-variables)
1103 (setq mode-line-process '(":%s")
1104 mode-name "DCC-Chat"
1105 major-mode 'erc-dcc-chat-mode
1106 erc-send-input-line-function 'erc-dcc-chat-send-input-line
1107 erc-default-recipients '(dcc))
1108 (use-local-map erc-dcc-chat-mode-map)
1109 (run-hooks 'erc-dcc-chat-mode-hook))
1111 (defun erc-dcc-chat-send-input-line (recipient line &optional force)
1112 "Send LINE to the remote end.
1113 Argument RECIPIENT should always be the symbol dcc, and force
1114 is ignored."
1115 ;; FIXME: We need to get rid of all force arguments one day!
1116 (if (eq recipient 'dcc)
1117 (process-send-string
1118 (get-buffer-process (current-buffer)) line)
1119 (error "erc-dcc-chat-send-input-line in %s" (current-buffer))))
1121 (defun erc-dcc-chat (nick &optional pproc)
1122 "Open a socket for incoming connections, and send a chat request to the
1123 other client."
1124 (interactive "sNick: ")
1125 (when (null pproc) (if (processp erc-server-process)
1126 (setq pproc erc-server-process)
1127 (error "Can not find parent process")))
1128 (let* ((sproc (erc-dcc-server "dcc-chat-out"
1129 'erc-dcc-chat-filter
1130 'erc-dcc-chat-sentinel))
1131 (contact (process-contact sproc)))
1132 (erc-dcc-list-add 'OCHAT nick sproc pproc)
1133 (process-send-string pproc
1134 (format "PRIVMSG %s :\C-aDCC CHAT chat %s %d\C-a\n"
1135 nick
1136 (erc-ip-to-decimal (nth 0 contact)) (nth 1 contact)))))
1138 (defvar erc-dcc-from)
1139 (make-variable-buffer-local 'erc-dcc-from)
1141 (defvar erc-dcc-unprocessed-output)
1142 (make-variable-buffer-local 'erc-dcc-unprocessed-output)
1144 (defun erc-dcc-chat-setup (entry)
1145 "Setup a DCC chat buffer, returning the buffer."
1146 (let* ((nick (erc-extract-nick (plist-get entry :nick)))
1147 (buffer (generate-new-buffer
1148 (format erc-dcc-chat-buffer-name-format nick)))
1149 (proc (plist-get entry :peer))
1150 (parent-proc (plist-get entry :parent)))
1151 (erc-setup-buffer buffer)
1152 ;; buffer is now the current buffer.
1153 (erc-dcc-chat-mode)
1154 (setq erc-server-process parent-proc)
1155 (setq erc-dcc-from nick)
1156 (setq erc-dcc-entry-data entry)
1157 (setq erc-dcc-unprocessed-output "")
1158 (setq erc-insert-marker (set-marker (make-marker) (point-max)))
1159 (setq erc-input-marker (make-marker))
1160 (erc-display-prompt buffer (point-max))
1161 (set-process-buffer proc buffer)
1162 (add-hook 'kill-buffer-hook 'erc-dcc-chat-buffer-killed nil t)
1163 (run-hook-with-args 'erc-dcc-chat-connect-hook proc)
1164 buffer))
1166 (defun erc-dcc-chat-accept (entry parent-proc)
1167 "Accept an incoming DCC connection and open a DCC window"
1168 (let* ((nick (erc-extract-nick (plist-get entry :nick)))
1169 buffer proc)
1170 (setq proc
1171 (funcall erc-dcc-connect-function
1172 "dcc-chat" nil
1173 (plist-get entry :ip)
1174 (string-to-number (plist-get entry :port))
1175 entry))
1176 ;; XXX: connected, should we kill the ip/port properties?
1177 (setq entry (plist-put entry :peer proc))
1178 (setq entry (plist-put entry :parent parent-proc))
1179 (set-process-filter proc 'erc-dcc-chat-filter)
1180 (set-process-sentinel proc 'erc-dcc-chat-sentinel)
1181 (setq buffer (erc-dcc-chat-setup entry))))
1183 (defun erc-dcc-chat-filter (proc str)
1184 (let ((orig-buffer (current-buffer)))
1185 (unwind-protect
1186 (progn
1187 (set-buffer (process-buffer proc))
1188 (setq erc-dcc-unprocessed-output
1189 (concat erc-dcc-unprocessed-output str))
1190 (run-hook-with-args 'erc-dcc-chat-filter-hook proc
1191 erc-dcc-unprocessed-output))
1192 (set-buffer orig-buffer))))
1194 (defun erc-dcc-chat-parse-output (proc str)
1195 (save-match-data
1196 (let ((posn 0)
1197 line)
1198 (while (string-match "\n" str posn)
1199 (setq line (substring str posn (match-beginning 0)))
1200 (setq posn (match-end 0))
1201 (erc-display-message
1202 nil nil proc
1203 'dcc-chat-privmsg ?n (erc-propertize erc-dcc-from 'face
1204 'erc-nick-default-face) ?m line))
1205 (setq erc-dcc-unprocessed-output (substring str posn)))))
1207 (defun erc-dcc-chat-buffer-killed ()
1208 (erc-dcc-chat-close "killed buffer"))
1210 (defun erc-dcc-chat-close (&optional event)
1211 "Close a DCC chat, removing any associated processes and tidying up
1212 `erc-dcc-list'"
1213 (let ((proc (plist-get erc-dcc-entry-data :peer))
1214 (evt (or event "")))
1215 (when proc
1216 (setq erc-dcc-list (delq erc-dcc-entry-data erc-dcc-list))
1217 (run-hook-with-args 'erc-dcc-chat-exit-hook proc)
1218 (delete-process proc)
1219 (erc-display-message
1220 nil 'notice erc-server-process
1221 'dcc-chat-ended ?n erc-dcc-from ?t (current-time-string) ?e evt)
1222 (setq erc-dcc-entry-data (plist-put erc-dcc-entry-data :peer nil)))))
1224 (defun erc-dcc-chat-sentinel (proc event)
1225 (let ((buf (current-buffer))
1226 (elt (erc-dcc-member :peer proc)))
1227 ;; the sentinel is also notified when the connection is opened, so don't
1228 ;; immediately kill it again
1229 ;(message "buf %s elt %S evt %S" buf elt event)
1230 (unwind-protect
1231 (if (string-match "^open from" event)
1232 (erc-dcc-chat-setup elt)
1233 (erc-dcc-chat-close event))
1234 (set-buffer buf))))
1236 (defun erc-dcc-no-such-nick (proc parsed)
1237 "Detect and handle no-such-nick replies from the IRC server."
1238 (let* ((elt (erc-dcc-member :nick (second (erc-response.command-args parsed))
1239 :parent proc))
1240 (peer (plist-get elt :peer)))
1241 (when (or (and (processp peer) (not (eq (process-status peer) 'open)))
1242 elt)
1243 ;; Since we already created an entry before sending the CTCP
1244 ;; message, we now remove it, if it doesn't point to a process
1245 ;; which is already open.
1246 (setq erc-dcc-list (delq elt erc-dcc-list))
1247 (if (processp peer) (delete-process peer)))
1248 nil))
1250 (provide 'erc-dcc)
1252 ;;; erc-dcc.el ends here
1254 ;; Local Variables:
1255 ;; indent-tabs-mode: nil
1256 ;; End:
1258 ;; arch-tag: cda5a6b3-c510-4dbe-b699-84cccfa04edb