1;;; erc-xdcc.el --- XDCC file-server support for ERC
2
3;; Copyright (C) 2003, 2004, 2006, 2007 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 2, 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.
35Your 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.
60A list of messages, each consisting of strings and expressions, expressions
61being evaluated and should return stings."
62  :group 'erc-dcc
63  :type '(repeat (repeat :tag "Message" (choice string sexp))))
64
65;;;###autoload
66(defun erc-xdcc-add-file (file)
67  "Add a file to `erc-xdcc-files'."
68  (interactive "fFilename to add to XDCC: ")
69  (if (file-exists-p file)
70      (add-to-list 'erc-xdcc-files file)))
71
72(defun erc-xdcc-reply (proc nick msg)
73  (process-send-string proc
74   (format "PRIVMSG %s :%s\n" nick msg)))
75
76;; CTCP query handlers
77
78(defvar erc-ctcp-query-XDCC-hook '(erc-xdcc)
79  "Hook called whenever a CTCP XDCC message is received.")
80
81(defun erc-xdcc (proc nick login host to query)
82  "Handle incoming CTCP XDCC queries."
83  (when erc-xdcc-verbose-flag
84    (erc-display-message nil 'notice proc
85     (format "XDCC %s (%s@%s) sends %S" nick login host query)))
86  (let* ((args (cdr (delete "" (split-string query " "))))
87	 (handler (cdr (assoc (downcase (car args)) erc-xdcc-handler-alist))))
88    (if (and handler (functionp handler))
89	(funcall handler proc nick login host (cdr args))
90      (erc-xdcc-reply
91       proc nick
92       (format "Unknown XDCC sub-command, try \"/ctcp %s XDCC help\""
93	       (erc-current-nick))))))
94
95(defun erc-xdcc-help (proc nick login host args)
96  "Send basic help information to NICK."
97  (mapc
98   (lambda (msg)
99     (erc-xdcc-reply proc nick
100      (mapconcat (lambda (elt) (if (stringp elt) elt (eval elt))) msg "")))
101   erc-xdcc-help-text))
102
103(defun erc-xdcc-list (proc nick login host args)
104  "Show the contents of `erc-xdcc-files' via privmsg to NICK."
105  (if (null erc-xdcc-files)
106      (erc-xdcc-reply proc nick "No files offered, sorry")
107    (erc-xdcc-reply proc nick "Num  Filename")
108    (erc-xdcc-reply proc nick "---  -------------")
109    (let ((n 0))
110      (dolist (file erc-xdcc-files)
111	(erc-xdcc-reply proc nick
112	 (format "%02d.  %s"
113		 (setq n (1+ n))
114		 (erc-dcc-file-to-name file)))))))
115
116(defun erc-xdcc-send (proc nick login host args)
117  "Send a file to NICK."
118  (let ((n (string-to-number (car args)))
119	(len (length erc-xdcc-files)))
120    (cond
121     ((= len 0)
122      (erc-xdcc-reply proc nick "No files offered, sorry"))
123     ((or (< n 1) (> n len))
124      (erc-xdcc-reply proc nick (format "%d out of range" n)))
125     (t (erc-dcc-send-file nick (nth (1- n) erc-xdcc-files) proc)))))
126
127(provide 'erc-xdcc)
128
129;; arch-tag: a13b62fe-2399-4562-af4e-f18a8dd4b9c8
130;;; erc-xdcc.el ends here
131