erc-xdcc.el 4.4 KB

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