1 ;;; erc-dcc.el --- CTCP DCC module for ERC
3 ;; Copyright (C) 1993, 1994, 1995, 1998, 2002, 2003, 2004, 2006, 2007, 2008
4 ;; 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/>.
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.
39 ;; To use this file, put
44 ;; /dcc chat nick - Either accept pending chat offer from nick, or offer
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.
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
)))
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
77 (defcustom erc-dcc-verbose nil
78 "*If non-nil, be verbose about DCC activity reporting."
82 (defvar erc-dcc-list nil
83 "List of DCC connections. Looks like:
84 ((:nick \"nick!user@host\" :type GET :peer proc :parent proc :size size :file file)
85 (:nick \"nick!user@host\" :type CHAT :peer proc :parent proc)
86 (:nick \"nick\" :type SEND :peer server-proc :parent parent-proc :file
87 file :sent <marker> :confirmed <marker>))
89 :nick - a user or userhost for the peer. combine with :parent to reach them
91 :type - the type of DCC connection - SEND for outgoing files, GET for
92 incoming, and CHAT for both directions. To tell which end started
93 the DCC chat, look at :peer
95 :peer - the other end of the DCC connection. In the case of outgoing DCCs,
96 this represents a server process until a connection is established
98 :parent - the server process where the dcc connection was established.
99 Note that this can be nil or an invalid process since a DCC
100 connection is in general independent from a particular server
101 connection after it was established.
103 :file - for outgoing sends, the full path to the file. for incoming sends,
104 the suggested filename or vetted filename
106 :size - size of the file, may be nil on incoming DCCs")
108 (defun erc-dcc-list-add (type nick peer parent
&rest args
)
109 "Add a new entry of type TYPE to `erc-dcc-list' and return it."
113 (append (list :nick nick
:type type
:peer peer
:parent parent
) args
)
116 ;; This function takes all the usual args as open-network-stream, plus one
117 ;; more: the entry data from erc-dcc-list for this particular process.
118 (defvar erc-dcc-connect-function
'erc-dcc-open-network-stream
)
120 (defun erc-dcc-open-network-stream (procname buffer addr port entry
)
121 (if nil
; (fboundp 'open-network-stream-nowait) ;; this currently crashes
123 (open-network-stream-nowait procname buffer addr port
)
124 (open-network-stream procname buffer addr port
)))
128 '((dcc-chat-discarded
129 .
"DCC: previous chat request from %n (%u@%h) discarded")
130 (dcc-chat-ended .
"DCC: chat with %n ended %t: %e")
131 (dcc-chat-no-request .
"DCC: chat request from %n not found")
132 (dcc-chat-offered .
"DCC: chat offered by %n (%u@%h:%p)")
133 (dcc-chat-offer .
"DCC: offering chat to %n")
134 (dcc-chat-accept .
"DCC: accepting chat from %n")
135 (dcc-chat-privmsg .
"=%n= %m")
136 (dcc-closed .
"DCC: Closed %T from %n")
137 (dcc-command-undefined
138 .
"DCC: %c undefined subcommand. GET, CHAT and LIST are defined.")
139 (dcc-ctcp-errmsg .
"DCC: `%s' is not a DCC subcommand known to this client")
140 (dcc-ctcp-unknown .
"DCC: unknown dcc command `%q' from %n (%u@%h)")
141 (dcc-get-bytes-received .
"DCC: %f: %b bytes received")
143 .
"DCC: file %f transfer complete (%s bytes in %t seconds)")
144 (dcc-get-cmd-aborted .
"DCC: Aborted getting %f from %n")
145 (dcc-get-file-too-long
146 .
"DCC: %f: File longer than sender claimed; aborting transfer")
147 (dcc-get-notfound .
"DCC: %n hasn't offered %f for DCC transfer")
148 (dcc-list-head .
"DCC: From Type Active Size Filename")
149 (dcc-list-line .
"DCC: -------- ---- ------ ------------ --------")
150 (dcc-list-item .
"DCC: %-8n %-4t %-6a %-12s %f")
151 (dcc-list-end .
"DCC: End of list.")
152 (dcc-malformed .
"DCC: error: %n (%u@%h) sent malformed request: %q")
154 .
"DCC: possibly bogus request: %p is a privileged port.")
155 (dcc-request-bogus .
"DCC: bogus dcc `%r' from %n (%u@%h)")
156 (dcc-send-finished .
"DCC: SEND of %f to %n finished (size %s)")
157 (dcc-send-offered .
"DCC: file %f offered by %n (%u@%h) (size %s)")
158 (dcc-send-offer .
"DCC: offering %f to %n")))
160 ;;; Misc macros and utility functions
162 (defun erc-dcc-member (&rest args
)
163 "Return the first matching entry in `erc-dcc-list' which satisfies the
164 constraints given as a plist in ARGS. Returns nil on no match.
166 The property :nick is treated specially, if it contains a '!' character,
167 it is treated as a nick!user@host string, and compared with the :nick property
168 value of the individual elements using string-equal. Otherwise it is
169 compared with `erc-nick-equal-p' which is IRC case-insensitive."
170 (let ((list erc-dcc-list
)
172 ;; for each element in erc-dcc-list
173 (while (and list
(not result
))
174 (let ((elt (car list
))
177 ;; loop through the constraints
178 (while (and prem cont
)
179 (let ((prop (car prem
))
181 (setq prem
(cddr prem
)
182 ;; plist-member is a predicate in xemacs
183 test
(and (plist-member elt prop
)
184 (plist-get elt prop
)))
185 ;; if the property exists and is equal, we continue, else, try the
186 ;; next element of the list
187 (or (and (eq prop
:nick
) (string-match "!" val
)
188 test
(string-equal test val
))
192 (erc-extract-nick test
)
193 (erc-extract-nick val
)))
199 (setq list
(cdr list
)))))
202 (defun erc-pack-int (value)
203 "Convert an integer into a packed string."
204 (let* ((len (ceiling (/ value
256.0)))
205 (str (make-string len ?a
))
208 (aset str i
(% value
256))
209 (setq value
(/ value
256))
213 (defun erc-unpack-int (str)
214 "Unpack a packed string into an integer."
215 (let ((len (length str
))
219 (setq num
(+ num
(lsh (aref str
(- len count
1)) (* 8 count
))))
220 (setq count
(1+ count
)))
223 (defconst erc-dcc-ipv4-regexp
225 (mapconcat #'identity
(make-list 4 "\\([0-9]\\{1,3\\}\\)") "\\.")
228 (defun erc-ip-to-decimal (ip)
229 "Convert IP address to its decimal representation.
230 Argument IP is the address as a string. The result is also a string."
231 (interactive "sIP Address: ")
232 (if (not (string-match erc-dcc-ipv4-regexp ip
))
233 (error "Not an IP address")
236 (let ((n (string-to-number str
)))
237 (if (and (>= n
0) (< n
256))
239 (error "%d out of range" n
))))
240 (split-string ip
"\\.")))
241 (res (+ (* (car ips
) 16777216.0)
242 (* (nth 1 ips
) 65536.0)
243 (* (nth 2 ips
) 256.0)
246 (message "%s is %.0f" ip res
)
247 (format "%.0f" res
)))))
249 (defun erc-decimal-to-ip (dec)
250 "Convert a decimal representation DEC to an IP address.
251 The result is also a string."
253 (setq dec
(string-to-number (concat dec
".0"))))
254 (let* ((first (floor (/ dec
16777216.0)))
255 (first-rest (- dec
(* first
16777216.0)))
256 (second (floor (/ first-rest
65536.0)))
257 (second-rest (- first-rest
(* second
65536.0)))
258 (third (floor (/ second-rest
256.0)))
259 (third-rest (- second-rest
(* third
256.0)))
260 (fourth (floor third-rest
)))
261 (format "%s.%s.%s.%s" first second third fourth
)))
265 (defcustom erc-dcc-listen-host nil
266 "IP address to listen on when offering files.
267 Should be set to a string or nil. If nil, automatic detection of
268 the host interface to use will be attempted."
270 :type
(list 'choice
(list 'const
:tag
"Auto-detect" nil
)
271 (list 'string
:tag
"IP-address"
272 :valid-regexp erc-dcc-ipv4-regexp
)))
274 (defcustom erc-dcc-public-host nil
275 "IP address to use for outgoing DCC offers.
276 Should be set to a string or nil. If nil, use the value of
277 `erc-dcc-listen-host'."
279 :type
(list 'choice
(list 'const
:tag
"Same as erc-dcc-listen-host" nil
)
280 (list 'string
:tag
"IP-address"
281 :valid-regexp erc-dcc-ipv4-regexp
)))
283 (defcustom erc-dcc-send-request
'ask
284 "*How to treat incoming DCC Send requests.
285 'ask - Report the Send request, and wait for the user to manually accept it
286 You might want to set `erc-dcc-auto-masks' for this.
287 'auto - Automatically accept the request and begin downloading the file
288 'ignore - Ignore incoming DCC Send requests completely."
290 :type
'(choice (const ask
) (const auto
) (const ignore
)))
292 (defun erc-dcc-get-host (proc)
293 "Returns the local IP address used for an open PROCess."
294 (format-network-address (process-contact proc
:local
) t
))
296 (defun erc-dcc-host ()
297 "Determine the IP address we are using.
298 If variable `erc-dcc-host' is non-nil, use it. Otherwise call
299 `erc-dcc-get-host' on the erc-server-process."
300 (or erc-dcc-listen-host
(erc-dcc-get-host erc-server-process
)
301 (error "Unable to determine local address")))
303 (defcustom erc-dcc-port-range nil
304 "If nil, any available user port is used for outgoing DCC connections.
305 If set to a cons, it specifies a range of ports to use in the form (min . max)"
308 (const :tag
"Any port" nil
)
309 (cons :tag
"Port range"
310 (integer :tag
"Lower port")
311 (integer :tag
"Upper port"))))
313 (defcustom erc-dcc-auto-masks nil
314 "List of regexps matching user identifiers whose DCC send offers should be
315 accepted automatically. A user identifier has the form \"nick!login@host\".
316 For instance, to accept all incoming DCC send offers automatically, add the
317 string \".*!.*@.*\" to this list."
319 :type
'(repeat regexp
))
321 (defun erc-dcc-server (name filter sentinel
)
322 "Start listening on a port for an incoming DCC connection. Returns the newly
323 created subprocess, or nil."
324 (let ((port (or (and erc-dcc-port-range
(car erc-dcc-port-range
)) t
))
325 (upper (and erc-dcc-port-range
(cdr erc-dcc-port-range
)))
331 (make-network-process :name name
339 :log
#'erc-dcc-server-accept
341 (when (processp process
)
342 (when (fboundp 'set-process-coding-system
)
343 (set-process-coding-system process
'binary
'binary
))
344 (when (fboundp 'set-process-filter-multibyte
)
345 (set-process-filter-multibyte process nil
))))
347 (unless (and (string= "Cannot bind server socket" (cadr err
))
348 (string= "address already in use" (caddr err
)))
349 (signal (car err
) (cdr err
)))
350 (setq port
(1+ port
))
351 (unless (< port upper
)
352 (error "No available ports in erc-dcc-port-range")))))
355 (defun erc-dcc-server-accept (server client message
)
356 "Log an accepted DCC offer, then terminate the listening process and set up
357 the accepted connection."
358 (erc-log (format "(erc-dcc-server-accept): server %s client %s message %s"
359 server client message
))
360 (when (and (string-match "^accept from " message
)
361 (processp server
) (processp client
))
362 (let ((elt (erc-dcc-member :peer server
)))
363 ;; change the entry in erc-dcc-list from the listening process to the
365 (setq elt
(plist-put elt
:peer client
))
366 ;; delete the listening process, as we've accepted the connection
367 (delete-process server
))))
369 ;;; Interactive command handling
371 (defcustom erc-dcc-get-default-directory nil
372 "*Default directory for incoming DCC file transfers.
373 If this is nil, then the current value of `default-directory' is used."
375 :type
'(choice (const nil
:tag
"Default directory") directory
))
378 (defun erc-cmd-DCC (cmd &rest args
)
379 "Parser for /dcc command.
380 This figures out the dcc subcommand and calls the appropriate routine to
381 handle it. The function dispatched should be named \"erc-dcc-do-FOO-command\",
382 where FOO is one of CLOSE, GET, SEND, LIST, CHAT, etc."
384 (let ((fn (intern-soft (concat "erc-dcc-do-" (upcase cmd
) "-command"))))
386 (apply fn erc-server-process args
)
389 'dcc-command-undefined ?c cmd
)
390 (apropos "erc-dcc-do-.*-command")
394 (defun pcomplete/erc-mode
/DCC
()
395 "Provides completion for the /DCC command."
396 (pcomplete-here (append '("chat" "close" "get" "list")
397 (when (fboundp 'make-network-process
) '("send"))))
399 (case (intern (downcase (pcomplete-arg 1)))
400 (chat (mapcar (lambda (elt) (plist-get elt
:nick
))
403 (eq (plist-get elt
:type
) 'CHAT
))
405 (close (remove-duplicates
406 (mapcar (lambda (elt) (symbol-name (plist-get elt
:type
)))
407 erc-dcc-list
) :test
'string
=))
408 (get (mapcar #'erc-dcc-nick
411 (eq (plist-get elt
:type
) 'GET
))
413 (send (pcomplete-erc-all-nicks))))
415 (case (intern (downcase (pcomplete-arg 2)))
416 (get (mapcar (lambda (elt) (plist-get elt
:file
))
419 (and (eq (plist-get elt
:type
) 'GET
)
420 (erc-nick-equal-p (erc-extract-nick
421 (plist-get elt
:nick
))
424 (close (mapcar #'erc-dcc-nick
427 (eq (plist-get elt
:type
)
428 (intern (upcase (pcomplete-arg 1)))))
430 (send (pcomplete-entries)))))
432 (defun erc-dcc-do-CHAT-command (proc &optional nick
)
434 (let ((elt (erc-dcc-member :nick nick
:type
'CHAT
:parent proc
)))
435 (if (and elt
(not (processp (plist-get elt
:peer
))))
436 ;; accept an existing chat offer
437 ;; FIXME: perhaps /dcc accept like other clients?
438 (progn (erc-dcc-chat-accept elt erc-server-process
)
441 'dcc-chat-accept ?n nick
)
443 (erc-dcc-chat nick erc-server-process
)
446 'dcc-chat-offer ?n nick
)
449 (defun erc-dcc-do-CLOSE-command (proc &optional type nick
)
450 "/dcc close type nick
451 type and nick are optional."
452 ;; FIXME, should also work if only nick is specified
453 (when (string-match (concat "^\\s-*\\(\\S-+\\)? *\\("
454 erc-valid-nick-regexp
"\\)?\\s-*$") line
)
455 (let ((type (when (match-string 1 line
)
456 (intern (upcase (match-string 1 line
)))))
457 (nick (match-string 2 line
))
461 (setq ret
(erc-dcc-member :type type
:nick nick
))
462 (setq ret
(erc-dcc-member :type type
)))
464 ;; found a match - delete process if it exists.
465 (and (processp (plist-get ret
:peer
))
466 (delete-process (plist-get ret
:peer
)))
467 (setq erc-dcc-list
(delq ret erc-dcc-list
))
471 ?T
(plist-get ret
:type
)
472 ?n
(erc-extract-nick (plist-get ret
:nick
))))))
475 (defun erc-dcc-do-GET-command (proc nick
&rest file
)
476 "Do a DCC GET command. NICK is the person who is sending the file.
477 FILE is the filename. If FILE is split into multiple arguments,
478 re-join the arguments, separated by a space.
479 PROC is the server process."
480 (setq file
(and file
(mapconcat #'identity file
" ")))
481 (let* ((elt (erc-dcc-member :nick nick
:type
'GET
))
482 (filename (or file
(plist-get elt
:file
) "unknown")))
484 (let* ((file (read-file-name
485 (format "Local filename (default %s): "
486 (file-name-nondirectory filename
))
487 (or erc-dcc-get-default-directory
489 (expand-file-name (file-name-nondirectory filename
)
490 (or erc-dcc-get-default-directory
491 default-directory
)))))
492 (cond ((file-exists-p file
)
493 (if (yes-or-no-p (format "File %s exists. Overwrite? "
495 (erc-dcc-get-file elt file proc
)
497 nil
'(notice error
) proc
499 ?n nick ?f filename
)))
501 (erc-dcc-get-file elt file proc
))))
503 nil
'(notice error
) 'active
504 'dcc-get-notfound ?n nick ?f filename
))))
506 (defun erc-dcc-do-LIST-command (proc)
507 "This is the handler for the /dcc list command.
508 It lists the current state of `erc-dcc-list' in an easy to read manner."
509 (let ((alist erc-dcc-list
)
518 (setq elt
(car alist
)
521 (setq size
(or (and (plist-member elt
:size
)
522 (plist-get elt
:size
))
525 (cond ((null size
) "")
526 ((numberp size
) (number-to-string size
))
527 ((string= size
"") "unknown")))
531 ?n
(erc-dcc-nick elt
)
532 ?t
(plist-get elt
:type
)
533 ?a
(if (processp (plist-get elt
:peer
))
534 (process-status (plist-get elt
:peer
))
537 (if (and (eq 'GET
(plist-get elt
:type
))
538 (plist-member elt
:file
)
539 (buffer-live-p (get-buffer (plist-get elt
:file
)))
540 (plist-member elt
:size
))
541 (concat " (" (number-to-string
544 (get-buffer (plist-get elt
:file
)))
545 (plist-get elt
:size
))))
547 ?f
(or (and (plist-member elt
:file
) (plist-get elt
:file
)) "")))
553 (defun erc-dcc-do-SEND-command (proc nick
&rest file
)
554 "Offer FILE to NICK by sending a ctcp dcc send message.
555 If FILE is split into multiple arguments, re-join the arguments,
556 separated by a space."
557 (setq file
(and file
(mapconcat #'identity file
" ")))
558 (if (file-exists-p file
)
562 'dcc-send-offer ?n nick ?f file
)
563 (erc-dcc-send-file nick file
) t
)
564 (erc-display-message nil
'(notice error
) proc
"File not found") t
))
566 ;;; Server message handling (i.e. messages from remote users)
569 (defvar erc-ctcp-query-DCC-hook
'(erc-ctcp-query-DCC)
570 "Hook variable for CTCP DCC queries")
572 (defvar erc-dcc-query-handler-alist
573 '(("SEND" . erc-dcc-handle-ctcp-send
)
574 ("CHAT" . erc-dcc-handle-ctcp-chat
)))
577 (defun erc-ctcp-query-DCC (proc nick login host to query
)
578 "The function called when a CTCP DCC request is detected by the client.
579 It examines the DCC subcommand, and calls the appropriate routine for
581 (let* ((cmd (cadr (split-string query
" ")))
582 (handler (cdr (assoc cmd erc-dcc-query-handler-alist
))))
584 (funcall handler proc query nick login host to
)
585 ;; FIXME: Send a ctcp error notice to the remote end?
587 nil
'(notice error
) proc
589 ?q query ?n nick ?u login ?h host
))))
591 (defconst erc-dcc-ctcp-query-send-regexp
592 "^DCC SEND \\([^ ]+\\) \\([0-9]+\\) \\([0-9]+\\) *\\([0-9]*\\)")
594 (defun erc-dcc-handle-ctcp-send (proc query nick login host to
)
595 "This is called if a CTCP DCC SEND subcommand is sent to the client.
596 It extracts the information about the dcc request and adds it to
598 (unless (eq erc-dcc-send-request
'ignore
)
600 ((not (erc-current-nick-p to
))
601 ;; DCC SEND requests must be sent to you, and you alone.
605 ?r
"SEND" ?n nick ?u login ?h host
))
606 ((string-match erc-dcc-ctcp-query-send-regexp query
)
607 (let ((filename (match-string 1 query
))
608 (ip (erc-decimal-to-ip (match-string 2 query
)))
609 (port (match-string 3 query
))
610 (size (match-string 4 query
)))
611 ;; FIXME: a warning really should also be sent
612 ;; if the ip address != the host the dcc sender is on.
616 ?f filename ?n nick ?u login ?h host
617 ?s
(if (string= size
"") "unknown" size
))
618 (and (< (string-to-number port
) 1025)
624 'GET
(format "%s!%s@%s" nick login host
)
626 :ip ip
:port port
:file filename
627 :size
(string-to-number size
))
628 (if (and (eq erc-dcc-send-request
'auto
)
629 (erc-dcc-auto-mask-p (format "\"%s!%s@%s\"" nick login host
)))
630 (erc-dcc-get-file (car erc-dcc-list
) filename proc
))))
635 ?n nick ?u login ?h host ?q query
)))))
637 (defun erc-dcc-auto-mask-p (spec)
638 "Takes a full SPEC of a user in the form \"nick!login@host\" and
639 matches against all the regexp's in `erc-dcc-auto-masks'. If any
640 match, returns that regexp and nil otherwise."
641 (let ((lst erc-dcc-auto-masks
))
643 (not (string-match (car lst
) spec
)))
644 (setq lst
(cdr lst
)))
645 (and lst
(car lst
))))
647 (defconst erc-dcc-ctcp-query-chat-regexp
648 "^DCC CHAT +chat +\\([0-9]+\\) +\\([0-9]+\\)")
650 (defcustom erc-dcc-chat-request
'ask
651 "*How to treat incoming DCC Chat requests.
652 'ask - Report the Chat request, and wait for the user to manually accept it
653 'auto - Automatically accept the request and open a new chat window
654 'ignore - Ignore incoming DCC chat requests completely."
656 :type
'(choice (const ask
) (const auto
) (const ignore
)))
658 (defun erc-dcc-handle-ctcp-chat (proc query nick login host to
)
659 (unless (eq erc-dcc-chat-request
'ignore
)
661 (;; DCC CHAT requests must be sent to you, and you alone.
662 (not (erc-current-nick-p to
))
664 nil
'(notice error
) proc
665 'dcc-request-bogus ?r
"CHAT" ?n nick ?u login ?h host
))
666 ((string-match erc-dcc-ctcp-query-chat-regexp query
)
667 ;; We need to use let* here, since erc-dcc-member might clutter
669 (let* ((ip (erc-decimal-to-ip (match-string 1 query
)))
670 (port (match-string 2 query
))
671 (elt (erc-dcc-member :nick nick
:type
'CHAT
)))
672 ;; FIXME: A warning really should also be sent if the ip
673 ;; address != the host the dcc sender is on.
677 ?n nick ?u login ?h host ?p port
)
678 (and (< (string-to-number port
) 1025)
681 'dcc-privileged-port ?p port
))
683 ;; XXX: why are we updating ip/port on the existing connection?
684 (setq elt
(plist-put (plist-put elt
:port port
) :ip ip
))
687 'dcc-chat-discarded ?n nick ?u login ?h host
))
690 'CHAT
(format "%s!%s@%s" nick login host
)
693 (if (eq erc-dcc-chat-request
'auto
)
694 (erc-dcc-chat-accept (erc-dcc-member :nick nick
:type
'CHAT
)
698 nil
'(notice error
) proc
699 'dcc-malformed ?n nick ?u login ?h host ?q query
)))))
702 (defvar erc-dcc-entry-data nil
703 "Holds the `erc-dcc-list' entry for this DCC connection.")
704 (make-variable-buffer-local 'erc-dcc-entry-data
)
708 (defcustom erc-dcc-block-size
1024
709 "*Block size to use for DCC SEND sessions."
713 (defcustom erc-dcc-pump-bytes nil
714 "*If set to an integer, keep sending until that number of bytes are
717 :type
'(choice (const nil
) integer
))
719 (defsubst erc-dcc-get-parent
(proc)
720 (plist-get (erc-dcc-member :peer proc
) :parent
))
722 (defun erc-dcc-send-block (proc)
723 "Send one block of data.
724 PROC is the process-object of the DCC connection. Returns the number of
726 (let* ((elt (erc-dcc-member :peer proc
))
727 (confirmed-marker (plist-get elt
:sent
))
728 (sent-marker (plist-get elt
:sent
)))
729 (with-current-buffer (process-buffer proc
)
730 (when erc-dcc-verbose
732 nil
'notice
(erc-dcc-get-parent proc
)
733 (format "DCC: Confirmed %d, sent %d, sending block now"
734 (- confirmed-marker
(point-min))
735 (- sent-marker
(point-min)))))
736 (let* ((end (min (+ sent-marker erc-dcc-block-size
)
738 (string (buffer-substring-no-properties sent-marker end
)))
739 (when (< sent-marker end
)
740 (set-marker sent-marker end
)
741 (process-send-string proc string
))
744 (defun erc-dcc-send-filter (proc string
)
745 (let* ((size (erc-unpack-int string
))
746 (elt (erc-dcc-member :peer proc
))
747 (parent (plist-get elt
:parent
))
748 (sent-marker (plist-get elt
:sent
))
749 (confirmed-marker (plist-get elt
:confirmed
)))
750 (with-current-buffer (process-buffer proc
)
751 (set-marker confirmed-marker
(+ (point-min) size
))
753 ((and (= confirmed-marker sent-marker
)
754 (= confirmed-marker
(point-max)))
758 ?n
(plist-get elt
:nick
)
760 ?s
(number-to-string (- sent-marker
(point-min))))
761 (setq erc-dcc-list
(delete elt erc-dcc-list
))
762 (set-buffer-modified-p nil
)
763 (kill-buffer (current-buffer))
764 (delete-process proc
))
765 ((<= confirmed-marker sent-marker
)
766 (while (and (< (- sent-marker confirmed-marker
)
767 (or erc-dcc-pump-bytes
769 (> (erc-dcc-send-block proc
) 0))))
770 ((> confirmed-marker sent-marker
)
773 (format "DCC: Client confirmed too much (%s vs %s)!"
774 (marker-position confirmed-marker
)
775 (marker-position sent-marker
)))
776 (set-buffer-modified-p nil
)
777 (kill-buffer (current-buffer))
778 (delete-process proc
))))))
780 (defun erc-dcc-display-send (proc)
782 nil
'notice
(erc-dcc-get-parent proc
)
783 (format "DCC: SEND connect from %s"
784 (format-network-address (process-contact proc
:remote
)))))
786 (defcustom erc-dcc-send-connect-hook
787 '(erc-dcc-display-send erc-dcc-send-block
)
788 "*Hook run whenever the remote end of a DCC SEND offer connected to your
793 (defun erc-dcc-nick (plist)
794 "Extract the nickname portion of the :nick property value in PLIST."
795 (erc-extract-nick (plist-get plist
:nick
)))
797 (defun erc-dcc-send-sentinel (proc event
)
798 (let* ((elt (erc-dcc-member :peer proc
)))
800 ((string-match "^open from " event
)
802 (let ((buf (marker-buffer (plist-get elt
:sent
))))
803 (with-current-buffer buf
804 (set-process-buffer proc buf
)
805 (setq erc-dcc-entry-data elt
)))
806 (run-hook-with-args 'erc-dcc-send-connect-hook proc
))))))
808 (defun erc-dcc-find-file (file)
809 (with-current-buffer (generate-new-buffer (file-name-nondirectory file
))
810 (insert-file-contents-literally file
)
811 (setq buffer-file-name file
)
814 (defun erc-dcc-file-to-name (file)
816 (insert (file-name-nondirectory file
))
817 (subst-char-in-region (point-min) (point-max) ? ?_ t
)
820 (defun erc-dcc-send-file (nick file
&optional pproc
)
821 "Open a socket for incoming connections, and send a CTCP send request to the
823 (interactive "sNick: \nfFile: ")
824 (when (null pproc
) (if (processp erc-server-process
)
825 (setq pproc erc-server-process
)
826 (error "Can not find parent process")))
827 (if (featurep 'make-network-process
)
828 (let* ((buffer (erc-dcc-find-file file
))
829 (size (buffer-size buffer
))
830 (start (with-current-buffer buffer
831 (set-marker (make-marker) (point-min))))
832 (sproc (erc-dcc-server "dcc-send"
834 'erc-dcc-send-sentinel
))
835 (contact (process-contact sproc
)))
837 'SEND nick sproc pproc
838 :file file
:size size
839 :sent start
:confirmed
(copy-marker start
))
841 pproc
(format "PRIVMSG %s :\C-aDCC SEND %s %s %d %d\C-a\n"
842 nick
(erc-dcc-file-to-name file
)
843 (erc-ip-to-decimal (or erc-dcc-public-host
847 (error "`make-network-process' not supported by your Emacs")))
851 (defcustom erc-dcc-receive-cache
(* 1024 512)
852 "Number of bytes to let the receive buffer grow before flushing it."
856 (defvar erc-dcc-byte-count nil
)
857 (make-variable-buffer-local 'erc-dcc-byte-count
)
858 (defvar erc-dcc-file-name nil
)
859 (make-variable-buffer-local 'erc-dcc-file-name
)
861 (defun erc-dcc-get-file (entry file parent-proc
)
862 "This function does the work of setting up a transfer from the remote client
863 to the local one over a tcp connection. This involves setting up a process
864 filter and a process sentinel, and making the connection."
865 (let* ((buffer (generate-new-buffer (file-name-nondirectory file
)))
867 (with-current-buffer buffer
869 (buffer-disable-undo (current-buffer))
870 ;; This is necessary to have the buffer saved as-is in GNU
872 ;; XEmacs change: We don't have `set-buffer-multibyte', setting
873 ;; coding system to 'binary below takes care of us.
874 (when (fboundp 'set-buffer-multibyte
)
875 (set-buffer-multibyte nil
))
877 (setq mode-line-process
'(":%s")
880 (setq erc-dcc-file-name file
)
882 ;; Truncate the given file to size 0 before appending to it.
883 (write-region (point) (point) erc-dcc-file-name nil
'nomessage
)
885 (setq erc-server-process parent-proc
886 erc-dcc-entry-data entry
)
887 (setq erc-dcc-byte-count
0)
889 (funcall erc-dcc-connect-function
891 (plist-get entry
:ip
)
892 (string-to-number (plist-get entry
:port
))
894 (set-process-buffer proc buffer
)
895 (set-process-coding-system proc
'binary
'binary
)
896 (set-buffer-file-coding-system 'binary t
)
898 (set-process-filter proc
'erc-dcc-get-filter
)
899 (set-process-sentinel proc
'erc-dcc-get-sentinel
)
900 (setq entry
(plist-put entry
:start-time
(erc-current-time)))
901 (setq entry
(plist-put entry
:peer proc
)))))
903 (defun erc-dcc-append-contents (buffer file
)
904 "Append the contents of BUFFER to FILE.
905 The contents of the BUFFER will then be erased."
906 (with-current-buffer buffer
907 (let ((coding-system-for-write 'binary
))
908 (write-region (point-min) (point-max) erc-dcc-file-name t
'nomessage
)
911 (defun erc-dcc-get-filter (proc str
)
912 "This is the process filter for transfers from other clients to this one.
913 It reads incoming bytes from the network and stores them in the DCC
914 buffer, and sends back the replies after each block of data per the DCC
915 protocol spec. Well not really. We write back a reply after each read,
916 rather than every 1024 byte block, but nobody seems to care."
917 (with-current-buffer (process-buffer proc
)
918 (let ((inhibit-read-only t
))
919 (goto-char (point-max))
920 (insert (string-make-unibyte str
))
922 (setq erc-dcc-byte-count
(+ (length str
) erc-dcc-byte-count
))
923 (when (> (point-max) erc-dcc-receive-cache
)
924 (erc-dcc-append-contents (current-buffer) erc-dcc-file-name
))
928 nil
'notice erc-server-process
929 'dcc-get-bytes-received
930 ?f
(file-name-nondirectory buffer-file-name
)
931 ?b
(number-to-string erc-dcc-byte-count
)))
933 ((and (> (plist-get erc-dcc-entry-data
:size
) 0)
934 (> erc-dcc-byte-count
(plist-get erc-dcc-entry-data
:size
)))
936 nil
'(error notice
) 'active
937 'dcc-get-file-too-long
938 ?f
(file-name-nondirectory buffer-file-name
))
939 (delete-process proc
))
942 proc
(erc-pack-int erc-dcc-byte-count
)))))))
945 (defun erc-dcc-get-sentinel (proc event
)
946 "This is the process sentinel for CTCP DCC SEND connections.
947 It shuts down the connection and notifies the user that the
948 transfer is complete."
949 ;; FIXME, we should look at EVENT, and also check size.
950 (with-current-buffer (process-buffer proc
)
951 (delete-process proc
)
952 (setq erc-dcc-list
(delete erc-dcc-entry-data erc-dcc-list
))
953 (unless (= (point-min) (point-max))
954 (setq erc-dcc-byte-count
(+ (buffer-size) erc-dcc-byte-count
))
955 (erc-dcc-append-contents (current-buffer) erc-dcc-file-name
))
957 nil
'notice erc-server-process
960 ?s
(number-to-string erc-dcc-byte-count
)
962 (erc-time-diff (plist-get erc-dcc-entry-data
:start-time
)
963 (erc-current-time)))))
964 (kill-buffer (process-buffer proc
))
965 (delete-process proc
))
969 (defcustom erc-dcc-chat-buffer-name-format
"DCC-CHAT-%s"
970 "*Format to use for DCC Chat buffer names."
974 (defcustom erc-dcc-chat-mode-hook nil
975 "*Hook calls when `erc-dcc-chat-mode' finished setting up the buffer."
979 (defcustom erc-dcc-chat-connect-hook nil
984 (defcustom erc-dcc-chat-exit-hook nil
989 (defun erc-cmd-CREQ (line &optional force
)
990 "Set or get the DCC chat request flag.
991 Possible values are: ask, auto, ignore."
992 (when (string-match "^\\s-*\\(auto\\|ask\\|ignore\\)?$" line
)
993 (let ((cmd (match-string 1 line
)))
997 (format "Set DCC Chat requests to %S"
998 (setq erc-dcc-chat-request
(intern cmd
))))
999 (erc-display-message nil
'notice
'active
1000 (format "DCC Chat requests are set to %S"
1001 erc-dcc-chat-request
)))
1004 (defun erc-cmd-SREQ (line &optional force
)
1005 "Set or get the DCC send request flag.
1006 Possible values are: ask, auto, ignore."
1007 (when (string-match "^\\s-*\\(auto\\|ask\\|ignore\\)?$" line
)
1008 (let ((cmd (match-string 1 line
)))
1010 (erc-display-message
1012 (format "Set DCC Send requests to %S"
1013 (setq erc-dcc-send-request
(intern cmd
))))
1014 (erc-display-message nil
'notice
'active
1015 (format "DCC Send requests are set to %S"
1016 erc-dcc-send-request
)))
1019 (defun pcomplete/erc-mode
/CREQ
()
1020 (pcomplete-here '("auto" "ask" "ignore")))
1021 (defalias 'pcomplete
/erc-mode
/SREQ
'pcomplete
/erc-mode
/CREQ
)
1023 (defvar erc-dcc-chat-filter-hook
'(erc-dcc-chat-parse-output)
1024 "*Hook to run after doing parsing (and possible insertion) of DCC messages.")
1026 (defvar erc-dcc-chat-mode-map
1027 (let ((map (make-sparse-keymap)))
1028 (define-key map
(kbd "RET") 'erc-send-current-line
)
1029 (define-key map
"\t" 'erc-complete-word
)
1031 "Keymap for `erc-dcc-mode'.")
1033 (defun erc-dcc-chat-mode ()
1034 "Major mode for wasting time via DCC chat."
1036 (kill-all-local-variables)
1037 (setq mode-line-process
'(":%s")
1038 mode-name
"DCC-Chat"
1039 major-mode
'erc-dcc-chat-mode
1040 erc-send-input-line-function
'erc-dcc-chat-send-input-line
1041 erc-default-recipients
'(dcc))
1042 (use-local-map erc-dcc-chat-mode-map
)
1043 (run-hooks 'erc-dcc-chat-mode-hook
))
1045 (defun erc-dcc-chat-send-input-line (recipient line
&optional force
)
1046 "Send LINE to the remote end.
1047 Argument RECIPIENT should always be the symbol dcc, and force
1049 ;; FIXME: We need to get rid of all force arguments one day!
1050 (if (eq recipient
'dcc
)
1051 (process-send-string
1052 (get-buffer-process (current-buffer)) line
)
1053 (error "erc-dcc-chat-send-input-line in %s" (current-buffer))))
1055 (defun erc-dcc-chat (nick &optional pproc
)
1056 "Open a socket for incoming connections, and send a chat request to the
1058 (interactive "sNick: ")
1059 (when (null pproc
) (if (processp erc-server-process
)
1060 (setq pproc erc-server-process
)
1061 (error "Can not find parent process")))
1062 (let* ((sproc (erc-dcc-server "dcc-chat-out"
1063 'erc-dcc-chat-filter
1064 'erc-dcc-chat-sentinel
))
1065 (contact (process-contact sproc
)))
1066 (erc-dcc-list-add 'OCHAT nick sproc pproc
)
1067 (process-send-string pproc
1068 (format "PRIVMSG %s :\C-aDCC CHAT chat %s %d\C-a\n"
1070 (erc-ip-to-decimal (nth 0 contact
)) (nth 1 contact
)))))
1072 (defvar erc-dcc-from
)
1073 (make-variable-buffer-local 'erc-dcc-from
)
1075 (defvar erc-dcc-unprocessed-output
)
1076 (make-variable-buffer-local 'erc-dcc-unprocessed-output
)
1078 (defun erc-dcc-chat-setup (entry)
1079 "Setup a DCC chat buffer, returning the buffer."
1080 (let* ((nick (erc-extract-nick (plist-get entry
:nick
)))
1081 (buffer (generate-new-buffer
1082 (format erc-dcc-chat-buffer-name-format nick
)))
1083 (proc (plist-get entry
:peer
))
1084 (parent-proc (plist-get entry
:parent
)))
1085 (erc-setup-buffer buffer
)
1086 ;; buffer is now the current buffer.
1088 (setq erc-server-process parent-proc
)
1089 (setq erc-dcc-from nick
)
1090 (setq erc-dcc-entry-data entry
)
1091 (setq erc-dcc-unprocessed-output
"")
1092 (setq erc-insert-marker
(set-marker (make-marker) (point-max)))
1093 (setq erc-input-marker
(make-marker))
1094 (erc-display-prompt buffer
(point-max))
1095 (set-process-buffer proc buffer
)
1096 (add-hook 'kill-buffer-hook
'erc-dcc-chat-buffer-killed nil t
)
1097 (run-hook-with-args 'erc-dcc-chat-connect-hook proc
)
1100 (defun erc-dcc-chat-accept (entry parent-proc
)
1101 "Accept an incoming DCC connection and open a DCC window"
1102 (let* ((nick (erc-extract-nick (plist-get entry
:nick
)))
1105 (funcall erc-dcc-connect-function
1107 (plist-get entry
:ip
)
1108 (string-to-number (plist-get entry
:port
))
1110 ;; XXX: connected, should we kill the ip/port properties?
1111 (setq entry
(plist-put entry
:peer proc
))
1112 (setq entry
(plist-put entry
:parent parent-proc
))
1113 (set-process-filter proc
'erc-dcc-chat-filter
)
1114 (set-process-sentinel proc
'erc-dcc-chat-sentinel
)
1115 (setq buffer
(erc-dcc-chat-setup entry
))))
1117 (defun erc-dcc-chat-filter (proc str
)
1118 (let ((orig-buffer (current-buffer)))
1121 (set-buffer (process-buffer proc
))
1122 (setq erc-dcc-unprocessed-output
1123 (concat erc-dcc-unprocessed-output str
))
1124 (run-hook-with-args 'erc-dcc-chat-filter-hook proc
1125 erc-dcc-unprocessed-output
))
1126 (set-buffer orig-buffer
))))
1128 (defun erc-dcc-chat-parse-output (proc str
)
1132 (while (string-match "\n" str posn
)
1133 (setq line
(substring str posn
(match-beginning 0)))
1134 (setq posn
(match-end 0))
1135 (erc-display-message
1137 'dcc-chat-privmsg ?n
(erc-propertize erc-dcc-from
'face
1138 'erc-nick-default-face
) ?m line
))
1139 (setq erc-dcc-unprocessed-output
(substring str posn
)))))
1141 (defun erc-dcc-chat-buffer-killed ()
1142 (erc-dcc-chat-close "killed buffer"))
1144 (defun erc-dcc-chat-close (&optional event
)
1145 "Close a DCC chat, removing any associated processes and tidying up
1147 (let ((proc (plist-get erc-dcc-entry-data
:peer
))
1148 (evt (or event
"")))
1150 (setq erc-dcc-list
(delq erc-dcc-entry-data erc-dcc-list
))
1151 (run-hook-with-args 'erc-dcc-chat-exit-hook proc
)
1152 (delete-process proc
)
1153 (erc-display-message
1154 nil
'notice erc-server-process
1155 'dcc-chat-ended ?n erc-dcc-from ?t
(current-time-string) ?e evt
)
1156 (setq erc-dcc-entry-data
(plist-put erc-dcc-entry-data
:peer nil
)))))
1158 (defun erc-dcc-chat-sentinel (proc event
)
1159 (let ((buf (current-buffer))
1160 (elt (erc-dcc-member :peer proc
)))
1161 ;; the sentinel is also notified when the connection is opened, so don't
1162 ;; immediately kill it again
1163 ;(message "buf %s elt %S evt %S" buf elt event)
1165 (if (string-match "^open from" event
)
1166 (erc-dcc-chat-setup elt
)
1167 (erc-dcc-chat-close event
))
1170 (defun erc-dcc-no-such-nick (proc parsed
)
1171 "Detect and handle no-such-nick replies from the IRC server."
1172 (let* ((elt (erc-dcc-member :nick
(second (erc-response.command-args parsed
))
1174 (peer (plist-get elt
:peer
)))
1175 (when (or (and (processp peer
) (not (eq (process-status peer
) 'open
)))
1177 ;; Since we already created an entry before sending the CTCP
1178 ;; message, we now remove it, if it doesn't point to a process
1179 ;; which is already open.
1180 (setq erc-dcc-list
(delq elt erc-dcc-list
))
1181 (if (processp peer
) (delete-process peer
)))
1186 ;;; erc-dcc.el ends here
1189 ;; indent-tabs-mode: nil
1192 ;; arch-tag: cda5a6b3-c510-4dbe-b699-84cccfa04edb