]>
Commit | Line | Data |
---|---|---|
4860501e GB |
1 | #include <stdarg.h> |
2 | #include <stdio.h> | |
3 | #include <string.h> | |
4 | #include "../core/schedule.h" | |
5 | #include "../control/control.h" | |
6 | #include "../newsearch/newsearch.h" | |
7 | #include "../newsearch/parser.h" | |
8 | ||
9 | typedef struct nickwatch { | |
10 | int id; | |
11 | ||
6cd0364c GB |
12 | char createdby[64]; |
13 | int hits; | |
4860501e GB |
14 | char term[512]; |
15 | parsertree *tree; | |
16 | ||
17 | struct nickwatch *next; | |
18 | } nickwatch; | |
19 | ||
20 | typedef struct nickwatchevent { | |
21 | char description[128]; | |
59694aa9 | 22 | struct nickwatchevent *next; |
4860501e GB |
23 | } nickwatchevent; |
24 | ||
25 | static nickwatch *nickwatches; | |
26 | static int nextnickwatch = 1; | |
59694aa9 | 27 | static int nickwatchext; |
4860501e GB |
28 | |
29 | static void nw_dummyreply(nick *np, char *format, ...) { } | |
4860501e GB |
30 | static void nw_dummywall(int level, char *format, ...) { } |
31 | ||
6cd0364c | 32 | static nickwatch *nw_currentwatch; |
59694aa9 | 33 | static array nw_pendingnicks; |
4860501e GB |
34 | |
35 | static void nw_printnick(searchCtx *ctx, nick *sender, nick *np) { | |
36 | char hostbuf[HOSTLEN+NICKLEN+USERLEN+4]; | |
59694aa9 GB |
37 | char events[512]; |
38 | nickwatchevent *nwe = np->exts[nickwatchext]; | |
39 | int len; | |
4860501e | 40 | |
6cd0364c GB |
41 | nw_currentwatch->hits++; |
42 | ||
59694aa9 GB |
43 | events[0] = '\0'; |
44 | len = 0; | |
45 | ||
46 | for (nwe = np->exts[nickwatchext]; nwe; nwe = nwe->next) { | |
47 | if (len > 0) | |
48 | len += snprintf(events + len, sizeof(events) - len, ", "); | |
49 | ||
50 | len += snprintf(events + len, sizeof(events) - len, "%s", nwe->description); | |
51 | } | |
52 | ||
53 | controlwall(NO_OPER, NL_HITS, "nickwatch(#%d, %s): %s [%s] (%s) (%s)", nw_currentwatch->id, events, visiblehostmask(np,hostbuf), | |
4860501e GB |
54 | IPtostr(np->ipaddress), printflags(np->umodes, umodeflags), np->realname->name->content); |
55 | } | |
56 | ||
59694aa9 | 57 | static void nwe_enqueue(nick *np, const char *format, ...) { |
4860501e GB |
58 | nickwatchevent *nwe; |
59 | va_list va; | |
59694aa9 | 60 | int slot; |
4860501e GB |
61 | |
62 | nwe = malloc(sizeof(nickwatchevent)); | |
4860501e GB |
63 | |
64 | va_start(va, format); | |
65 | vsnprintf(nwe->description, sizeof(nwe->description), format, va); | |
66 | va_end(va); | |
67 | ||
59694aa9 GB |
68 | nwe->next = np->exts[nickwatchext]; |
69 | np->exts[nickwatchext] = nwe; | |
4860501e | 70 | |
59694aa9 GB |
71 | slot = array_getfreeslot(&nw_pendingnicks); |
72 | ((nick **)nw_pendingnicks.content)[slot] = np; | |
4860501e GB |
73 | } |
74 | ||
59694aa9 GB |
75 | static void nwe_clear(nick *np) { |
76 | nickwatchevent *nwe, *next; | |
4860501e | 77 | |
59694aa9 GB |
78 | for (nwe = np->exts[nickwatchext]; nwe; nwe = next) { |
79 | next = nwe->next; | |
80 | free(nwe); | |
4860501e | 81 | } |
59694aa9 GB |
82 | |
83 | np->exts[nickwatchext] = NULL; | |
84 | } | |
85 | ||
86 | static void nw_sched_processevents(void *arg) { | |
87 | nickwatch *nw; | |
45eb43cb GB |
88 | int i, slot; |
89 | unsigned int marker; | |
59694aa9 | 90 | nick *np; |
45eb43cb | 91 | array nicks; |
4860501e | 92 | |
45eb43cb GB |
93 | array_init(&nicks, sizeof(nick *)); |
94 | marker = nextnickmarker(); | |
4860501e | 95 | |
59694aa9 GB |
96 | for (i = 0; i < nw_pendingnicks.cursi; i++) { |
97 | np = ((nick **)nw_pendingnicks.content)[i]; | |
45eb43cb GB |
98 | |
99 | if (!np) | |
100 | continue; | |
101 | ||
102 | if (np->marker != marker) { | |
103 | np->marker = marker; | |
104 | slot = array_getfreeslot(&nicks); | |
105 | ((nick **)nicks.content)[slot] = np; | |
106 | } | |
59694aa9 GB |
107 | } |
108 | ||
109 | array_free(&nw_pendingnicks); | |
110 | array_init(&nw_pendingnicks, sizeof(nick *)); | |
45eb43cb GB |
111 | |
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); | |
115 | } | |
116 | ||
117 | for (i = 0; i < nicks.cursi; i++) { | |
118 | np = ((nick **)nicks.content)[i]; | |
119 | nwe_clear(np); | |
120 | } | |
121 | ||
122 | array_free(&nicks); | |
4860501e GB |
123 | } |
124 | ||
125 | static void nw_hook_newnick(int hooknum, void *arg) { | |
126 | nick *np = arg; | |
59694aa9 GB |
127 | nwe_enqueue(np, "new user"); |
128 | } | |
129 | ||
18afb5fb GB |
130 | static void nw_hook_account(int hooknum, void *arg) { |
131 | nick *np = arg; | |
132 | nwe_enqueue(np, "logged in with account %s", np->authname); | |
133 | } | |
134 | ||
59694aa9 GB |
135 | static void nw_hook_lostnick(int hooknum, void *arg) { |
136 | nick *np = arg; | |
137 | int i; | |
138 | ||
139 | nwe_clear(np); | |
140 | ||
9d938546 | 141 | for (i = 0; i < nw_pendingnicks.cursi; i++) |
59694aa9 | 142 | if (((nick **)nw_pendingnicks.content)[i] == np) |
9d938546 | 143 | ((nick **)nw_pendingnicks.content)[i] = NULL; |
4860501e GB |
144 | } |
145 | ||
9a9336db GB |
146 | static void nw_hook_rename(int hooknum, void *arg) { |
147 | void **args = arg; | |
148 | nick *np = args[0]; | |
149 | char *oldnick = args[1]; | |
59694aa9 | 150 | nwe_enqueue(np, "renamed from %s", oldnick); |
9a9336db GB |
151 | } |
152 | ||
474d3d62 GB |
153 | static void nw_hook_umodechange(int hooknum, void *arg) { |
154 | void **args = arg; | |
155 | nick *np = args[0]; | |
156 | flag_t oldmodes = (uintptr_t)args[1]; | |
157 | char buf[64]; | |
158 | strncpy(buf, printflags(np->umodes, umodeflags), sizeof(buf)); | |
59694aa9 | 159 | nwe_enqueue(np, "umodes %s -> %s", printflags(oldmodes, umodeflags), buf); |
474d3d62 GB |
160 | } |
161 | ||
80e32fcd GB |
162 | static void nw_hook_message(int hooknum, void *arg) { |
163 | void **args = arg; | |
164 | nick *np = args[0]; | |
165 | int isnotice = (uintptr_t)args[2]; | |
59694aa9 | 166 | nwe_enqueue(np, isnotice ? "notice" : "message"); |
80e32fcd GB |
167 | } |
168 | ||
4860501e GB |
169 | static void nw_hook_joinchannel(int hooknum, void *arg) { |
170 | void **args = arg; | |
171 | channel *cp = args[0]; | |
172 | nick *np = args[1]; | |
59694aa9 | 173 | nwe_enqueue(np, "join channel %s", cp->index->name->content); |
4860501e GB |
174 | } |
175 | ||
176 | static int nw_cmd_nickwatch(void *source, int cargc, char **cargv) { | |
177 | nick *sender = source; | |
178 | nickwatch *nw; | |
179 | parsertree *tree; | |
180 | ||
181 | if (cargc < 1) | |
182 | return CMD_USAGE; | |
183 | ||
184 | tree = parse_string(reg_nicksearch, cargv[0]); | |
185 | if (!tree) { | |
186 | displaystrerror(controlreply, sender, cargv[0]); | |
187 | return CMD_ERROR; | |
188 | } | |
189 | ||
190 | nw = malloc(sizeof(nickwatch)); | |
191 | nw->id = nextnickwatch++; | |
6cd0364c GB |
192 | snprintf(nw->createdby, sizeof(nw->createdby), "#%s", sender->authname); |
193 | nw->hits = 0; | |
4860501e GB |
194 | strncpy(nw->term, cargv[0], sizeof(nw->term)); |
195 | nw->tree = parse_string(reg_nicksearch, cargv[0]); | |
196 | nw->next = nickwatches; | |
197 | nickwatches = nw; | |
198 | ||
199 | controlreply(sender, "Done."); | |
200 | ||
201 | return CMD_OK; | |
202 | } | |
203 | ||
204 | static int nw_cmd_nickunwatch(void *source, int cargc, char **cargv) { | |
205 | nick *sender = source; | |
206 | nickwatch **pnext, *nw; | |
207 | int id; | |
208 | ||
209 | if (cargc < 1) | |
210 | return CMD_USAGE; | |
211 | ||
212 | id = atoi(cargv[0]); | |
213 | ||
214 | for (pnext = &nickwatches; *pnext; pnext = &((*pnext)->next)) { | |
215 | nw = *pnext; | |
216 | ||
217 | if (nw->id == id) { | |
218 | parse_free(nw->tree); | |
219 | *pnext = nw->next; | |
220 | free(nw); | |
221 | ||
222 | controlreply(sender, "Done."); | |
223 | return CMD_OK; | |
224 | } | |
225 | } | |
226 | ||
227 | controlreply(sender, "Nickwatch #%d not found.", id); | |
228 | ||
229 | return CMD_ERROR; | |
230 | } | |
231 | ||
232 | static int nw_cmd_nickwatches(void *source, int cargc, char **cargv) { | |
233 | nick *sender = source; | |
234 | nickwatch *nw; | |
235 | ||
6cd0364c | 236 | controlreply(sender, "ID Created By Hits Term"); |
4860501e GB |
237 | |
238 | for (nw = nickwatches; nw; nw = nw->next) | |
6cd0364c | 239 | controlreply(sender, "%-5d %-15s %-7d %s", nw->id, nw->createdby, nw->hits, nw->term); |
4860501e GB |
240 | |
241 | controlreply(sender, "--- End of nickwatches."); | |
242 | ||
243 | return CMD_OK; | |
244 | } | |
245 | ||
246 | void _init(void) { | |
59694aa9 GB |
247 | nickwatchext = registernickext("nickwatch"); |
248 | ||
249 | array_init(&nw_pendingnicks, sizeof(nick *)); | |
250 | ||
4860501e GB |
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."); | |
254 | ||
255 | registerhook(HOOK_NICK_NEWNICK, &nw_hook_newnick); | |
18afb5fb | 256 | registerhook(HOOK_NICK_ACCOUNT, &nw_hook_account); |
59694aa9 | 257 | registerhook(HOOK_NICK_LOSTNICK, &nw_hook_lostnick); |
9a9336db | 258 | registerhook(HOOK_NICK_RENAME, &nw_hook_rename); |
474d3d62 | 259 | registerhook(HOOK_NICK_MODECHANGE, &nw_hook_umodechange); |
80e32fcd | 260 | registerhook(HOOK_NICK_MESSAGE, &nw_hook_message); |
4860501e GB |
261 | registerhook(HOOK_CHANNEL_CREATE, &nw_hook_joinchannel); |
262 | registerhook(HOOK_CHANNEL_JOIN, &nw_hook_joinchannel); | |
59694aa9 GB |
263 | |
264 | schedulerecurring(time(NULL) + 5, 0, 1, &nw_sched_processevents, NULL); | |
4860501e GB |
265 | } |
266 | ||
267 | void _fini(void) { | |
268 | nickwatch *nw, *next; | |
269 | ||
270 | deregistercontrolcmd("nickwatch", &nw_cmd_nickwatch); | |
271 | deregistercontrolcmd("nickunwatch", &nw_cmd_nickunwatch); | |
272 | deregistercontrolcmd("nickwatches", &nw_cmd_nickwatches); | |
273 | ||
274 | deregisterhook(HOOK_NICK_NEWNICK, &nw_hook_newnick); | |
18afb5fb | 275 | deregisterhook(HOOK_NICK_ACCOUNT, &nw_hook_account); |
59694aa9 | 276 | deregisterhook(HOOK_NICK_LOSTNICK, &nw_hook_lostnick); |
9a9336db | 277 | deregisterhook(HOOK_NICK_RENAME, &nw_hook_rename); |
474d3d62 | 278 | deregisterhook(HOOK_NICK_MODECHANGE, &nw_hook_umodechange); |
80e32fcd | 279 | deregisterhook(HOOK_NICK_MESSAGE, &nw_hook_message); |
4860501e GB |
280 | deregisterhook(HOOK_CHANNEL_CREATE, &nw_hook_joinchannel); |
281 | deregisterhook(HOOK_CHANNEL_JOIN, &nw_hook_joinchannel); | |
282 | ||
59694aa9 GB |
283 | deleteallschedules(&nw_sched_processevents); |
284 | ||
285 | /* Process all pending events */ | |
286 | nw_sched_processevents(NULL); | |
287 | ||
288 | array_free(&nw_pendingnicks); | |
289 | ||
290 | releasenickext(nickwatchext); | |
291 | ||
4860501e GB |
292 | for (nw = nickwatches; nw; nw = next) { |
293 | next = nw->next; | |
294 | ||
295 | parse_free(nw->tree); | |
296 | free(nw); | |
297 | } | |
298 | } |