]>
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];
25 static nickwatch
*nickwatches
;
26 static int nextnickwatch
= 1;
28 static void nw_dummyreply(nick
*np
, char *format
, ...) { }
30 static void nw_dummywall(int level
, char *format
, ...) { }
32 static nickwatch
*nw_currentwatch
;
33 static nickwatchevent
*nw_currentevent
;
35 static void nw_printnick(searchCtx
*ctx
, nick
*sender
, nick
*np
) {
36 char hostbuf
[HOSTLEN
+NICKLEN
+USERLEN
+4];
38 nw_currentwatch
->hits
++;
40 controlwall(NO_OPER
, NL_HITS
, "nickwatch(#%d, %s): %s [%s] (%s) (%s)", nw_currentwatch
->id
, nw_currentevent
->description
, visiblehostmask(np
,hostbuf
),
41 IPtostr(np
->ipaddress
), printflags(np
->umodes
, umodeflags
), np
->realname
->name
->content
);
44 static nickwatchevent
*nwe_new(nick
*np
, const char *format
, ...) {
48 nwe
= malloc(sizeof(nickwatchevent
));
49 nwe
->numeric
= np
->numeric
;
52 vsnprintf(nwe
->description
, sizeof(nwe
->description
), format
, va
);
58 static void nwe_free(nickwatchevent
*nwe
) {
62 static void nw_sched_processevent(void *arg
) {
63 nickwatchevent
*nwe
= arg
;
67 np
= getnickbynumeric(nwe
->numeric
);
73 nw_currentevent
= nwe
;
75 for (nw
= nickwatches
; nw
; nw
= nw
->next
) {
77 ast_nicksearch(nw
->tree
->root
, &nw_dummyreply
, mynick
, &nw_dummywall
, &nw_printnick
, NULL
, NULL
, 10, np
);
83 static void nw_hook_newnick(int hooknum
, void *arg
) {
85 nickwatchevent
*nwe
= nwe_new(np
, "new user");
86 scheduleoneshot(0, nw_sched_processevent
, nwe
);
89 static void nw_hook_rename(int hooknum
, void *arg
) {
92 char *oldnick
= args
[1];
93 nickwatchevent
*nwe
= nwe_new(np
, "renamed from %s", oldnick
);
94 scheduleoneshot(0, nw_sched_processevent
, nwe
);
97 static void nw_hook_umodechange(int hooknum
, void *arg
) {
100 flag_t oldmodes
= (uintptr_t)args
[1];
102 strncpy(buf
, printflags(np
->umodes
, umodeflags
), sizeof(buf
));
103 nickwatchevent
*nwe
= nwe_new(np
, "umodes %s -> %s", printflags(oldmodes
, umodeflags
), buf
);
104 scheduleoneshot(0, nw_sched_processevent
, nwe
);
107 static void nw_hook_message(int hooknum
, void *arg
) {
110 int isnotice
= (uintptr_t)args
[2];
111 nickwatchevent
*nwe
= nwe_new(np
, isnotice
? "notice" : "message");
112 scheduleoneshot(0, nw_sched_processevent
, nwe
);
115 static void nw_hook_joinchannel(int hooknum
, void *arg
) {
117 channel
*cp
= args
[0];
119 nickwatchevent
*nwe
= nwe_new(np
, "join channel %s", cp
->index
->name
->content
);
120 scheduleoneshot(0, nw_sched_processevent
, nwe
);
123 static int nw_cmd_nickwatch(void *source
, int cargc
, char **cargv
) {
124 nick
*sender
= source
;
131 tree
= parse_string(reg_nicksearch
, cargv
[0]);
133 displaystrerror(controlreply
, sender
, cargv
[0]);
137 nw
= malloc(sizeof(nickwatch
));
138 nw
->id
= nextnickwatch
++;
139 snprintf(nw
->createdby
, sizeof(nw
->createdby
), "#%s", sender
->authname
);
141 strncpy(nw
->term
, cargv
[0], sizeof(nw
->term
));
142 nw
->tree
= parse_string(reg_nicksearch
, cargv
[0]);
143 nw
->next
= nickwatches
;
146 controlreply(sender
, "Done.");
151 static int nw_cmd_nickunwatch(void *source
, int cargc
, char **cargv
) {
152 nick
*sender
= source
;
153 nickwatch
**pnext
, *nw
;
161 for (pnext
= &nickwatches
; *pnext
; pnext
= &((*pnext
)->next
)) {
165 parse_free(nw
->tree
);
169 controlreply(sender
, "Done.");
174 controlreply(sender
, "Nickwatch #%d not found.", id
);
179 static int nw_cmd_nickwatches(void *source
, int cargc
, char **cargv
) {
180 nick
*sender
= source
;
183 controlreply(sender
, "ID Created By Hits Term");
185 for (nw
= nickwatches
; nw
; nw
= nw
->next
)
186 controlreply(sender
, "%-5d %-15s %-7d %s", nw
->id
, nw
->createdby
, nw
->hits
, nw
->term
);
188 controlreply(sender
, "--- End of nickwatches.");
194 registercontrolhelpcmd("nickwatch", NO_OPER
, 1, &nw_cmd_nickwatch
, "Usage: nickwatch <nicksearch term>\nAdds a nickwatch entry.");
195 registercontrolhelpcmd("nickunwatch", NO_OPER
, 1, &nw_cmd_nickunwatch
, "Usage: nickunwatch <#id>\nRemoves a nickwatch entry.");
196 registercontrolhelpcmd("nickwatches", NO_OPER
, 0, &nw_cmd_nickwatches
, "Usage: nickwatches\nLists nickwatches.");
198 registerhook(HOOK_NICK_NEWNICK
, &nw_hook_newnick
);
199 registerhook(HOOK_NICK_RENAME
, &nw_hook_rename
);
200 registerhook(HOOK_NICK_MODECHANGE
, &nw_hook_umodechange
);
201 registerhook(HOOK_NICK_MESSAGE
, &nw_hook_message
);
202 registerhook(HOOK_CHANNEL_CREATE
, &nw_hook_joinchannel
);
203 registerhook(HOOK_CHANNEL_JOIN
, &nw_hook_joinchannel
);
207 nickwatch
*nw
, *next
;
209 deregistercontrolcmd("nickwatch", &nw_cmd_nickwatch
);
210 deregistercontrolcmd("nickunwatch", &nw_cmd_nickunwatch
);
211 deregistercontrolcmd("nickwatches", &nw_cmd_nickwatches
);
213 deregisterhook(HOOK_NICK_NEWNICK
, &nw_hook_newnick
);
214 deregisterhook(HOOK_NICK_RENAME
, &nw_hook_rename
);
215 deregisterhook(HOOK_NICK_MODECHANGE
, &nw_hook_umodechange
);
216 deregisterhook(HOOK_NICK_MESSAGE
, &nw_hook_message
);
217 deregisterhook(HOOK_CHANNEL_CREATE
, &nw_hook_joinchannel
);
218 deregisterhook(HOOK_CHANNEL_JOIN
, &nw_hook_joinchannel
);
220 for (nw
= nickwatches
; nw
; nw
= next
) {
223 parse_free(nw
->tree
);