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