]>
jfr.im git - irc/quakenet/newserv.git/blob - nickwatch/nickwatch.c
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
) {
93 array_init(&nicks
, sizeof(nick
*));
94 marker
= nextnickmarker();
96 for (i
= 0; i
< nw_pendingnicks
.cursi
; i
++) {
97 np
= ((nick
**)nw_pendingnicks
.content
)[i
];
102 if (np
->marker
!= marker
) {
104 slot
= array_getfreeslot(&nicks
);
105 ((nick
**)nicks
.content
)[slot
] = np
;
109 array_free(&nw_pendingnicks
);
110 array_init(&nw_pendingnicks
, sizeof(nick
*));
112 for (nw
= nickwatches
; nw
; nw
= nw
->next
) {
113 nw_currentwatch
= nw
;
114 ast_nicksearch(nw
->tree
->root
, &nw_dummyreply
, mynick
, &nw_dummywall
, &nw_printnick
, NULL
, NULL
, 10, &nicks
);
117 for (i
= 0; i
< nicks
.cursi
; i
++) {
118 np
= ((nick
**)nicks
.content
)[i
];
125 static void nw_hook_newnick(int hooknum
, void *arg
) {
127 nwe_enqueue(np
, "new user");
130 static void nw_hook_account(int hooknum
, void *arg
) {
132 nwe_enqueue(np
, "logged in with account %s", np
->authname
);
135 static void nw_hook_lostnick(int hooknum
, void *arg
) {
141 for (i
= 0; i
< nw_pendingnicks
.cursi
; i
++)
142 if (((nick
**)nw_pendingnicks
.content
)[i
] == np
)
143 ((nick
**)nw_pendingnicks
.content
)[i
] = NULL
;
146 static void nw_hook_rename(int hooknum
, void *arg
) {
149 char *oldnick
= args
[1];
150 nwe_enqueue(np
, "renamed from %s", oldnick
);
153 static void nw_hook_umodechange(int hooknum
, void *arg
) {
156 flag_t oldmodes
= (uintptr_t)args
[1];
158 strncpy(buf
, printflags(np
->umodes
, umodeflags
), sizeof(buf
));
159 nwe_enqueue(np
, "umodes %s -> %s", printflags(oldmodes
, umodeflags
), buf
);
162 static void nw_hook_message(int hooknum
, void *arg
) {
165 int isnotice
= (uintptr_t)args
[2];
166 nwe_enqueue(np
, isnotice
? "notice" : "message");
169 static void nw_hook_joinchannel(int hooknum
, void *arg
) {
171 channel
*cp
= args
[0];
173 nwe_enqueue(np
, "join channel %s", cp
->index
->name
->content
);
176 static int nw_cmd_nickwatch(void *source
, int cargc
, char **cargv
) {
177 nick
*sender
= source
;
184 tree
= parse_string(reg_nicksearch
, cargv
[0]);
186 displaystrerror(controlreply
, sender
, cargv
[0]);
190 nw
= malloc(sizeof(nickwatch
));
191 nw
->id
= nextnickwatch
++;
192 snprintf(nw
->createdby
, sizeof(nw
->createdby
), "#%s", sender
->authname
);
194 strncpy(nw
->term
, cargv
[0], sizeof(nw
->term
));
195 nw
->tree
= parse_string(reg_nicksearch
, cargv
[0]);
196 nw
->next
= nickwatches
;
199 controlreply(sender
, "Done.");
204 static int nw_cmd_nickunwatch(void *source
, int cargc
, char **cargv
) {
205 nick
*sender
= source
;
206 nickwatch
**pnext
, *nw
;
214 for (pnext
= &nickwatches
; *pnext
; pnext
= &((*pnext
)->next
)) {
218 parse_free(nw
->tree
);
222 controlreply(sender
, "Done.");
227 controlreply(sender
, "Nickwatch #%d not found.", id
);
232 static int nw_cmd_nickwatches(void *source
, int cargc
, char **cargv
) {
233 nick
*sender
= source
;
236 controlreply(sender
, "ID Created By Hits Term");
238 for (nw
= nickwatches
; nw
; nw
= nw
->next
)
239 controlreply(sender
, "%-5d %-15s %-7d %s", nw
->id
, nw
->createdby
, nw
->hits
, nw
->term
);
241 controlreply(sender
, "--- End of nickwatches.");
247 nickwatchext
= registernickext("nickwatch");
249 array_init(&nw_pendingnicks
, sizeof(nick
*));
251 registercontrolhelpcmd("nickwatch", NO_OPER
, 1, &nw_cmd_nickwatch
, "Usage: nickwatch <nicksearch term>\nAdds a nickwatch entry.");
252 registercontrolhelpcmd("nickunwatch", NO_OPER
, 1, &nw_cmd_nickunwatch
, "Usage: nickunwatch <#id>\nRemoves a nickwatch entry.");
253 registercontrolhelpcmd("nickwatches", NO_OPER
, 0, &nw_cmd_nickwatches
, "Usage: nickwatches\nLists nickwatches.");
255 registerhook(HOOK_NICK_NEWNICK
, &nw_hook_newnick
);
256 registerhook(HOOK_NICK_ACCOUNT
, &nw_hook_account
);
257 registerhook(HOOK_NICK_LOSTNICK
, &nw_hook_lostnick
);
258 registerhook(HOOK_NICK_RENAME
, &nw_hook_rename
);
259 registerhook(HOOK_NICK_MODECHANGE
, &nw_hook_umodechange
);
260 registerhook(HOOK_NICK_MESSAGE
, &nw_hook_message
);
261 registerhook(HOOK_CHANNEL_CREATE
, &nw_hook_joinchannel
);
262 registerhook(HOOK_CHANNEL_JOIN
, &nw_hook_joinchannel
);
264 schedulerecurring(time(NULL
) + 5, 0, 1, &nw_sched_processevents
, NULL
);
268 nickwatch
*nw
, *next
;
270 deregistercontrolcmd("nickwatch", &nw_cmd_nickwatch
);
271 deregistercontrolcmd("nickunwatch", &nw_cmd_nickunwatch
);
272 deregistercontrolcmd("nickwatches", &nw_cmd_nickwatches
);
274 deregisterhook(HOOK_NICK_NEWNICK
, &nw_hook_newnick
);
275 deregisterhook(HOOK_NICK_ACCOUNT
, &nw_hook_account
);
276 deregisterhook(HOOK_NICK_LOSTNICK
, &nw_hook_lostnick
);
277 deregisterhook(HOOK_NICK_RENAME
, &nw_hook_rename
);
278 deregisterhook(HOOK_NICK_MODECHANGE
, &nw_hook_umodechange
);
279 deregisterhook(HOOK_NICK_MESSAGE
, &nw_hook_message
);
280 deregisterhook(HOOK_CHANNEL_CREATE
, &nw_hook_joinchannel
);
281 deregisterhook(HOOK_CHANNEL_JOIN
, &nw_hook_joinchannel
);
283 deleteallschedules(&nw_sched_processevents
);
285 /* Process all pending events */
286 nw_sched_processevents(NULL
);
288 array_free(&nw_pendingnicks
);
290 releasenickext(nickwatchext
);
292 for (nw
= nickwatches
; nw
; nw
= next
) {
295 parse_free(nw
->tree
);