]>
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; | |
88 | int i; | |
89 | nick *np; | |
4860501e GB |
90 | |
91 | for (nw = nickwatches; nw; nw = nw->next) { | |
6cd0364c | 92 | nw_currentwatch = nw; |
59694aa9 | 93 | ast_nicksearch(nw->tree->root, &nw_dummyreply, mynick, &nw_dummywall, &nw_printnick, NULL, NULL, 10, &nw_pendingnicks); |
4860501e GB |
94 | } |
95 | ||
59694aa9 GB |
96 | for (i = 0; i < nw_pendingnicks.cursi; i++) { |
97 | np = ((nick **)nw_pendingnicks.content)[i]; | |
98 | nwe_clear(np); | |
99 | } | |
100 | ||
101 | array_free(&nw_pendingnicks); | |
102 | array_init(&nw_pendingnicks, sizeof(nick *)); | |
4860501e GB |
103 | } |
104 | ||
105 | static void nw_hook_newnick(int hooknum, void *arg) { | |
106 | nick *np = arg; | |
59694aa9 GB |
107 | nwe_enqueue(np, "new user"); |
108 | } | |
109 | ||
110 | static void nw_hook_lostnick(int hooknum, void *arg) { | |
111 | nick *np = arg; | |
112 | int i; | |
113 | ||
114 | nwe_clear(np); | |
115 | ||
116 | for (i = 0; i < nw_pendingnicks.cursi;) | |
117 | if (((nick **)nw_pendingnicks.content)[i] == np) | |
118 | array_delslot(&nw_pendingnicks, i); | |
119 | else | |
120 | i++; | |
4860501e GB |
121 | } |
122 | ||
9a9336db GB |
123 | static void nw_hook_rename(int hooknum, void *arg) { |
124 | void **args = arg; | |
125 | nick *np = args[0]; | |
126 | char *oldnick = args[1]; | |
59694aa9 | 127 | nwe_enqueue(np, "renamed from %s", oldnick); |
9a9336db GB |
128 | } |
129 | ||
474d3d62 GB |
130 | static void nw_hook_umodechange(int hooknum, void *arg) { |
131 | void **args = arg; | |
132 | nick *np = args[0]; | |
133 | flag_t oldmodes = (uintptr_t)args[1]; | |
134 | char buf[64]; | |
135 | strncpy(buf, printflags(np->umodes, umodeflags), sizeof(buf)); | |
59694aa9 | 136 | nwe_enqueue(np, "umodes %s -> %s", printflags(oldmodes, umodeflags), buf); |
474d3d62 GB |
137 | } |
138 | ||
80e32fcd GB |
139 | static void nw_hook_message(int hooknum, void *arg) { |
140 | void **args = arg; | |
141 | nick *np = args[0]; | |
142 | int isnotice = (uintptr_t)args[2]; | |
59694aa9 | 143 | nwe_enqueue(np, isnotice ? "notice" : "message"); |
80e32fcd GB |
144 | } |
145 | ||
4860501e GB |
146 | static void nw_hook_joinchannel(int hooknum, void *arg) { |
147 | void **args = arg; | |
148 | channel *cp = args[0]; | |
149 | nick *np = args[1]; | |
59694aa9 | 150 | nwe_enqueue(np, "join channel %s", cp->index->name->content); |
4860501e GB |
151 | } |
152 | ||
153 | static int nw_cmd_nickwatch(void *source, int cargc, char **cargv) { | |
154 | nick *sender = source; | |
155 | nickwatch *nw; | |
156 | parsertree *tree; | |
157 | ||
158 | if (cargc < 1) | |
159 | return CMD_USAGE; | |
160 | ||
161 | tree = parse_string(reg_nicksearch, cargv[0]); | |
162 | if (!tree) { | |
163 | displaystrerror(controlreply, sender, cargv[0]); | |
164 | return CMD_ERROR; | |
165 | } | |
166 | ||
167 | nw = malloc(sizeof(nickwatch)); | |
168 | nw->id = nextnickwatch++; | |
6cd0364c GB |
169 | snprintf(nw->createdby, sizeof(nw->createdby), "#%s", sender->authname); |
170 | nw->hits = 0; | |
4860501e GB |
171 | strncpy(nw->term, cargv[0], sizeof(nw->term)); |
172 | nw->tree = parse_string(reg_nicksearch, cargv[0]); | |
173 | nw->next = nickwatches; | |
174 | nickwatches = nw; | |
175 | ||
176 | controlreply(sender, "Done."); | |
177 | ||
178 | return CMD_OK; | |
179 | } | |
180 | ||
181 | static int nw_cmd_nickunwatch(void *source, int cargc, char **cargv) { | |
182 | nick *sender = source; | |
183 | nickwatch **pnext, *nw; | |
184 | int id; | |
185 | ||
186 | if (cargc < 1) | |
187 | return CMD_USAGE; | |
188 | ||
189 | id = atoi(cargv[0]); | |
190 | ||
191 | for (pnext = &nickwatches; *pnext; pnext = &((*pnext)->next)) { | |
192 | nw = *pnext; | |
193 | ||
194 | if (nw->id == id) { | |
195 | parse_free(nw->tree); | |
196 | *pnext = nw->next; | |
197 | free(nw); | |
198 | ||
199 | controlreply(sender, "Done."); | |
200 | return CMD_OK; | |
201 | } | |
202 | } | |
203 | ||
204 | controlreply(sender, "Nickwatch #%d not found.", id); | |
205 | ||
206 | return CMD_ERROR; | |
207 | } | |
208 | ||
209 | static int nw_cmd_nickwatches(void *source, int cargc, char **cargv) { | |
210 | nick *sender = source; | |
211 | nickwatch *nw; | |
212 | ||
6cd0364c | 213 | controlreply(sender, "ID Created By Hits Term"); |
4860501e GB |
214 | |
215 | for (nw = nickwatches; nw; nw = nw->next) | |
6cd0364c | 216 | controlreply(sender, "%-5d %-15s %-7d %s", nw->id, nw->createdby, nw->hits, nw->term); |
4860501e GB |
217 | |
218 | controlreply(sender, "--- End of nickwatches."); | |
219 | ||
220 | return CMD_OK; | |
221 | } | |
222 | ||
223 | void _init(void) { | |
59694aa9 GB |
224 | nickwatchext = registernickext("nickwatch"); |
225 | ||
226 | array_init(&nw_pendingnicks, sizeof(nick *)); | |
227 | ||
4860501e GB |
228 | registercontrolhelpcmd("nickwatch", NO_OPER, 1, &nw_cmd_nickwatch, "Usage: nickwatch <nicksearch term>\nAdds a nickwatch entry."); |
229 | registercontrolhelpcmd("nickunwatch", NO_OPER, 1, &nw_cmd_nickunwatch, "Usage: nickunwatch <#id>\nRemoves a nickwatch entry."); | |
230 | registercontrolhelpcmd("nickwatches", NO_OPER, 0, &nw_cmd_nickwatches, "Usage: nickwatches\nLists nickwatches."); | |
231 | ||
232 | registerhook(HOOK_NICK_NEWNICK, &nw_hook_newnick); | |
59694aa9 | 233 | registerhook(HOOK_NICK_LOSTNICK, &nw_hook_lostnick); |
9a9336db | 234 | registerhook(HOOK_NICK_RENAME, &nw_hook_rename); |
474d3d62 | 235 | registerhook(HOOK_NICK_MODECHANGE, &nw_hook_umodechange); |
80e32fcd | 236 | registerhook(HOOK_NICK_MESSAGE, &nw_hook_message); |
4860501e GB |
237 | registerhook(HOOK_CHANNEL_CREATE, &nw_hook_joinchannel); |
238 | registerhook(HOOK_CHANNEL_JOIN, &nw_hook_joinchannel); | |
59694aa9 GB |
239 | |
240 | schedulerecurring(time(NULL) + 5, 0, 1, &nw_sched_processevents, NULL); | |
4860501e GB |
241 | } |
242 | ||
243 | void _fini(void) { | |
244 | nickwatch *nw, *next; | |
245 | ||
246 | deregistercontrolcmd("nickwatch", &nw_cmd_nickwatch); | |
247 | deregistercontrolcmd("nickunwatch", &nw_cmd_nickunwatch); | |
248 | deregistercontrolcmd("nickwatches", &nw_cmd_nickwatches); | |
249 | ||
250 | deregisterhook(HOOK_NICK_NEWNICK, &nw_hook_newnick); | |
59694aa9 | 251 | deregisterhook(HOOK_NICK_LOSTNICK, &nw_hook_lostnick); |
9a9336db | 252 | deregisterhook(HOOK_NICK_RENAME, &nw_hook_rename); |
474d3d62 | 253 | deregisterhook(HOOK_NICK_MODECHANGE, &nw_hook_umodechange); |
80e32fcd | 254 | deregisterhook(HOOK_NICK_MESSAGE, &nw_hook_message); |
4860501e GB |
255 | deregisterhook(HOOK_CHANNEL_CREATE, &nw_hook_joinchannel); |
256 | deregisterhook(HOOK_CHANNEL_JOIN, &nw_hook_joinchannel); | |
257 | ||
59694aa9 GB |
258 | deleteallschedules(&nw_sched_processevents); |
259 | ||
260 | /* Process all pending events */ | |
261 | nw_sched_processevents(NULL); | |
262 | ||
263 | array_free(&nw_pendingnicks); | |
264 | ||
265 | releasenickext(nickwatchext); | |
266 | ||
4860501e GB |
267 | for (nw = nickwatches; nw; nw = next) { |
268 | next = nw->next; | |
269 | ||
270 | parse_free(nw->tree); | |
271 | free(nw); | |
272 | } | |
273 | } |