a9cfb34a13ef180abec85283044c616b461d76ba
[packages] / xemacs-packages / zenirc / src / zenirc-doto.el
1 ;;; zenirc-doto.el --- do things to who, list, links replies
2
3 ;; Copyright (C) 1993, 1994 Ben A. Mesander
4 ;; Copyright (C) 1998 Per Persson
5
6 ;; Author: Ben A. Mesander <ben@gnu.ai.mit.edu>
7 ;; Maintainer: pp@sno.pp.se
8 ;; Keywords: extensions
9 ;; Created: 1994/07/22
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15 ;;
16 ;; This program 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
22 ;; along with this program; if not, you can either send email to this
23 ;; program's maintainer or write to: The Free Software Foundation,
24 ;; Inc.; 675 Massachusetts Avenue; Cambridge, MA 02139, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (require 'zenirc)
31 (require 'zenirc-command-queue)
32
33 (defun zenirc-doto-install-message-catalogs ()
34   (zenirc-lang-define-catalog 'english
35    '((doto-lame-args . "[info] %s: lame argument(s)."))))
36
37 \f
38 ;; WHO reply handlers
39 ;; example: send everyone on IRC a DCC chat request from the
40 ;; telnet port of a pentagon computer.
41 ;; /dotowho 0 (process-send-string proc (concat "PRIVMSG " (aref whoreply 7)
42 ;;   " :\C-aDCC CHAT chat 2261613455 23\C-a\n"))
43
44 ;; 315 (end of who) handler
45 (defun zenirc-do-to-who-end (proc parsedmsg)
46   (zenirc-delete-hook 'zenirc-server-352-hook 'zenirc-do-to-who)
47   (zenirc-delete-hook 'zenirc-server-315-hook 'zenirc-do-to-who-end)
48   (setq zenirc-run-next-hook nil))
49
50 ;; 352 (who reply) handler
51 ;; proc is the zenirc process.
52 ;; whoreply is the parsed server message array.
53 (defun zenirc-do-to-who (proc whoreply)
54   (zenirc-do-to-who-function proc whoreply)
55   (setq zenirc-run-next-hook nil))
56
57 ;; command interface
58 ;; /dotowho #victims (lisp-form)
59 ;; lisp-form can use "proc", which will be set to the zenirc process
60 ;; and "whoreply" which will be a parsed servermessage array containing
61 ;; a 352 reply.
62 (defvar zenirc-command-dotowho-hook '(zenirc-command-dotowho))
63
64 (defun zenirc-command-dotowho (proc parsedcmd)
65   (let* ((arg (zenirc-parse-firstword (cdr parsedcmd)))
66          (victim (car arg))
67          (command (cdr arg)))
68     (if (or (string= "" victim)
69             (string= "" command))
70         (zenirc-message proc 'doto-lame-args "/dotowho"))
71     (zenirc-dotowho victim (read command))))
72
73 ;; programmatic interface
74 (defun zenirc-dotowho (victim command)
75   (fset 'zenirc-do-to-who-function (list 'lambda '(proc whoreply) command))
76   (zenirc-add-hook 'zenirc-server-315-hook 'zenirc-do-to-who-end)
77   (zenirc-add-hook 'zenirc-server-352-hook 'zenirc-do-to-who)
78   (process-send-string proc (concat "WHO " victim "\n")))
79
80 \f
81 ;; example of using who
82 ;; /op channel - op everyone on a channel
83 (defvar zenirc-op-list nil)
84
85 (defvar zenirc-command-op-hook '(zenirc-command-op))
86
87 (defun zenirc-command-op (proc parsedcmd)
88   (let ((victim (cdr parsedcmd)))
89     (if (string= victim "")
90         (zenirc-message proc 'doto-lame-args "/op")
91       (setq zenirc-op-list nil)
92       (zenirc-dotowho
93        victim
94        '(if (not (string-match "@" (aref whoreply 8)))
95             (setq zenirc-op-list (cons (aref whoreply 7) zenirc-op-list))))
96       (zenirc-add-hook 'zenirc-server-315-hook 'zenirc-doto-op-end))))
97
98 (defun zenirc-doto-op-end (proc parsedmsg)
99   (let* ((channel (aref parsedmsg 3))
100          (oplen (length zenirc-op-list))
101          (triples (* 3 (/ oplen 3)))
102          (remainder (% oplen 3))
103          (i 0)
104          (nicks nil))
105     (while (< i triples)
106       (setq nicks (concat (nth i zenirc-op-list) " "
107                           (nth (1+ i) zenirc-op-list) " "
108                           (nth (+ 2 i) zenirc-op-list) "\n"))
109       (process-send-string proc (concat "mode " channel " +ooo " nicks))
110       (setq i (+ 3 i)))
111     (cond
112      ((eq remainder 2)
113       (setq nicks (concat (nth (- oplen 2) zenirc-op-list) " "
114                           (nth (1- oplen) zenirc-op-list) "\n"))
115       (process-send-string proc (concat "mode " channel " +oo " nicks)))
116      ((eq remainder 1)
117       (process-send-string
118        proc (concat "mode " channel " +o "
119                     (nth (1- oplen) zenirc-op-list) "\n"))))
120     (zenirc-delete-hook 'zenirc-server-315-hook 'zenirc-op-end)))
121
122 \f
123 ;; server link stuff
124
125 ;; 365 (end of links) handler
126 (defun zenirc-do-to-links-end (proc parsedmsg)
127   (zenirc-delete-hook 'zenirc-server-364-hook 'zenirc-do-to-links)
128   (zenirc-delete-hook 'zenirc-server-365-hook 'zenirc-do-to-links-end)
129   (setq zenirc-run-next-hook nil))
130
131 ;; 364 (links reply) handler
132 ;; proc is the zenirc process.
133 ;; linksreply is the parsed server message array.
134 (defun zenirc-do-to-links (proc linksreply)
135   (zenirc-do-to-links-function proc linksreply)
136   (setq zenirc-run-next-hook nil))
137
138 ;; command interface
139 ;; /dotolinks (lisp-form)
140 ;; lisp-form can use "proc", which will be set to the zenirc process
141 ;; and "linksreply" which will be a parsed servermessage array containing
142 ;; a 364 reply.
143 (defvar zenirc-command-dotolinks-hook '(zenirc-command-dotolinks))
144
145 (defun zenirc-command-dotolinks (proc parsedcmd)
146   (let ((arg (cdr parsedcmd)))
147     (if (string= "" arg)
148         (zenirc-message proc 'doto-lame-args "/dotolinks")
149       (zenirc-dotolinks (read arg)))))
150
151 ;; programmatic interface
152 (defun zenirc-dotolinks (command)
153   (fset 'zenirc-do-to-links-function (list 'lambda '(proc linksreply) command))
154   (zenirc-add-hook 'zenirc-server-365-hook 'zenirc-do-to-links-end)
155   (zenirc-add-hook 'zenirc-server-364-hook 'zenirc-do-to-links)
156   (process-send-string proc "LINKS\n"))
157
158 (defvar zenirc-command-serverversions-hook '(zenirc-server-versions))
159 (defun zenirc-server-versions (proc parsedcmd)
160   (zenirc-dotolinks '(zenirc-queue-command (concat "VERSION "
161                                                    (aref linksreply 4)
162                                                    "\n"))))
163
164 \f
165 ;; Do things to /list reply
166 ;; 321 RPL_LISTSTART
167 ;; 322 RPL_LIST
168 ;; 323 RPL_LISTEND
169 ;; Example: set the topic of every channel on irc to be AT&T YOU WILL
170 ;; while talking on #twilight_zone
171 ;; /dotolist (if (not (string= (aref listreply 3) "*")) (progn
172 ;; (zenirc-queue-command (concat "JOIN " (aref listreply 3) "\n"))
173 ;; (zenirc-queue-command '(setq zenirc-current-victim "#twilight_zone"))
174 ;; (zenirc-queue-command (concat "TOPIC " (aref listreply 3)
175 ;; " :AT&T YOU WILL!!!!\n")) (zenirc-queue-command (concat "PART "
176 ;; (aref listreply 3) "\n"))))
177
178 ;; 323 (end of list) handler
179 (defun zenirc-do-to-list-end (proc parsedmsg)
180   (zenirc-delete-hook 'zenirc-server-322-hook 'zenirc-do-to-list)
181   (zenirc-delete-hook 'zenirc-server-323-hook 'zenirc-do-to-list-end)
182   (setq zenirc-run-next-hook nil))
183
184 ;; 322 (list reply) handler
185 ;; proc is the zenirc process.
186 ;; listreply is the parsed server message array.
187 (defun zenirc-do-to-list (proc listreply)
188   (zenirc-do-to-list-function proc listreply)
189   (setq zenirc-run-next-hook nil))
190
191 ;; 321 (list start) handler
192 (defun zenirc-do-to-list-start (proc parsedmsg)
193   (zenirc-delete-hook 'zenirc-server-321-hook 'zenirc-do-to-list-start)
194   (setq zenirc-run-next-hook nil))
195
196 ;; command interface
197 ;; /dotolist (lisp-form)
198 ;; lisp-form can use "proc", which will be set to the zenirc process
199 ;; and "listreply" which will be a parsed servermessage array containing
200 ;; a 322 reply.
201 (defvar zenirc-command-dotolist-hook '(zenirc-command-dotolist))
202
203 (defun zenirc-command-dotolist (proc parsedcmd)
204   (let* ((arg (cdr parsedcmd)))
205     (if (string= "" arg)
206         (zenirc-message proc 'doto-lame-args "/dotolist"))
207     (zenirc-dotolist (read arg))))
208
209 ;; programmatic interface
210 (defun zenirc-dotolist (command)
211   (fset 'zenirc-do-to-list-function (list 'lambda '(proc listreply) command))
212   (zenirc-add-hook 'zenirc-server-323-hook 'zenirc-do-to-list-end)
213   (zenirc-add-hook 'zenirc-server-322-hook 'zenirc-do-to-list)
214   (zenirc-add-hook 'zenirc-server-321-hook 'zenirc-do-to-list-start)
215   (process-send-string proc "LIST\n"))
216
217 (provide 'zenirc-doto)
218
219 (zenirc-doto-install-message-catalogs)
220
221 ;;; End of zenirc-doto.el