Replace "Maintainer: FSF" with the emacs-devel mailing address
[bpt/emacs.git] / lisp / erc / erc-xdcc.el
CommitLineData
597993cf
MB
1;;; erc-xdcc.el --- XDCC file-server support for ERC
2
ba318903 3;; Copyright (C) 2003-2004, 2006-2014 Free Software Foundation, Inc.
597993cf
MB
4
5;; Author: Mario Lang <mlang@delysid.org>
34dc21db 6;; Maintainer: emacs-devel@gnu.org
597993cf
MB
7;; Keywords: comm, processes
8
9;; This file is part of GNU Emacs.
10
4ee57b2a 11;; GNU Emacs is free software: you can redistribute it and/or modify
597993cf 12;; it under the terms of the GNU General Public License as published by
4ee57b2a
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
597993cf
MB
15
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.
20
21;; You should have received a copy of the GNU General Public License
4ee57b2a 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
597993cf
MB
23
24;;; Commentary:
25
9cc8d0b6 26;; This file provides a very simple XDCC file server for ERC.
597993cf
MB
27
28;;; Code:
29
30(require 'erc-dcc)
31
32(defcustom erc-xdcc-files nil
fb7ada5f 33 "List of files to offer via XDCC.
597993cf
MB
34Your friends should issue \"/ctcp yournick XDCC list\" to see this."
35 :group 'erc-dcc
36 :type '(repeat file))
37
38(defcustom erc-xdcc-verbose-flag t
fb7ada5f 39 "Report XDCC CTCP requests in the server buffer."
597993cf
MB
40 :group 'erc-dcc
41 :type 'boolean)
42
43(defcustom erc-xdcc-handler-alist
44 '(("help" . erc-xdcc-help)
45 ("list" . erc-xdcc-list)
46 ("send" . erc-xdcc-send))
fb7ada5f 47 "Sub-command handler alist for XDCC CTCP queries."
597993cf
MB
48 :group 'erc-dcc
49 :type '(alist :key-type (string :tag "Sub-command") :value-type function))
50
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."))
fb7ada5f 58 "Help text sent in response to XDCC help command.
597993cf 59A list of messages, each consisting of strings and expressions, expressions
c0943d3d 60being evaluated and should return strings."
597993cf
MB
61 :group 'erc-dcc
62 :type '(repeat (repeat :tag "Message" (choice string sexp))))
63
5e56b3fb
MO
64;;;###autoload (autoload 'erc-xdcc-mode "erc-xdcc")
65(define-erc-module xdcc nil
66 "Act as an XDCC file-server."
67 nil nil)
68
597993cf
MB
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)))
75
76(defun erc-xdcc-reply (proc nick msg)
77 (process-send-string proc
78 (format "PRIVMSG %s :%s\n" nick msg)))
79
80;; CTCP query handlers
81
82(defvar erc-ctcp-query-XDCC-hook '(erc-xdcc)
83 "Hook called whenever a CTCP XDCC message is received.")
84
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))))))
98
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))
106
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)))))))
119
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)))))
130
131(provide 'erc-xdcc)
132
597993cf 133;;; erc-xdcc.el ends here
5e56b3fb
MO
134;;
135;; Local Variables:
136;; indent-tabs-mode: t
137;; tab-width: 8
138;; End:
139