]>
Commit | Line | Data |
---|---|---|
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 | ||
12 | char createdby[64]; | |
13 | int hits; | |
14 | char term[512]; | |
15 | parsertree *tree; | |
16 | ||
17 | struct nickwatch *next; | |
18 | } nickwatch; | |
19 | ||
20 | typedef struct nickwatchevent { | |
21 | char description[128]; | |
22 | long numeric; | |
23 | } nickwatchevent; | |
24 | ||
25 | static nickwatch *nickwatches; | |
26 | static int nextnickwatch = 1; | |
27 | ||
28 | static void nw_dummyreply(nick *np, char *format, ...) { } | |
29 | ||
30 | static void nw_dummywall(int level, char *format, ...) { } | |
31 | ||
32 | static nickwatch *nw_currentwatch; | |
33 | static nickwatchevent *nw_currentevent; | |
34 | ||
35 | static void nw_printnick(searchCtx *ctx, nick *sender, nick *np) { | |
36 | char hostbuf[HOSTLEN+NICKLEN+USERLEN+4]; | |
37 | ||
38 | nw_currentwatch->hits++; | |
39 | ||
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); | |
42 | } | |
43 | ||
44 | static nickwatchevent *nwe_new(nick *np, const char *format, ...) { | |
45 | nickwatchevent *nwe; | |
46 | va_list va; | |
47 | ||
48 | nwe = malloc(sizeof(nickwatchevent)); | |
49 | nwe->numeric = np->numeric; | |
50 | ||
51 | va_start(va, format); | |
52 | vsnprintf(nwe->description, sizeof(nwe->description), format, va); | |
53 | va_end(va); | |
54 | ||
55 | return nwe; | |
56 | } | |
57 | ||
58 | static void nwe_free(nickwatchevent *nwe) { | |
59 | free(nwe); | |
60 | } | |
61 | ||
62 | static void nw_sched_processevent(void *arg) { | |
63 | nickwatchevent *nwe = arg; | |
64 | nick *np; | |
65 | nickwatch *nw; | |
66 | ||
67 | np = getnickbynumeric(nwe->numeric); | |
68 | ||
69 | if (!np) { | |
70 | nwe_free(nwe); | |
71 | return; | |
72 | } | |
73 | nw_currentevent = nwe; | |
74 | ||
75 | for (nw = nickwatches; nw; nw = nw->next) { | |
76 | nw_currentwatch = nw; | |
77 | ast_nicksearch(nw->tree->root, &nw_dummyreply, mynick, &nw_dummywall, &nw_printnick, NULL, NULL, 10, np); | |
78 | } | |
79 | ||
80 | nwe_free(nwe); | |
81 | } | |
82 | ||
83 | static void nw_hook_newnick(int hooknum, void *arg) { | |
84 | nick *np = arg; | |
85 | nickwatchevent *nwe = nwe_new(np, "new user"); | |
86 | scheduleoneshot(0, nw_sched_processevent, nwe); | |
87 | } | |
88 | ||
89 | static void nw_hook_rename(int hooknum, void *arg) { | |
90 | void **args = arg; | |
91 | nick *np = args[0]; | |
92 | char *oldnick = args[1]; | |
93 | nickwatchevent *nwe = nwe_new(np, "renamed from %s", oldnick); | |
94 | scheduleoneshot(0, nw_sched_processevent, nwe); | |
95 | } | |
96 | ||
97 | static void nw_hook_umodechange(int hooknum, void *arg) { | |
98 | void **args = arg; | |
99 | nick *np = args[0]; | |
100 | flag_t oldmodes = (uintptr_t)args[1]; | |
101 | char buf[64]; | |
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); | |
105 | } | |
106 | ||
107 | static void nw_hook_message(int hooknum, void *arg) { | |
108 | void **args = arg; | |
109 | nick *np = args[0]; | |
110 | int isnotice = (uintptr_t)args[2]; | |
111 | nickwatchevent *nwe = nwe_new(np, isnotice ? "notice" : "message"); | |
112 | scheduleoneshot(0, nw_sched_processevent, nwe); | |
113 | } | |
114 | ||
115 | static void nw_hook_joinchannel(int hooknum, void *arg) { | |
116 | void **args = arg; | |
117 | channel *cp = args[0]; | |
118 | nick *np = args[1]; | |
119 | nickwatchevent *nwe = nwe_new(np, "join channel %s", cp->index->name->content); | |
120 | scheduleoneshot(0, nw_sched_processevent, nwe); | |
121 | } | |
122 | ||
123 | static int nw_cmd_nickwatch(void *source, int cargc, char **cargv) { | |
124 | nick *sender = source; | |
125 | nickwatch *nw; | |
126 | parsertree *tree; | |
127 | ||
128 | if (cargc < 1) | |
129 | return CMD_USAGE; | |
130 | ||
131 | tree = parse_string(reg_nicksearch, cargv[0]); | |
132 | if (!tree) { | |
133 | displaystrerror(controlreply, sender, cargv[0]); | |
134 | return CMD_ERROR; | |
135 | } | |
136 | ||
137 | nw = malloc(sizeof(nickwatch)); | |
138 | nw->id = nextnickwatch++; | |
139 | snprintf(nw->createdby, sizeof(nw->createdby), "#%s", sender->authname); | |
140 | nw->hits = 0; | |
141 | strncpy(nw->term, cargv[0], sizeof(nw->term)); | |
142 | nw->tree = parse_string(reg_nicksearch, cargv[0]); | |
143 | nw->next = nickwatches; | |
144 | nickwatches = nw; | |
145 | ||
146 | controlreply(sender, "Done."); | |
147 | ||
148 | return CMD_OK; | |
149 | } | |
150 | ||
151 | static int nw_cmd_nickunwatch(void *source, int cargc, char **cargv) { | |
152 | nick *sender = source; | |
153 | nickwatch **pnext, *nw; | |
154 | int id; | |
155 | ||
156 | if (cargc < 1) | |
157 | return CMD_USAGE; | |
158 | ||
159 | id = atoi(cargv[0]); | |
160 | ||
161 | for (pnext = &nickwatches; *pnext; pnext = &((*pnext)->next)) { | |
162 | nw = *pnext; | |
163 | ||
164 | if (nw->id == id) { | |
165 | parse_free(nw->tree); | |
166 | *pnext = nw->next; | |
167 | free(nw); | |
168 | ||
169 | controlreply(sender, "Done."); | |
170 | return CMD_OK; | |
171 | } | |
172 | } | |
173 | ||
174 | controlreply(sender, "Nickwatch #%d not found.", id); | |
175 | ||
176 | return CMD_ERROR; | |
177 | } | |
178 | ||
179 | static int nw_cmd_nickwatches(void *source, int cargc, char **cargv) { | |
180 | nick *sender = source; | |
181 | nickwatch *nw; | |
182 | ||
183 | controlreply(sender, "ID Created By Hits Term"); | |
184 | ||
185 | for (nw = nickwatches; nw; nw = nw->next) | |
186 | controlreply(sender, "%-5d %-15s %-7d %s", nw->id, nw->createdby, nw->hits, nw->term); | |
187 | ||
188 | controlreply(sender, "--- End of nickwatches."); | |
189 | ||
190 | return CMD_OK; | |
191 | } | |
192 | ||
193 | void _init(void) { | |
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."); | |
197 | ||
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); | |
204 | } | |
205 | ||
206 | void _fini(void) { | |
207 | nickwatch *nw, *next; | |
208 | ||
209 | deregistercontrolcmd("nickwatch", &nw_cmd_nickwatch); | |
210 | deregistercontrolcmd("nickunwatch", &nw_cmd_nickunwatch); | |
211 | deregistercontrolcmd("nickwatches", &nw_cmd_nickwatches); | |
212 | ||
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); | |
219 | ||
220 | for (nw = nickwatches; nw; nw = next) { | |
221 | next = nw->next; | |
222 | ||
223 | parse_free(nw->tree); | |
224 | free(nw); | |
225 | } | |
226 | } |