Commit | Line | Data |
---|---|---|
597993cf MB |
1 | ;;; erc-xdcc.el --- XDCC file-server support for ERC |
2 | ||
73b0cd50 | 3 | ;; Copyright (C) 2003-2004, 2006-2011 Free Software Foundation, Inc. |
597993cf MB |
4 | |
5 | ;; Author: Mario Lang <mlang@delysid.org> | |
6 | ;; Keywords: comm, processes | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
4ee57b2a | 10 | ;; GNU Emacs is free software: you can redistribute it and/or modify |
597993cf | 11 | ;; it under the terms of the GNU General Public License as published by |
4ee57b2a GM |
12 | ;; the Free Software Foundation, either version 3 of the License, or |
13 | ;; (at your option) any later version. | |
597993cf MB |
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 | |
4ee57b2a | 21 | ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. |
597993cf MB |
22 | |
23 | ;;; Commentary: | |
24 | ||
9cc8d0b6 | 25 | ;; This file provides a very simple XDCC file server for ERC. |
597993cf MB |
26 | |
27 | ;;; Code: | |
28 | ||
29 | (require 'erc-dcc) | |
30 | ||
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)) | |
36 | ||
37 | (defcustom erc-xdcc-verbose-flag t | |
38 | "*Report XDCC CTCP requests in the server buffer." | |
39 | :group 'erc-dcc | |
40 | :type 'boolean) | |
41 | ||
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)) | |
49 | ||
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 | |
c0943d3d | 59 | being evaluated and should return strings." |
597993cf MB |
60 | :group 'erc-dcc |
61 | :type '(repeat (repeat :tag "Message" (choice string sexp)))) | |
62 | ||
5e56b3fb MO |
63 | ;;;###autoload (autoload 'erc-xdcc-mode "erc-xdcc") |
64 | (define-erc-module xdcc nil | |
65 | "Act as an XDCC file-server." | |
66 | nil nil) | |
67 | ||
597993cf MB |
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))) | |
74 | ||
75 | (defun erc-xdcc-reply (proc nick msg) | |
76 | (process-send-string proc | |
77 | (format "PRIVMSG %s :%s\n" nick msg))) | |
78 | ||
79 | ;; CTCP query handlers | |
80 | ||
81 | (defvar erc-ctcp-query-XDCC-hook '(erc-xdcc) | |
82 | "Hook called whenever a CTCP XDCC message is received.") | |
83 | ||
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)))))) | |
97 | ||
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)) | |
105 | ||
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))))))) | |
118 | ||
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))))) | |
129 | ||
130 | (provide 'erc-xdcc) | |
131 | ||
597993cf | 132 | ;;; erc-xdcc.el ends here |
5e56b3fb MO |
133 | ;; |
134 | ;; Local Variables: | |
135 | ;; indent-tabs-mode: t | |
136 | ;; tab-width: 8 | |
137 | ;; End: | |
138 |