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