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