]>
jfr.im git - irc/quakenet/newserv.git/blob - nickwatch/nickwatch.c
4 #include "../core/schedule.h"
5 #include "../lib/irc_string.h"
6 #include "../lib/splitline.h"
7 #include "../control/control.h"
8 #include "../newsearch/newsearch.h"
9 #include "../newsearch/parser.h"
11 #define NW_FORMAT_TIME "%d/%m/%y %H:%M GMT"
12 #define NW_DURATION_MAX (60*60*24*7) // 7 days
14 typedef struct nickwatch
{
24 struct nickwatch
*next
;
27 typedef struct nickwatchevent
{
28 char description
[128];
29 struct nickwatchevent
*next
;
32 static nickwatch
*nickwatches
;
33 static int nextnickwatch
= 1;
34 static int nickwatchext
;
36 static void nw_dummyreply(nick
*np
, char *format
, ...) { }
37 static void nw_dummywall(int level
, char *format
, ...) { }
39 static nickwatch
*nw_currentwatch
;
40 static array nw_pendingnicks
;
42 static int nw_nickunwatch(int id
) {
43 nickwatch
**pnext
, *nw
;
45 for (pnext
= &nickwatches
; *pnext
; pnext
= &((*pnext
)->next
)) {
59 static void nw_printnick(searchCtx
*ctx
, nick
*sender
, nick
*np
) {
60 char hostbuf
[HOSTLEN
+NICKLEN
+USERLEN
+4], modebuf
[34];
62 nickwatchevent
*nwe
= np
->exts
[nickwatchext
];
65 nw_currentwatch
->hits
++;
66 nw_currentwatch
->lastactive
= time(NULL
);
71 for (nwe
= np
->exts
[nickwatchext
]; nwe
; nwe
= nwe
->next
) {
73 len
+= snprintf(events
+ len
, sizeof(events
) - len
, ", ");
75 len
+= snprintf(events
+ len
, sizeof(events
) - len
, "%s", nwe
->description
);
78 strncpy(modebuf
, printflags(np
->umodes
, umodeflags
), sizeof(modebuf
));
80 controlwall(NO_OPER
, NL_HITS
, "nickwatch(#%d, %s): %s [%s] (%s) (%s)", nw_currentwatch
->id
, events
, visiblehostmask(np
,hostbuf
),
81 IPtostr(np
->ipaddress
), modebuf
, np
->realname
->name
->content
);
84 static void nwe_enqueue(nick
*np
, const char *format
, ...) {
89 nwe
= malloc(sizeof(nickwatchevent
));
92 vsnprintf(nwe
->description
, sizeof(nwe
->description
), format
, va
);
95 nwe
->next
= np
->exts
[nickwatchext
];
96 np
->exts
[nickwatchext
] = nwe
;
98 slot
= array_getfreeslot(&nw_pendingnicks
);
99 ((nick
**)nw_pendingnicks
.content
)[slot
] = np
;
102 static void nwe_clear(nick
*np
) {
103 nickwatchevent
*nwe
, *next
;
105 for (nwe
= np
->exts
[nickwatchext
]; nwe
; nwe
= next
) {
110 np
->exts
[nickwatchext
] = NULL
;
113 static void nw_sched_processevents(void *arg
) {
114 nickwatch
*nw
, *next
;
119 time_t now
= time(NULL
);
121 array_init(&nicks
, sizeof(nick
*));
122 marker
= nextnickmarker();
124 for (i
= 0; i
< nw_pendingnicks
.cursi
; i
++) {
125 np
= ((nick
**)nw_pendingnicks
.content
)[i
];
130 if (np
->marker
!= marker
) {
132 slot
= array_getfreeslot(&nicks
);
133 ((nick
**)nicks
.content
)[slot
] = np
;
137 array_free(&nw_pendingnicks
);
138 array_init(&nw_pendingnicks
, sizeof(nick
*));
140 for (nw
= nickwatches
; nw
; nw
= next
) {
141 nw_currentwatch
= nw
;
143 ast_nicksearch(nw
->tree
->root
, &nw_dummyreply
, mynick
, &nw_dummywall
, &nw_printnick
, NULL
, NULL
, 10, &nicks
);
144 if (nw
->expiry
&& nw
->expiry
<= now
) {
145 controlwall(NO_OPER
, NL_HITS
, "nickwatch(#%d) by %s expired (%d hits): %s", nw
->id
, nw
->createdby
, nw
->hits
, nw
->term
);
146 nw_nickunwatch(nw
->id
);
150 for (i
= 0; i
< nicks
.cursi
; i
++) {
151 np
= ((nick
**)nicks
.content
)[i
];
158 static void nw_hook_newnick(int hooknum
, void *arg
) {
160 nwe_enqueue(np
, "new user");
163 static void nw_hook_account(int hooknum
, void *arg
) {
165 nwe_enqueue(np
, "logged in with account %s", np
->authname
);
168 static void nw_hook_lostnick(int hooknum
, void *arg
) {
174 for (i
= 0; i
< nw_pendingnicks
.cursi
; i
++)
175 if (((nick
**)nw_pendingnicks
.content
)[i
] == np
)
176 ((nick
**)nw_pendingnicks
.content
)[i
] = NULL
;
179 static void nw_hook_rename(int hooknum
, void *arg
) {
182 char *oldnick
= args
[1];
183 nwe_enqueue(np
, "renamed from %s", oldnick
);
186 static void nw_hook_umodechange(int hooknum
, void *arg
) {
189 flag_t oldmodes
= (uintptr_t)args
[1];
191 strncpy(buf
, printflags(np
->umodes
, umodeflags
), sizeof(buf
));
192 nwe_enqueue(np
, "umodes %s -> %s", printflags(oldmodes
, umodeflags
), buf
);
195 static void nw_hook_message(int hooknum
, void *arg
) {
198 int isnotice
= (uintptr_t)args
[2];
199 nwe_enqueue(np
, isnotice
? "notice" : "message");
202 static void nw_hook_joinchannel(int hooknum
, void *arg
) {
204 channel
*cp
= args
[0];
206 nwe_enqueue(np
, "join channel %s", cp
->index
->name
->content
);
209 static int nw_cmd_nickwatch(void *source
, int cargc
, char **cargv
) {
210 nick
*sender
= source
;
213 time_t duration
= NW_DURATION_MAX
;
216 for (i
= 0; i
< cargc
&& cargv
[i
][0] == '-'; i
++) {
217 switch (cargv
[i
][1]) {
221 duration
= durationtolong(cargv
[i
]);
222 if (!duration
|| duration
> NW_DURATION_MAX
) {
223 controlreply(sender
, "Invalid duration. Maximum: %s.", longtoduration(NW_DURATION_MAX
, 1));
236 rejoinline(cargv
[i
],cargc
-i
);
238 tree
= parse_string(reg_nicksearch
, cargv
[i
]);
240 displaystrerror(controlreply
, sender
, cargv
[i
]);
244 nw
= malloc(sizeof(nickwatch
));
245 nw
->id
= nextnickwatch
++;
246 snprintf(nw
->createdby
, sizeof(nw
->createdby
), "#%s", sender
->authname
);
249 nw
->expiry
= duration
+ time(NULL
);
250 strncpy(nw
->term
, cargv
[i
], sizeof(nw
->term
));
252 nw
->next
= nickwatches
;
255 controlreply(sender
, "Done.");
260 static int nw_cmd_nickunwatch(void *source
, int cargc
, char **cargv
) {
261 nick
*sender
= source
;
269 if (nw_nickunwatch(id
)) {
270 controlreply(sender
, "Nickwatch #%d not found.", id
);
274 controlreply(sender
, "Done.");
278 static void nw_formattime(time_t time
, char *buf
, size_t bufsize
) {
280 strncpy(buf
, "(never)", bufsize
);
282 strftime(buf
, bufsize
, NW_FORMAT_TIME
, gmtime(&time
));
286 static int nw_cmd_nickwatches(void *source
, int cargc
, char **cargv
) {
287 nick
*sender
= source
;
289 char timebuf1
[20], timebuf2
[20];
291 controlreply(sender
, "ID Created By Hits Expires Last active Term");
293 for (nw
= nickwatches
; nw
; nw
= nw
->next
) {
294 nw_formattime(nw
->expiry
, timebuf1
, sizeof(timebuf1
));
295 nw_formattime(nw
->lastactive
, timebuf2
, sizeof(timebuf2
));
296 controlreply(sender
, "%-5d %-15s %-7d %-18s %-18s %s", nw
->id
, nw
->createdby
, nw
->hits
, timebuf1
, timebuf2
, nw
->term
);
299 controlreply(sender
, "--- End of nickwatches.");
305 nickwatchext
= registernickext("nickwatch");
307 array_init(&nw_pendingnicks
, sizeof(nick
*));
309 registercontrolhelpcmd("nickwatch", NO_OPER
, 3, &nw_cmd_nickwatch
, "Usage: nickwatch ?-d <duration (e.g. 12h5m)>? <nicksearch term>\nAdds a nickwatch entry.");
310 registercontrolhelpcmd("nickunwatch", NO_OPER
, 1, &nw_cmd_nickunwatch
, "Usage: nickunwatch <#id>\nRemoves a nickwatch entry.");
311 registercontrolhelpcmd("nickwatches", NO_OPER
, 0, &nw_cmd_nickwatches
, "Usage: nickwatches\nLists nickwatches.");
313 registerhook(HOOK_NICK_NEWNICK
, &nw_hook_newnick
);
314 registerhook(HOOK_NICK_ACCOUNT
, &nw_hook_account
);
315 registerhook(HOOK_NICK_LOSTNICK
, &nw_hook_lostnick
);
316 registerhook(HOOK_NICK_RENAME
, &nw_hook_rename
);
317 registerhook(HOOK_NICK_MODECHANGE
, &nw_hook_umodechange
);
318 registerhook(HOOK_NICK_MESSAGE
, &nw_hook_message
);
319 registerhook(HOOK_CHANNEL_CREATE
, &nw_hook_joinchannel
);
320 registerhook(HOOK_CHANNEL_JOIN
, &nw_hook_joinchannel
);
322 schedulerecurring(time(NULL
) + 5, 0, 1, &nw_sched_processevents
, NULL
);
326 nickwatch
*nw
, *next
;
328 deregistercontrolcmd("nickwatch", &nw_cmd_nickwatch
);
329 deregistercontrolcmd("nickunwatch", &nw_cmd_nickunwatch
);
330 deregistercontrolcmd("nickwatches", &nw_cmd_nickwatches
);
332 deregisterhook(HOOK_NICK_NEWNICK
, &nw_hook_newnick
);
333 deregisterhook(HOOK_NICK_ACCOUNT
, &nw_hook_account
);
334 deregisterhook(HOOK_NICK_LOSTNICK
, &nw_hook_lostnick
);
335 deregisterhook(HOOK_NICK_RENAME
, &nw_hook_rename
);
336 deregisterhook(HOOK_NICK_MODECHANGE
, &nw_hook_umodechange
);
337 deregisterhook(HOOK_NICK_MESSAGE
, &nw_hook_message
);
338 deregisterhook(HOOK_CHANNEL_CREATE
, &nw_hook_joinchannel
);
339 deregisterhook(HOOK_CHANNEL_JOIN
, &nw_hook_joinchannel
);
341 deleteallschedules(&nw_sched_processevents
);
343 /* Process all pending events */
344 nw_sched_processevents(NULL
);
346 array_free(&nw_pendingnicks
);
348 releasenickext(nickwatchext
);
350 for (nw
= nickwatches
; nw
; nw
= next
) {
353 parse_free(nw
->tree
);