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