]>
Commit | Line | Data |
---|---|---|
1 | /* proto-p10.c - IRC protocol output | |
2 | * Copyright 2000-2004 srvx Development Team | |
3 | * | |
4 | * This file is part of x3. | |
5 | * | |
6 | * x3 is free software; you can redistribute it and/or modify | |
7 | * it under the terms of the GNU General Public License as published by | |
8 | * the Free Software Foundation; either version 2 of the License, or | |
9 | * (at your option) any later version. | |
10 | * | |
11 | * This program is distributed in the hope that it will be useful, | |
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
14 | * GNU General Public License for more details. | |
15 | * | |
16 | * You should have received a copy of the GNU General Public License | |
17 | * along with srvx; if not, write to the Free Software Foundation, | |
18 | * Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. | |
19 | */ | |
20 | ||
21 | #include "nickserv.h" | |
22 | #include "chanserv.h" | |
23 | #include "hosthiding.h" | |
24 | #include "proto-common.c" | |
25 | #include "opserv.h" | |
26 | ||
27 | /* Full commands. */ | |
28 | #define CMD_ACCOUNT "ACCOUNT" | |
29 | #define CMD_ADMIN "ADMIN" | |
30 | #define CMD_ASLL "ASLL" | |
31 | #define CMD_AWAY "AWAY" | |
32 | #define CMD_BURST "BURST" | |
33 | #define CMD_CLEARMODE "CLEARMODE" | |
34 | #define CMD_CLOSE "CLOSE" | |
35 | #define CMD_CNOTICE "CNOTICE" | |
36 | #define CMD_CONNECT "CONNECT" | |
37 | #define CMD_CPRIVMSG "CPRIVMSG" | |
38 | #define CMD_CREATE "CREATE" | |
39 | #define CMD_DESTRUCT "DESTRUCT" | |
40 | #define CMD_DESYNCH "DESYNCH" | |
41 | #define CMD_DIE "DIE" | |
42 | #define CMD_DNS "DNS" | |
43 | #define CMD_EOB "END_OF_BURST" | |
44 | #define CMD_EOB_ACK "EOB_ACK" | |
45 | #define CMD_ERROR "ERROR" | |
46 | #define CMD_FAKEHOST "FAKE" | |
47 | #define CMD_GET "GET" | |
48 | #define CMD_GLINE "GLINE" | |
49 | #define CMD_HASH "HASH" | |
50 | #define CMD_HELP "HELP" | |
51 | #define CMD_INFO "INFO" | |
52 | #define CMD_INVITE "INVITE" | |
53 | #define CMD_ISON "ISON" | |
54 | #define CMD_JOIN "JOIN" | |
55 | #define CMD_JUPE "JUPE" | |
56 | #define CMD_KICK "KICK" | |
57 | #define CMD_KILL "KILL" | |
58 | #define CMD_LINKS "LINKS" | |
59 | #define CMD_LIST "LIST" | |
60 | #define CMD_LUSERS "LUSERS" | |
61 | #define CMD_MAP "MAP" | |
62 | #define CMD_MARK "MARK" | |
63 | #define CMD_MODE "MODE" | |
64 | #define CMD_MOTD "MOTD" | |
65 | #define CMD_NAMES "NAMES" | |
66 | #define CMD_NICK "NICK" | |
67 | #define CMD_NOTICE "NOTICE" | |
68 | #define CMD_OPER "OPER" | |
69 | #define CMD_OPMODE "OPMODE" | |
70 | #define CMD_PART "PART" | |
71 | #define CMD_PASS "PASS" | |
72 | #define CMD_PING "PING" | |
73 | #define CMD_PONG "PONG" | |
74 | #define CMD_POST "POST" | |
75 | #define CMD_PRIVMSG "PRIVMSG" | |
76 | #define CMD_PRIVS "PRIVS" | |
77 | #define CMD_PROTO "PROTO" | |
78 | #define CMD_QUIT "QUIT" | |
79 | #define CMD_REHASH "REHASH" | |
80 | #define CMD_RESET "RESET" | |
81 | #define CMD_RESTART "RESTART" | |
82 | #define CMD_RPING "RPING" | |
83 | #define CMD_RPONG "RPONG" | |
84 | #define CMD_SERVER "SERVER" | |
85 | #define CMD_SERVLIST "SERVLIST" | |
86 | #define CMD_SERVSET "SERVSET" | |
87 | #define CMD_SET "SET" | |
88 | #define CMD_SETTIME "SETTIME" | |
89 | #define CMD_SHUN "SHUN" | |
90 | #define CMD_SILENCE "SILENCE" | |
91 | #define CMD_SQUERY "SQUERY" | |
92 | #define CMD_SQUIT "SQUIT" | |
93 | #define CMD_STATS "STATS" | |
94 | #define CMD_SVSNICK "SVSNICK" | |
95 | #define CMD_SWHOIS "SWHOIS" | |
96 | #define CMD_TIME "TIME" | |
97 | #define CMD_TOPIC "TOPIC" | |
98 | #define CMD_TRACE "TRACE" | |
99 | #define CMD_UPING "UPING" | |
100 | #define CMD_USER "USER" | |
101 | #define CMD_USERHOST "USERHOST" | |
102 | #define CMD_USERIP "USERIP" | |
103 | #define CMD_VERSION "VERSION" | |
104 | #define CMD_WALLCHOPS "WALLCHOPS" | |
105 | #define CMD_WALLOPS "WALLOPS" | |
106 | #define CMD_WALLHOPS "WALLHOPS" | |
107 | #define CMD_WALLUSERS "WALLUSERS" | |
108 | #define CMD_WALLVOICES "WALLVOICES" | |
109 | #define CMD_WALLHOPS "WALLHOPS" | |
110 | #define CMD_WHO "WHO" | |
111 | #define CMD_WHOIS "WHOIS" | |
112 | #define CMD_WHOWAS "WHOWAS" | |
113 | ||
114 | /* Tokenized commands. */ | |
115 | #define TOK_ACCOUNT "AC" | |
116 | #define TOK_ADMIN "AD" | |
117 | #define TOK_ASLL "LL" | |
118 | #define TOK_AWAY "A" | |
119 | #define TOK_BURST "B" | |
120 | #define TOK_CLEARMODE "CM" | |
121 | #define TOK_CLOSE "CLOSE" | |
122 | #define TOK_CNOTICE "CN" | |
123 | #define TOK_CONNECT "CO" | |
124 | #define TOK_CPRIVMSG "CP" | |
125 | #define TOK_CREATE "C" | |
126 | #define TOK_DESTRUCT "DE" | |
127 | #define TOK_DESYNCH "DS" | |
128 | #define TOK_DIE "DIE" | |
129 | #define TOK_DNS "DNS" | |
130 | #define TOK_EOB "EB" | |
131 | #define TOK_EOB_ACK "EA" | |
132 | #define TOK_ERROR "Y" | |
133 | #define TOK_EXEMPT "EX" | |
134 | #define TOK_FAKEHOST "FA" | |
135 | #define TOK_GET "GET" | |
136 | #define TOK_GLINE "GL" | |
137 | #define TOK_HASH "HASH" | |
138 | #define TOK_HELP "HELP" | |
139 | #define TOK_INFO "F" | |
140 | #define TOK_INVITE "I" | |
141 | #define TOK_ISON "ISON" | |
142 | #define TOK_JOIN "J" | |
143 | #define TOK_JUPE "JU" | |
144 | #define TOK_KICK "K" | |
145 | #define TOK_KILL "D" | |
146 | #define TOK_LINKS "LI" | |
147 | #define TOK_LIST "LIST" | |
148 | #define TOK_LUSERS "LU" | |
149 | #define TOK_MAP "MAP" | |
150 | #define TOK_MARK "MK" | |
151 | #define TOK_MODE "M" | |
152 | #define TOK_MOTD "MO" | |
153 | #define TOK_NAMES "E" | |
154 | #define TOK_NICK "N" | |
155 | #define TOK_NOTICE "O" | |
156 | #define TOK_OPER "OPER" | |
157 | #define TOK_OPMODE "OM" | |
158 | #define TOK_PART "L" | |
159 | #define TOK_PASS "PA" | |
160 | #define TOK_PING "G" | |
161 | #define TOK_PONG "Z" | |
162 | #define TOK_POST "POST" | |
163 | #define TOK_PRIVMSG "P" | |
164 | #define TOK_PRIVS "PRIVS" | |
165 | #define TOK_PROTO "PROTO" | |
166 | #define TOK_QUIT "Q" | |
167 | #define TOK_REHASH "REHASH" | |
168 | #define TOK_RESET "RESET" | |
169 | #define TOK_RESTART "RESTART" | |
170 | #define TOK_RPING "RI" | |
171 | #define TOK_RPONG "RO" | |
172 | #define TOK_SERVER "S" | |
173 | #define TOK_SERVLIST "SERVSET" | |
174 | #define TOK_SERVSET "SERVSET" | |
175 | #define TOK_SET "SET" | |
176 | #define TOK_SETTIME "SE" | |
177 | #define TOK_SHUN "SU" | |
178 | #define TOK_SILENCE "U" | |
179 | #define TOK_SQUERY "SQUERY" | |
180 | #define TOK_SQUIT "SQ" | |
181 | #define TOK_STATS "R" | |
182 | #define TOK_SVSNICK "SN" | |
183 | #define TOK_SWHOIS "SW" | |
184 | #define TOK_TIME "TI" | |
185 | #define TOK_TOPIC "T" | |
186 | #define TOK_TRACE "TR" | |
187 | #define TOK_UPING "UP" | |
188 | #define TOK_USER "USER" | |
189 | #define TOK_USERHOST "USERHOST" | |
190 | #define TOK_USERIP "USERIP" | |
191 | #define TOK_VERSION "V" | |
192 | #define TOK_WALLCHOPS "WC" | |
193 | #define TOK_WALLOPS "WA" | |
194 | #define TOK_WALLHOPS "WH" | |
195 | #define TOK_WALLUSERS "WU" | |
196 | #define TOK_WALLVOICES "WV" | |
197 | #define TOK_WALLHOPS "WH" | |
198 | #define TOK_WHO "H" | |
199 | #define TOK_WHOIS "W" | |
200 | #define TOK_WHOWAS "X" | |
201 | ||
202 | /* Protocol messages; aliased to full commands or tokens depending | |
203 | on compile-time configuration. ircu prefers tokens WITH THE | |
204 | EXCEPTION OF THE SERVER AND PASS COMMANDS, which cannot be | |
205 | tokenized, because clients' (ie. a linking server) commands are | |
206 | only checked against the full command list. | |
207 | */ | |
208 | #if defined(ENABLE_TOKENS) | |
209 | #define TYPE(NAME) TOK_ ## NAME | |
210 | #else /* !ENABLE_TOKENS */ | |
211 | #define TYPE(NAME) CMD_ ## NAME | |
212 | #endif /* ENABLE_TOKENS */ | |
213 | ||
214 | #define P10_ACCOUNT TYPE(ACCOUNT) | |
215 | #define P10_ADMIN TYPE(ADMIN) | |
216 | #define P10_ASLL TYPE(ASLL) | |
217 | #define P10_AWAY TYPE(AWAY) | |
218 | #define P10_BURST TYPE(BURST) | |
219 | #define P10_CLEARMODE TYPE(CLEARMODE) | |
220 | #define P10_CLOSE TYPE(CLOSE) | |
221 | #define P10_CNOTICE TYPE(CNOTICE) | |
222 | #define P10_CONNECT TYPE(CONNECT) | |
223 | #define P10_CPRIVMSG TYPE(CPRIVMSG) | |
224 | #define P10_CREATE TYPE(CREATE) | |
225 | #define P10_DESTRUCT TYPE(DESTRUCT) | |
226 | #define P10_DESYNCH TYPE(DESYNCH) | |
227 | #define P10_DIE TYPE(DIE) | |
228 | #define P10_DNS TYPE(DNS) | |
229 | #define P10_EOB TYPE(EOB) | |
230 | #define P10_EOB_ACK TYPE(EOB_ACK) | |
231 | #define P10_ERROR TYPE(ERROR) | |
232 | #define P10_FAKEHOST TYPE(FAKEHOST) | |
233 | #define P10_GET TYPE(GET) | |
234 | #define P10_GLINE TYPE(GLINE) | |
235 | #define P10_HASH TYPE(HASH) | |
236 | #define P10_HELP TYPE(HELP) | |
237 | #define P10_INFO TYPE(INFO) | |
238 | #define P10_INVITE TYPE(INVITE) | |
239 | #define P10_ISON TYPE(ISON) | |
240 | #define P10_JOIN TYPE(JOIN) | |
241 | #define P10_JUPE TYPE(JUPE) | |
242 | #define P10_KICK TYPE(KICK) | |
243 | #define P10_KILL TYPE(KILL) | |
244 | #define P10_LINKS TYPE(LINKS) | |
245 | #define P10_LIST TYPE(LIST) | |
246 | #define P10_LUSERS TYPE(LUSERS) | |
247 | #define P10_MAP TYPE(MAP) | |
248 | #define P10_MARK TYPE(MARK) | |
249 | #define P10_MODE TYPE(MODE) | |
250 | #define P10_MOTD TYPE(MOTD) | |
251 | #define P10_NAMES TYPE(NAMES) | |
252 | #define P10_NICK TYPE(NICK) | |
253 | #define P10_NOTICE TYPE(NOTICE) | |
254 | #define P10_OPER TYPE(OPER) | |
255 | #define P10_OPMODE TYPE(OPMODE) | |
256 | #define P10_PART TYPE(PART) | |
257 | #define P10_PASS CMD_PASS | |
258 | #define P10_PING TYPE(PING) | |
259 | #define P10_PONG TYPE(PONG) | |
260 | #define P10_POST TYPE(POST) | |
261 | #define P10_PRIVMSG TYPE(PRIVMSG) | |
262 | #define P10_PRIVS TYPE(PRIVS) | |
263 | #define P10_PROTO TYPE(PROTO) | |
264 | #define P10_QUIT TYPE(QUIT) | |
265 | #define P10_REHASH TYPE(REHASH) | |
266 | #define P10_RESET TYPE(RESET) | |
267 | #define P10_RESTART TYPE(RESTART) | |
268 | #define P10_RPING TYPE(RPING) | |
269 | #define P10_RPONG TYPE(RPONG) | |
270 | #define P10_SERVER CMD_SERVER | |
271 | #define P10_SERVLIST TYPE(SERVLIST) | |
272 | #define P10_SERVSET TYPE(SERVSET) | |
273 | #define P10_SET TYPE(SET) | |
274 | #define P10_SETTIME TYPE(SETTIME) | |
275 | #define P10_SHUN TYPE(SHUN) | |
276 | #define P10_SILENCE TYPE(SILENCE) | |
277 | #define P10_SQUERY TYPE(SQUERY) | |
278 | #define P10_SQUIT TYPE(SQUIT) | |
279 | #define P10_STATS TYPE(STATS) | |
280 | #define P10_SVSNICK TYPE(SVSNICK) | |
281 | #define P10_SWHOIS TYPE(SWHOIS) | |
282 | #define P10_TIME TYPE(TIME) | |
283 | #define P10_TOPIC TYPE(TOPIC) | |
284 | #define P10_TRACE TYPE(TRACE) | |
285 | #define P10_UPING TYPE(UPING) | |
286 | #define P10_USER TYPE(USER) | |
287 | #define P10_USERHOST TYPE(USERHOST) | |
288 | #define P10_USERIP TYPE(USERIP) | |
289 | #define P10_VERSION TYPE(VERSION) | |
290 | #define P10_WALLCHOPS TYPE(WALLCHOPS) | |
291 | #define P10_WALLOPS TYPE(WALLOPS) | |
292 | #define P10_WALLHOPS TYPE(WALLHOPS) | |
293 | #define P10_WALLUSERS TYPE(WALLUSERS) | |
294 | #define P10_WALLVOICES TYPE(WALLVOICES) | |
295 | #define P10_WHO TYPE(WHO) | |
296 | #define P10_WHOIS TYPE(WHOIS) | |
297 | #define P10_WHOWAS TYPE(WHOWAS) | |
298 | #define P10_EXEMPT TYPE(EXEMPT) | |
299 | ||
300 | /* Servers claiming to have a boot or link time before PREHISTORY | |
301 | * trigger errors to the log. We hope no server has been running | |
302 | * constantly since September 1994. :) | |
303 | */ | |
304 | #define PREHISTORY 780000000 | |
305 | ||
306 | static struct server *servers_num[64*64]; | |
307 | static privmsg_func_t *privmsg_funcs; | |
308 | static unsigned int num_privmsg_funcs; | |
309 | static privmsg_func_t *notice_funcs; | |
310 | static unsigned int num_notice_funcs; | |
311 | static struct dict *unbursted_channels; | |
312 | static char *his_servername; | |
313 | static char *his_servercomment; | |
314 | static int extended_accounts; | |
315 | ||
316 | static struct userNode *AddUser(struct server* uplink, const char *nick, const char *ident, const char *hostname, const char *modes, const char *numeric, const char *userinfo, time_t timestamp, const char *realip); | |
317 | ||
318 | extern int off_channel; | |
319 | ||
320 | static int parse_oplevel(char *str); | |
321 | ||
322 | /* Numerics can be XYY, XYYY, or XXYYY; with X's identifying the | |
323 | * server and Y's indentifying the client on that server. */ | |
324 | struct server* | |
325 | GetServerN(const char *numeric) | |
326 | { | |
327 | switch (strlen(numeric)) { | |
328 | default: | |
329 | return servers_num[base64toint(numeric, 2)]; | |
330 | case 4: | |
331 | case 3: | |
332 | case 1: | |
333 | return servers_num[base64toint(numeric, 1)]; | |
334 | case 0: | |
335 | return NULL; | |
336 | } | |
337 | } | |
338 | ||
339 | struct userNode* | |
340 | GetUserN(const char *numeric) /* using numeric */ | |
341 | { | |
342 | struct userNode *un; | |
343 | struct server *s; | |
344 | int n, slen, ulen; | |
345 | ||
346 | switch (strlen(numeric)) { | |
347 | default: | |
348 | log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): numeric too long!", numeric); | |
349 | return NULL; | |
350 | case 5: slen = 2; ulen = 3; break; | |
351 | case 4: slen = 1; ulen = 3; break; | |
352 | case 3: slen = 1; ulen = 2; break; | |
353 | case 2: case 1: case 0: | |
354 | log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): numeric too short!", numeric); | |
355 | return NULL; | |
356 | } | |
357 | if (!(s = servers_num[base64toint(numeric, slen)])) { | |
358 | log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): couldn't find server (len=%d)!", numeric, slen); | |
359 | return NULL; | |
360 | } | |
361 | n = base64toint(numeric+slen, ulen) & s->num_mask; | |
362 | if (!(un = s->users[n])) { | |
363 | log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s) couldn't find user!", numeric); | |
364 | } | |
365 | return un; | |
366 | } | |
367 | ||
368 | static void | |
369 | privmsg_user_helper(struct userNode *un, void *data) | |
370 | { | |
371 | struct privmsg_desc *pd = data; | |
372 | unsigned int num = un->num_local; | |
373 | if (!pd->is_notice) { | |
374 | if ((num < num_privmsg_funcs) && privmsg_funcs[num]) { | |
375 | privmsg_funcs[num](pd->user, un, pd->text, pd->is_qualified); | |
376 | } | |
377 | } else { | |
378 | if ((num < num_notice_funcs) && notice_funcs[num]) { | |
379 | notice_funcs[num](pd->user, un, pd->text, pd->is_qualified); | |
380 | } | |
381 | } | |
382 | } | |
383 | ||
384 | /* equiv to user doing /connect server port target */ | |
385 | void irc_connect(struct userNode *user, char *server, unsigned int port, struct server *target) | |
386 | { | |
387 | putsock("%s " P10_CONNECT " %s %d %s", user->numeric, server, port, target->numeric); | |
388 | } | |
389 | ||
390 | void | |
391 | irc_squit_route(struct server *srv, const char *message, ...) | |
392 | { | |
393 | va_list arg_list; | |
394 | char buffer[MAXLEN]; | |
395 | va_start(arg_list, message); | |
396 | vsnprintf(buffer, MAXLEN-2, message, arg_list); | |
397 | buffer[MAXLEN-1] = 0; | |
398 | ||
399 | /* When would we squit ourselves exactly?? -Rubin */ | |
400 | if(srv == self && cManager.uplink->state == CONNECTED ) { | |
401 | unsigned int i; | |
402 | ||
403 | /* Quit all clients linked to me. */ | |
404 | for(i = 0; i <= self->num_mask; i++) { | |
405 | if(!self->users[i]) | |
406 | continue; | |
407 | irc_quit(self->users[i], buffer); | |
408 | } | |
409 | } | |
410 | ||
411 | putsock("%s " P10_SQUIT " %s %d :%s", self->numeric, srv->name, 0, buffer); | |
412 | ||
413 | if(srv == self) { | |
414 | /* Force a reconnect to the currently selected server. */ | |
415 | cManager.uplink->tries = 0; | |
416 | log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", buffer); | |
417 | close_socket(); | |
418 | } | |
419 | } | |
420 | ||
421 | void | |
422 | irc_server(struct server *srv) | |
423 | { | |
424 | char extranum[COMBO_NUMERIC_LEN+1]; | |
425 | ||
426 | inttobase64(extranum, srv->num_mask, (srv->numeric[1] || (srv->num_mask >= 64*64)) ? 3 : 2); | |
427 | if (srv == self) { | |
428 | putsock(P10_SERVER " %s %d %li %li J10 %s%s +s6 :%s", | |
429 | srv->name, srv->hops+1, srv->boot, srv->link, srv->numeric, extranum, srv->description); | |
430 | } else { | |
431 | putsock("%s " P10_SERVER " %s %d %li %li %c10 %s%s +s6 :%s", | |
432 | self->numeric, srv->name, srv->hops+1, srv->boot, srv->link, (srv->self_burst ? 'J' : 'P'), srv->numeric, extranum, srv->description); | |
433 | } | |
434 | } | |
435 | ||
436 | static void | |
437 | irc_p10_pton(irc_in_addr_t *ip, const char *input) | |
438 | { | |
439 | if (strlen(input) == 6) { | |
440 | unsigned int value; | |
441 | memset(ip, 0, 6 * sizeof(ip->in6[0])); | |
442 | value = base64toint(input, 6); | |
443 | if (value) | |
444 | ip->in6[5] = htons(65535); | |
445 | ip->in6[6] = htons(value >> 16); | |
446 | ip->in6[7] = htons(value & 65535); | |
447 | } else { | |
448 | unsigned int pos = 0; | |
449 | do { | |
450 | if (*input == '_') { | |
451 | unsigned int left; | |
452 | for (left = (25 - strlen(input)) / 3; left; left--) | |
453 | ip->in6[pos++] = 0; | |
454 | input++; | |
455 | } else { | |
456 | ip->in6[pos++] = ntohs(base64toint(input, 3)); | |
457 | input += 3; | |
458 | } | |
459 | } while (pos < 8); | |
460 | } | |
461 | } | |
462 | ||
463 | static void | |
464 | irc_p10_ntop(char *output, const irc_in_addr_t *ip) | |
465 | { | |
466 | if (!irc_in_addr_is_valid(*ip)) { | |
467 | strcpy(output, "AAAAAA"); | |
468 | } else if (irc_in_addr_is_ipv4(*ip)) { | |
469 | unsigned int in4; | |
470 | in4 = (ntohs(ip->in6[6]) << 16) | ntohs(ip->in6[7]); | |
471 | inttobase64(output, in4, 6); | |
472 | output[6] = '\0'; | |
473 | } else if (irc_in_addr_is_ipv6(*ip)) { | |
474 | unsigned int max_start, max_zeros, curr_zeros, zero, ii; | |
475 | /* Can start by printing out the leading non-zero parts. */ | |
476 | for (ii = 0; (ip->in6[ii]) && (ii < 8); ++ii) { | |
477 | inttobase64(output, ntohs(ip->in6[ii]), 3); | |
478 | output += 3; | |
479 | } | |
480 | /* Find the longest run of zeros. */ | |
481 | for (max_start = zero = ii, max_zeros = curr_zeros = 0; ii < 8; ++ii) { | |
482 | if (!ip->in6[ii]) | |
483 | curr_zeros++; | |
484 | else if (curr_zeros > max_zeros) { | |
485 | max_start = ii - curr_zeros; | |
486 | max_zeros = curr_zeros; | |
487 | curr_zeros = 0; | |
488 | } | |
489 | } | |
490 | if (curr_zeros > max_zeros) { | |
491 | max_start = ii - curr_zeros; | |
492 | max_zeros = curr_zeros; | |
493 | curr_zeros = 0; | |
494 | } | |
495 | /* Print the rest of the address */ | |
496 | for (ii = zero; ii < 8; ) { | |
497 | if ((ii == max_start) && max_zeros) { | |
498 | *output++ = '_'; | |
499 | ii += max_zeros; | |
500 | } else { | |
501 | inttobase64(output, ntohs(ip->in6[ii]), 3); | |
502 | output += 3; | |
503 | } | |
504 | } | |
505 | *output = '\0'; | |
506 | } else { | |
507 | strcpy(output, "???"); | |
508 | } | |
509 | } | |
510 | ||
511 | void | |
512 | irc_user(struct userNode *user) | |
513 | { | |
514 | char b64ip[25]; | |
515 | if (!user) | |
516 | return; | |
517 | irc_p10_ntop(b64ip, &user->ip); | |
518 | if (user->modes) { | |
519 | int modelen; | |
520 | char modes[32]; | |
521 | ||
522 | modelen = 0; | |
523 | if (IsOper(user)) | |
524 | modes[modelen++] = 'o'; | |
525 | if (IsInvisible(user)) | |
526 | modes[modelen++] = 'i'; | |
527 | if (IsWallOp(user)) | |
528 | modes[modelen++] = 'w'; | |
529 | if (IsService(user)) | |
530 | modes[modelen++] = 'k'; | |
531 | if (IsServNotice(user)) | |
532 | modes[modelen++] = 's'; | |
533 | if (IsDeaf(user)) | |
534 | modes[modelen++] = 'd'; | |
535 | if (IsGlobal(user)) | |
536 | modes[modelen++] = 'g'; | |
537 | // sethost - reed/apples | |
538 | // if (IsHelperIrcu(user)) | |
539 | if (IsSetHost(user)) | |
540 | modes[modelen++] = 'h'; | |
541 | if (IsFakeHost(user)) | |
542 | modes[modelen++] = 'f'; | |
543 | if (IsHiddenHost(user)) | |
544 | modes[modelen++] = 'x'; | |
545 | modes[modelen] = 0; | |
546 | ||
547 | /* we don't need to put the + in modes because it's in the format string. */ | |
548 | putsock("%s " P10_NICK " %s %d %li %s %s +%s %s %s :%s", | |
549 | user->uplink->numeric, user->nick, user->uplink->hops+1, user->timestamp, user->ident, user->hostname, modes, b64ip, user->numeric, user->info); | |
550 | } else { | |
551 | putsock("%s " P10_NICK " %s %d %li %s %s %s %s :%s", | |
552 | user->uplink->numeric, user->nick, user->uplink->hops+1, user->timestamp, user->ident, user->hostname, b64ip, user->numeric, user->info); | |
553 | } | |
554 | } | |
555 | ||
556 | void | |
557 | irc_rename(struct userNode *user, const char *new_handle) | |
558 | { | |
559 | if(extended_accounts) | |
560 | putsock("%s " P10_ACCOUNT " %s M %s", self->numeric, user->numeric, new_handle); | |
561 | } | |
562 | ||
563 | void | |
564 | irc_delete(struct userNode *user) | |
565 | { | |
566 | if(extended_accounts) | |
567 | putsock("%s " P10_ACCOUNT " %s U", self->numeric, user->numeric); | |
568 | } | |
569 | ||
570 | void | |
571 | irc_account(struct userNode *user, const char *stamp, time_t timestamp) | |
572 | { | |
573 | if(extended_accounts) | |
574 | putsock("%s " P10_ACCOUNT " %s R %s %lu", self->numeric, user->numeric, stamp, timestamp); | |
575 | else | |
576 | putsock("%s " P10_ACCOUNT " %s %s %lu", self->numeric, user->numeric, stamp, timestamp); | |
577 | } | |
578 | ||
579 | void | |
580 | irc_fakehost(struct userNode *user, const char *host) | |
581 | { | |
582 | putsock("%s " P10_FAKEHOST " %s %s", self->numeric, user->numeric, host); | |
583 | } | |
584 | ||
585 | void | |
586 | irc_regnick(UNUSED_ARG(struct userNode *user)) | |
587 | { | |
588 | /* no operation here */ | |
589 | } | |
590 | ||
591 | void | |
592 | irc_nick(struct userNode *user, UNUSED_ARG(const char *old_nick)) | |
593 | { | |
594 | putsock("%s " P10_NICK " %s "FMT_TIME_T, user->numeric, user->nick, now); | |
595 | } | |
596 | ||
597 | void | |
598 | irc_fetchtopic(struct userNode *from, const char *to) | |
599 | { | |
600 | if (!from || !to) | |
601 | return; | |
602 | putsock("%s " P10_TOPIC " %s", from->numeric, to); | |
603 | } | |
604 | ||
605 | void | |
606 | irc_squit(struct server *srv, const char *message, const char *service_message) | |
607 | { | |
608 | if (!service_message) | |
609 | service_message = message; | |
610 | ||
611 | /* Are we leaving the network? */ | |
612 | if (srv == self && cManager.uplink->state == CONNECTED) { | |
613 | unsigned int i; | |
614 | ||
615 | /* Quit all clients linked to me. */ | |
616 | for (i = 0; i <= self->num_mask; i++) { | |
617 | if (!self->users[i]) | |
618 | continue; | |
619 | irc_quit(self->users[i], service_message); | |
620 | } | |
621 | } | |
622 | ||
623 | putsock("%s " P10_SQUIT " %s %d :%s", self->numeric, srv->name, 0, message); | |
624 | ||
625 | if (srv == self) { | |
626 | /* Force a reconnect to the currently selected server. */ | |
627 | cManager.uplink->tries = 0; | |
628 | log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", message); | |
629 | close_socket(); | |
630 | } | |
631 | } | |
632 | ||
633 | void | |
634 | irc_wallchops(struct userNode *from, const char *to, const char *message) | |
635 | { | |
636 | putsock("%s " P10_WALLCHOPS " %s :%s", from->numeric, to, message); | |
637 | } | |
638 | ||
639 | void | |
640 | irc_wallops(const char *format, ...) | |
641 | { | |
642 | va_list arg_list; | |
643 | char buffer[MAXLEN]; | |
644 | va_start(arg_list, format); | |
645 | vsnprintf(buffer, MAXLEN-2, format, arg_list); | |
646 | buffer[MAXLEN-1] = 0; | |
647 | putsock("%s " P10_WALLOPS " :%s", self->numeric, buffer); | |
648 | } | |
649 | ||
650 | void | |
651 | irc_notice(struct userNode *from, const char *to, const char *message) | |
652 | { | |
653 | putsock("%s " P10_NOTICE " %s :%s", from->numeric, to, message); | |
654 | } | |
655 | ||
656 | void | |
657 | irc_notice_user(struct userNode *from, struct userNode *to, const char *message) | |
658 | { | |
659 | putsock("%s " P10_NOTICE " %s :%s", from->numeric, to->numeric, message); | |
660 | } | |
661 | ||
662 | void | |
663 | irc_privmsg(struct userNode *from, const char *to, const char *message) | |
664 | { | |
665 | putsock("%s " P10_PRIVMSG " %s :%s", from->numeric, to, message); | |
666 | } | |
667 | ||
668 | void | |
669 | irc_eob(void) | |
670 | { | |
671 | putsock("%s " P10_EOB, self->numeric); | |
672 | } | |
673 | ||
674 | void | |
675 | irc_eob_ack(void) | |
676 | { | |
677 | putsock("%s " P10_EOB_ACK, self->numeric); | |
678 | } | |
679 | ||
680 | void | |
681 | irc_ping(const char *payload) | |
682 | { | |
683 | putsock("%s " P10_PING " :%s", self->numeric, payload); | |
684 | } | |
685 | ||
686 | void | |
687 | irc_pong(const char *who, const char *data) | |
688 | { | |
689 | putsock("%s " P10_PONG " %s :%s", self->numeric, who, data); | |
690 | } | |
691 | ||
692 | void | |
693 | irc_pong_asll(const char *who, const char *orig_ts) | |
694 | { | |
695 | char *delim; | |
696 | struct timeval orig; | |
697 | struct timeval now; | |
698 | int diff; | |
699 | ||
700 | orig.tv_sec = strtoul(orig_ts, &delim, 10); | |
701 | orig.tv_usec = (*delim == '.') ? strtoul(delim + 1, NULL, 10) : 0; | |
702 | gettimeofday(&now, NULL); | |
703 | diff = (now.tv_sec - orig.tv_sec) * 1000 + (now.tv_usec - orig.tv_usec) / 1000; | |
704 | putsock("%s " P10_PONG " %s %s %d " FMT_TIME_T ".%06u", self->numeric, who, orig_ts, diff, now.tv_sec, (unsigned)now.tv_usec); | |
705 | } | |
706 | ||
707 | void | |
708 | irc_pass(const char *passwd) | |
709 | { | |
710 | putsock(P10_PASS " :%s", passwd); | |
711 | } | |
712 | ||
713 | void | |
714 | irc_introduce(const char *passwd) | |
715 | { | |
716 | void timed_send_ping(void *data); | |
717 | ||
718 | self->self_burst = self->burst = 1; | |
719 | irc_pass(passwd); | |
720 | irc_server(self); | |
721 | burst_length = 0; | |
722 | timeq_add(now + ping_freq, timed_send_ping, 0); | |
723 | } | |
724 | ||
725 | void | |
726 | irc_gline(struct server *srv, struct gline *gline, int silent) | |
727 | { | |
728 | putsock("%s " P10_GLINE " %s +%s %ld :%s<%s> %s", | |
729 | self->numeric, (srv ? srv->numeric : "*"), gline->target, gline->expires-now, silent ? "AUTO " : "", gline->issuer, gline->reason); | |
730 | } | |
731 | ||
732 | void | |
733 | irc_shun(struct server *srv, struct shun *shun) | |
734 | { | |
735 | putsock("%s " P10_SHUN " %s +%s %ld :<%s> %s", | |
736 | self->numeric, (srv ? srv->numeric : "*"), shun->target, shun->expires-now, shun->issuer, shun->reason); | |
737 | } | |
738 | ||
739 | void | |
740 | irc_settime(const char *srv_name_mask, time_t new_time) | |
741 | { | |
742 | ioset_set_time(new_time); | |
743 | if (!strcmp(srv_name_mask, "*")) | |
744 | srv_name_mask = ""; | |
745 | putsock("%s " P10_SETTIME " " FMT_TIME_T " %s", self->numeric, new_time, srv_name_mask); | |
746 | } | |
747 | ||
748 | void | |
749 | irc_ungline(const char *mask) | |
750 | { | |
751 | putsock("%s " P10_GLINE " * -%s", self->numeric, mask); | |
752 | } | |
753 | ||
754 | void | |
755 | irc_unshun(const char *mask) | |
756 | { | |
757 | putsock("%s " P10_SHUN " * -%s", self->numeric, mask); | |
758 | } | |
759 | ||
760 | static void | |
761 | irc_burst(struct chanNode *chan) | |
762 | { | |
763 | char burst_line[512]; | |
764 | int pos, base_len, len; | |
765 | struct modeNode *mn; | |
766 | struct banNode *bn; | |
767 | struct exemptNode *en; | |
768 | long last_mode=-1; | |
769 | unsigned int n; | |
770 | ||
771 | base_len = sprintf(burst_line, "%s " P10_BURST " %s " FMT_TIME_T " ", | |
772 | self->numeric, chan->name, chan->timestamp); | |
773 | len = irc_make_chanmode(chan, burst_line+base_len); | |
774 | pos = base_len + len; | |
775 | if (len) | |
776 | burst_line[pos++] = ' '; | |
777 | ||
778 | if(chan->members.used < 1) | |
779 | return; /* dont burst empty channels (created by discrims) */ | |
780 | /* dump the users */ | |
781 | for (n=0; n<chan->members.used; n++) { | |
782 | mn = chan->members.list[n]; | |
783 | if (pos > 500) { | |
784 | burst_line[pos-1] = 0; /* -1 to back up over the space or comma */ | |
785 | putsock("%s", burst_line); | |
786 | pos = base_len; | |
787 | last_mode = -1; | |
788 | } | |
789 | memcpy(burst_line+pos, mn->user->numeric, strlen(mn->user->numeric)); | |
790 | pos += strlen(mn->user->numeric); | |
791 | if (mn->modes && (mn->modes != last_mode)) { | |
792 | last_mode = mn->modes; | |
793 | burst_line[pos++] = ':'; | |
794 | if (last_mode & MODE_CHANOP) | |
795 | burst_line[pos++] = 'o'; | |
796 | if (last_mode & MODE_HALFOP) | |
797 | burst_line[pos++] = 'h'; | |
798 | if (last_mode & MODE_VOICE) | |
799 | burst_line[pos++] = 'v'; | |
800 | } | |
801 | if ((n+1)<chan->members.used) | |
802 | burst_line[pos++] = ','; | |
803 | } | |
804 | if (chan->banlist.used) { | |
805 | /* dump the bans */ | |
806 | if (pos+2+strlen(chan->banlist.list[0]->ban) > 505) { | |
807 | burst_line[pos-1] = 0; | |
808 | putsock("%s", burst_line); | |
809 | pos = base_len; | |
810 | } else { | |
811 | burst_line[pos++] = ' '; | |
812 | } | |
813 | ||
814 | burst_line[pos++] = ':'; | |
815 | burst_line[pos++] = '%'; | |
816 | base_len = pos; | |
817 | for (n=0; n<chan->banlist.used; n++) { | |
818 | bn = chan->banlist.list[n]; | |
819 | len = strlen(bn->ban); | |
820 | if (pos+len+1 > 510) { | |
821 | burst_line[pos-1] = 0; /* -1 to back up over the space or comma */ | |
822 | putsock("%s", burst_line); | |
823 | pos = base_len; | |
824 | } | |
825 | memcpy(burst_line+pos, bn->ban, len); | |
826 | pos += len; | |
827 | burst_line[pos++] = ' '; | |
828 | } | |
829 | } | |
830 | if (chan->exemptlist.used) { | |
831 | /* dump the exempt */ | |
832 | if (pos+2+strlen(chan->exemptlist.list[0]->exempt) > 505) { | |
833 | burst_line[pos-1] = 0; | |
834 | putsock("%s", burst_line); | |
835 | pos = base_len; | |
836 | } else { | |
837 | burst_line[pos++] = ' '; | |
838 | } | |
839 | ||
840 | burst_line[pos++] = ' '; | |
841 | burst_line[pos++] = '~'; | |
842 | burst_line[pos++] = ' '; | |
843 | base_len = pos; | |
844 | for (n=0; n<chan->exemptlist.used; n++) { | |
845 | en = chan->exemptlist.list[n]; | |
846 | len = strlen(en->exempt); | |
847 | if (pos+len+1 > 510) { | |
848 | burst_line[pos-1] = 0; /* -1 to back up over the space or comma */ | |
849 | putsock("%s", burst_line); | |
850 | pos = base_len; | |
851 | } | |
852 | memcpy(burst_line+pos, en->exempt, len); | |
853 | pos += len; | |
854 | burst_line[pos++] = ' '; | |
855 | } | |
856 | } | |
857 | /* print the last line */ | |
858 | burst_line[pos] = 0; | |
859 | putsock("%s", burst_line); | |
860 | } | |
861 | ||
862 | void | |
863 | irc_quit(struct userNode *user, const char *message) | |
864 | { | |
865 | putsock("%s " P10_QUIT " :%s", user->numeric, message); | |
866 | } | |
867 | ||
868 | void | |
869 | irc_error(const char *to, const char *message) | |
870 | { | |
871 | if (to) { | |
872 | putsock("%s " P10_ERROR " :%s", to, message); | |
873 | } else { | |
874 | putsock(":%s " P10_ERROR " :%s", self->name, message); | |
875 | } | |
876 | } | |
877 | ||
878 | void | |
879 | irc_kill(struct userNode *from, struct userNode *target, const char *message) | |
880 | { | |
881 | if (from) { | |
882 | putsock("%s " P10_KILL " %s :%s!%s (%s)", | |
883 | from->numeric, target->numeric, self->name, from->nick, message); | |
884 | } else { | |
885 | putsock("%s " P10_KILL " %s :%s (%s)", | |
886 | self->numeric, target->numeric, self->name, message); | |
887 | } | |
888 | } | |
889 | ||
890 | void | |
891 | irc_mode(struct userNode *from, struct chanNode *target, const char *modes) | |
892 | { | |
893 | call_channel_mode_funcs(from, target, (char **)&modes, 0); | |
894 | putsock("%s " P10_MODE " %s %s "FMT_TIME_T, | |
895 | (from ? from->numeric : self->numeric), | |
896 | target->name, modes, target->timestamp); | |
897 | } | |
898 | ||
899 | /* Added to allow services to mode users | |
900 | 2005 - 8 - 10 by Life4Christ | |
901 | */ | |
902 | void | |
903 | irc_umode(struct userNode *target, const char *modes) | |
904 | { | |
905 | putsock("%s " P10_MODE " %s %s ",self->numeric,target->nick, modes); | |
906 | } | |
907 | ||
908 | ||
909 | void | |
910 | irc_invite(struct userNode *from, struct userNode *who, struct chanNode *to) | |
911 | { | |
912 | putsock("%s " P10_INVITE " %s %s", from->numeric, who->nick, to->name); | |
913 | } | |
914 | ||
915 | void | |
916 | irc_silence(struct userNode *who, const char *mask, int add) | |
917 | { | |
918 | putsock("%s " P10_SILENCE " %s %s%s", self->numeric, who->numeric, add ? "+" : "-", mask); | |
919 | } | |
920 | ||
921 | void | |
922 | irc_join(struct userNode *who, struct chanNode *what) | |
923 | { | |
924 | if (what->members.used == 1) { | |
925 | putsock("%s " P10_CREATE " %s %lu", | |
926 | who->numeric, what->name, what->timestamp); | |
927 | } else { | |
928 | putsock("%s " P10_JOIN " %s %lu", who->numeric, what->name, what->timestamp); | |
929 | } | |
930 | } | |
931 | ||
932 | void | |
933 | irc_kick(struct userNode *who, struct userNode *target, struct chanNode *channel, const char *msg) | |
934 | { | |
935 | const char *numeric; | |
936 | struct modeNode *mn = GetUserMode(channel, who); | |
937 | numeric = ((mn && (mn->modes & MODE_CHANOP)) || off_channel) ? who->numeric : self->numeric; | |
938 | putsock("%s " P10_KICK " %s %s :%s", | |
939 | numeric, channel->name, target->numeric, msg); | |
940 | } | |
941 | ||
942 | void | |
943 | irc_stats(struct userNode *from, struct server *target, char type) | |
944 | { | |
945 | putsock("%s " P10_STATS " %c :%s", from->numeric, type, target->numeric); | |
946 | } | |
947 | ||
948 | void | |
949 | irc_svsnick(struct userNode *from, struct userNode *target, const char *newnick) | |
950 | { | |
951 | putsock("%s " P10_SVSNICK " %s %s "FMT_TIME_T, from->uplink->numeric, target->numeric, newnick, now); | |
952 | } | |
953 | ||
954 | void | |
955 | irc_swhois(struct userNode *from, struct userNode *target, const char *message) | |
956 | { | |
957 | putsock("%s " P10_SWHOIS " %s %s%s", from->uplink->numeric, target->numeric, message ? ":" : "", | |
958 | message ? message : ""); | |
959 | ||
960 | } | |
961 | ||
962 | void | |
963 | irc_part(struct userNode *who, struct chanNode *what, const char *reason) | |
964 | { | |
965 | if (reason) { | |
966 | putsock("%s " P10_PART " %s :%s", who->numeric, what->name, reason); | |
967 | } else { | |
968 | putsock("%s " P10_PART " %s", who->numeric, what->name); | |
969 | } | |
970 | } | |
971 | ||
972 | void | |
973 | irc_topic(struct userNode *service, struct userNode *who, struct chanNode *what, const char *topic) | |
974 | { | |
975 | ||
976 | int type = 4; | |
977 | int host_in_topic = 0; | |
978 | const char *hstr, *tstr; | |
979 | char *host, *hostmask; | |
980 | char shost[MAXLEN]; | |
981 | char sident[MAXLEN]; | |
982 | ||
983 | tstr = conf_get_data("server/type", RECDB_QSTRING); | |
984 | hstr = conf_get_data("server/host_in_topic", RECDB_QSTRING); | |
985 | if(tstr) | |
986 | type = atoi(tstr); | |
987 | else | |
988 | type = 4;/* default to 040 style topics */ | |
989 | ||
990 | if (hstr) { | |
991 | if (IsFakeHost(who)) | |
992 | safestrncpy(shost, who->fakehost, sizeof(shost)); | |
993 | else if (IsSetHost(who)) { | |
994 | hostmask = strdup(who->sethost); | |
995 | if ((host = (strrchr(hostmask, '@')))) | |
996 | *host++ = '\0'; | |
997 | else | |
998 | host = hostmask; | |
999 | ||
1000 | safestrncpy(sident, hostmask, sizeof(shost)); | |
1001 | safestrncpy(shost, host, sizeof(shost)); | |
1002 | } else | |
1003 | safestrncpy(shost, who->hostname, sizeof(shost)); | |
1004 | ||
1005 | host_in_topic = atoi(hstr); | |
1006 | } | |
1007 | ||
1008 | if (type == 5) { | |
1009 | putsock("%s " P10_TOPIC " %s %s%s%s%s%s " FMT_TIME_T " " FMT_TIME_T " :%s", service->numeric, what->name, | |
1010 | who->nick, host_in_topic ? "!" : "", host_in_topic ? (IsSetHost(who) ? sident : who->ident) : "", | |
1011 | host_in_topic ? "@" : "", host_in_topic ? shost : "", what->timestamp, now, topic); | |
1012 | } else { | |
1013 | who = service; | |
1014 | putsock("%s " P10_TOPIC " %s :%s", who->numeric, what->name, topic); | |
1015 | } | |
1016 | } | |
1017 | ||
1018 | void | |
1019 | irc_raw(const char *what) | |
1020 | { | |
1021 | putsock("%s", what); | |
1022 | } | |
1023 | ||
1024 | void | |
1025 | irc_numeric(struct userNode *user, unsigned int num, const char *format, ...) | |
1026 | { | |
1027 | va_list arg_list; | |
1028 | char buffer[MAXLEN]; | |
1029 | va_start(arg_list, format); | |
1030 | vsnprintf(buffer, MAXLEN-2, format, arg_list); | |
1031 | buffer[MAXLEN-1] = 0; | |
1032 | putsock(":%s %03d %s %s", self->name, num, user->nick, buffer); | |
1033 | } | |
1034 | ||
1035 | static void send_burst(void); | |
1036 | ||
1037 | static void | |
1038 | change_nicklen(int new_nicklen) | |
1039 | { | |
1040 | unsigned int nn; | |
1041 | char new_nick[NICKLEN+1]; | |
1042 | struct userNode *user; | |
1043 | ||
1044 | nicklen = new_nicklen; | |
1045 | /* fix up any users we have here */ | |
1046 | for (nn=0; nn<=self->num_mask; nn++) { | |
1047 | if (!(user = self->users[nn])) | |
1048 | continue; | |
1049 | safestrncpy(new_nick, user->nick, sizeof(new_nick)); | |
1050 | new_nick[nicklen] = 0; | |
1051 | NickChange(user, new_nick, 1); | |
1052 | } | |
1053 | } | |
1054 | ||
1055 | static CMD_FUNC(cmd_whois) | |
1056 | { | |
1057 | struct userNode *from; | |
1058 | struct userNode *who; | |
1059 | ||
1060 | if (argc < 3) | |
1061 | return 0; | |
1062 | if (!(from = GetUserH(origin))) { | |
1063 | log_module(MAIN_LOG, LOG_ERROR, "Could not find WHOIS origin user %s", origin); | |
1064 | return 0; | |
1065 | } | |
1066 | if(!(who = GetUserH(argv[2]))) { | |
1067 | irc_numeric(from, ERR_NOSUCHNICK, "%s@%s :No such nick", argv[2], self->name); | |
1068 | return 1; | |
1069 | } | |
1070 | if (IsHiddenHost(who) && !IsOper(from)) { | |
1071 | /* Just stay quiet. */ | |
1072 | return 1; | |
1073 | } | |
1074 | irc_numeric(from, RPL_WHOISUSER, "%s %s %s * :%s", who->nick, who->ident, who->hostname, who->info); | |
1075 | if (his_servername && his_servercomment) | |
1076 | irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, his_servername, his_servercomment); | |
1077 | else | |
1078 | irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, who->uplink->name, who->uplink->description); | |
1079 | if (IsOper(who)) | |
1080 | irc_numeric(from, RPL_WHOISOPERATOR, "%s :is a megalomaniacal power hungry tyrant", who->nick); | |
1081 | irc_numeric(from, RPL_ENDOFWHOIS, "%s :End of /WHOIS list", who->nick); | |
1082 | return 1; | |
1083 | } | |
1084 | ||
1085 | static CMD_FUNC(cmd_server) | |
1086 | { | |
1087 | struct server *srv; | |
1088 | const char *str; | |
1089 | ||
1090 | if (argc < 8) | |
1091 | return 0; | |
1092 | if (self->uplink) { | |
1093 | /* another server introduced us */ | |
1094 | srv = AddServer(GetServerH(origin), argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]); | |
1095 | if (!srv) | |
1096 | return 0; | |
1097 | srv->self_burst = argv[5][0] == 'J'; | |
1098 | srv->burst = 1; | |
1099 | } else { | |
1100 | /* this must be our uplink */ | |
1101 | srv = self->uplink = AddServer(self, argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]); | |
1102 | if (!srv) | |
1103 | return 0; | |
1104 | srv->self_burst = argv[5][0] == 'J'; | |
1105 | srv->burst = 1; | |
1106 | if ((argv[7][0] == '+') && !force_n2k) { | |
1107 | log_module(MAIN_LOG, LOG_WARNING, "Got Undernet-style SERVER message but \"force_n2k\" not on."); | |
1108 | } | |
1109 | send_burst(); | |
1110 | } | |
1111 | ||
1112 | /* Fix up our timestamps if necessary. */ | |
1113 | if (srv->boot <= PREHISTORY) { | |
1114 | /* Server from the mists of time.. */ | |
1115 | if (srv->hops == 1) { | |
1116 | log_module(MAIN_LOG, LOG_ERROR, "Server %s claims to have booted at time "FMT_TIME_T". This is absurd.", srv->name, srv->boot); | |
1117 | } | |
1118 | } else if ((str = conf_get_data("server/reliable_clock", RECDB_QSTRING)) | |
1119 | && enabled_string(str)) { | |
1120 | /* If we have a reliable clock, we just keep our current time. */ | |
1121 | } else { | |
1122 | if (srv->boot <= self->boot) { | |
1123 | /* The other server is older than us. Accept their timestamp. | |
1124 | * Alternately, we are same age, but we accept their time | |
1125 | * since we are linking to them. */ | |
1126 | self->boot = srv->boot; | |
1127 | ioset_set_time(srv->link); | |
1128 | } | |
1129 | } | |
1130 | if (srv == self->uplink) { | |
1131 | extern time_t burst_begin; | |
1132 | burst_begin = now; | |
1133 | } | |
1134 | return 1; | |
1135 | } | |
1136 | ||
1137 | ||
1138 | static CMD_FUNC(cmd_eob) | |
1139 | { | |
1140 | struct server *sender; | |
1141 | dict_iterator_t it; | |
1142 | unsigned int ii; | |
1143 | ||
1144 | if (!(sender = GetServerH(origin))) | |
1145 | return 0; | |
1146 | if (sender == self->uplink) { | |
1147 | cManager.uplink->state = CONNECTED; | |
1148 | for (it = dict_first(unbursted_channels); it; it = iter_next(it)) | |
1149 | irc_burst(iter_data(it)); | |
1150 | dict_delete(unbursted_channels); | |
1151 | unbursted_channels = NULL; | |
1152 | irc_eob(); | |
1153 | irc_eob_ack(); | |
1154 | ||
1155 | /* now that we know who our uplink is, | |
1156 | * we can center the routing map and activate auto-routing. | |
1157 | */ | |
1158 | activate_routing(NULL, NULL, NULL); | |
1159 | } | |
1160 | sender->self_burst = 0; | |
1161 | recalc_bursts(sender); | |
1162 | for (ii=0; ii<slf_used; ii++) | |
1163 | slf_list[ii](sender); | |
1164 | /* let auto-routing figure out if we were | |
1165 | * wating on this server to link a child to it */ | |
1166 | /* DONT call this if uplink is _US_ */ | |
1167 | if(sender->uplink != self) | |
1168 | routing_handle_connect(sender->name, sender->uplink->name); | |
1169 | return 1; | |
1170 | } | |
1171 | ||
1172 | static CMD_FUNC(cmd_eob_ack) | |
1173 | { | |
1174 | extern time_t burst_begin; | |
1175 | ||
1176 | if (GetServerH(origin) == self->uplink) { | |
1177 | burst_length = now - burst_begin; | |
1178 | self->self_burst = self->burst = 0; | |
1179 | } | |
1180 | cManager.uplink->state = CONNECTED; | |
1181 | return 1; | |
1182 | } | |
1183 | ||
1184 | static CMD_FUNC(cmd_ping) | |
1185 | { | |
1186 | struct server *srv; | |
1187 | struct userNode *un; | |
1188 | ||
1189 | if (argc > 3) | |
1190 | irc_pong_asll(argv[2], argv[3]); | |
1191 | else if ((srv = GetServerH(origin))) | |
1192 | irc_pong(self->name, srv->numeric); | |
1193 | else if ((un = GetUserH(origin))) | |
1194 | irc_pong(self->name, un->numeric); | |
1195 | else | |
1196 | irc_pong(self->name, origin); | |
1197 | ||
1198 | timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA); | |
1199 | timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA); | |
1200 | timeq_add(now + ping_freq, timed_send_ping, 0); | |
1201 | received_ping(); | |
1202 | return 1; | |
1203 | } | |
1204 | ||
1205 | static CMD_FUNC(cmd_error_nick) | |
1206 | { | |
1207 | /* Go back to original IRC length .. and try to reconnect :/ */ | |
1208 | change_nicklen(9); | |
1209 | irc_squit(self, "Got erroneous nickname, truncating nicks.", NULL); | |
1210 | return 1; | |
1211 | } | |
1212 | ||
1213 | struct create_desc { | |
1214 | struct userNode *user; | |
1215 | time_t when; | |
1216 | }; | |
1217 | ||
1218 | static void | |
1219 | join_helper(struct chanNode *chan, void *data) | |
1220 | { | |
1221 | struct create_desc *cd = data; | |
1222 | AddChannelUser(cd->user, chan); | |
1223 | } | |
1224 | ||
1225 | static void | |
1226 | create_helper(char *name, void *data) | |
1227 | { | |
1228 | struct create_desc *cd = data; | |
1229 | ||
1230 | if (!strcmp(name, "0")) { | |
1231 | while (cd->user->channels.used > 0) | |
1232 | DelChannelUser(cd->user, cd->user->channels.list[0]->channel, 0, 0); | |
1233 | return; | |
1234 | } | |
1235 | ||
1236 | AddChannelUser(cd->user, AddChannel(name, cd->when, NULL, NULL, NULL)); | |
1237 | } | |
1238 | ||
1239 | static CMD_FUNC(cmd_create) | |
1240 | { | |
1241 | struct create_desc cd; | |
1242 | struct userNode *user; | |
1243 | ||
1244 | if ((argc < 3) || !(user = GetUserH(origin))) | |
1245 | return 0; | |
1246 | cd.user = user; | |
1247 | cd.when = atoi(argv[2]); | |
1248 | parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd); | |
1249 | return 1; | |
1250 | } | |
1251 | ||
1252 | static CMD_FUNC(cmd_join) | |
1253 | { | |
1254 | struct create_desc cd; | |
1255 | ||
1256 | if (!(cd.user = GetUserH(origin))) | |
1257 | return 0; | |
1258 | if (argc < 2) | |
1259 | return 0; | |
1260 | else if (argc < 3) | |
1261 | cd.when = now; | |
1262 | else | |
1263 | cd.when = atoi(argv[2]); | |
1264 | parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd); | |
1265 | return 1; | |
1266 | } | |
1267 | ||
1268 | static CMD_FUNC(cmd_pong) | |
1269 | { | |
1270 | if (argc < 3) | |
1271 | return 0; | |
1272 | if (!strcmp(argv[2], self->name)) { | |
1273 | timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA); | |
1274 | timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA); | |
1275 | timeq_add(now + ping_freq, timed_send_ping, 0); | |
1276 | received_ping(); | |
1277 | } | |
1278 | return 1; | |
1279 | } | |
1280 | ||
1281 | static CMD_FUNC(cmd_nick) | |
1282 | { | |
1283 | struct userNode *user; | |
1284 | if ((user = GetUserH(origin))) { | |
1285 | /* nick change (since the source is a user numeric) */ | |
1286 | if (argc < 2) | |
1287 | return 0; | |
1288 | NickChange(user, argv[1], 1); | |
1289 | } else { | |
1290 | struct server *serv; | |
1291 | char modes[MAXLEN]; | |
1292 | /* new nick */ | |
1293 | if (argc < 9) | |
1294 | return 0; | |
1295 | serv = GetServerH(origin); | |
1296 | if (argc > 9) | |
1297 | unsplit_string(argv+6, argc-9, modes); | |
1298 | else | |
1299 | strcpy(modes, "+"); | |
1300 | AddUser(serv, argv[1], argv[4], argv[5], modes, argv[argc-2], argv[argc-1], atoi(argv[3]), argv[argc-3]); | |
1301 | } | |
1302 | return 1; | |
1303 | } | |
1304 | ||
1305 | static CMD_FUNC(cmd_account) | |
1306 | { | |
1307 | struct userNode *user; | |
1308 | struct server *server; | |
1309 | struct handle_info *hi; | |
1310 | ||
1311 | if ((argc < 3) || !origin || !(server = GetServerH(origin))) | |
1312 | return 0; /* Origin must be server. */ | |
1313 | ||
1314 | /* This next line appears to tremple origin.. why? */ | |
1315 | user = GetUserN(argv[1]); | |
1316 | if (!user) | |
1317 | return 1; /* A QUIT probably passed the ACCOUNT. */ | |
1318 | ||
1319 | if(!extended_accounts) /* any need for this function without? */ | |
1320 | return 1; | |
1321 | ||
1322 | if(!strcmp(argv[2],"C")) | |
1323 | { | |
1324 | if((hi = loc_auth(argv[4], argv[5]))) | |
1325 | { | |
1326 | /* Return a AC A */ | |
1327 | putsock("%s " P10_ACCOUNT " %s A %s %lu", self->numeric, server->numeric , argv[3], hi->registered); | |
1328 | ||
1329 | } | |
1330 | else | |
1331 | { | |
1332 | /* Return a AC D */ | |
1333 | putsock("%s " P10_ACCOUNT " %s D %s", self->numeric, server->numeric , argv[3]); | |
1334 | } | |
1335 | return 1; | |
1336 | } | |
1337 | else if(!strcmp(argv[2],"R")) | |
1338 | call_account_func(user, argv[3]); | |
1339 | else | |
1340 | call_account_func(user, argv[2]); /* For backward compatability */ | |
1341 | return 1; | |
1342 | } | |
1343 | ||
1344 | static CMD_FUNC(cmd_fakehost) | |
1345 | { | |
1346 | struct userNode *user; | |
1347 | ||
1348 | if ((argc < 3) || !origin || !GetServerH(origin)) | |
1349 | return 0; | |
1350 | if (!(user = GetUserN(argv[1]))) | |
1351 | return 1; | |
1352 | assign_fakehost(user, argv[2], 0); | |
1353 | return 1; | |
1354 | } | |
1355 | ||
1356 | static CMD_FUNC(cmd_burst) | |
1357 | { | |
1358 | extern int rel_age; | |
1359 | char modes[MAXLEN], *members = ""; | |
1360 | static char exemptlist[MAXLEN], banlist[MAXLEN]; | |
1361 | unsigned int next = 3, res = 1; | |
1362 | int ctype = 0, echeck = 0, bcheck = 0; | |
1363 | struct chanNode *cNode; | |
1364 | struct userNode *un; | |
1365 | struct modeNode *mNode; | |
1366 | long mode; | |
1367 | int oplevel = -1; | |
1368 | char *user, *end, sep; | |
1369 | time_t in_timestamp; | |
1370 | char* parm = NULL; | |
1371 | ||
1372 | if (argc < 3) | |
1373 | return 0; | |
1374 | modes[0] = 0; | |
1375 | ||
1376 | exemptlist[0] = 0; | |
1377 | banlist[0] = 0; | |
1378 | ||
1379 | while (next < argc) { | |
1380 | switch (argv[next][0]) { | |
1381 | case '+': { | |
1382 | const char *pos; | |
1383 | int n_modes; | |
1384 | for (pos=argv[next], n_modes = 1; *pos; pos++) | |
1385 | if ((*pos == 'k') || (*pos == 'l') || (*pos == 'A') | |
1386 | || (*pos == 'U')) | |
1387 | n_modes++; | |
1388 | unsplit_string(argv+next, n_modes, modes); | |
1389 | next += n_modes; | |
1390 | break; | |
1391 | } | |
1392 | case '%': { | |
1393 | for(parm = mysep(&argv[next], " "); /* parm = first param */ | |
1394 | parm; /* While param is not null */ | |
1395 | parm = mysep(&argv[next], " ") /* parm = next param */ | |
1396 | ) | |
1397 | { | |
1398 | switch (parm[0]) { | |
1399 | case '%': { | |
1400 | ctype = 1; | |
1401 | break; | |
1402 | } | |
1403 | case '~': { | |
1404 | ctype = 2; | |
1405 | break; | |
1406 | } | |
1407 | default: { | |
1408 | break; | |
1409 | } | |
1410 | } | |
1411 | if (ctype == 1) { | |
1412 | if (bcheck == 0) { | |
1413 | /* strip % char off start of very first ban */ | |
1414 | if (strlen(parm) > 1) { | |
1415 | strncat(banlist, strtok(parm, "%"), sizeof(banlist) - 1 - strlen(banlist)); | |
1416 | strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist)); | |
1417 | } | |
1418 | bcheck = 1; | |
1419 | } else { | |
1420 | strncat(banlist, parm, sizeof(banlist) - 1 - strlen(banlist)); | |
1421 | strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist)); | |
1422 | } | |
1423 | } else if (ctype == 2) { | |
1424 | if (echeck == 0) { | |
1425 | echeck = 1; | |
1426 | } else { | |
1427 | strncat(exemptlist, parm, sizeof(exemptlist) - 1 - strlen(exemptlist)); | |
1428 | strncat(exemptlist, " ", sizeof(exemptlist) - 1 - strlen(exemptlist)); | |
1429 | } | |
1430 | } | |
1431 | } | |
1432 | next++; | |
1433 | break; | |
1434 | } | |
1435 | default: members = argv[next++]; break; | |
1436 | } | |
1437 | } | |
1438 | ||
1439 | in_timestamp = atoi(argv[2]); | |
1440 | if ((cNode = dict_find(unbursted_channels, argv[1], NULL))) { | |
1441 | cNode->timestamp = in_timestamp; | |
1442 | dict_remove(unbursted_channels, cNode->name); | |
1443 | irc_burst(cNode); | |
1444 | } | |
1445 | cNode = AddChannel(argv[1], in_timestamp, modes, banlist, exemptlist); | |
1446 | ||
1447 | /* Burst channel members in now. */ | |
1448 | for (user = members, sep = *members, mode = 0; sep; user = end) { | |
1449 | for (end = user + 3; isalnum(*end) || *end == '[' || *end == ']'; end++) ; | |
1450 | sep = *end++; end[-1] = 0; | |
1451 | if (sep == ':') { | |
1452 | mode = 0; | |
1453 | while ((sep = *end++)) { | |
1454 | if (sep == 'o') { | |
1455 | mode |= MODE_CHANOP; | |
1456 | oplevel = -1; | |
1457 | } else if (sep == 'h') { | |
1458 | mode |= MODE_HALFOP; | |
1459 | oplevel = -1; | |
1460 | } else if (sep == 'v') { | |
1461 | mode |= MODE_VOICE; | |
1462 | oplevel = -1; | |
1463 | } else if (isdigit(sep)) { | |
1464 | mode |= MODE_CHANOP; | |
1465 | if (oplevel >= 0) | |
1466 | oplevel += parse_oplevel(end); | |
1467 | else | |
1468 | oplevel = parse_oplevel(end); | |
1469 | while (isdigit(*end)) end++; | |
1470 | } else | |
1471 | break; | |
1472 | } | |
1473 | if (rel_age < 0) | |
1474 | mode = 0; | |
1475 | } | |
1476 | if (!(un = GetUserN(user))) { | |
1477 | res = 0; | |
1478 | continue; | |
1479 | } | |
1480 | if ((mNode = AddChannelUser(un, cNode))) { | |
1481 | mNode->modes = mode; | |
1482 | mNode->oplevel = oplevel; | |
1483 | } | |
1484 | } | |
1485 | ||
1486 | return res; | |
1487 | } | |
1488 | ||
1489 | static CMD_FUNC(cmd_mode) | |
1490 | { | |
1491 | struct chanNode *cn; | |
1492 | struct userNode *un; | |
1493 | char *sethost; // sethost - reed/apples | |
1494 | int i; // sethost - reed/apples | |
1495 | ||
1496 | if (argc < 3) | |
1497 | return 0; | |
1498 | if (!IsChannelName(argv[1])) { | |
1499 | un = GetUserH(argv[1]); | |
1500 | if (!un) { | |
1501 | log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose mode is changing.", argv[1]); | |
1502 | return 0; | |
1503 | } | |
1504 | // sethost - reed/apples | |
1505 | if (argc == 3) | |
1506 | mod_usermode(un, argv[2]); | |
1507 | else { | |
1508 | sethost = malloc(strlen(argv[2]) + 1 + strlen(argv[3]) + 1); | |
1509 | i = 0; | |
1510 | while((sethost[i++] = *argv[2]++)); | |
1511 | i--; | |
1512 | sethost[i++] = ' '; | |
1513 | while((sethost[i++] = *argv[3]++)); | |
1514 | mod_usermode(un, sethost); // sethost - reed/apples | |
1515 | } | |
1516 | ||
1517 | return 1; | |
1518 | } | |
1519 | ||
1520 | if (!(cn = GetChannel(argv[1]))) { | |
1521 | log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]); | |
1522 | return 0; | |
1523 | } | |
1524 | if ((un = GetUserH(origin))) { | |
1525 | struct modeNode *mn; | |
1526 | /* Update idle time for person setting the mode */ | |
1527 | if ((mn = GetUserMode(cn, un))) | |
1528 | mn->idle_since = now; | |
1529 | } else { | |
1530 | /* If it came from a server, reset timestamp to re-sync. */ | |
1531 | cn->timestamp = atoi(argv[argc-1]); | |
1532 | } | |
1533 | ||
1534 | return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER|(un ? MC_NOTIFY : 0)); | |
1535 | } | |
1536 | ||
1537 | static CMD_FUNC(cmd_opmode) | |
1538 | { | |
1539 | struct chanNode *cn; | |
1540 | struct userNode *un; | |
1541 | ||
1542 | if (argc < 3) | |
1543 | return 0; | |
1544 | ||
1545 | if (!(cn = GetChannel(argv[1]))) { | |
1546 | log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]); | |
1547 | return 0; | |
1548 | } | |
1549 | if (!(un = GetUserH(origin))) { | |
1550 | log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting OPMODE.", origin); | |
1551 | return 0; | |
1552 | } | |
1553 | if (!IsOper(un)) { | |
1554 | log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using OPMODE.", un->nick); | |
1555 | return 0; | |
1556 | } | |
1557 | ||
1558 | return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER); /* do NOT announce opmode locally */ | |
1559 | } | |
1560 | ||
1561 | static int clear_chanmode(struct chanNode *channel, const char *modes); | |
1562 | ||
1563 | static CMD_FUNC(cmd_clearmode) | |
1564 | { | |
1565 | struct chanNode *cn; | |
1566 | struct userNode *un; | |
1567 | ||
1568 | if (argc < 3) | |
1569 | return 0; | |
1570 | ||
1571 | if (!(cn = GetChannel(argv[1]))) { | |
1572 | log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]); | |
1573 | return 0; | |
1574 | } | |
1575 | if (!(un = GetUserH(origin))) { | |
1576 | log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting CLEARMODE.", origin); | |
1577 | return 0; | |
1578 | } | |
1579 | if (!IsOper(un)) { | |
1580 | log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using CLEARMODE.", un->nick); | |
1581 | return 0; | |
1582 | } | |
1583 | ||
1584 | return clear_chanmode(cn, argv[2]); | |
1585 | } | |
1586 | ||
1587 | static CMD_FUNC(cmd_topic) | |
1588 | { | |
1589 | struct chanNode *cn; | |
1590 | time_t chan_ts, topic_ts; | |
1591 | struct userNode *user; | |
1592 | ||
1593 | if (argc < 3) | |
1594 | return 0; | |
1595 | if (!(cn = GetChannel(argv[1]))) { | |
1596 | log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose topic is being set", argv[1]); | |
1597 | return 0; | |
1598 | } | |
1599 | ||
1600 | ||
1601 | if (argc == 5) { /* Asuka / Topic Bursting IRCu's */ | |
1602 | user = GetUserH(origin); | |
1603 | chan_ts = atoi(argv[2]); | |
1604 | topic_ts = atoi(argv[3]); | |
1605 | } else if (argc >= 6) { /* Nefarious 0.5.0 */ | |
1606 | user = GetUserH(strtok(argv[2], "!")); | |
1607 | chan_ts = atoi(argv[3]); | |
1608 | topic_ts = atoi(argv[4]); | |
1609 | } else { /* Regular IRCu (No Topic Bursting)*/ | |
1610 | user = GetUserH(origin); | |
1611 | chan_ts = cn->timestamp; | |
1612 | topic_ts = now; | |
1613 | } | |
1614 | ||
1615 | SetChannelTopic(cn, user, user, argv[argc-1], 0); | |
1616 | cn->topic_time = topic_ts; | |
1617 | return 1; | |
1618 | } | |
1619 | ||
1620 | static CMD_FUNC(cmd_num_topic) | |
1621 | { | |
1622 | struct chanNode *cn; | |
1623 | ||
1624 | if (!argv[0]) | |
1625 | return 0; /* huh? */ | |
1626 | if (argv[2]) { | |
1627 | cn = GetChannel(argv[2]); | |
1628 | if (!cn) { | |
1629 | log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s in topic reply", argv[2]); | |
1630 | return 0; | |
1631 | } | |
1632 | } else | |
1633 | return 0; | |
1634 | ||
1635 | switch (atoi(argv[0])) { | |
1636 | case 331: | |
1637 | cn->topic_time = 0; | |
1638 | break; /* no topic */ | |
1639 | case 332: | |
1640 | if (argc < 4) | |
1641 | return 0; | |
1642 | safestrncpy(cn->topic, unsplit_string(argv+3, argc-3, NULL), sizeof(cn->topic)); | |
1643 | break; | |
1644 | case 333: | |
1645 | if (argc < 5) | |
1646 | return 0; | |
1647 | safestrncpy(cn->topic_nick, argv[3], sizeof(cn->topic_nick)); | |
1648 | cn->topic_time = atoi(argv[4]); | |
1649 | break; | |
1650 | default: | |
1651 | return 0; /* should never happen */ | |
1652 | } | |
1653 | return 1; | |
1654 | } | |
1655 | ||
1656 | static CMD_FUNC(cmd_num_gline) | |
1657 | { | |
1658 | if (argc < 6) | |
1659 | return 0; | |
1660 | gline_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0, 0); | |
1661 | return 1; | |
1662 | } | |
1663 | ||
1664 | static CMD_FUNC(cmd_num_shun) | |
1665 | { | |
1666 | if (argc < 6) | |
1667 | return 0; | |
1668 | shun_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0); | |
1669 | return 1; | |
1670 | } | |
1671 | ||
1672 | static CMD_FUNC(cmd_quit) | |
1673 | { | |
1674 | struct userNode *user; | |
1675 | if (argc < 2) | |
1676 | return 0; | |
1677 | /* Sometimes we get a KILL then a QUIT or the like, so we don't want to | |
1678 | * call DelUser unless we have the user in our grasp. */ | |
1679 | if ((user = GetUserH(origin))) | |
1680 | DelUser(user, NULL, false, argv[1]); | |
1681 | return 1; | |
1682 | } | |
1683 | ||
1684 | static CMD_FUNC(cmd_kill) | |
1685 | { | |
1686 | struct userNode *user; | |
1687 | if (argc < 2) | |
1688 | return 0; | |
1689 | user = GetUserN(argv[1]); | |
1690 | if (!user) { | |
1691 | /* If we get a KILL for a non-existent user, it could be a | |
1692 | * Ghost response to a KILL we sent out earlier. So we only | |
1693 | * whine if the target is local. | |
1694 | */ | |
1695 | if (!strncmp(argv[1], self->numeric, strlen(self->numeric))) | |
1696 | log_module(MAIN_LOG, LOG_ERROR, "Unable to find kill victim %s", argv[1]); | |
1697 | return 0; | |
1698 | } | |
1699 | ||
1700 | if (IsLocal(user) && IsService(user)) { | |
1701 | /* TODO: rate limit this so silly things don't happen. */ | |
1702 | ReintroduceUser(user); | |
1703 | return 1; | |
1704 | } | |
1705 | ||
1706 | DelUser(user, NULL, false, argv[2]); | |
1707 | return 1; | |
1708 | } | |
1709 | ||
1710 | static CMD_FUNC(cmd_part) | |
1711 | { | |
1712 | struct userNode *user; | |
1713 | ||
1714 | if (argc < 2) | |
1715 | return 0; | |
1716 | user = GetUserH(origin); | |
1717 | if (!user) | |
1718 | return 0; | |
1719 | parse_foreach(argv[1], part_helper, NULL, NULL, NULL, user); | |
1720 | return 1; | |
1721 | } | |
1722 | ||
1723 | static CMD_FUNC(cmd_silence) | |
1724 | { | |
1725 | struct userNode *user; | |
1726 | char *mask; | |
1727 | char *new_mask; | |
1728 | unsigned int i; | |
1729 | ||
1730 | if (argc < 2) | |
1731 | return 0; | |
1732 | ||
1733 | user = GetUserN(argv[1]); | |
1734 | ||
1735 | /* Sanity, go nuts if this happens */ | |
1736 | if (!user) | |
1737 | return 0; | |
1738 | ||
1739 | /* We can safely ignore this if a user adding a silence is not | |
1740 | * ignored. However this brings up a TODO. If a user logs in and | |
1741 | * they have silences on the IRCd then we need to set them here | |
1742 | * somehow | |
1743 | */ | |
1744 | if (!user->handle_info) | |
1745 | return 1; | |
1746 | ||
1747 | mask = argv[2]; | |
1748 | ||
1749 | if (*mask == '-') { | |
1750 | for (i=0; i<user->handle_info->ignores->used; i++) { | |
1751 | if (!irccasecmp(mask+1, user->handle_info->ignores->list[i])) | |
1752 | user->handle_info->ignores->list[i] = user->handle_info->ignores->list[--user->handle_info->ignores->used]; | |
1753 | } | |
1754 | } else { | |
1755 | for (i=0; i<user->handle_info->ignores->used; i++) { | |
1756 | if (!strcmp(mask+1, user->handle_info->ignores->list[i])) | |
1757 | return 1; /* Already on the users NickServ ignore list, safely ignore */ | |
1758 | } | |
1759 | ||
1760 | new_mask = strdup(mask+1); | |
1761 | string_list_append(user->handle_info->ignores, new_mask); | |
1762 | } | |
1763 | return 1; | |
1764 | } | |
1765 | ||
1766 | static CMD_FUNC(cmd_kick) | |
1767 | { | |
1768 | if (argc < 3) | |
1769 | return 0; | |
1770 | ChannelUserKicked(GetUserH(origin), GetUserN(argv[2]), GetChannel(argv[1])); | |
1771 | return 1; | |
1772 | } | |
1773 | ||
1774 | static CMD_FUNC(cmd_squit) | |
1775 | { | |
1776 | struct server *server; | |
1777 | char *uplink; | |
1778 | ||
1779 | if (argc < 4) | |
1780 | return 0; | |
1781 | if (!(server = GetServerH(argv[1]))) | |
1782 | return 0; | |
1783 | ||
1784 | if (server == self->uplink) { | |
1785 | /* Force a reconnect to the currently selected server. */ | |
1786 | cManager.uplink->tries = 0; | |
1787 | log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", argv[3]); | |
1788 | close_socket(); | |
1789 | return 1; | |
1790 | } | |
1791 | ||
1792 | uplink = strdup(server->uplink->name); | |
1793 | DelServer(server, 0, argv[3]); | |
1794 | /* if its a pingout and pingout connecting is enabled | |
1795 | or its a read error and readerror connecting is enabled | |
1796 | or were doing a "N" and i need to connect that server somewhere */ | |
1797 | routing_handle_squit(argv[1], uplink, argv[3]); | |
1798 | free(uplink); | |
1799 | return 1; | |
1800 | } | |
1801 | ||
1802 | static CMD_FUNC(cmd_privmsg) | |
1803 | { | |
1804 | struct privmsg_desc pd; | |
1805 | if (argc != 3) | |
1806 | return 0; | |
1807 | pd.user = GetUserH(origin); | |
1808 | if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user))) | |
1809 | return 1; | |
1810 | pd.is_notice = 0; | |
1811 | pd.text = argv[2]; | |
1812 | parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd); | |
1813 | return 1; | |
1814 | } | |
1815 | ||
1816 | static CMD_FUNC(cmd_notice) | |
1817 | { | |
1818 | struct privmsg_desc pd; | |
1819 | struct server *srv; | |
1820 | int nuser = 0; | |
1821 | ||
1822 | if (argc != 3) | |
1823 | return 0; | |
1824 | ||
1825 | pd.user = GetUserH(origin); | |
1826 | if(!pd.user) | |
1827 | nuser = 1; | |
1828 | if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user))) { | |
1829 | } | |
1830 | else { | |
1831 | pd.is_notice = 1; | |
1832 | pd.text = argv[2]; | |
1833 | parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd); | |
1834 | } | |
1835 | ||
1836 | srv = GetServerH(origin); | |
1837 | if(srv) { | |
1838 | char *sargv[MAXNUMPARAMS]; | |
1839 | int sargc; | |
1840 | ||
1841 | sargc = split_line(argv[2], true, MAXNUMPARAMS, sargv); | |
1842 | ||
1843 | if(!strcasecmp(sargv[0], "Connect:")) { | |
1844 | /* :Connect: Host shoe.loxxin.net not listed in ircd.conf */ | |
1845 | if(!strcasecmp(sargv[3], "not") && !strcasecmp(sargv[4], "listed")) { | |
1846 | routing_handle_connect_failure(srv, sargv[2], unsplit_string(sargv+3, sargc-3, NULL)); | |
1847 | } | |
1848 | } | |
1849 | else if(!strcasecmp(sargv[0], "Link")) { | |
1850 | /* :Link with mephisto.etheria.cx cancelled: Server mephisto.etheria.cx[216.46.33.71] | |
1851 | * | |
1852 | * :Link with laptop.afternet.org cancelled: Connection refused | |
1853 | */ | |
1854 | if(!strcasecmp(sargv[3], "cancelled:")) { | |
1855 | routing_handle_connect_failure(srv, sargv[2], unsplit_string(sargv+4, sargc-4, NULL)); | |
1856 | } | |
1857 | } | |
1858 | } | |
1859 | return 1; | |
1860 | } | |
1861 | ||
1862 | static CMD_FUNC(cmd_away) | |
1863 | { | |
1864 | struct userNode *uNode; | |
1865 | ||
1866 | uNode = GetUserH(origin); | |
1867 | if (!uNode) | |
1868 | return 1; | |
1869 | if (argc < 2) | |
1870 | uNode->modes &= ~FLAGS_AWAY; | |
1871 | else | |
1872 | uNode->modes |= FLAGS_AWAY; | |
1873 | return 1; | |
1874 | } | |
1875 | ||
1876 | static CMD_FUNC(cmd_gline) | |
1877 | { | |
1878 | if (argc < 3) | |
1879 | return 0; | |
1880 | if (argv[2][0] == '+') { | |
1881 | if (argc < 5) | |
1882 | return 0; | |
1883 | gline_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0, 0); | |
1884 | return 1; | |
1885 | } else if (argv[2][0] == '-') { | |
1886 | gline_remove(argv[2]+1, 0); | |
1887 | return 1; | |
1888 | } else | |
1889 | return 0; | |
1890 | } | |
1891 | ||
1892 | static CMD_FUNC(cmd_shun) | |
1893 | { | |
1894 | if (argc < 3) | |
1895 | return 0; | |
1896 | if (argv[2][0] == '+') { | |
1897 | if (argc < 5) | |
1898 | return 0; | |
1899 | shun_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0); | |
1900 | return 1; | |
1901 | } else if (argv[2][0] == '-') { | |
1902 | shun_remove(argv[2]+1, 0); | |
1903 | return 1; | |
1904 | } else | |
1905 | return 0; | |
1906 | } | |
1907 | ||
1908 | static CMD_FUNC(cmd_svsnick) | |
1909 | { | |
1910 | struct userNode *target, *dest; | |
1911 | if ((argc < 4) | |
1912 | || !(target = GetUserN(argv[1])) | |
1913 | || !IsLocal(target) | |
1914 | || (dest = GetUserH(argv[2]))) | |
1915 | return 0; | |
1916 | NickChange(target, argv[2], 0); | |
1917 | return 1; | |
1918 | } | |
1919 | ||
1920 | static oper_func_t *of_list; | |
1921 | static unsigned int of_size = 0, of_used = 0; | |
1922 | ||
1923 | void | |
1924 | free_user(struct userNode *user) | |
1925 | { | |
1926 | free(user->nick); | |
1927 | free(user); | |
1928 | } | |
1929 | ||
1930 | static void | |
1931 | parse_cleanup(void) | |
1932 | { | |
1933 | unsigned int nn; | |
1934 | free(of_list); | |
1935 | free(privmsg_funcs); | |
1936 | free(notice_funcs); | |
1937 | free(mcf_list); | |
1938 | dict_delete(irc_func_dict); | |
1939 | for (nn=0; nn<dead_users.used; nn++) | |
1940 | free_user(dead_users.list[nn]); | |
1941 | userList_clean(&dead_users); | |
1942 | } | |
1943 | ||
1944 | static void | |
1945 | p10_conf_reload(void) { | |
1946 | hidden_host_suffix = conf_get_data("server/hidden_host", RECDB_QSTRING); | |
1947 | } | |
1948 | ||
1949 | static void | |
1950 | remove_unbursted_channel(struct chanNode *cNode) { | |
1951 | if (unbursted_channels) | |
1952 | dict_remove(unbursted_channels, cNode->name); | |
1953 | } | |
1954 | ||
1955 | void | |
1956 | init_parse(void) | |
1957 | { | |
1958 | const char *str, *desc; | |
1959 | int numnick, usermask, max_users; | |
1960 | char numer[COMBO_NUMERIC_LEN+1]; | |
1961 | ||
1962 | /* read config items */ | |
1963 | str = conf_get_data("server/extended_accounts", RECDB_QSTRING); | |
1964 | extended_accounts = str ? enabled_string(str) : 1; | |
1965 | str = conf_get_data("server/ping_freq", RECDB_QSTRING); | |
1966 | ping_freq = str ? ParseInterval(str) : 120; | |
1967 | str = conf_get_data("server/ping_timeout", RECDB_QSTRING); | |
1968 | ping_timeout = str ? ParseInterval(str) : 30; | |
1969 | str = conf_get_data("server/force_n2k", RECDB_QSTRING); | |
1970 | force_n2k = str ? enabled_string(str) : 1; | |
1971 | str = conf_get_data("server/numeric", RECDB_QSTRING); | |
1972 | if (!str) { | |
1973 | log_module(MAIN_LOG, LOG_ERROR, "No server/numeric entry in config file."); | |
1974 | exit(1); | |
1975 | } | |
1976 | numnick = atoi(str); | |
1977 | str = conf_get_data("server/max_users", RECDB_QSTRING); | |
1978 | max_users = str ? atoi(str) : 4096; | |
1979 | for (usermask = 4; usermask < max_users; usermask <<= 1) ; | |
1980 | usermask--; | |
1981 | if ((numnick < 64) && (usermask < 4096) && !force_n2k) | |
1982 | inttobase64(numer, (numnick << 12) + (usermask & 0x00fff), 3); | |
1983 | else | |
1984 | inttobase64(numer, (numnick << 18) + (usermask & 0x3ffff), 5); | |
1985 | ||
1986 | str = conf_get_data("server/his_servername", RECDB_QSTRING); | |
1987 | his_servername = str ? strdup(str) : NULL; | |
1988 | str = conf_get_data("server/his_servercomment", RECDB_QSTRING); | |
1989 | his_servercomment = str ? strdup(str) : NULL; | |
1990 | ||
1991 | str = conf_get_data("server/hostname", RECDB_QSTRING); | |
1992 | desc = conf_get_data("server/description", RECDB_QSTRING); | |
1993 | if (!str || !desc) { | |
1994 | log_module(MAIN_LOG, LOG_ERROR, "No server/hostname entry in config file."); | |
1995 | exit(1); | |
1996 | } | |
1997 | self = AddServer(NULL, str, 0, boot_time, now, numer, desc); | |
1998 | conf_register_reload(p10_conf_reload); | |
1999 | ||
2000 | irc_func_dict = dict_new(); | |
2001 | dict_insert(irc_func_dict, CMD_BURST, cmd_burst); | |
2002 | dict_insert(irc_func_dict, TOK_BURST, cmd_burst); | |
2003 | dict_insert(irc_func_dict, CMD_CREATE, cmd_create); | |
2004 | dict_insert(irc_func_dict, TOK_CREATE, cmd_create); | |
2005 | dict_insert(irc_func_dict, CMD_EOB, cmd_eob); | |
2006 | dict_insert(irc_func_dict, TOK_EOB, cmd_eob); | |
2007 | dict_insert(irc_func_dict, CMD_EOB_ACK, cmd_eob_ack); | |
2008 | dict_insert(irc_func_dict, TOK_EOB_ACK, cmd_eob_ack); | |
2009 | dict_insert(irc_func_dict, CMD_MODE, cmd_mode); | |
2010 | dict_insert(irc_func_dict, TOK_MODE, cmd_mode); | |
2011 | dict_insert(irc_func_dict, CMD_NICK, cmd_nick); | |
2012 | dict_insert(irc_func_dict, TOK_NICK, cmd_nick); | |
2013 | dict_insert(irc_func_dict, CMD_ACCOUNT, cmd_account); | |
2014 | dict_insert(irc_func_dict, TOK_ACCOUNT, cmd_account); | |
2015 | dict_insert(irc_func_dict, CMD_FAKEHOST, cmd_fakehost); | |
2016 | dict_insert(irc_func_dict, TOK_FAKEHOST, cmd_fakehost); | |
2017 | dict_insert(irc_func_dict, CMD_PASS, cmd_pass); | |
2018 | dict_insert(irc_func_dict, TOK_PASS, cmd_pass); | |
2019 | dict_insert(irc_func_dict, CMD_PING, cmd_ping); | |
2020 | dict_insert(irc_func_dict, TOK_PING, cmd_ping); | |
2021 | dict_insert(irc_func_dict, CMD_PRIVMSG, cmd_privmsg); | |
2022 | dict_insert(irc_func_dict, TOK_PRIVMSG, cmd_privmsg); | |
2023 | dict_insert(irc_func_dict, CMD_PONG, cmd_pong); | |
2024 | dict_insert(irc_func_dict, TOK_PONG, cmd_pong); | |
2025 | dict_insert(irc_func_dict, CMD_QUIT, cmd_quit); | |
2026 | dict_insert(irc_func_dict, TOK_QUIT, cmd_quit); | |
2027 | dict_insert(irc_func_dict, CMD_SERVER, cmd_server); | |
2028 | dict_insert(irc_func_dict, TOK_SERVER, cmd_server); | |
2029 | dict_insert(irc_func_dict, CMD_JOIN, cmd_join); | |
2030 | dict_insert(irc_func_dict, TOK_JOIN, cmd_join); | |
2031 | dict_insert(irc_func_dict, CMD_PART, cmd_part); | |
2032 | dict_insert(irc_func_dict, TOK_PART, cmd_part); | |
2033 | dict_insert(irc_func_dict, CMD_ERROR, cmd_error); | |
2034 | dict_insert(irc_func_dict, TOK_ERROR, cmd_error); | |
2035 | dict_insert(irc_func_dict, CMD_TOPIC, cmd_topic); | |
2036 | dict_insert(irc_func_dict, TOK_TOPIC, cmd_topic); | |
2037 | dict_insert(irc_func_dict, CMD_AWAY, cmd_away); | |
2038 | dict_insert(irc_func_dict, TOK_AWAY, cmd_away); | |
2039 | dict_insert(irc_func_dict, CMD_SILENCE, cmd_silence); | |
2040 | dict_insert(irc_func_dict, TOK_SILENCE, cmd_silence); | |
2041 | dict_insert(irc_func_dict, CMD_KICK, cmd_kick); | |
2042 | dict_insert(irc_func_dict, TOK_KICK, cmd_kick); | |
2043 | dict_insert(irc_func_dict, CMD_SQUIT, cmd_squit); | |
2044 | dict_insert(irc_func_dict, TOK_SQUIT, cmd_squit); | |
2045 | dict_insert(irc_func_dict, CMD_KILL, cmd_kill); | |
2046 | dict_insert(irc_func_dict, TOK_KILL, cmd_kill); | |
2047 | dict_insert(irc_func_dict, CMD_NOTICE, cmd_notice); | |
2048 | dict_insert(irc_func_dict, TOK_NOTICE, cmd_notice); | |
2049 | dict_insert(irc_func_dict, CMD_STATS, cmd_stats); | |
2050 | dict_insert(irc_func_dict, TOK_STATS, cmd_stats); | |
2051 | dict_insert(irc_func_dict, CMD_SVSNICK, cmd_svsnick); | |
2052 | dict_insert(irc_func_dict, TOK_SVSNICK, cmd_svsnick); | |
2053 | dict_insert(irc_func_dict, CMD_SWHOIS, cmd_dummy); | |
2054 | dict_insert(irc_func_dict, TOK_SWHOIS, cmd_dummy); | |
2055 | dict_insert(irc_func_dict, CMD_WHOIS, cmd_whois); | |
2056 | dict_insert(irc_func_dict, TOK_WHOIS, cmd_whois); | |
2057 | dict_insert(irc_func_dict, CMD_GLINE, cmd_gline); | |
2058 | dict_insert(irc_func_dict, TOK_GLINE, cmd_gline); | |
2059 | dict_insert(irc_func_dict, CMD_SHUN, cmd_shun); | |
2060 | dict_insert(irc_func_dict, TOK_SHUN, cmd_shun); | |
2061 | dict_insert(irc_func_dict, CMD_OPMODE, cmd_opmode); | |
2062 | dict_insert(irc_func_dict, TOK_OPMODE, cmd_opmode); | |
2063 | dict_insert(irc_func_dict, CMD_CLEARMODE, cmd_clearmode); | |
2064 | dict_insert(irc_func_dict, TOK_CLEARMODE, cmd_clearmode); | |
2065 | dict_insert(irc_func_dict, CMD_VERSION, cmd_version); | |
2066 | dict_insert(irc_func_dict, TOK_VERSION, cmd_version); | |
2067 | dict_insert(irc_func_dict, CMD_ADMIN, cmd_admin); | |
2068 | dict_insert(irc_func_dict, TOK_ADMIN, cmd_admin); | |
2069 | ||
2070 | /* In P10, DESTRUCT doesn't do anything except be broadcast to servers. | |
2071 | * Apparently to obliterate channels from any servers that think they | |
2072 | * exist? | |
2073 | */ | |
2074 | dict_insert(irc_func_dict, CMD_DESTRUCT, cmd_dummy); | |
2075 | dict_insert(irc_func_dict, TOK_DESTRUCT, cmd_dummy); | |
2076 | /* Ignore invites */ | |
2077 | dict_insert(irc_func_dict, CMD_INVITE, cmd_dummy); | |
2078 | dict_insert(irc_func_dict, TOK_INVITE, cmd_dummy); | |
2079 | /* DESYNCH is just informational, so ignore it */ | |
2080 | dict_insert(irc_func_dict, CMD_DESYNCH, cmd_dummy); | |
2081 | dict_insert(irc_func_dict, TOK_DESYNCH, cmd_dummy); | |
2082 | /* Ignore channel operator notices. */ | |
2083 | dict_insert(irc_func_dict, CMD_WALLCHOPS, cmd_dummy); | |
2084 | dict_insert(irc_func_dict, TOK_WALLCHOPS, cmd_dummy); | |
2085 | dict_insert(irc_func_dict, CMD_WALLVOICES, cmd_dummy); | |
2086 | dict_insert(irc_func_dict, TOK_WALLVOICES, cmd_dummy); | |
2087 | dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy); | |
2088 | dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy); | |
2089 | /* Ignore opers being silly. */ | |
2090 | dict_insert(irc_func_dict, CMD_WALLOPS, cmd_dummy); | |
2091 | dict_insert(irc_func_dict, TOK_WALLOPS, cmd_dummy); | |
2092 | dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy); | |
2093 | dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy); | |
2094 | dict_insert(irc_func_dict, TOK_WALLUSERS, cmd_dummy); | |
2095 | /* Ignore dnsbl exemptions */ | |
2096 | dict_insert(irc_func_dict, TOK_EXEMPT, cmd_dummy); | |
2097 | dict_insert(irc_func_dict, TOK_MARK, cmd_dummy); | |
2098 | /* Ignore privs */ | |
2099 | dict_insert(irc_func_dict, TOK_PRIVS, cmd_dummy); | |
2100 | /* Ignore remote luser */ | |
2101 | dict_insert(irc_func_dict, TOK_LUSERS, cmd_dummy); | |
2102 | /* We have reliable clock! Always! Wraaa! */ | |
2103 | dict_insert(irc_func_dict, CMD_SETTIME, cmd_dummy); | |
2104 | dict_insert(irc_func_dict, TOK_SETTIME, cmd_dummy); | |
2105 | ||
2106 | /* ignore /trace and /motd commands targetted at us */ | |
2107 | dict_insert(irc_func_dict, TOK_TRACE, cmd_dummy); | |
2108 | dict_insert(irc_func_dict, TOK_MOTD, cmd_dummy); | |
2109 | ||
2110 | /* handle topics */ | |
2111 | dict_insert(irc_func_dict, "331", cmd_num_topic); | |
2112 | dict_insert(irc_func_dict, "332", cmd_num_topic); | |
2113 | dict_insert(irc_func_dict, "333", cmd_num_topic); | |
2114 | dict_insert(irc_func_dict, "345", cmd_dummy); /* blah has been invited to blah */ | |
2115 | dict_insert(irc_func_dict, "432", cmd_error_nick); /* Erroneus [sic] nickname */ | |
2116 | /* ban list resetting */ | |
2117 | /* "stats g" responses */ | |
2118 | dict_insert(irc_func_dict, "247", cmd_num_gline); | |
2119 | dict_insert(irc_func_dict, "542", cmd_num_shun); | |
2120 | dict_insert(irc_func_dict, "219", cmd_dummy); /* "End of /STATS report" */ | |
2121 | /* other numeric responses we might get */ | |
2122 | dict_insert(irc_func_dict, "401", cmd_dummy); /* target left network */ | |
2123 | dict_insert(irc_func_dict, "403", cmd_dummy); /* no such channel */ | |
2124 | dict_insert(irc_func_dict, "404", cmd_dummy); /* cannot send to channel */ | |
2125 | dict_insert(irc_func_dict, "439", cmd_dummy); /* target change too fast */ | |
2126 | dict_insert(irc_func_dict, "441", cmd_dummy); /* target isn't on that channel */ | |
2127 | dict_insert(irc_func_dict, "442", cmd_dummy); /* you aren't on that channel */ | |
2128 | dict_insert(irc_func_dict, "443", cmd_dummy); /* is already on channel (after invite?) */ | |
2129 | dict_insert(irc_func_dict, "461", cmd_dummy); /* Not enough parameters (after TOPIC w/ 0 args) */ | |
2130 | dict_insert(irc_func_dict, "467", cmd_dummy); /* Channel key already set */ | |
2131 | ||
2132 | num_privmsg_funcs = 16; | |
2133 | privmsg_funcs = malloc(sizeof(privmsg_func_t)*num_privmsg_funcs); | |
2134 | memset(privmsg_funcs, 0, sizeof(privmsg_func_t)*num_privmsg_funcs); | |
2135 | ||
2136 | num_notice_funcs = 16; | |
2137 | notice_funcs = malloc(sizeof(privmsg_func_t)*num_notice_funcs); | |
2138 | memset(notice_funcs, 0, sizeof(privmsg_func_t)*num_notice_funcs); | |
2139 | ||
2140 | userList_init(&dead_users); | |
2141 | reg_del_channel_func(remove_unbursted_channel); | |
2142 | reg_exit_func(parse_cleanup); | |
2143 | } | |
2144 | ||
2145 | int | |
2146 | parse_line(char *line, int recursive) | |
2147 | { | |
2148 | char *argv[MAXNUMPARAMS], *origin; | |
2149 | int argc, cmd, res=0; | |
2150 | cmd_func_t *func; | |
2151 | ||
2152 | argc = split_line(line, true, MAXNUMPARAMS, argv); | |
2153 | cmd = self->uplink || !argv[0][1] || !argv[0][2]; | |
2154 | if (argc > cmd) { | |
2155 | if (cmd) { | |
2156 | if (argv[0][0] == ':') { | |
2157 | origin = argv[0]+1; | |
2158 | } else if (!argv[0][1] || !argv[0][2]) { | |
2159 | struct server *sNode = GetServerN(argv[0]); | |
2160 | origin = sNode ? sNode->name : 0; | |
2161 | } else { | |
2162 | struct userNode *uNode = GetUserN(argv[0]); | |
2163 | origin = uNode ? uNode->nick : 0; | |
2164 | } | |
2165 | } else | |
2166 | origin = 0; | |
2167 | if ((func = dict_find(irc_func_dict, argv[cmd], NULL))) | |
2168 | res = func(origin, argc-cmd, argv+cmd); | |
2169 | } | |
2170 | if (!res) { | |
2171 | log_module(MAIN_LOG, LOG_ERROR, "PARSE ERROR on line: %s", unsplit_string(argv, argc, NULL)); | |
2172 | } else if (!recursive) { | |
2173 | unsigned int i; | |
2174 | for (i=0; i<dead_users.used; i++) | |
2175 | free_user(dead_users.list[i]); | |
2176 | dead_users.used = 0; | |
2177 | } | |
2178 | return res; | |
2179 | } | |
2180 | ||
2181 | static void | |
2182 | parse_foreach(char *target_list, foreach_chanfunc cf, foreach_nonchan nc, foreach_userfunc uf, foreach_nonuser nu, void *data) | |
2183 | { | |
2184 | char *j, old; | |
2185 | ||
2186 | do { | |
2187 | j = target_list; | |
2188 | while (*j != 0 && *j != ',') | |
2189 | j++; | |
2190 | old = *j; | |
2191 | *j = 0; | |
2192 | ||
2193 | if (IsChannelName(target_list) | |
2194 | || (target_list[0] == '0' && target_list[1] == '\0')) { | |
2195 | struct chanNode *chan = GetChannel(target_list); | |
2196 | ||
2197 | if (chan) { | |
2198 | if (cf) | |
2199 | cf(chan, data); | |
2200 | } else { | |
2201 | if (nc) | |
2202 | nc(target_list, data); | |
2203 | } | |
2204 | } else { | |
2205 | struct userNode *user; | |
2206 | struct privmsg_desc *pd = data; | |
2207 | ||
2208 | pd->is_qualified = 0; | |
2209 | if (*target_list == '@') { | |
2210 | user = NULL; | |
2211 | } else if (strchr(target_list, '@')) { | |
2212 | struct server *server; | |
2213 | ||
2214 | pd->is_qualified = 1; | |
2215 | user = GetUserH(strtok(target_list, "@")); | |
2216 | server = GetServerH(strtok(NULL, "@")); | |
2217 | ||
2218 | if (user && (user->uplink != server)) { | |
2219 | /* Don't attempt to index into any arrays | |
2220 | using a user's numeric on another server. */ | |
2221 | user = NULL; | |
2222 | } | |
2223 | } else { | |
2224 | user = GetUserN(target_list); | |
2225 | } | |
2226 | ||
2227 | if (user) { | |
2228 | if (uf) | |
2229 | uf(user, data); | |
2230 | } else { | |
2231 | if (nu) | |
2232 | nu(target_list, data); | |
2233 | } | |
2234 | } | |
2235 | target_list = j+1; | |
2236 | } while (old == ','); | |
2237 | } | |
2238 | ||
2239 | static int | |
2240 | get_local_numeric(void) | |
2241 | { | |
2242 | static unsigned int next_numeric = 0; | |
2243 | if (self->clients > self->num_mask) | |
2244 | return -1; | |
2245 | while (self->users[next_numeric]) | |
2246 | if (++next_numeric > self->num_mask) | |
2247 | next_numeric = 0; | |
2248 | return next_numeric; | |
2249 | } | |
2250 | ||
2251 | static void | |
2252 | make_numeric(struct server *svr, int local_num, char *outbuf) | |
2253 | { | |
2254 | int slen, llen; | |
2255 | ||
2256 | if (force_n2k || svr->numeric[1]) { | |
2257 | slen = 2; | |
2258 | llen = 3; | |
2259 | } else { | |
2260 | slen = 1; | |
2261 | llen = (local_num < 64*64) ? 2 : 3; | |
2262 | } | |
2263 | strncpy(outbuf, svr->numeric, slen); | |
2264 | inttobase64(outbuf+slen, local_num, llen); | |
2265 | outbuf[slen+llen] = 0; | |
2266 | } | |
2267 | ||
2268 | struct server * | |
2269 | AddServer(struct server *uplink, const char *name, int hops, time_t boot, time_t link, const char *numeric, const char *description) | |
2270 | { | |
2271 | struct server* sNode; | |
2272 | int slen, mlen; | |
2273 | ||
2274 | if ((sNode = GetServerN(numeric))) { | |
2275 | /* This means we're trying to re-add an existant server. | |
2276 | * To be safe, we should forget the previous incarnation. | |
2277 | * (And all its linked servers.) | |
2278 | * | |
2279 | * It usually only happens in replays when the original | |
2280 | * had a ping timeout and the replay didn't (because | |
2281 | * replaying a ping timeout invariably gets things wrong). | |
2282 | */ | |
2283 | DelServer(sNode, 0, NULL); | |
2284 | } | |
2285 | ||
2286 | switch (strlen(numeric)) { | |
2287 | case 5: slen = 2; mlen = 3; break; | |
2288 | case 4: slen = 1; mlen = 3; break; | |
2289 | case 3: slen = 1; mlen = 2; break; | |
2290 | default: | |
2291 | log_module(MAIN_LOG, LOG_ERROR, "AddServer(\"%s\", \"%s\", ...): Numeric %s has invalid length.", uplink->name, name, numeric); | |
2292 | return NULL; | |
2293 | } | |
2294 | ||
2295 | sNode = calloc(1, sizeof(*sNode)); | |
2296 | sNode->uplink = uplink; | |
2297 | safestrncpy(sNode->name, name, sizeof(sNode->name)); | |
2298 | sNode->num_mask = base64toint(numeric+slen, mlen); | |
2299 | sNode->hops = hops; | |
2300 | sNode->boot = boot; | |
2301 | sNode->link = link; | |
2302 | strncpy(sNode->numeric, numeric, slen); | |
2303 | safestrncpy(sNode->description, description, sizeof(sNode->description)); | |
2304 | sNode->users = calloc(sNode->num_mask+1, sizeof(*sNode->users)); | |
2305 | serverList_init(&sNode->children); | |
2306 | if (sNode->uplink) { | |
2307 | /* uplink may be NULL if we're just building ourself */ | |
2308 | serverList_append(&sNode->uplink->children, sNode); | |
2309 | } | |
2310 | servers_num[base64toint(numeric, slen)] = sNode; | |
2311 | dict_insert(servers, sNode->name, sNode); | |
2312 | return sNode; | |
2313 | } | |
2314 | ||
2315 | void DelServer(struct server* serv, int announce, const char *message) | |
2316 | { | |
2317 | unsigned int i; | |
2318 | ||
2319 | /* If we receive an ERROR command before the SERVER | |
2320 | * command a NULL server can be passed */ | |
2321 | if (!serv) | |
2322 | return; | |
2323 | ||
2324 | /* Hrm, what's the right way to SQUIT some other server? | |
2325 | * (This code is only to handle killing juped servers.) */ | |
2326 | if (announce && (serv->uplink == self) && (serv != self->uplink)) | |
2327 | irc_squit(serv, message, NULL); | |
2328 | ||
2329 | /* must recursively remove servers linked to this one first */ | |
2330 | for (i=serv->children.used;i>0;) | |
2331 | if (serv->children.list[--i] != self) | |
2332 | DelServer(serv->children.list[i], false, NULL); | |
2333 | ||
2334 | /* clean up server's user hash tables */ | |
2335 | for (i=0;i<=serv->num_mask;i++) | |
2336 | if (serv->users[i]) | |
2337 | DelUser(serv->users[i], NULL, false, "server delinked"); | |
2338 | ||
2339 | /* delete server */ | |
2340 | if (serv->uplink) | |
2341 | serverList_remove(&serv->uplink->children, serv); | |
2342 | if (serv == self->uplink) | |
2343 | self->uplink = NULL; | |
2344 | servers_num[base64toint(serv->numeric, strlen(serv->numeric))] = NULL; | |
2345 | dict_remove(servers, serv->name); | |
2346 | serverList_clean(&serv->children); | |
2347 | free(serv->users); | |
2348 | free(serv); | |
2349 | } | |
2350 | ||
2351 | struct userNode * | |
2352 | AddService(const char *nick, const char *modes, const char *desc, const char *hostname) | |
2353 | { | |
2354 | char numeric[COMBO_NUMERIC_LEN+1]; | |
2355 | int local_num = get_local_numeric(); | |
2356 | time_t timestamp = now; | |
2357 | struct userNode *old_user = GetUserH(nick); | |
2358 | ||
2359 | if (old_user) { | |
2360 | if (IsLocal(old_user)) | |
2361 | return old_user; | |
2362 | timestamp = old_user->timestamp - 1; | |
2363 | } | |
2364 | if (local_num == -1) { | |
2365 | log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for service %s", nick); | |
2366 | return 0; | |
2367 | } | |
2368 | if (!hostname) | |
2369 | hostname = self->name; | |
2370 | make_numeric(self, local_num, numeric); | |
2371 | /* TODO: Make these modes part of the conf file */ | |
2372 | return AddUser(self, nick, nick, hostname, modes ? modes : "+oik", numeric, desc, now, "AAAAAA"); | |
2373 | } | |
2374 | ||
2375 | struct userNode * | |
2376 | AddClone(const char *nick, const char *ident, const char *hostname, const char *desc) | |
2377 | { | |
2378 | char numeric[COMBO_NUMERIC_LEN+1]; | |
2379 | int local_num = get_local_numeric(); | |
2380 | time_t timestamp = now; | |
2381 | struct userNode *old_user = GetUserH(nick); | |
2382 | ||
2383 | if (old_user) { | |
2384 | if (IsLocal(old_user)) | |
2385 | return old_user; | |
2386 | timestamp = old_user->timestamp - 1; | |
2387 | } | |
2388 | if (local_num == -1) { | |
2389 | log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for clone %s", nick); | |
2390 | return 0; | |
2391 | } | |
2392 | make_numeric(self, local_num, numeric); | |
2393 | return AddUser(self, nick, ident, hostname, "+i", numeric, desc, timestamp, "AAAAAA"); | |
2394 | } | |
2395 | ||
2396 | int | |
2397 | is_valid_nick(const char *nick) { | |
2398 | unsigned int ii; | |
2399 | /* IRC has some of The Most Fucked-Up ideas about character sets | |
2400 | * in the world.. */ | |
2401 | if (!isalpha(*nick) && !strchr("{|}~[\\]^_`", *nick)) | |
2402 | return 0; | |
2403 | for (ii = 0; nick[ii]; ++ii) | |
2404 | if (!isalnum(nick[ii]) && !strchr("{|}~[\\]^-_`", nick[ii])) | |
2405 | return 0; | |
2406 | if (strlen(nick) > nicklen) | |
2407 | return 0; | |
2408 | return 1; | |
2409 | } | |
2410 | ||
2411 | static struct userNode* | |
2412 | AddUser(struct server* uplink, const char *nick, const char *ident, const char *hostname, const char *modes, const char *numeric, const char *userinfo, time_t timestamp, const char *realip) | |
2413 | { | |
2414 | struct userNode *oldUser, *uNode; | |
2415 | unsigned int n, ignore_user; | |
2416 | ||
2417 | if ((strlen(numeric) < 3) || (strlen(numeric) > 5)) { | |
2418 | log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): numeric %s wrong length!", uplink, nick, numeric); | |
2419 | return NULL; | |
2420 | } | |
2421 | ||
2422 | if (!uplink) { | |
2423 | log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s doesn't exist!", uplink, nick, numeric); | |
2424 | return NULL; | |
2425 | } | |
2426 | ||
2427 | if (uplink != GetServerN(numeric)) { | |
2428 | log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s differs from nominal uplink %s.", uplink, nick, numeric, uplink->name); | |
2429 | return NULL; | |
2430 | } | |
2431 | ||
2432 | if (!is_valid_nick(nick)) { | |
2433 | log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): invalid nickname detected.", uplink, nick); | |
2434 | return NULL; | |
2435 | } | |
2436 | ||
2437 | ignore_user = 0; | |
2438 | if ((oldUser = GetUserH(nick))) { | |
2439 | if (IsLocal(oldUser) && (IsService(oldUser) || IsPersistent(oldUser))) { | |
2440 | /* The service should collide the new user off. */ | |
2441 | oldUser->timestamp = timestamp - 1; | |
2442 | irc_user(oldUser); | |
2443 | } | |
2444 | if (oldUser->timestamp > timestamp) { | |
2445 | /* "Old" user is really newer; remove them */ | |
2446 | DelUser(oldUser, 0, 1, "Overruled by older nick"); | |
2447 | } else { | |
2448 | /* User being added is too new; do not add them to | |
2449 | * clients, but do add them to the server's list, since it | |
2450 | * will send a KILL and QUIT soon. */ | |
2451 | ignore_user = 1; | |
2452 | } | |
2453 | } | |
2454 | ||
2455 | /* create new usernode and set all values */ | |
2456 | uNode = calloc(1, sizeof(*uNode)); | |
2457 | uNode->nick = strdup(nick); | |
2458 | safestrncpy(uNode->ident, ident, sizeof(uNode->ident)); | |
2459 | safestrncpy(uNode->info, userinfo, sizeof(uNode->info)); | |
2460 | safestrncpy(uNode->hostname, hostname, sizeof(uNode->hostname)); | |
2461 | safestrncpy(uNode->numeric, numeric, sizeof(uNode->numeric)); | |
2462 | irc_p10_pton(&uNode->ip, realip); | |
2463 | ||
2464 | if (irc_in_addr_is_ipv4(uNode->ip)) { | |
2465 | make_virtip((char*)irc_ntoa(&uNode->ip), (char*)irc_ntoa(&uNode->ip), uNode->cryptip); | |
2466 | make_virthost((char*)irc_ntoa(&uNode->ip), uNode->hostname, uNode->crypthost); | |
2467 | } else if (irc_in_addr_is_ipv6(uNode->ip)) { | |
2468 | make_ipv6virthost((char*)irc_ntoa(&uNode->ip), uNode->hostname, uNode->crypthost); | |
2469 | } | |
2470 | ||
2471 | uNode->timestamp = timestamp; | |
2472 | modeList_init(&uNode->channels); | |
2473 | uNode->uplink = uplink; | |
2474 | if (++uNode->uplink->clients > uNode->uplink->max_clients) { | |
2475 | uNode->uplink->max_clients = uNode->uplink->clients; | |
2476 | } | |
2477 | uNode->num_local = base64toint(numeric+strlen(uNode->uplink->numeric), 3) & uNode->uplink->num_mask; | |
2478 | uNode->uplink->users[uNode->num_local] = uNode; | |
2479 | mod_usermode(uNode, modes); | |
2480 | if (ignore_user) | |
2481 | return uNode; | |
2482 | ||
2483 | dict_insert(clients, uNode->nick, uNode); | |
2484 | if (dict_size(clients) > max_clients) { | |
2485 | max_clients = dict_size(clients); | |
2486 | max_clients_time = now; | |
2487 | } | |
2488 | if (IsLocal(uNode)) | |
2489 | irc_user(uNode); | |
2490 | for (n=0; n<nuf_used; n++) | |
2491 | if (nuf_list[n](uNode)) | |
2492 | break; | |
2493 | ||
2494 | if ((uNode->loc == 1) && (uNode->handle_info)) | |
2495 | send_func_list(uNode); | |
2496 | ||
2497 | return uNode; | |
2498 | } | |
2499 | ||
2500 | /* removes user from it's server's hash table and nick hash table */ | |
2501 | void | |
2502 | DelUser(struct userNode* user, struct userNode *killer, int announce, const char *why) | |
2503 | { | |
2504 | unsigned int n; | |
2505 | ||
2506 | verify(user); | |
2507 | ||
2508 | /* mark them as dead, in case anybody cares */ | |
2509 | user->dead = 1; | |
2510 | ||
2511 | /* remove pending adduser commands */ | |
2512 | wipe_adduser_pending(NULL, user); | |
2513 | ||
2514 | /* remove user from all channels */ | |
2515 | while (user->channels.used > 0) | |
2516 | DelChannelUser(user, user->channels.list[user->channels.used-1]->channel, false, 0); | |
2517 | ||
2518 | /* Call these in reverse order so ChanServ can update presence | |
2519 | information before NickServ nukes the handle_info. */ | |
2520 | for (n = duf_used; n > 0; ) | |
2521 | duf_list[--n](user, killer, why); | |
2522 | ||
2523 | user->uplink->clients--; | |
2524 | user->uplink->users[user->num_local] = NULL; | |
2525 | if (IsOper(user)) | |
2526 | userList_remove(&curr_opers, user); | |
2527 | /* remove from global dictionary, but not if after a collide */ | |
2528 | if (user == dict_find(clients, user->nick, NULL)) | |
2529 | dict_remove(clients, user->nick); | |
2530 | ||
2531 | if (IsInvisible(user)) | |
2532 | invis_clients--; | |
2533 | ||
2534 | if (announce) { | |
2535 | if (IsLocal(user)) | |
2536 | irc_quit(user, why); | |
2537 | else | |
2538 | irc_kill(killer, user, why); | |
2539 | } | |
2540 | ||
2541 | modeList_clean(&user->channels); | |
2542 | /* We don't free them, in case we try to privmsg them or something | |
2543 | * (like when a stupid oper kills themself). We just put them onto | |
2544 | * a list of clients that get freed after processing each line. | |
2545 | */ | |
2546 | if (dead_users.size) | |
2547 | userList_append(&dead_users, user); | |
2548 | else | |
2549 | free_user(user); | |
2550 | } | |
2551 | ||
2552 | static void call_oper_funcs(struct userNode *user); | |
2553 | ||
2554 | void mod_usermode(struct userNode *user, const char *mode_change) { | |
2555 | int add = 1; | |
2556 | const char *word = mode_change; | |
2557 | ||
2558 | if (!user || !mode_change) | |
2559 | return; | |
2560 | ||
2561 | call_user_mode_funcs(user, mode_change); | |
2562 | ||
2563 | while (*word != ' ' && *word) word++; | |
2564 | while (*word == ' ') word++; | |
2565 | while (1) { | |
2566 | #define do_user_mode(FLAG) do { if (add) user->modes |= FLAG; else user->modes &= ~FLAG; } while (0) | |
2567 | switch (*mode_change++) { | |
2568 | case 0: case ' ': return; | |
2569 | case '+': add = 1; break; | |
2570 | case '-': add = 0; break; | |
2571 | case 'o': | |
2572 | if (add) { | |
2573 | if(!IsOper(user)) { /* Dont re-oper an oper */ | |
2574 | userList_append(&curr_opers, user); | |
2575 | call_oper_funcs(user); | |
2576 | } | |
2577 | } else { | |
2578 | userList_remove(&curr_opers, user); | |
2579 | } | |
2580 | do_user_mode(FLAGS_OPER); | |
2581 | break; | |
2582 | case 'O': do_user_mode(FLAGS_LOCOP); break; | |
2583 | case 'i': do_user_mode(FLAGS_INVISIBLE); | |
2584 | if (add) | |
2585 | invis_clients++; | |
2586 | else | |
2587 | invis_clients--; | |
2588 | break; | |
2589 | case 'w': do_user_mode(FLAGS_WALLOP); break; | |
2590 | case 's': do_user_mode(FLAGS_SERVNOTICE); break; | |
2591 | case 'd': do_user_mode(FLAGS_DEAF); break; | |
2592 | case 'k': do_user_mode(FLAGS_SERVICE); break; | |
2593 | case 'g': do_user_mode(FLAGS_GLOBAL); break; | |
2594 | // sethost - reed/apples | |
2595 | // case 'h': do_user_mode(FLAGS_HELPER); break; | |
2596 | // I check if there's an 'h' in the first part, and if there, | |
2597 | // then everything after the space becomes their new host. | |
2598 | case 'h': do_user_mode(FLAGS_SETHOST); | |
2599 | if (*word) { | |
2600 | char sethost[MAXLEN]; | |
2601 | unsigned int ii; | |
2602 | for (ii=0; (*word != ' ') && (*word != '\0'); ) | |
2603 | sethost[ii++] = *word++; | |
2604 | sethost[ii] = 0; | |
2605 | while (*word == ' ') | |
2606 | word++; | |
2607 | safestrncpy(user->sethost, sethost, sizeof(user->sethost)); | |
2608 | } | |
2609 | break; | |
2610 | case 'x': do_user_mode(FLAGS_HIDDEN_HOST); break; | |
2611 | case 'r': | |
2612 | if (*word) { | |
2613 | char tag[MAXLEN]; | |
2614 | unsigned int ii; | |
2615 | for (ii=0; (*word != ' ') && (*word != '\0'); ) | |
2616 | tag[ii++] = *word++; | |
2617 | tag[ii] = 0; | |
2618 | while (*word == ' ') | |
2619 | word++; | |
2620 | call_account_func(user, tag); | |
2621 | } | |
2622 | break; | |
2623 | case 'f': | |
2624 | if (*word) { | |
2625 | char host[MAXLEN]; | |
2626 | unsigned int ii; | |
2627 | for (ii=0; (*word != ' ') && (*word != '\0'); ) | |
2628 | host[ii++] = *word++; | |
2629 | host[ii] = 0; | |
2630 | while (*word == ' ') | |
2631 | word++; | |
2632 | assign_fakehost(user, host, 0); | |
2633 | } | |
2634 | break; | |
2635 | } | |
2636 | #undef do_user_mode | |
2637 | } | |
2638 | } | |
2639 | ||
2640 | struct mod_chanmode * | |
2641 | mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, unsigned int flags, short base_oplevel) | |
2642 | { | |
2643 | struct mod_chanmode *change; | |
2644 | unsigned int ii, in_arg, ch_arg, add; | |
2645 | ||
2646 | if (argc == 0) | |
2647 | return NULL; | |
2648 | if (!(change = mod_chanmode_alloc(argc - 1))) | |
2649 | return NULL; | |
2650 | ||
2651 | for (ii = ch_arg = 0, in_arg = add = 1; | |
2652 | (modes[0][ii] != '\0') && (modes[0][ii] != ' '); | |
2653 | ++ii) { | |
2654 | switch (modes[0][ii]) { | |
2655 | case '+': | |
2656 | add = 1; | |
2657 | break; | |
2658 | case '-': | |
2659 | add = 0; | |
2660 | break; | |
2661 | #define do_chan_mode(FLAG) do { if (add) change->modes_set |= (FLAG), change->modes_clear &= ~(FLAG); else change->modes_clear |= (FLAG), change->modes_set &= ~(FLAG); } while(0) | |
2662 | case 'C': do_chan_mode(MODE_NOCTCPS); break; | |
2663 | case 'D': do_chan_mode(MODE_DELAYJOINS); break; | |
2664 | case 'c': do_chan_mode(MODE_NOCOLORS); break; | |
2665 | case 'i': do_chan_mode(MODE_INVITEONLY); break; | |
2666 | case 'm': do_chan_mode(MODE_MODERATED); break; | |
2667 | case 'n': do_chan_mode(MODE_NOPRIVMSGS); break; | |
2668 | case 'p': do_chan_mode(MODE_PRIVATE); break; | |
2669 | case 'r': do_chan_mode(MODE_REGONLY); break; | |
2670 | case 's': do_chan_mode(MODE_SECRET); break; | |
2671 | case 't': do_chan_mode(MODE_TOPICLIMIT); break; | |
2672 | case 'S': do_chan_mode(MODE_STRIPCOLOR); break; | |
2673 | case 'M': do_chan_mode(MODE_MODUNREG); break; | |
2674 | case 'N': do_chan_mode(MODE_NONOTICE); break; | |
2675 | case 'Q': do_chan_mode(MODE_NOQUITMSGS); break; | |
2676 | case 'T': do_chan_mode(MODE_NOAMSG); break; | |
2677 | case 'O': do_chan_mode(MODE_OPERSONLY); break; | |
2678 | case 'Z': do_chan_mode(MODE_SSLONLY); break; | |
2679 | case 'L': do_chan_mode(MODE_HIDEMODE); break; | |
2680 | case 'z': | |
2681 | if (!(flags & MCP_REGISTERED)) { | |
2682 | do_chan_mode(MODE_REGISTERED); | |
2683 | } else { | |
2684 | mod_chanmode_free(change); | |
2685 | return NULL; | |
2686 | } | |
2687 | break; | |
2688 | #undef do_chan_mode | |
2689 | case 'l': | |
2690 | if (add) { | |
2691 | if (in_arg >= argc) | |
2692 | goto error; | |
2693 | change->modes_set |= MODE_LIMIT; | |
2694 | change->new_limit = atoi(modes[in_arg++]); | |
2695 | } else { | |
2696 | change->modes_set &= ~MODE_LIMIT; | |
2697 | change->modes_clear |= MODE_LIMIT; | |
2698 | } | |
2699 | break; | |
2700 | case 'k': | |
2701 | if (add) { | |
2702 | if (in_arg >= argc) | |
2703 | goto error; | |
2704 | change->modes_set |= MODE_KEY; | |
2705 | safestrncpy(change->new_key, modes[in_arg++], sizeof(change->new_key)); | |
2706 | } else { | |
2707 | change->modes_clear |= MODE_KEY; | |
2708 | if (!(flags & MCP_KEY_FREE)) { | |
2709 | if (in_arg >= argc) | |
2710 | goto error; | |
2711 | in_arg++; | |
2712 | } | |
2713 | } | |
2714 | break; | |
2715 | case 'U': | |
2716 | if (add) | |
2717 | { | |
2718 | if (in_arg >= argc) | |
2719 | goto error; | |
2720 | change->modes_set |= MODE_UPASS; | |
2721 | safestrncpy(change->new_upass, modes[in_arg++], sizeof(change->new_upass)); | |
2722 | } else { | |
2723 | change->modes_clear |= MODE_UPASS; | |
2724 | if (!(flags & MCP_UPASS_FREE)) { | |
2725 | if (in_arg >= argc) | |
2726 | goto error; | |
2727 | in_arg++; | |
2728 | } | |
2729 | } | |
2730 | break; | |
2731 | case 'A': | |
2732 | if (add) { | |
2733 | if (in_arg >= argc) | |
2734 | goto error; | |
2735 | change->modes_set |= MODE_APASS; | |
2736 | safestrncpy(change->new_apass, modes[in_arg++], sizeof(change->new_apass)); | |
2737 | } else { | |
2738 | change->modes_clear |= MODE_APASS; | |
2739 | if (!(flags & MCP_APASS_FREE)) { | |
2740 | if (in_arg >= argc) | |
2741 | goto error; | |
2742 | in_arg++; | |
2743 | } | |
2744 | } | |
2745 | break; | |
2746 | case 'b': | |
2747 | if (!(flags & MCP_ALLOW_OVB)) | |
2748 | goto error; | |
2749 | if (in_arg >= argc) | |
2750 | goto error; | |
2751 | change->args[ch_arg].mode = MODE_BAN; | |
2752 | if (!add) | |
2753 | change->args[ch_arg].mode |= MODE_REMOVE; | |
2754 | change->args[ch_arg++].u.hostmask = modes[in_arg++]; | |
2755 | break; | |
2756 | case 'e': | |
2757 | if (!(flags & MCP_ALLOW_OVB)) | |
2758 | goto error; | |
2759 | if (in_arg >= argc) | |
2760 | goto error; | |
2761 | change->args[ch_arg].mode = MODE_EXEMPT; | |
2762 | if (!add) | |
2763 | change->args[ch_arg].mode |= MODE_REMOVE; | |
2764 | change->args[ch_arg++].u.hostmask = modes[in_arg++]; | |
2765 | break; | |
2766 | case 'o': case 'h': case 'v': | |
2767 | { | |
2768 | struct userNode *victim; | |
2769 | char *oplevel_str; | |
2770 | int oplevel; | |
2771 | ||
2772 | if (in_arg >= argc) | |
2773 | goto error; | |
2774 | oplevel_str = strchr(modes[in_arg], ':'); | |
2775 | if (oplevel_str) | |
2776 | { | |
2777 | /* XXYYY M #channel +o XXYYY:<oplevel> */ | |
2778 | *oplevel_str++ = '\0'; | |
2779 | oplevel = parse_oplevel(oplevel_str); | |
2780 | if (oplevel <= base_oplevel && !(flags & MCP_FROM_SERVER)) | |
2781 | oplevel = base_oplevel + 1; | |
2782 | } | |
2783 | else if (channel->modes & MODE_UPASS) | |
2784 | oplevel = base_oplevel + 1; | |
2785 | else | |
2786 | oplevel = -1; | |
2787 | ||
2788 | /* Check that oplevel is within bounds. */ | |
2789 | if (oplevel > MAXOPLEVEL) | |
2790 | oplevel = MAXOPLEVEL; | |
2791 | ||
2792 | if (!(flags & MCP_ALLOW_OVB)) | |
2793 | goto error; | |
2794 | if (in_arg >= argc) | |
2795 | goto error; | |
2796 | ||
2797 | if (modes[0][ii] == 'o') | |
2798 | change->args[ch_arg].mode = MODE_CHANOP; | |
2799 | else if (modes[0][ii] == 'h') | |
2800 | change->args[ch_arg].mode = MODE_HALFOP; | |
2801 | else if (modes[0][ii] == 'v') | |
2802 | change->args[ch_arg].mode = MODE_VOICE; | |
2803 | ||
2804 | if (!add) | |
2805 | change->args[ch_arg].mode |= MODE_REMOVE; | |
2806 | if (flags & MCP_FROM_SERVER) | |
2807 | victim = GetUserN(modes[in_arg++]); | |
2808 | else | |
2809 | victim = GetUserH(modes[in_arg++]); | |
2810 | if (!victim) | |
2811 | continue; | |
2812 | if ((change->args[ch_arg].u.member = GetUserMode(channel, victim))) | |
2813 | { | |
2814 | /* Apply the oplevel change */ | |
2815 | change->args[ch_arg].u.member->oplevel = oplevel; | |
2816 | ch_arg++; | |
2817 | } | |
2818 | break; | |
2819 | } | |
2820 | default: | |
2821 | if (!(flags & MCP_FROM_SERVER)) | |
2822 | goto error; | |
2823 | break; | |
2824 | } | |
2825 | } | |
2826 | change->argc = ch_arg; /* in case any turned out to be ignored */ | |
2827 | if (change->modes_set & MODE_SECRET) { | |
2828 | change->modes_set &= ~(MODE_PRIVATE); | |
2829 | change->modes_clear |= MODE_PRIVATE; | |
2830 | } else if (change->modes_set & MODE_PRIVATE) { | |
2831 | change->modes_set &= ~(MODE_SECRET); | |
2832 | change->modes_clear |= MODE_SECRET; | |
2833 | } | |
2834 | return change; | |
2835 | error: | |
2836 | mod_chanmode_free(change); | |
2837 | return NULL; | |
2838 | } | |
2839 | ||
2840 | struct chanmode_buffer { | |
2841 | char modes[MAXLEN]; | |
2842 | char args[MAXLEN]; | |
2843 | struct chanNode *channel; | |
2844 | struct userNode *actor; | |
2845 | unsigned int modes_used; | |
2846 | unsigned int args_used; | |
2847 | size_t chname_len; | |
2848 | unsigned int is_add : 1; | |
2849 | unsigned int is_chanop : 1; | |
2850 | }; | |
2851 | ||
2852 | static void | |
2853 | mod_chanmode_append(struct chanmode_buffer *buf, char ch, const char *arg) | |
2854 | { | |
2855 | size_t arg_len = strlen(arg); | |
2856 | if (buf->modes_used > (MAXMODEPARAMS) || | |
2857 | buf->modes_used + buf->args_used + buf->chname_len + arg_len > 450) { | |
2858 | memcpy(buf->modes + buf->modes_used, buf->args, buf->args_used); | |
2859 | buf->modes[buf->modes_used + buf->args_used] = '\0'; | |
2860 | irc_mode((buf->is_chanop ? buf->actor : NULL), buf->channel, buf->modes); | |
2861 | buf->modes[0] = buf->is_add ? '+' : '-'; | |
2862 | buf->modes_used = 1; | |
2863 | buf->args_used = 0; | |
2864 | } | |
2865 | buf->modes[buf->modes_used++] = ch; | |
2866 | buf->args[buf->args_used++] = ' '; | |
2867 | memcpy(buf->args + buf->args_used, arg, arg_len); | |
2868 | buf->args_used += arg_len; | |
2869 | } | |
2870 | ||
2871 | void | |
2872 | mod_chanmode_announce(struct userNode *who, struct chanNode *channel, struct mod_chanmode *change) | |
2873 | { | |
2874 | struct chanmode_buffer chbuf; | |
2875 | unsigned int arg; | |
2876 | struct modeNode *mn; | |
2877 | char int_buff[32], mode = '\0'; | |
2878 | ||
2879 | assert(change->argc <= change->alloc_argc); | |
2880 | memset(&chbuf, 0, sizeof(chbuf)); | |
2881 | chbuf.channel = channel; | |
2882 | chbuf.actor = who; | |
2883 | chbuf.chname_len = strlen(channel->name); | |
2884 | ||
2885 | mn = GetUserMode(channel, who); | |
2886 | if ((mn && (mn->modes & MODE_CHANOP)) || off_channel) | |
2887 | chbuf.is_chanop = 1; | |
2888 | ||
2889 | /* First remove modes */ | |
2890 | chbuf.is_add = 0; | |
2891 | if (change->modes_clear) { | |
2892 | if (mode != '-') | |
2893 | chbuf.modes[chbuf.modes_used++] = mode = '-'; | |
2894 | #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR | |
2895 | DO_MODE_CHAR(PRIVATE, 'p'); | |
2896 | DO_MODE_CHAR(SECRET, 's'); | |
2897 | DO_MODE_CHAR(MODERATED, 'm'); | |
2898 | DO_MODE_CHAR(TOPICLIMIT, 't'); | |
2899 | DO_MODE_CHAR(INVITEONLY, 'i'); | |
2900 | DO_MODE_CHAR(NOPRIVMSGS, 'n'); | |
2901 | DO_MODE_CHAR(LIMIT, 'l'); | |
2902 | DO_MODE_CHAR(DELAYJOINS, 'D'); | |
2903 | DO_MODE_CHAR(REGONLY, 'r'); | |
2904 | DO_MODE_CHAR(NOCOLORS, 'c'); | |
2905 | DO_MODE_CHAR(NOCTCPS, 'C'); | |
2906 | DO_MODE_CHAR(STRIPCOLOR, 'S'); | |
2907 | DO_MODE_CHAR(MODUNREG, 'M'); | |
2908 | DO_MODE_CHAR(NONOTICE, 'N'); | |
2909 | DO_MODE_CHAR(NOQUITMSGS, 'Q'); | |
2910 | DO_MODE_CHAR(NOAMSG, 'T'); | |
2911 | DO_MODE_CHAR(OPERSONLY, 'O'); | |
2912 | DO_MODE_CHAR(REGISTERED, 'z'); | |
2913 | DO_MODE_CHAR(SSLONLY, 'Z'); | |
2914 | DO_MODE_CHAR(HIDEMODE, 'L'); | |
2915 | #undef DO_MODE_CHAR | |
2916 | if (change->modes_clear & channel->modes & MODE_KEY) | |
2917 | mod_chanmode_append(&chbuf, 'k', channel->key); | |
2918 | if (change->modes_clear & channel->modes & MODE_UPASS) | |
2919 | mod_chanmode_append(&chbuf, 'U', channel->upass); | |
2920 | if (change->modes_clear * channel->modes & MODE_APASS) | |
2921 | mod_chanmode_append(&chbuf, 'A', channel->apass); | |
2922 | } | |
2923 | for (arg = 0; arg < change->argc; ++arg) { | |
2924 | if (!(change->args[arg].mode & MODE_REMOVE)) | |
2925 | continue; | |
2926 | if (mode != '-') | |
2927 | chbuf.modes[chbuf.modes_used++] = mode = '-'; | |
2928 | switch (change->args[arg].mode & ~MODE_REMOVE) { | |
2929 | case MODE_BAN: | |
2930 | mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask); | |
2931 | break; | |
2932 | case MODE_EXEMPT: | |
2933 | mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask); | |
2934 | break; | |
2935 | default: | |
2936 | if (change->args[arg].mode & MODE_CHANOP) | |
2937 | mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric); | |
2938 | if (change->args[arg].mode & MODE_HALFOP) | |
2939 | mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric); | |
2940 | if (change->args[arg].mode & MODE_VOICE) | |
2941 | mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric); | |
2942 | break; | |
2943 | } | |
2944 | } | |
2945 | ||
2946 | /* Then set them */ | |
2947 | chbuf.is_add = 1; | |
2948 | if (change->modes_set) { | |
2949 | if (mode != '+') | |
2950 | chbuf.modes[chbuf.modes_used++] = mode = '+'; | |
2951 | #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR | |
2952 | DO_MODE_CHAR(PRIVATE, 'p'); | |
2953 | DO_MODE_CHAR(SECRET, 's'); | |
2954 | DO_MODE_CHAR(MODERATED, 'm'); | |
2955 | DO_MODE_CHAR(TOPICLIMIT, 't'); | |
2956 | DO_MODE_CHAR(INVITEONLY, 'i'); | |
2957 | DO_MODE_CHAR(NOPRIVMSGS, 'n'); | |
2958 | DO_MODE_CHAR(DELAYJOINS, 'D'); | |
2959 | DO_MODE_CHAR(REGONLY, 'r'); | |
2960 | DO_MODE_CHAR(NOCOLORS, 'c'); | |
2961 | DO_MODE_CHAR(NOCTCPS, 'C'); | |
2962 | DO_MODE_CHAR(STRIPCOLOR, 'S'); | |
2963 | DO_MODE_CHAR(MODUNREG, 'M'); | |
2964 | DO_MODE_CHAR(NONOTICE, 'N'); | |
2965 | DO_MODE_CHAR(NOQUITMSGS, 'Q'); | |
2966 | DO_MODE_CHAR(NOAMSG, 'T'); | |
2967 | DO_MODE_CHAR(OPERSONLY, 'O'); | |
2968 | DO_MODE_CHAR(REGISTERED, 'z'); | |
2969 | DO_MODE_CHAR(SSLONLY, 'Z'); | |
2970 | DO_MODE_CHAR(HIDEMODE, 'L'); | |
2971 | #undef DO_MODE_CHAR | |
2972 | if(change->modes_set & MODE_KEY) | |
2973 | mod_chanmode_append(&chbuf, 'k', change->new_key); | |
2974 | if (change->modes_set & MODE_UPASS) | |
2975 | mod_chanmode_append(&chbuf, 'U', change->new_upass); | |
2976 | if (change->modes_set & MODE_APASS) | |
2977 | mod_chanmode_append(&chbuf, 'A', change->new_apass); | |
2978 | if(change->modes_set & MODE_LIMIT) { | |
2979 | sprintf(int_buff, "%d", change->new_limit); | |
2980 | mod_chanmode_append(&chbuf, 'l', int_buff); | |
2981 | } | |
2982 | } | |
2983 | for (arg = 0; arg < change->argc; ++arg) { | |
2984 | if (change->args[arg].mode & MODE_REMOVE) | |
2985 | continue; | |
2986 | if (mode != '+') | |
2987 | chbuf.modes[chbuf.modes_used++] = mode = '+'; | |
2988 | switch (change->args[arg].mode) { | |
2989 | case MODE_BAN: | |
2990 | mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask); | |
2991 | break; | |
2992 | case MODE_EXEMPT: | |
2993 | mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask); | |
2994 | break; | |
2995 | default: | |
2996 | if (change->args[arg].mode & MODE_CHANOP) | |
2997 | mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric); | |
2998 | if (change->args[arg].mode & MODE_HALFOP) | |
2999 | mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric); | |
3000 | if (change->args[arg].mode & MODE_VOICE) | |
3001 | mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric); | |
3002 | break; | |
3003 | } | |
3004 | } | |
3005 | ||
3006 | /* Flush the buffer and apply changes locally */ | |
3007 | if (chbuf.modes_used > 0) { | |
3008 | memcpy(chbuf.modes + chbuf.modes_used, chbuf.args, chbuf.args_used); | |
3009 | chbuf.modes[chbuf.modes_used + chbuf.args_used] = '\0'; | |
3010 | irc_mode((chbuf.is_chanop ? chbuf.actor : NULL), chbuf.channel, chbuf.modes); | |
3011 | } | |
3012 | mod_chanmode_apply(who, channel, change); | |
3013 | } | |
3014 | ||
3015 | char * | |
3016 | mod_chanmode_format(struct mod_chanmode *change, char *outbuff) | |
3017 | { | |
3018 | unsigned int used = 0; | |
3019 | assert(change->argc <= change->alloc_argc); | |
3020 | if (change->modes_clear) { | |
3021 | outbuff[used++] = '-'; | |
3022 | #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) outbuff[used++] = CHAR | |
3023 | DO_MODE_CHAR(PRIVATE, 'p'); | |
3024 | DO_MODE_CHAR(SECRET, 's'); | |
3025 | DO_MODE_CHAR(MODERATED, 'm'); | |
3026 | DO_MODE_CHAR(TOPICLIMIT, 't'); | |
3027 | DO_MODE_CHAR(INVITEONLY, 'i'); | |
3028 | DO_MODE_CHAR(NOPRIVMSGS, 'n'); | |
3029 | DO_MODE_CHAR(LIMIT, 'l'); | |
3030 | DO_MODE_CHAR(KEY, 'k'); | |
3031 | DO_MODE_CHAR(UPASS, 'U'); | |
3032 | DO_MODE_CHAR(APASS, 'A'); | |
3033 | DO_MODE_CHAR(DELAYJOINS, 'D'); | |
3034 | DO_MODE_CHAR(REGONLY, 'r'); | |
3035 | DO_MODE_CHAR(NOCOLORS, 'c'); | |
3036 | DO_MODE_CHAR(NOCTCPS, 'C'); | |
3037 | DO_MODE_CHAR(STRIPCOLOR, 'S'); | |
3038 | DO_MODE_CHAR(MODUNREG, 'M'); | |
3039 | DO_MODE_CHAR(NONOTICE, 'N'); | |
3040 | DO_MODE_CHAR(NOQUITMSGS, 'Q'); | |
3041 | DO_MODE_CHAR(NOAMSG, 'T'); | |
3042 | DO_MODE_CHAR(OPERSONLY, 'O'); | |
3043 | DO_MODE_CHAR(REGISTERED, 'z'); | |
3044 | DO_MODE_CHAR(SSLONLY, 'Z'); | |
3045 | DO_MODE_CHAR(HIDEMODE, 'L'); | |
3046 | #undef DO_MODE_CHAR | |
3047 | } | |
3048 | if (change->modes_set) { | |
3049 | outbuff[used++] = '+'; | |
3050 | #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) outbuff[used++] = CHAR | |
3051 | DO_MODE_CHAR(PRIVATE, 'p'); | |
3052 | DO_MODE_CHAR(SECRET, 's'); | |
3053 | DO_MODE_CHAR(MODERATED, 'm'); | |
3054 | DO_MODE_CHAR(TOPICLIMIT, 't'); | |
3055 | DO_MODE_CHAR(INVITEONLY, 'i'); | |
3056 | DO_MODE_CHAR(NOPRIVMSGS, 'n'); | |
3057 | DO_MODE_CHAR(DELAYJOINS, 'D'); | |
3058 | DO_MODE_CHAR(REGONLY, 'r'); | |
3059 | DO_MODE_CHAR(NOCOLORS, 'c'); | |
3060 | DO_MODE_CHAR(NOCTCPS, 'C'); | |
3061 | DO_MODE_CHAR(STRIPCOLOR, 'S'); | |
3062 | DO_MODE_CHAR(MODUNREG, 'M'); | |
3063 | DO_MODE_CHAR(NONOTICE, 'N'); | |
3064 | DO_MODE_CHAR(NOQUITMSGS, 'Q'); | |
3065 | DO_MODE_CHAR(NOAMSG, 'T'); | |
3066 | DO_MODE_CHAR(OPERSONLY, 'O'); | |
3067 | DO_MODE_CHAR(REGISTERED, 'z'); | |
3068 | DO_MODE_CHAR(SSLONLY, 'Z'); | |
3069 | DO_MODE_CHAR(HIDEMODE, 'L'); | |
3070 | #undef DO_MODE_CHAR | |
3071 | switch (change->modes_set & (MODE_KEY|MODE_LIMIT|MODE_APASS|MODE_UPASS)) { | |
3072 | /* Doing this implementation has been a pain in the arse, I hope I didn't forget a possible combination */ | |
3073 | case MODE_KEY|MODE_LIMIT|MODE_APASS|MODE_UPASS: | |
3074 | used += sprintf(outbuff+used, "lkAU %d %s %s %s", change->new_limit, change->new_key,change->new_apass, change->new_upass); | |
3075 | break; | |
3076 | ||
3077 | case MODE_KEY|MODE_LIMIT|MODE_APASS: | |
3078 | used += sprintf(outbuff+used, "lkA %d %s %s", change->new_limit, change->new_key, change->new_apass); | |
3079 | break; | |
3080 | case MODE_KEY|MODE_LIMIT|MODE_UPASS: | |
3081 | used += sprintf(outbuff+used, "lkU %d %s %s", change->new_limit, change->new_key, change->new_upass); | |
3082 | break; | |
3083 | case MODE_KEY|MODE_APASS|MODE_UPASS: | |
3084 | used += sprintf(outbuff+used, "kAU %s %s %s", change->new_key, change->new_apass, change->new_upass); | |
3085 | break; | |
3086 | ||
3087 | case MODE_KEY|MODE_APASS: | |
3088 | used += sprintf(outbuff+used, "kA %s %s", change->new_key, change->new_apass); | |
3089 | break; | |
3090 | case MODE_KEY|MODE_UPASS: | |
3091 | used += sprintf(outbuff+used, "kU %s %s", change->new_key, change->new_upass); | |
3092 | break; | |
3093 | case MODE_KEY|MODE_LIMIT: | |
3094 | used += sprintf(outbuff+used, "lk %d %s", change->new_limit, change->new_key); | |
3095 | break; | |
3096 | ||
3097 | case MODE_LIMIT|MODE_UPASS: | |
3098 | used += sprintf(outbuff+used, "lU %d %s", change->new_limit, change->new_upass); | |
3099 | break; | |
3100 | case MODE_LIMIT|MODE_APASS: | |
3101 | used += sprintf(outbuff+used, "lA %d %s", change->new_limit, change->new_apass); | |
3102 | break; | |
3103 | case MODE_APASS|MODE_UPASS: | |
3104 | used += sprintf(outbuff+used, "AU %s %s", change->new_apass, change->new_upass); | |
3105 | ||
3106 | case MODE_LIMIT|MODE_APASS|MODE_UPASS: | |
3107 | used += sprintf(outbuff+used, "lAU %d %s %s", change->new_limit, change->new_apass, change->new_upass); | |
3108 | break; | |
3109 | ||
3110 | case MODE_APASS: | |
3111 | used += sprintf(outbuff+used, "A %s", change->new_apass); | |
3112 | break; | |
3113 | case MODE_UPASS: | |
3114 | used += sprintf(outbuff+used, "U %s", change->new_upass); | |
3115 | break; | |
3116 | case MODE_KEY: | |
3117 | used += sprintf(outbuff+used, "k %s", change->new_key); | |
3118 | break; | |
3119 | case MODE_LIMIT: | |
3120 | used += sprintf(outbuff+used, "l %d", change->new_limit); | |
3121 | break; | |
3122 | } | |
3123 | } | |
3124 | outbuff[used] = 0; | |
3125 | return outbuff; | |
3126 | } | |
3127 | ||
3128 | static int | |
3129 | clear_chanmode(struct chanNode *channel, const char *modes) | |
3130 | { | |
3131 | unsigned int remove; | |
3132 | ||
3133 | for (remove = 0; *modes; modes++) { | |
3134 | switch (*modes) { | |
3135 | case 'o': remove |= MODE_CHANOP; break; | |
3136 | case 'h': remove |= MODE_HALFOP; break; | |
3137 | case 'v': remove |= MODE_VOICE; break; | |
3138 | case 'p': remove |= MODE_PRIVATE; break; | |
3139 | case 's': remove |= MODE_SECRET; break; | |
3140 | case 'm': remove |= MODE_MODERATED; break; | |
3141 | case 't': remove |= MODE_TOPICLIMIT; break; | |
3142 | case 'i': remove |= MODE_INVITEONLY; break; | |
3143 | case 'n': remove |= MODE_NOPRIVMSGS; break; | |
3144 | case 'k': | |
3145 | remove |= MODE_KEY; | |
3146 | channel->key[0] = '\0'; | |
3147 | break; | |
3148 | case 'A': | |
3149 | remove |= MODE_APASS; | |
3150 | channel->apass[0] = '\0'; | |
3151 | break; | |
3152 | case 'U': | |
3153 | remove |= MODE_UPASS; | |
3154 | channel->upass[0] = '\0'; | |
3155 | break; | |
3156 | case 'l': | |
3157 | remove |= MODE_LIMIT; | |
3158 | channel->limit = 0; | |
3159 | break; | |
3160 | case 'b': remove |= MODE_BAN; break; | |
3161 | case 'e': remove |= MODE_EXEMPT; break; | |
3162 | case 'D': remove |= MODE_DELAYJOINS; break; | |
3163 | case 'r': remove |= MODE_REGONLY; break; | |
3164 | case 'c': remove |= MODE_NOCOLORS; break; | |
3165 | case 'C': remove |= MODE_NOCTCPS; break; | |
3166 | case 'S': remove |= MODE_STRIPCOLOR; break; | |
3167 | case 'M': remove |= MODE_MODUNREG; break; | |
3168 | case 'N': remove |= MODE_NONOTICE; break; | |
3169 | case 'Q': remove |= MODE_NOQUITMSGS; break; | |
3170 | case 'T': remove |= MODE_NOAMSG; break; | |
3171 | case 'O': remove |= MODE_OPERSONLY; break; | |
3172 | case 'z': remove |= MODE_REGISTERED; break; | |
3173 | case 'Z': remove |= MODE_SSLONLY; break; | |
3174 | case 'L': remove |= MODE_HIDEMODE; break; | |
3175 | } | |
3176 | } | |
3177 | ||
3178 | if (!remove) | |
3179 | return 1; | |
3180 | ||
3181 | /* Remove simple modes. */ | |
3182 | channel->modes &= ~remove; | |
3183 | ||
3184 | /* If removing bans, kill 'em all. */ | |
3185 | if ((remove & MODE_BAN) && channel->banlist.used) { | |
3186 | unsigned int i; | |
3187 | for (i=0; i<channel->banlist.used; i++) | |
3188 | free(channel->banlist.list[i]); | |
3189 | channel->banlist.used = 0; | |
3190 | } | |
3191 | ||
3192 | /* If removing exempts, kill 'em all. */ | |
3193 | if ((remove & MODE_EXEMPT) && channel->exemptlist.used) { | |
3194 | unsigned int i; | |
3195 | for (i=0; i<channel->exemptlist.used; i++) | |
3196 | free(channel->exemptlist.list[i]); | |
3197 | channel->exemptlist.used = 0; | |
3198 | } | |
3199 | ||
3200 | /* Remove member modes. */ | |
3201 | if ((remove & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE)) && channel->members.used) { | |
3202 | int mask = ~(remove & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE)); | |
3203 | unsigned int i; | |
3204 | ||
3205 | for (i = 0; i < channel->members.used; i++) | |
3206 | channel->members.list[i]->modes &= mask; | |
3207 | } | |
3208 | ||
3209 | return 1; | |
3210 | } | |
3211 | ||
3212 | void | |
3213 | reg_privmsg_func(struct userNode *user, privmsg_func_t handler) | |
3214 | { | |
3215 | unsigned int numeric = user->num_local; | |
3216 | if (numeric >= num_privmsg_funcs) { | |
3217 | int newnum = numeric + 8; | |
3218 | privmsg_funcs = realloc(privmsg_funcs, newnum*sizeof(privmsg_func_t)); | |
3219 | memset(privmsg_funcs+num_privmsg_funcs, 0, (newnum-num_privmsg_funcs)*sizeof(privmsg_func_t)); | |
3220 | num_privmsg_funcs = newnum; | |
3221 | } | |
3222 | if (privmsg_funcs[numeric]) | |
3223 | log_module(MAIN_LOG, LOG_WARNING, "re-registering new privmsg handler for numeric %d", numeric); | |
3224 | privmsg_funcs[numeric] = handler; | |
3225 | } | |
3226 | ||
3227 | void | |
3228 | unreg_privmsg_func(struct userNode *user, privmsg_func_t handler) | |
3229 | { | |
3230 | unsigned int x; | |
3231 | ||
3232 | if (!user || handler) | |
3233 | return; /* this really only works with users */ | |
3234 | ||
3235 | memset(privmsg_funcs+user->num_local, 0, sizeof(privmsg_func_t)); | |
3236 | ||
3237 | for (x = user->num_local+1; x < num_privmsg_funcs; x++) | |
3238 | memmove(privmsg_funcs+x-1, privmsg_funcs+x, sizeof(privmsg_func_t)); | |
3239 | ||
3240 | privmsg_funcs = realloc(privmsg_funcs, num_privmsg_funcs*sizeof(privmsg_func_t)); | |
3241 | num_privmsg_funcs--; | |
3242 | } | |
3243 | ||
3244 | ||
3245 | void | |
3246 | reg_notice_func(struct userNode *user, privmsg_func_t handler) | |
3247 | { | |
3248 | unsigned int numeric = user->num_local; | |
3249 | if (numeric >= num_notice_funcs) { | |
3250 | int newnum = numeric + 8; | |
3251 | notice_funcs = realloc(notice_funcs, newnum*sizeof(privmsg_func_t)); | |
3252 | memset(notice_funcs+num_notice_funcs, 0, (newnum-num_notice_funcs)*sizeof(privmsg_func_t)); | |
3253 | num_notice_funcs = newnum; | |
3254 | } | |
3255 | if (notice_funcs[numeric]) | |
3256 | log_module(MAIN_LOG, LOG_WARNING, "re-registering new notice handler for numeric %d", numeric); | |
3257 | notice_funcs[numeric] = handler; | |
3258 | } | |
3259 | ||
3260 | void | |
3261 | unreg_notice_func(struct userNode *user, privmsg_func_t handler) | |
3262 | { | |
3263 | unsigned int x; | |
3264 | ||
3265 | if (!user || handler) | |
3266 | return; /* this really only works with users */ | |
3267 | ||
3268 | memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t)); | |
3269 | ||
3270 | for (x = user->num_local+1; x < num_notice_funcs; x++) | |
3271 | memmove(notice_funcs+x-1, notice_funcs+x, sizeof(privmsg_func_t)); | |
3272 | ||
3273 | memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t)); | |
3274 | notice_funcs = realloc(notice_funcs, num_notice_funcs*sizeof(privmsg_func_t)); | |
3275 | num_notice_funcs--; | |
3276 | } | |
3277 | ||
3278 | void | |
3279 | reg_oper_func(oper_func_t handler) | |
3280 | { | |
3281 | if (of_used == of_size) { | |
3282 | if (of_size) { | |
3283 | of_size <<= 1; | |
3284 | of_list = realloc(of_list, of_size*sizeof(oper_func_t)); | |
3285 | } else { | |
3286 | of_size = 8; | |
3287 | of_list = malloc(of_size*sizeof(oper_func_t)); | |
3288 | } | |
3289 | } | |
3290 | of_list[of_used++] = handler; | |
3291 | } | |
3292 | ||
3293 | static void | |
3294 | call_oper_funcs(struct userNode *user) | |
3295 | { | |
3296 | unsigned int n; | |
3297 | if (IsLocal(user)) | |
3298 | return; | |
3299 | for (n=0; n<of_used; n++) | |
3300 | of_list[n](user); | |
3301 | } | |
3302 | ||
3303 | static void | |
3304 | send_burst(void) | |
3305 | { | |
3306 | unsigned int i, hop, max_hop=1; | |
3307 | dict_iterator_t it; | |
3308 | ||
3309 | /* burst (juped) servers, closest first (except self, which is sent already) */ | |
3310 | for (i=0; i<ArrayLength(servers_num); i++) | |
3311 | if (servers_num[i] && servers_num[i]->hops > max_hop) | |
3312 | max_hop = servers_num[i]->hops; | |
3313 | for (hop=1; hop<=max_hop; hop++) { | |
3314 | for (i=0;i<ArrayLength(servers_num);i++) { | |
3315 | if (servers_num[i] | |
3316 | && (servers_num[i]->hops == hop) | |
3317 | && (servers_num[i] != self->uplink)) | |
3318 | irc_server(servers_num[i]); | |
3319 | } | |
3320 | } | |
3321 | ||
3322 | /* burst local nicks */ | |
3323 | for (i=0; i<=self->num_mask; i++) | |
3324 | if (self->users[i]) | |
3325 | irc_user(self->users[i]); | |
3326 | ||
3327 | /* build dict of unbursted channel names (just copy existing channels) */ | |
3328 | unbursted_channels = dict_new(); | |
3329 | for (it = dict_first(channels); it; it = iter_next(it)) | |
3330 | dict_insert(unbursted_channels, iter_key(it), iter_data(it)); | |
3331 | } | |
3332 | ||
3333 | /* | |
3334 | * Oplevel parsing | |
3335 | */ | |
3336 | static int | |
3337 | parse_oplevel(char *str) | |
3338 | { | |
3339 | int oplevel = 0; | |
3340 | while (isdigit(*str)) | |
3341 | oplevel = oplevel * 10 + *str++ - '0'; | |
3342 | return oplevel; | |
3343 | } | |
3344 |