Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / erc / erc-xdcc.el
blob5e650d01cb49b2bdc2443b55f34def341855b955
1 ;;; erc-xdcc.el --- XDCC file-server support for ERC
3 ;; Copyright (C) 2003-2004, 2006-2014 Free Software Foundation, Inc.
5 ;; Author: Mario Lang <mlang@delysid.org>
6 ;; Maintainer: FSF
7 ;; Keywords: comm, processes
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; This file provides a very simple XDCC file server for ERC.
28 ;;; Code:
30 (require 'erc-dcc)
32 (defcustom erc-xdcc-files nil
33 "List of files to offer via XDCC.
34 Your friends should issue \"/ctcp yournick XDCC list\" to see this."
35 :group 'erc-dcc
36 :type '(repeat file))
38 (defcustom erc-xdcc-verbose-flag t
39 "Report XDCC CTCP requests in the server buffer."
40 :group 'erc-dcc
41 :type 'boolean)
43 (defcustom erc-xdcc-handler-alist
44 '(("help" . erc-xdcc-help)
45 ("list" . erc-xdcc-list)
46 ("send" . erc-xdcc-send))
47 "Sub-command handler alist for XDCC CTCP queries."
48 :group 'erc-dcc
49 :type '(alist :key-type (string :tag "Sub-command") :value-type function))
51 (defcustom erc-xdcc-help-text
52 '(("Hey " nick ", wondering how this works? Pretty easy.")
53 ("Available commands: XDCC ["
54 (mapconcat 'car erc-xdcc-handler-alist "|") "]")
55 ("Type \"/ctcp " (erc-current-nick)
56 " XDCC list\" to see the list of offered files, then type \"/ctcp "
57 (erc-current-nick) " XDCC send #\" to get a particular file number."))
58 "Help text sent in response to XDCC help command.
59 A list of messages, each consisting of strings and expressions, expressions
60 being evaluated and should return strings."
61 :group 'erc-dcc
62 :type '(repeat (repeat :tag "Message" (choice string sexp))))
64 ;;;###autoload (autoload 'erc-xdcc-mode "erc-xdcc")
65 (define-erc-module xdcc nil
66 "Act as an XDCC file-server."
67 nil nil)
69 ;;;###autoload
70 (defun erc-xdcc-add-file (file)
71 "Add a file to `erc-xdcc-files'."
72 (interactive "fFilename to add to XDCC: ")
73 (if (file-exists-p file)
74 (add-to-list 'erc-xdcc-files file)))
76 (defun erc-xdcc-reply (proc nick msg)
77 (process-send-string proc
78 (format "PRIVMSG %s :%s\n" nick msg)))
80 ;; CTCP query handlers
82 (defvar erc-ctcp-query-XDCC-hook '(erc-xdcc)
83 "Hook called whenever a CTCP XDCC message is received.")
85 (defun erc-xdcc (proc nick login host to query)
86 "Handle incoming CTCP XDCC queries."
87 (when erc-xdcc-verbose-flag
88 (erc-display-message nil 'notice proc
89 (format "XDCC %s (%s@%s) sends %S" nick login host query)))
90 (let* ((args (cdr (delete "" (split-string query " "))))
91 (handler (cdr (assoc (downcase (car args)) erc-xdcc-handler-alist))))
92 (if (and handler (functionp handler))
93 (funcall handler proc nick login host (cdr args))
94 (erc-xdcc-reply
95 proc nick
96 (format "Unknown XDCC sub-command, try \"/ctcp %s XDCC help\""
97 (erc-current-nick))))))
99 (defun erc-xdcc-help (proc nick login host args)
100 "Send basic help information to NICK."
101 (mapc
102 (lambda (msg)
103 (erc-xdcc-reply proc nick
104 (mapconcat (lambda (elt) (if (stringp elt) elt (eval elt))) msg "")))
105 erc-xdcc-help-text))
107 (defun erc-xdcc-list (proc nick login host args)
108 "Show the contents of `erc-xdcc-files' via privmsg to NICK."
109 (if (null erc-xdcc-files)
110 (erc-xdcc-reply proc nick "No files offered, sorry")
111 (erc-xdcc-reply proc nick "Num Filename")
112 (erc-xdcc-reply proc nick "--- -------------")
113 (let ((n 0))
114 (dolist (file erc-xdcc-files)
115 (erc-xdcc-reply proc nick
116 (format "%02d. %s"
117 (setq n (1+ n))
118 (erc-dcc-file-to-name file)))))))
120 (defun erc-xdcc-send (proc nick login host args)
121 "Send a file to NICK."
122 (let ((n (string-to-number (car args)))
123 (len (length erc-xdcc-files)))
124 (cond
125 ((= len 0)
126 (erc-xdcc-reply proc nick "No files offered, sorry"))
127 ((or (< n 1) (> n len))
128 (erc-xdcc-reply proc nick (format "%d out of range" n)))
129 (t (erc-dcc-send-file nick (nth (1- n) erc-xdcc-files) proc)))))
131 (provide 'erc-xdcc)
133 ;;; erc-xdcc.el ends here
135 ;; Local Variables:
136 ;; indent-tabs-mode: t
137 ;; tab-width: 8
138 ;; End: