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