]>
jfr.im git - irc/quakenet/newserv.git/blob - nickwatch/nickwatch.c
c3bfa110b96efab6fe4d1cc1326e35f000b5334f
4 #include "../core/schedule.h"
5 #include "../control/control.h"
6 #include "../newsearch/newsearch.h"
7 #include "../newsearch/parser.h"
9 typedef struct nickwatch
{
17 struct nickwatch
*next
;
20 typedef struct nickwatchevent
{
21 char description
[128];
22 struct nickwatchevent
*next
;
25 static nickwatch
*nickwatches
;
26 static int nextnickwatch
= 1;
27 static int nickwatchext
;
29 static void nw_dummyreply(nick
*np
, char *format
, ...) { }
30 static void nw_dummywall(int level
, char *format
, ...) { }
32 static nickwatch
*nw_currentwatch
;
33 static array nw_pendingnicks
;
35 static void nw_printnick(searchCtx
*ctx
, nick
*sender
, nick
*np
) {
36 char hostbuf
[HOSTLEN
+NICKLEN
+USERLEN
+4];
38 nickwatchevent
*nwe
= np
->exts
[nickwatchext
];
41 nw_currentwatch
->hits
++;
46 for (nwe
= np
->exts
[nickwatchext
]; nwe
; nwe
= nwe
->next
) {
48 len
+= snprintf(events
+ len
, sizeof(events
) - len
, ", ");
50 len
+= snprintf(events
+ len
, sizeof(events
) - len
, "%s", nwe
->description
);
53 controlwall(NO_OPER
, NL_HITS
, "nickwatch(#%d, %s): %s [%s] (%s) (%s)", nw_currentwatch
->id
, events
, visiblehostmask(np
,hostbuf
),
54 IPtostr(np
->ipaddress
), printflags(np
->umodes
, umodeflags
), np
->realname
->name
->content
);
57 static void nwe_enqueue(nick
*np
, const char *format
, ...) {
62 nwe
= malloc(sizeof(nickwatchevent
));
65 vsnprintf(nwe
->description
, sizeof(nwe
->description
), format
, va
);
68 nwe
->next
= np
->exts
[nickwatchext
];
69 np
->exts
[nickwatchext
] = nwe
;
71 slot
= array_getfreeslot(&nw_pendingnicks
);
72 ((nick
**)nw_pendingnicks
.content
)[slot
] = np
;
75 static void nwe_clear(nick
*np
) {
76 nickwatchevent
*nwe
, *next
;
78 for (nwe
= np
->exts
[nickwatchext
]; nwe
; nwe
= next
) {
83 np
->exts
[nickwatchext
] = NULL
;
86 static void nw_sched_processevents(void *arg
) {
91 for (nw
= nickwatches
; nw
; nw
= nw
->next
) {
93 ast_nicksearch(nw
->tree
->root
, &nw_dummyreply
, mynick
, &nw_dummywall
, &nw_printnick
, NULL
, NULL
, 10, &nw_pendingnicks
);
96 for (i
= 0; i
< nw_pendingnicks
.cursi
; i
++) {
97 np
= ((nick
**)nw_pendingnicks
.content
)[i
];
101 array_free(&nw_pendingnicks
);
102 array_init(&nw_pendingnicks
, sizeof(nick
*));
105 static void nw_hook_newnick(int hooknum
, void *arg
) {
107 nwe_enqueue(np
, "new user");
110 static void nw_hook_lostnick(int hooknum
, void *arg
) {
116 for (i
= 0; i
< nw_pendingnicks
.cursi
;)
117 if (((nick
**)nw_pendingnicks
.content
)[i
] == np
)
118 array_delslot(&nw_pendingnicks
, i
);
123 static void nw_hook_rename(int hooknum
, void *arg
) {
126 char *oldnick
= args
[1];
127 nwe_enqueue(np
, "renamed from %s", oldnick
);
130 static void nw_hook_umodechange(int hooknum
, void *arg
) {
133 flag_t oldmodes
= (uintptr_t)args
[1];
135 strncpy(buf
, printflags(np
->umodes
, umodeflags
), sizeof(buf
));
136 nwe_enqueue(np
, "umodes %s -> %s", printflags(oldmodes
, umodeflags
), buf
);
139 static void nw_hook_message(int hooknum
, void *arg
) {
142 int isnotice
= (uintptr_t)args
[2];
143 nwe_enqueue(np
, isnotice
? "notice" : "message");
146 static void nw_hook_joinchannel(int hooknum
, void *arg
) {
148 channel
*cp
= args
[0];
150 nwe_enqueue(np
, "join channel %s", cp
->index
->name
->content
);
153 static int nw_cmd_nickwatch(void *source
, int cargc
, char **cargv
) {
154 nick
*sender
= source
;
161 tree
= parse_string(reg_nicksearch
, cargv
[0]);
163 displaystrerror(controlreply
, sender
, cargv
[0]);
167 nw
= malloc(sizeof(nickwatch
));
168 nw
->id
= nextnickwatch
++;
169 snprintf(nw
->createdby
, sizeof(nw
->createdby
), "#%s", sender
->authname
);
171 strncpy(nw
->term
, cargv
[0], sizeof(nw
->term
));
172 nw
->tree
= parse_string(reg_nicksearch
, cargv
[0]);
173 nw
->next
= nickwatches
;
176 controlreply(sender
, "Done.");
181 static int nw_cmd_nickunwatch(void *source
, int cargc
, char **cargv
) {
182 nick
*sender
= source
;
183 nickwatch
**pnext
, *nw
;
191 for (pnext
= &nickwatches
; *pnext
; pnext
= &((*pnext
)->next
)) {
195 parse_free(nw
->tree
);
199 controlreply(sender
, "Done.");
204 controlreply(sender
, "Nickwatch #%d not found.", id
);
209 static int nw_cmd_nickwatches(void *source
, int cargc
, char **cargv
) {
210 nick
*sender
= source
;
213 controlreply(sender
, "ID Created By Hits Term");
215 for (nw
= nickwatches
; nw
; nw
= nw
->next
)
216 controlreply(sender
, "%-5d %-15s %-7d %s", nw
->id
, nw
->createdby
, nw
->hits
, nw
->term
);
218 controlreply(sender
, "--- End of nickwatches.");
223 static int nw_cmd_nickburst(void *source
, int cargc
, char **cargv
) {
224 nick
*sender
= source
;
228 pargv
[0] = "(and (match (nick) \"*gunnar*\"))";
230 for (i
= 0; i
< 100; i
++)
231 nw_cmd_nickwatch(source
, 1, pargv
);
233 for (i
= 0; i
< 50000; i
++)
234 nwe_enqueue(sender
, "new user");
240 nickwatchext
= registernickext("nickwatch");
242 array_init(&nw_pendingnicks
, sizeof(nick
*));
244 registercontrolhelpcmd("nickwatch", NO_OPER
, 1, &nw_cmd_nickwatch
, "Usage: nickwatch <nicksearch term>\nAdds a nickwatch entry.");
245 registercontrolhelpcmd("nickunwatch", NO_OPER
, 1, &nw_cmd_nickunwatch
, "Usage: nickunwatch <#id>\nRemoves a nickwatch entry.");
246 registercontrolhelpcmd("nickwatches", NO_OPER
, 0, &nw_cmd_nickwatches
, "Usage: nickwatches\nLists nickwatches.");
247 registercontrolhelpcmd("nickburst", NO_OPER
, 0, &nw_cmd_nickburst
, "Usage: nickwatches\nSimulates a burst.");
249 registerhook(HOOK_NICK_NEWNICK
, &nw_hook_newnick
);
250 registerhook(HOOK_NICK_LOSTNICK
, &nw_hook_lostnick
);
251 registerhook(HOOK_NICK_RENAME
, &nw_hook_rename
);
252 registerhook(HOOK_NICK_MODECHANGE
, &nw_hook_umodechange
);
253 registerhook(HOOK_NICK_MESSAGE
, &nw_hook_message
);
254 registerhook(HOOK_CHANNEL_CREATE
, &nw_hook_joinchannel
);
255 registerhook(HOOK_CHANNEL_JOIN
, &nw_hook_joinchannel
);
257 schedulerecurring(time(NULL
) + 5, 0, 1, &nw_sched_processevents
, NULL
);
261 nickwatch
*nw
, *next
;
263 deregistercontrolcmd("nickwatch", &nw_cmd_nickwatch
);
264 deregistercontrolcmd("nickunwatch", &nw_cmd_nickunwatch
);
265 deregistercontrolcmd("nickwatches", &nw_cmd_nickwatches
);
266 deregistercontrolcmd("nickburst", &nw_cmd_nickburst
);
268 deregisterhook(HOOK_NICK_NEWNICK
, &nw_hook_newnick
);
269 deregisterhook(HOOK_NICK_LOSTNICK
, &nw_hook_lostnick
);
270 deregisterhook(HOOK_NICK_RENAME
, &nw_hook_rename
);
271 deregisterhook(HOOK_NICK_MODECHANGE
, &nw_hook_umodechange
);
272 deregisterhook(HOOK_NICK_MESSAGE
, &nw_hook_message
);
273 deregisterhook(HOOK_CHANNEL_CREATE
, &nw_hook_joinchannel
);
274 deregisterhook(HOOK_CHANNEL_JOIN
, &nw_hook_joinchannel
);
276 deleteallschedules(&nw_sched_processevents
);
278 /* Process all pending events */
279 nw_sched_processevents(NULL
);
281 array_free(&nw_pendingnicks
);
283 releasenickext(nickwatchext
);
285 for (nw
= nickwatches
; nw
; nw
= next
) {
288 parse_free(nw
->tree
);