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