]>
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 #define NW_FORMAT_TIME "%d/%m/%y %H:%M GMT"
11 typedef struct nickwatch
{
20 struct nickwatch
*next
;
23 typedef struct nickwatchevent
{
24 char description
[128];
25 struct nickwatchevent
*next
;
28 static nickwatch
*nickwatches
;
29 static int nextnickwatch
= 1;
30 static int nickwatchext
;
32 static void nw_dummyreply(nick
*np
, char *format
, ...) { }
33 static void nw_dummywall(int level
, char *format
, ...) { }
35 static nickwatch
*nw_currentwatch
;
36 static array nw_pendingnicks
;
38 static void nw_printnick(searchCtx
*ctx
, nick
*sender
, nick
*np
) {
39 char hostbuf
[HOSTLEN
+NICKLEN
+USERLEN
+4], modebuf
[34];
41 nickwatchevent
*nwe
= np
->exts
[nickwatchext
];
44 nw_currentwatch
->hits
++;
45 nw_currentwatch
->lastactive
= time(NULL
);
50 for (nwe
= np
->exts
[nickwatchext
]; nwe
; nwe
= nwe
->next
) {
52 len
+= snprintf(events
+ len
, sizeof(events
) - len
, ", ");
54 len
+= snprintf(events
+ len
, sizeof(events
) - len
, "%s", nwe
->description
);
57 strncpy(modebuf
, printflags(np
->umodes
, umodeflags
), sizeof(modebuf
));
59 controlwall(NO_OPER
, NL_HITS
, "nickwatch(#%d, %s): %s [%s] (%s) (%s)", nw_currentwatch
->id
, events
, visiblehostmask(np
,hostbuf
),
60 IPtostr(np
->ipaddress
), modebuf
, np
->realname
->name
->content
);
63 static void nwe_enqueue(nick
*np
, const char *format
, ...) {
68 nwe
= malloc(sizeof(nickwatchevent
));
71 vsnprintf(nwe
->description
, sizeof(nwe
->description
), format
, va
);
74 nwe
->next
= np
->exts
[nickwatchext
];
75 np
->exts
[nickwatchext
] = nwe
;
77 slot
= array_getfreeslot(&nw_pendingnicks
);
78 ((nick
**)nw_pendingnicks
.content
)[slot
] = np
;
81 static void nwe_clear(nick
*np
) {
82 nickwatchevent
*nwe
, *next
;
84 for (nwe
= np
->exts
[nickwatchext
]; nwe
; nwe
= next
) {
89 np
->exts
[nickwatchext
] = NULL
;
92 static void nw_sched_processevents(void *arg
) {
99 array_init(&nicks
, sizeof(nick
*));
100 marker
= nextnickmarker();
102 for (i
= 0; i
< nw_pendingnicks
.cursi
; i
++) {
103 np
= ((nick
**)nw_pendingnicks
.content
)[i
];
108 if (np
->marker
!= marker
) {
110 slot
= array_getfreeslot(&nicks
);
111 ((nick
**)nicks
.content
)[slot
] = np
;
115 array_free(&nw_pendingnicks
);
116 array_init(&nw_pendingnicks
, sizeof(nick
*));
118 for (nw
= nickwatches
; nw
; nw
= nw
->next
) {
119 nw_currentwatch
= nw
;
120 ast_nicksearch(nw
->tree
->root
, &nw_dummyreply
, mynick
, &nw_dummywall
, &nw_printnick
, NULL
, NULL
, 10, &nicks
);
123 for (i
= 0; i
< nicks
.cursi
; i
++) {
124 np
= ((nick
**)nicks
.content
)[i
];
131 static void nw_hook_newnick(int hooknum
, void *arg
) {
133 nwe_enqueue(np
, "new user");
136 static void nw_hook_account(int hooknum
, void *arg
) {
138 nwe_enqueue(np
, "logged in with account %s", np
->authname
);
141 static void nw_hook_lostnick(int hooknum
, void *arg
) {
147 for (i
= 0; i
< nw_pendingnicks
.cursi
; i
++)
148 if (((nick
**)nw_pendingnicks
.content
)[i
] == np
)
149 ((nick
**)nw_pendingnicks
.content
)[i
] = NULL
;
152 static void nw_hook_rename(int hooknum
, void *arg
) {
155 char *oldnick
= args
[1];
156 nwe_enqueue(np
, "renamed from %s", oldnick
);
159 static void nw_hook_umodechange(int hooknum
, void *arg
) {
162 flag_t oldmodes
= (uintptr_t)args
[1];
164 strncpy(buf
, printflags(np
->umodes
, umodeflags
), sizeof(buf
));
165 nwe_enqueue(np
, "umodes %s -> %s", printflags(oldmodes
, umodeflags
), buf
);
168 static void nw_hook_message(int hooknum
, void *arg
) {
171 int isnotice
= (uintptr_t)args
[2];
172 nwe_enqueue(np
, isnotice
? "notice" : "message");
175 static void nw_hook_joinchannel(int hooknum
, void *arg
) {
177 channel
*cp
= args
[0];
179 nwe_enqueue(np
, "join channel %s", cp
->index
->name
->content
);
182 static int nw_cmd_nickwatch(void *source
, int cargc
, char **cargv
) {
183 nick
*sender
= source
;
190 tree
= parse_string(reg_nicksearch
, cargv
[0]);
192 displaystrerror(controlreply
, sender
, cargv
[0]);
196 nw
= malloc(sizeof(nickwatch
));
197 nw
->id
= nextnickwatch
++;
198 snprintf(nw
->createdby
, sizeof(nw
->createdby
), "#%s", sender
->authname
);
201 strncpy(nw
->term
, cargv
[0], sizeof(nw
->term
));
202 nw
->tree
= parse_string(reg_nicksearch
, cargv
[0]);
203 nw
->next
= nickwatches
;
206 controlreply(sender
, "Done.");
211 static int nw_cmd_nickunwatch(void *source
, int cargc
, char **cargv
) {
212 nick
*sender
= source
;
213 nickwatch
**pnext
, *nw
;
221 for (pnext
= &nickwatches
; *pnext
; pnext
= &((*pnext
)->next
)) {
225 parse_free(nw
->tree
);
229 controlreply(sender
, "Done.");
234 controlreply(sender
, "Nickwatch #%d not found.", id
);
239 static int nw_cmd_nickwatches(void *source
, int cargc
, char **cargv
) {
240 nick
*sender
= source
;
244 controlreply(sender
, "ID Created By Hits Last active Term");
246 for (nw
= nickwatches
; nw
; nw
= nw
->next
) {
247 if (nw
->lastactive
== 0)
248 strncpy(timebuf
, "(never)", sizeof(timebuf
));
250 strftime(timebuf
, sizeof(timebuf
), NW_FORMAT_TIME
, gmtime(&nw
->lastactive
));
251 controlreply(sender
, "%-5d %-15s %-7d %-18s %s", nw
->id
, nw
->createdby
, nw
->hits
, timebuf
, nw
->term
);
254 controlreply(sender
, "--- End of nickwatches.");
260 nickwatchext
= registernickext("nickwatch");
262 array_init(&nw_pendingnicks
, sizeof(nick
*));
264 registercontrolhelpcmd("nickwatch", NO_OPER
, 1, &nw_cmd_nickwatch
, "Usage: nickwatch <nicksearch term>\nAdds a nickwatch entry.");
265 registercontrolhelpcmd("nickunwatch", NO_OPER
, 1, &nw_cmd_nickunwatch
, "Usage: nickunwatch <#id>\nRemoves a nickwatch entry.");
266 registercontrolhelpcmd("nickwatches", NO_OPER
, 0, &nw_cmd_nickwatches
, "Usage: nickwatches\nLists nickwatches.");
268 registerhook(HOOK_NICK_NEWNICK
, &nw_hook_newnick
);
269 registerhook(HOOK_NICK_ACCOUNT
, &nw_hook_account
);
270 registerhook(HOOK_NICK_LOSTNICK
, &nw_hook_lostnick
);
271 registerhook(HOOK_NICK_RENAME
, &nw_hook_rename
);
272 registerhook(HOOK_NICK_MODECHANGE
, &nw_hook_umodechange
);
273 registerhook(HOOK_NICK_MESSAGE
, &nw_hook_message
);
274 registerhook(HOOK_CHANNEL_CREATE
, &nw_hook_joinchannel
);
275 registerhook(HOOK_CHANNEL_JOIN
, &nw_hook_joinchannel
);
277 schedulerecurring(time(NULL
) + 5, 0, 1, &nw_sched_processevents
, NULL
);
281 nickwatch
*nw
, *next
;
283 deregistercontrolcmd("nickwatch", &nw_cmd_nickwatch
);
284 deregistercontrolcmd("nickunwatch", &nw_cmd_nickunwatch
);
285 deregistercontrolcmd("nickwatches", &nw_cmd_nickwatches
);
287 deregisterhook(HOOK_NICK_NEWNICK
, &nw_hook_newnick
);
288 deregisterhook(HOOK_NICK_ACCOUNT
, &nw_hook_account
);
289 deregisterhook(HOOK_NICK_LOSTNICK
, &nw_hook_lostnick
);
290 deregisterhook(HOOK_NICK_RENAME
, &nw_hook_rename
);
291 deregisterhook(HOOK_NICK_MODECHANGE
, &nw_hook_umodechange
);
292 deregisterhook(HOOK_NICK_MESSAGE
, &nw_hook_message
);
293 deregisterhook(HOOK_CHANNEL_CREATE
, &nw_hook_joinchannel
);
294 deregisterhook(HOOK_CHANNEL_JOIN
, &nw_hook_joinchannel
);
296 deleteallschedules(&nw_sched_processevents
);
298 /* Process all pending events */
299 nw_sched_processevents(NULL
);
301 array_free(&nw_pendingnicks
);
303 releasenickext(nickwatchext
);
305 for (nw
= nickwatches
; nw
; nw
= next
) {
308 parse_free(nw
->tree
);