first draft of threads documentation
[emacs.git] / lisp / erc / erc-xdcc.el
blob0328c407aa0b83e4e407a4b67a1a14e94d006a5f
1 ;;; erc-xdcc.el --- XDCC file-server support for ERC
3 ;; Copyright (C) 2003-2004, 2006-2012 Free Software Foundation, Inc.
5 ;; Author: Mario Lang <mlang@delysid.org>
6 ;; Keywords: comm, processes
8 ;; This file is part of GNU Emacs.
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 of the License, or
13 ;; (at your option) any later version.
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; This file provides a very simple XDCC file server for ERC.
27 ;;; Code:
29 (require 'erc-dcc)
31 (defcustom erc-xdcc-files nil
32 "List of files to offer via XDCC.
33 Your friends should issue \"/ctcp yournick XDCC list\" to see this."
34 :group 'erc-dcc
35 :type '(repeat file))
37 (defcustom erc-xdcc-verbose-flag t
38 "Report XDCC CTCP requests in the server buffer."
39 :group 'erc-dcc
40 :type 'boolean)
42 (defcustom erc-xdcc-handler-alist
43 '(("help" . erc-xdcc-help)
44 ("list" . erc-xdcc-list)
45 ("send" . erc-xdcc-send))
46 "Sub-command handler alist for XDCC CTCP queries."
47 :group 'erc-dcc
48 :type '(alist :key-type (string :tag "Sub-command") :value-type function))
50 (defcustom erc-xdcc-help-text
51 '(("Hey " nick ", wondering how this works? Pretty easy.")
52 ("Available commands: XDCC ["
53 (mapconcat 'car erc-xdcc-handler-alist "|") "]")
54 ("Type \"/ctcp " (erc-current-nick)
55 " XDCC list\" to see the list of offered files, then type \"/ctcp "
56 (erc-current-nick) " XDCC send #\" to get a particular file number."))
57 "Help text sent in response to XDCC help command.
58 A list of messages, each consisting of strings and expressions, expressions
59 being evaluated and should return strings."
60 :group 'erc-dcc
61 :type '(repeat (repeat :tag "Message" (choice string sexp))))
63 ;;;###autoload (autoload 'erc-xdcc-mode "erc-xdcc")
64 (define-erc-module xdcc nil
65 "Act as an XDCC file-server."
66 nil nil)
68 ;;;###autoload
69 (defun erc-xdcc-add-file (file)
70 "Add a file to `erc-xdcc-files'."
71 (interactive "fFilename to add to XDCC: ")
72 (if (file-exists-p file)
73 (add-to-list 'erc-xdcc-files file)))
75 (defun erc-xdcc-reply (proc nick msg)
76 (process-send-string proc
77 (format "PRIVMSG %s :%s\n" nick msg)))
79 ;; CTCP query handlers
81 (defvar erc-ctcp-query-XDCC-hook '(erc-xdcc)
82 "Hook called whenever a CTCP XDCC message is received.")
84 (defun erc-xdcc (proc nick login host to query)
85 "Handle incoming CTCP XDCC queries."
86 (when erc-xdcc-verbose-flag
87 (erc-display-message nil 'notice proc
88 (format "XDCC %s (%s@%s) sends %S" nick login host query)))
89 (let* ((args (cdr (delete "" (split-string query " "))))
90 (handler (cdr (assoc (downcase (car args)) erc-xdcc-handler-alist))))
91 (if (and handler (functionp handler))
92 (funcall handler proc nick login host (cdr args))
93 (erc-xdcc-reply
94 proc nick
95 (format "Unknown XDCC sub-command, try \"/ctcp %s XDCC help\""
96 (erc-current-nick))))))
98 (defun erc-xdcc-help (proc nick login host args)
99 "Send basic help information to NICK."
100 (mapc
101 (lambda (msg)
102 (erc-xdcc-reply proc nick
103 (mapconcat (lambda (elt) (if (stringp elt) elt (eval elt))) msg "")))
104 erc-xdcc-help-text))
106 (defun erc-xdcc-list (proc nick login host args)
107 "Show the contents of `erc-xdcc-files' via privmsg to NICK."
108 (if (null erc-xdcc-files)
109 (erc-xdcc-reply proc nick "No files offered, sorry")
110 (erc-xdcc-reply proc nick "Num Filename")
111 (erc-xdcc-reply proc nick "--- -------------")
112 (let ((n 0))
113 (dolist (file erc-xdcc-files)
114 (erc-xdcc-reply proc nick
115 (format "%02d. %s"
116 (setq n (1+ n))
117 (erc-dcc-file-to-name file)))))))
119 (defun erc-xdcc-send (proc nick login host args)
120 "Send a file to NICK."
121 (let ((n (string-to-number (car args)))
122 (len (length erc-xdcc-files)))
123 (cond
124 ((= len 0)
125 (erc-xdcc-reply proc nick "No files offered, sorry"))
126 ((or (< n 1) (> n len))
127 (erc-xdcc-reply proc nick (format "%d out of range" n)))
128 (t (erc-dcc-send-file nick (nth (1- n) erc-xdcc-files) proc)))))
130 (provide 'erc-xdcc)
132 ;;; erc-xdcc.el ends here
134 ;; Local Variables:
135 ;; indent-tabs-mode: t
136 ;; tab-width: 8
137 ;; End: