]> jfr.im git - irc/evilnet/x3.git/blame - src/mod-snoop.c
mod-python: generalised the setting of the PYTHONPATH environment variable
[irc/evilnet/x3.git] / src / mod-snoop.c
CommitLineData
d76ed9a9
AS
1/* mod-snoop.c - User surveillance module (per pomac's spec)
2 * Copyright 2002-2004 srvx Development Team
3 *
83ff05c3 4 * This file is part of x3.
d76ed9a9 5 *
d0f04f71 6 * x3 is free software; you can redistribute it and/or modify
d76ed9a9 7 * it under the terms of the GNU General Public License as published by
348683aa 8 * the Free Software Foundation; either version 3 of the License, or
d76ed9a9
AS
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with srvx; if not, write to the Free Software Foundation,
18 * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
19 */
20
21/* Adds new section to srvx.conf:
22 * "modules" {
23 * "snoop" {
24 * // Where to send snoop messages?
25 * "channel" "#wherever";
26 * // Which bot?
27 * "bot" "OpServ";
28 * // Show new users and joins from net joins? (off by default)
29 * "show_bursts" "0";
30 * };
31 * };
32 */
33
34#include "conf.h"
35#include "helpfile.h"
36#include "nickserv.h"
37
38#ifdef HAVE_NETINET_IN_H
39#include <netinet/in.h>
40#endif
41#ifdef HAVE_ARPA_INET_H
42#include <arpa/inet.h>
43#endif
44
45extern time_t now;
46static struct {
47 struct chanNode *channel;
48 struct userNode *bot;
49 unsigned int show_bursts : 1;
50 unsigned int enabled : 1;
51} snoop_cfg;
52static char timestamp[16];
53const char *snoop_module_deps[] = { NULL };
54
55static int finalized;
56int snoop_finalize(void);
57
58#define SNOOP(FORMAT, ARGS...) send_channel_message(snoop_cfg.channel, snoop_cfg.bot, "%s "FORMAT, timestamp , ## ARGS)
59#define UPDATE_TIMESTAMP() strftime(timestamp, sizeof(timestamp), "[%H:%M:%S]", localtime(&now))
60
61static void
62snoop_nick_change(struct userNode *user, const char *old_nick) {
63 if (!snoop_cfg.enabled) return;
64 UPDATE_TIMESTAMP();
65 SNOOP("$bNICK$b change %s -> %s", old_nick, user->nick);
66}
67
68static int
69snoop_join(struct modeNode *mNode) {
70 struct userNode *user = mNode->user;
71 struct chanNode *chan = mNode->channel;
72 if (!snoop_cfg.enabled) return 0;
73 if (user->uplink->burst && !snoop_cfg.show_bursts) return 0;
74 UPDATE_TIMESTAMP();
75 if (chan->members.used == 1) {
76 SNOOP("$bCREATE$b %s by %s", chan->name, user->nick);
77 } else {
78 SNOOP("$bJOIN$b %s by %s", chan->name, user->nick);
79 }
80 return 0;
81}
82
83static void
84snoop_part(struct modeNode *mn, const char *reason) {
85 if (!snoop_cfg.enabled) return;
86 if (mn->user->dead) return;
87 UPDATE_TIMESTAMP();
88 SNOOP("$bPART$b %s by %s (%s)", mn->channel->name, mn->user->nick, reason ? reason : "");
89}
90
91static void
92snoop_kick(struct userNode *kicker, struct userNode *victim, struct chanNode *chan) {
93 if (!snoop_cfg.enabled) return;
94 UPDATE_TIMESTAMP();
95 SNOOP("$bKICK$b %s from %s by %s", victim->nick, chan->name, (kicker ? kicker->nick : "some server"));
96}
97
98static int
99snoop_new_user(struct userNode *user) {
100 if (!snoop_cfg.enabled) return 0;
101 if (user->uplink->burst && !snoop_cfg.show_bursts) return 0;
102 UPDATE_TIMESTAMP();
2f61d1d7 103 SNOOP("$bNICK$b %s %s@%s (%s) [%s] on %s", user->nick, user->ident, user->hostname, user->handle_info?user->handle_info->handle:"", irc_ntoa(&user->ip), user->uplink->name);
d76ed9a9
AS
104 return 0;
105}
106
107static void
108snoop_del_user(struct userNode *user, struct userNode *killer, const char *why) {
109 if (!snoop_cfg.enabled) return;
110 UPDATE_TIMESTAMP();
111 if (killer) {
112 SNOOP("$bKILL$b %s (%s@%s, on %s) by %s (%s)", user->nick, user->ident, user->hostname, user->uplink->name, killer->nick, why);
113 } else {
114 SNOOP("$bQUIT$b %s (%s@%s, on %s) (%s)", user->nick, user->ident, user->hostname, user->uplink->name, why);
115 }
116}
117
118static void
119snoop_auth(struct userNode *user, UNUSED_ARG(struct handle_info *old_handle)) {
120 if (!snoop_cfg.enabled) return;
121 if (user->uplink->burst && !snoop_cfg.show_bursts) return;
122 if (user->handle_info) {
123 UPDATE_TIMESTAMP();
ffb204b6
AS
124 /* TODO: show old_handle info -> new handle info if they
125 * re-authed or something.
126 */
d76ed9a9
AS
127 SNOOP("$bAUTH$b %s as %s", user->nick, user->handle_info->handle);
128 }
129}
130
77627461 131static void
132snoop_user_mode(struct userNode *user, const char *mode_change) {
133 if (!snoop_cfg.enabled) return;
134 if (user->uplink->burst && !snoop_cfg.show_bursts) return;
135 if (!mode_change[1]) return; /* warning there has to be atleast one char in the buffer */
136 UPDATE_TIMESTAMP();
137 SNOOP("$bUMODE$b %s %s", user->nick, mode_change);
138}
139
140static void
141snoop_oper(struct userNode *user) {
142 if (!snoop_cfg.enabled) return;
143 if (user->uplink->burst && !snoop_cfg.show_bursts) return;
144 UPDATE_TIMESTAMP();
145 SNOOP("$bOPER$b %s!%s@%s [%s] on %s", user->nick, user->ident, user->hostname, irc_ntoa(&user->ip), user->uplink->name);
146}
147
148static void
149snoop_channel_mode(struct userNode *who, struct chanNode *channel, char **modes, unsigned int argc)
150{
151 if (!snoop_cfg.enabled) return;
152 if(who) {
153 if (who->uplink->burst && !snoop_cfg.show_bursts) {
154 return;
155 }
156 } else {
157 return; /* Dont show X3 etc modes */
158 }
159
160 static char targets[MAXLEN], string[MAXLEN];
161 struct userNode *un = NULL;
162 char *tmp = NULL, *tg = NULL, *md = NULL;
163 int add = 0;
164
165 string[0] = 0;
166 targets[0] = 0;
167
168 if (argc > 0)
169 unsplit_string(modes, argc, string);
170 else
171 strcpy(string, *modes);
172
173 if((tg = strchr(string, ' ')))
174 {
175 *tg++ = 0;
176 for(md = string; *md; md++)
177 {
178 if (*md == '+')
179 {
180 add = 1;
181 md++;
182 }
183 if (*md == '-')
184 {
185 add = 0;
186 md++;
187 }
188 switch(*md)
189 {
190 case 'k':
191 {
192 strcat(targets, " ");
193 if ((tmp = strchr(tg, ' ')))
194 *tmp++ = 0;
195 strcat(targets, tg);
196 if(tmp)
197 tg = tmp;
198 break;
199 }
200 case 'l':
201 {
202 if(add)
203 {
204 strcat(targets, " ");
205 if ((tmp = strchr(tg, ' ')))
206 *tmp++ = 0;
207 strcat(targets, tg);
208 if(tmp)
209 tg = tmp;
210 break;
211 }
212 }
213 case 'b':
214 {
215 strcat(targets, " ");
216 if ((tmp = strchr(tg, ' ')))
217 *tmp++ = 0;
218 strcat(targets, tg);
219 if(tmp)
220 tg = tmp;
221 break;
222 }
223 case 'e':
224 {
225 strcat(targets, " ");
226 if ((tmp = strchr(tg, ' ')))
227 *tmp++ = 0;
228 strcat(targets, tg);
229 if(tmp)
230 tg = tmp;
231 break;
232 }
233 case 'o':
234 {
235 strcat(targets, " ");
236 if ((tmp = strchr(tg, ' ')))
237 *tmp++ = 0;
238 if((un = GetUserN(tg)))
239 strcat(targets, un->nick);
240 else
241 strcat(targets, tg);
055f3f30
MB
242 if(tmp)
243 tg = tmp;
244 break;
245 }
246 case 'h':
247 {
248 strcat(targets, " ");
249 if ((tmp = strchr(tg, ' ')))
250 *tmp++ = 0;
251 if((un = GetUserN(tg)))
252 strcat(targets, un->nick);
253 else
254 strcat(targets, tg);
77627461 255 if(tmp)
256 tg = tmp;
257 break;
258 }
259 case 'v':
260 {
261 strcat(targets, " ");
262 if ((tmp = strchr(tg, ' ')))
263 *tmp++ = 0;
264 if((un = GetUserN(tg)))
265 strcat(targets, un->nick);
266 else
267 strcat(targets, tg);
268 if(tmp)
269 tg = tmp;
270 break;
271 }
272 }
273 }
274 }
275 UPDATE_TIMESTAMP();
276 if (who)
277 SNOOP("$bMODE$b %s %s%s by %s", channel->name, string, targets, who->nick);
278 else
279 SNOOP("$bMODE$b %s %s%s", channel->name, string, targets);
280}
281
d76ed9a9
AS
282static void
283snoop_conf_read(void) {
284 dict_t node;
285 char *str;
286
287 node = conf_get_data("modules/snoop", RECDB_OBJECT);
288 if (!node)
289 return;
290 str = database_get_data(node, "channel", RECDB_QSTRING);
291 if (!str)
292 return;
10a4e9ea 293 snoop_cfg.channel = AddChannel(str, now, "+sntim", NULL, NULL);
d76ed9a9
AS
294 if (!snoop_cfg.channel)
295 return;
296 str = database_get_data(node, "show_bursts", RECDB_QSTRING);
297 snoop_cfg.show_bursts = str ? enabled_string(str) : 0;
298 snoop_cfg.enabled = 1;
299 if (finalized)
300 snoop_finalize();
301}
302
303void
304snoop_cleanup(void) {
305 snoop_cfg.enabled = 0;
306 unreg_del_user_func(snoop_del_user);
307}
308
309int
310snoop_init(void) {
311 reg_exit_func(snoop_cleanup);
312 conf_register_reload(snoop_conf_read);
313 reg_nick_change_func(snoop_nick_change);
314 reg_join_func(snoop_join);
315 reg_part_func(snoop_part);
316 reg_kick_func(snoop_kick);
317 reg_new_user_func(snoop_new_user);
318 reg_del_user_func(snoop_del_user);
319 reg_auth_func(snoop_auth);
77627461 320 reg_channel_mode_func(snoop_channel_mode);
321 reg_user_mode_func(snoop_user_mode);
322 reg_oper_func(snoop_oper);
323
d76ed9a9
AS
324 return 1;
325}
326
327int
328snoop_finalize(void) {
329 struct mod_chanmode change;
330 dict_t node;
331 char *str;
332
333 finalized = 1;
334 node = conf_get_data("modules/snoop", RECDB_OBJECT);
335 if (!node)
336 return 0;
337 str = database_get_data(node, "bot", RECDB_QSTRING);
338 if (!str)
339 return 0;
340 snoop_cfg.bot = GetUserH(str);
341 if (!snoop_cfg.bot)
342 return 0;
343 mod_chanmode_init(&change);
344 change.argc = 1;
345 change.args[0].mode = MODE_CHANOP;
a32da4c7 346 change.args[0].u.member = AddChannelUser(snoop_cfg.bot, snoop_cfg.channel);
d76ed9a9
AS
347 mod_chanmode_announce(snoop_cfg.bot, snoop_cfg.channel, &change);
348 return 1;
349}