]> jfr.im git - irc/evilnet/x3.git/blob - src/proto-p10.c
Fixing small bug in x3.conf.example
[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
907 int type = 4;
908 const char *str;
909 str = conf_get_data("server/type", RECDB_QSTRING);
910 type = atoi(str);
911
912 if (type == 5) {
913 putsock("%s " P10_TOPIC " %s %s " FMT_TIME_T " " FMT_TIME_T " :%s", service->numeric, what->name, who->nick, what->timestamp, now, topic);
914 } else {
915 who = service; /* REMOVE LINE FOR NEFARIOUS 0.5.0 */
916 putsock("%s " P10_TOPIC " %s :%s", who->numeric, what->name, topic);
917 }
918 }
919
920 void
921 irc_raw(const char *what)
922 {
923 putsock("%s", what);
924 }
925
926 void
927 irc_numeric(struct userNode *user, unsigned int num, const char *format, ...)
928 {
929 va_list arg_list;
930 char buffer[MAXLEN];
931 va_start(arg_list, format);
932 vsnprintf(buffer, MAXLEN-2, format, arg_list);
933 buffer[MAXLEN-1] = 0;
934 putsock(":%s %03d %s %s", self->name, num, user->nick, buffer);
935 }
936
937 static void send_burst(void);
938
939 static void
940 change_nicklen(int new_nicklen)
941 {
942 unsigned int nn;
943 char new_nick[NICKLEN+1];
944 struct userNode *user;
945
946 nicklen = new_nicklen;
947 /* fix up any users we have here */
948 for (nn=0; nn<=self->num_mask; nn++) {
949 if (!(user = self->users[nn]))
950 continue;
951 safestrncpy(new_nick, user->nick, sizeof(new_nick));
952 new_nick[nicklen] = 0;
953 NickChange(user, new_nick, 1);
954 }
955 }
956
957 static CMD_FUNC(cmd_whois)
958 {
959 struct userNode *from;
960 struct userNode *who;
961
962 if (argc < 3)
963 return 0;
964 if (!(from = GetUserH(origin))) {
965 log_module(MAIN_LOG, LOG_ERROR, "Could not find WHOIS origin user %s", origin);
966 return 0;
967 }
968 if(!(who = GetUserH(argv[2]))) {
969 irc_numeric(from, ERR_NOSUCHNICK, "%s@%s :No such nick", argv[2], self->name);
970 return 1;
971 }
972 if (IsHiddenHost(who) && !IsOper(from)) {
973 /* Just stay quiet. */
974 return 1;
975 }
976 irc_numeric(from, RPL_WHOISUSER, "%s %s %s * :%s", who->nick, who->ident, who->hostname, who->info);
977 if (his_servername && his_servercomment)
978 irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, his_servername, his_servercomment);
979 else
980 irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, who->uplink->name, who->uplink->description);
981 if (IsOper(who))
982 irc_numeric(from, RPL_WHOISOPERATOR, "%s :is a megalomaniacal power hungry tyrant", who->nick);
983 irc_numeric(from, RPL_ENDOFWHOIS, "%s :End of /WHOIS list", who->nick);
984 return 1;
985 }
986
987 static CMD_FUNC(cmd_server)
988 {
989 struct server *srv;
990 const char *str;
991
992 if (argc < 8)
993 return 0;
994 if (self->uplink) {
995 /* another server introduced us */
996 srv = AddServer(GetServerH(origin), argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
997 if (!srv)
998 return 0;
999 srv->self_burst = argv[5][0] == 'J';
1000 srv->burst = 1;
1001 } else {
1002 /* this must be our uplink */
1003 srv = self->uplink = AddServer(self, argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
1004 if (!srv)
1005 return 0;
1006 srv->self_burst = argv[5][0] == 'J';
1007 srv->burst = 1;
1008 if ((argv[7][0] == '+') && !force_n2k) {
1009 log_module(MAIN_LOG, LOG_WARNING, "Got Undernet-style SERVER message but \"force_n2k\" not on.");
1010 }
1011 send_burst();
1012 }
1013
1014 /* Fix up our timestamps if necessary. */
1015 if (srv->boot <= PREHISTORY) {
1016 /* Server from the mists of time.. */
1017 if (srv->hops == 1) {
1018 log_module(MAIN_LOG, LOG_ERROR, "Server %s claims to have booted at time "FMT_TIME_T". This is absurd.", srv->name, srv->boot);
1019 }
1020 } else if ((str = conf_get_data("server/reliable_clock", RECDB_QSTRING))
1021 && enabled_string(str)) {
1022 /* If we have a reliable clock, we just keep our current time. */
1023 } else {
1024 if (srv->boot <= self->boot) {
1025 /* The other server is older than us. Accept their timestamp.
1026 * Alternately, we are same age, but we accept their time
1027 * since we are linking to them. */
1028 self->boot = srv->boot;
1029 ioset_set_time(srv->link);
1030 }
1031 }
1032 if (srv == self->uplink) {
1033 extern time_t burst_begin;
1034 burst_begin = now;
1035 }
1036 return 1;
1037 }
1038
1039 static CMD_FUNC(cmd_eob)
1040 {
1041 struct server *sender;
1042 dict_iterator_t it;
1043 unsigned int ii;
1044
1045 if (!(sender = GetServerH(origin)))
1046 return 0;
1047 if (sender == self->uplink) {
1048 cManager.uplink->state = CONNECTED;
1049 for (it = dict_first(unbursted_channels); it; it = iter_next(it))
1050 irc_burst(iter_data(it));
1051 dict_delete(unbursted_channels);
1052 unbursted_channels = NULL;
1053 irc_eob();
1054 irc_eob_ack();
1055 }
1056 sender->self_burst = 0;
1057 recalc_bursts(sender);
1058 for (ii=0; ii<slf_used; ii++)
1059 slf_list[ii](sender);
1060 return 1;
1061 }
1062
1063 static CMD_FUNC(cmd_eob_ack)
1064 {
1065 extern time_t burst_begin;
1066
1067 if (GetServerH(origin) == self->uplink) {
1068 burst_length = now - burst_begin;
1069 self->self_burst = self->burst = 0;
1070 }
1071 cManager.uplink->state = CONNECTED;
1072 return 1;
1073 }
1074
1075 static CMD_FUNC(cmd_ping)
1076 {
1077 struct server *srv;
1078 struct userNode *un;
1079
1080 if (argc > 3)
1081 irc_pong_asll(argv[2], argv[3]);
1082 else if ((srv = GetServerH(origin)))
1083 irc_pong(self->name, srv->numeric);
1084 else if ((un = GetUserH(origin)))
1085 irc_pong(self->name, un->numeric);
1086 else
1087 irc_pong(self->name, origin);
1088
1089 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1090 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1091 timeq_add(now + ping_freq, timed_send_ping, 0);
1092 received_ping();
1093 return 1;
1094 }
1095
1096 static CMD_FUNC(cmd_error_nick)
1097 {
1098 /* Go back to original IRC length .. and try to reconnect :/ */
1099 change_nicklen(9);
1100 irc_squit(self, "Got erroneous nickname, truncating nicks.", NULL);
1101 return 1;
1102 }
1103
1104 struct create_desc {
1105 struct userNode *user;
1106 time_t when;
1107 };
1108
1109 static void
1110 join_helper(struct chanNode *chan, void *data)
1111 {
1112 struct create_desc *cd = data;
1113 AddChannelUser(cd->user, chan);
1114 }
1115
1116 static void
1117 create_helper(char *name, void *data)
1118 {
1119 struct create_desc *cd = data;
1120
1121 if (!strcmp(name, "0")) {
1122 while (cd->user->channels.used > 0)
1123 DelChannelUser(cd->user, cd->user->channels.list[0]->channel, 0, 0);
1124 return;
1125 }
1126
1127 AddChannelUser(cd->user, AddChannel(name, cd->when, NULL, NULL, NULL));
1128 }
1129
1130 static CMD_FUNC(cmd_create)
1131 {
1132 struct create_desc cd;
1133 struct userNode *user;
1134
1135 if ((argc < 3) || !(user = GetUserH(origin)))
1136 return 0;
1137 cd.user = user;
1138 cd.when = atoi(argv[2]);
1139 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
1140 return 1;
1141 }
1142
1143 static CMD_FUNC(cmd_join)
1144 {
1145 struct create_desc cd;
1146
1147 if (!(cd.user = GetUserH(origin)))
1148 return 0;
1149 if (argc < 2)
1150 return 0;
1151 else if (argc < 3)
1152 cd.when = now;
1153 else
1154 cd.when = atoi(argv[2]);
1155 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
1156 return 1;
1157 }
1158
1159 static CMD_FUNC(cmd_pong)
1160 {
1161 if (argc < 3)
1162 return 0;
1163 if (!strcmp(argv[2], self->name)) {
1164 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1165 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1166 timeq_add(now + ping_freq, timed_send_ping, 0);
1167 received_ping();
1168 }
1169 return 1;
1170 }
1171
1172 static CMD_FUNC(cmd_nick)
1173 {
1174 struct userNode *user;
1175 if ((user = GetUserH(origin))) {
1176 /* nick change (since the source is a user numeric) */
1177 if (argc < 2)
1178 return 0;
1179 NickChange(user, argv[1], 1);
1180 } else {
1181 struct server *serv;
1182 char modes[MAXLEN];
1183 /* new nick */
1184 if (argc < 9)
1185 return 0;
1186 serv = GetServerH(origin);
1187 if (argc > 9)
1188 unsplit_string(argv+6, argc-9, modes);
1189 else
1190 strcpy(modes, "+");
1191 AddUser(serv, argv[1], argv[4], argv[5], modes, argv[argc-2], argv[argc-1], atoi(argv[3]), argv[argc-3]);
1192 }
1193 return 1;
1194 }
1195
1196 static CMD_FUNC(cmd_account)
1197 {
1198 struct userNode *user;
1199 struct server *server;
1200 struct handle_info *hi;
1201
1202 if ((argc < 3) || !origin || !(server = GetServerH(origin)))
1203 return 0; /* Origin must be server. */
1204
1205 /* This next line appears to tremple origin.. why? */
1206 user = GetUserN(argv[1]);
1207 if (!user)
1208 return 1; /* A QUIT probably passed the ACCOUNT. */
1209
1210 if(!extended_accounts) /* any need for this function without? */
1211 return 1;
1212
1213 if(!strcmp(argv[2],"C"))
1214 {
1215 if((hi = loc_auth(argv[4], argv[5])))
1216 {
1217 /* Return a AC A */
1218 putsock("%s " P10_ACCOUNT " %s A %s %lu", self->numeric, server->numeric , argv[3], hi->registered);
1219
1220 }
1221 else
1222 {
1223 /* Return a AC D */
1224 putsock("%s " P10_ACCOUNT " %s D %s", self->numeric, server->numeric , argv[3]);
1225 }
1226 return 1;
1227 }
1228 else if(!strcmp(argv[2],"R"))
1229 call_account_func(user, argv[3]);
1230 else
1231 call_account_func(user, argv[2]); /* For backward compatability */
1232 return 1;
1233 }
1234
1235 static CMD_FUNC(cmd_fakehost)
1236 {
1237 struct userNode *user;
1238
1239 if ((argc < 3) || !origin || !GetServerH(origin))
1240 return 0;
1241 if (!(user = GetUserN(argv[1])))
1242 return 1;
1243 assign_fakehost(user, argv[2], 0);
1244 return 1;
1245 }
1246
1247 static CMD_FUNC(cmd_burst)
1248 {
1249 extern int rel_age;
1250 char modes[MAXLEN], *members = "";
1251 static char exemptlist[MAXLEN], banlist[MAXLEN];
1252 unsigned int next = 3, res = 1;
1253 int ctype = 0, echeck = 0, bcheck = 0;
1254 struct chanNode *cNode;
1255 struct userNode *un;
1256 struct modeNode *mNode;
1257 long mode;
1258 int oplevel = -1;
1259 char *user, *end, sep;
1260 time_t in_timestamp;
1261 char* parm = NULL;
1262
1263 if (argc < 3)
1264 return 0;
1265 modes[0] = 0;
1266
1267 exemptlist[0] = 0;
1268 banlist[0] = 0;
1269
1270 while (next < argc) {
1271 switch (argv[next][0]) {
1272 case '+': {
1273 const char *pos;
1274 int n_modes;
1275 for (pos=argv[next], n_modes = 1; *pos; pos++)
1276 if ((*pos == 'k') || (*pos == 'l') || (*pos == 'A')
1277 || (*pos == 'U'))
1278 n_modes++;
1279 unsplit_string(argv+next, n_modes, modes);
1280 next += n_modes;
1281 break;
1282 }
1283 case '%': {
1284 for(parm = mysep(&argv[next], " "); /* parm = first param */
1285 parm; /* While param is not null */
1286 parm = mysep(&argv[next], " ") /* parm = next param */
1287 )
1288 {
1289 switch (parm[0]) {
1290 case '%': {
1291 ctype = 1;
1292 break;
1293 }
1294 case '~': {
1295 ctype = 2;
1296 break;
1297 }
1298 default: {
1299 break;
1300 }
1301 }
1302 if (ctype == 1) {
1303 if (bcheck == 0) {
1304 /* strip % char off start of very first ban */
1305 if (strlen(parm) > 1) {
1306 strncat(banlist, strtok(parm, "%"), sizeof(banlist) - 1 - strlen(banlist));
1307 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1308 }
1309 bcheck = 1;
1310 } else {
1311 strncat(banlist, parm, sizeof(banlist) - 1 - strlen(banlist));
1312 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1313 }
1314 } else if (ctype == 2) {
1315 if (echeck == 0) {
1316 echeck = 1;
1317 } else {
1318 strncat(exemptlist, parm, sizeof(exemptlist) - 1 - strlen(exemptlist));
1319 strncat(exemptlist, " ", sizeof(exemptlist) - 1 - strlen(exemptlist));
1320 }
1321 }
1322 }
1323 next++;
1324 break;
1325 }
1326 default: members = argv[next++]; break;
1327 }
1328 }
1329
1330 in_timestamp = atoi(argv[2]);
1331 if ((cNode = dict_find(unbursted_channels, argv[1], NULL))) {
1332 cNode->timestamp = in_timestamp;
1333 dict_remove(unbursted_channels, cNode->name);
1334 irc_burst(cNode);
1335 }
1336 cNode = AddChannel(argv[1], in_timestamp, modes, banlist, exemptlist);
1337
1338 /* Burst channel members in now. */
1339 for (user = members, sep = *members, mode = 0; sep; user = end) {
1340 for (end = user + 3; isalnum(*end) || *end == '[' || *end == ']'; end++) ;
1341 sep = *end++; end[-1] = 0;
1342 if (sep == ':') {
1343 mode = 0;
1344 while ((sep = *end++)) {
1345 if (sep == 'o') {
1346 mode |= MODE_CHANOP;
1347 oplevel = -1;
1348 } else if (sep == 'h') {
1349 mode |= MODE_HALFOP;
1350 oplevel = -1;
1351 } else if (sep == 'v') {
1352 mode |= MODE_VOICE;
1353 oplevel = -1;
1354 } else if (isdigit(sep)) {
1355 mode |= MODE_CHANOP;
1356 if (oplevel >= 0)
1357 oplevel += parse_oplevel(end);
1358 else
1359 oplevel = parse_oplevel(end);
1360 while (isdigit(*end)) end++;
1361 } else
1362 break;
1363 }
1364 if (rel_age < 0)
1365 mode = 0;
1366 }
1367 if (!(un = GetUserN(user))) {
1368 res = 0;
1369 continue;
1370 }
1371 if ((mNode = AddChannelUser(un, cNode))) {
1372 mNode->modes = mode;
1373 mNode->oplevel = oplevel;
1374 }
1375 }
1376
1377 return res;
1378 }
1379
1380 static CMD_FUNC(cmd_mode)
1381 {
1382 struct chanNode *cn;
1383 struct userNode *un;
1384 char *sethost; // sethost - reed/apples
1385 int i; // sethost - reed/apples
1386
1387 if (argc < 3)
1388 return 0;
1389 if (!IsChannelName(argv[1])) {
1390 un = GetUserH(argv[1]);
1391 if (!un) {
1392 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose mode is changing.", argv[1]);
1393 return 0;
1394 }
1395 // sethost - reed/apples
1396 if (argc == 3)
1397 mod_usermode(un, argv[2]);
1398 else {
1399 sethost = malloc(strlen(argv[2]) + 1 + strlen(argv[3]) + 1);
1400 i = 0;
1401 while((sethost[i++] = *argv[2]++));
1402 i--;
1403 sethost[i++] = ' ';
1404 while((sethost[i++] = *argv[3]++));
1405 mod_usermode(un, sethost); // sethost - reed/apples
1406 }
1407
1408 return 1;
1409 }
1410
1411 if (!(cn = GetChannel(argv[1]))) {
1412 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1413 return 0;
1414 }
1415 if ((un = GetUserH(origin))) {
1416 struct modeNode *mn;
1417 /* Update idle time for person setting the mode */
1418 if ((mn = GetUserMode(cn, un)))
1419 mn->idle_since = now;
1420 } else {
1421 /* If it came from a server, reset timestamp to re-sync. */
1422 cn->timestamp = atoi(argv[argc-1]);
1423 }
1424
1425 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER|(un ? MC_NOTIFY : 0));
1426 }
1427
1428 static CMD_FUNC(cmd_opmode)
1429 {
1430 struct chanNode *cn;
1431 struct userNode *un;
1432
1433 if (argc < 3)
1434 return 0;
1435
1436 if (!(cn = GetChannel(argv[1]))) {
1437 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1438 return 0;
1439 }
1440 if (!(un = GetUserH(origin))) {
1441 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting OPMODE.", origin);
1442 return 0;
1443 }
1444 if (!IsOper(un)) {
1445 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using OPMODE.", un->nick);
1446 return 0;
1447 }
1448
1449 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER); /* do NOT announce opmode locally */
1450 }
1451
1452 static int clear_chanmode(struct chanNode *channel, const char *modes);
1453
1454 static CMD_FUNC(cmd_clearmode)
1455 {
1456 struct chanNode *cn;
1457 struct userNode *un;
1458
1459 if (argc < 3)
1460 return 0;
1461
1462 if (!(cn = GetChannel(argv[1]))) {
1463 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1464 return 0;
1465 }
1466 if (!(un = GetUserH(origin))) {
1467 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting CLEARMODE.", origin);
1468 return 0;
1469 }
1470 if (!IsOper(un)) {
1471 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using CLEARMODE.", un->nick);
1472 return 0;
1473 }
1474
1475 return clear_chanmode(cn, argv[2]);
1476 }
1477
1478 static CMD_FUNC(cmd_topic)
1479 {
1480 struct chanNode *cn;
1481 time_t chan_ts, topic_ts;
1482 struct userNode *user;
1483
1484 if (argc < 3)
1485 return 0;
1486 if (!(cn = GetChannel(argv[1]))) {
1487 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose topic is being set", argv[1]);
1488 return 0;
1489 }
1490
1491
1492 if (argc == 5) { /* Asuka / Topic Bursting IRCu's */
1493 user = GetUserH(origin);
1494 chan_ts = atoi(argv[2]);
1495 topic_ts = atoi(argv[3]);
1496 } else if (argc >= 6) { /* Nefarious 0.5.0 */
1497 user = GetUserH(strtok(argv[2], "!"));
1498 chan_ts = atoi(argv[3]);
1499 topic_ts = atoi(argv[4]);
1500 } else { /* Regular IRCu (No Topic Bursting)*/
1501 user = GetUserH(origin);
1502 chan_ts = cn->timestamp;
1503 topic_ts = now;
1504 }
1505
1506 SetChannelTopic(cn, user, user, argv[argc-1], 0);
1507 cn->topic_time = topic_ts;
1508 return 1;
1509 }
1510
1511 static CMD_FUNC(cmd_num_topic)
1512 {
1513 struct chanNode *cn;
1514
1515 if (!argv[0])
1516 return 0; /* huh? */
1517 if (argv[2]) {
1518 cn = GetChannel(argv[2]);
1519 if (!cn) {
1520 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s in topic reply", argv[2]);
1521 return 0;
1522 }
1523 } else
1524 return 0;
1525
1526 switch (atoi(argv[0])) {
1527 case 331:
1528 cn->topic_time = 0;
1529 break; /* no topic */
1530 case 332:
1531 if (argc < 4)
1532 return 0;
1533 safestrncpy(cn->topic, unsplit_string(argv+3, argc-3, NULL), sizeof(cn->topic));
1534 break;
1535 case 333:
1536 if (argc < 5)
1537 return 0;
1538 safestrncpy(cn->topic_nick, argv[3], sizeof(cn->topic_nick));
1539 cn->topic_time = atoi(argv[4]);
1540 break;
1541 default:
1542 return 0; /* should never happen */
1543 }
1544 return 1;
1545 }
1546
1547 static CMD_FUNC(cmd_num_gline)
1548 {
1549 if (argc < 6)
1550 return 0;
1551 gline_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0, 0);
1552 return 1;
1553 }
1554
1555 static CMD_FUNC(cmd_num_shun)
1556 {
1557 if (argc < 6)
1558 return 0;
1559 shun_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0);
1560 return 1;
1561 }
1562
1563 static CMD_FUNC(cmd_quit)
1564 {
1565 struct userNode *user;
1566 if (argc < 2)
1567 return 0;
1568 /* Sometimes we get a KILL then a QUIT or the like, so we don't want to
1569 * call DelUser unless we have the user in our grasp. */
1570 if ((user = GetUserH(origin)))
1571 DelUser(user, NULL, false, argv[1]);
1572 return 1;
1573 }
1574
1575 static CMD_FUNC(cmd_kill)
1576 {
1577 struct userNode *user;
1578 if (argc < 2)
1579 return 0;
1580 user = GetUserN(argv[1]);
1581 if (!user) {
1582 /* If we get a KILL for a non-existent user, it could be a
1583 * Ghost response to a KILL we sent out earlier. So we only
1584 * whine if the target is local.
1585 */
1586 if (!strncmp(argv[1], self->numeric, strlen(self->numeric)))
1587 log_module(MAIN_LOG, LOG_ERROR, "Unable to find kill victim %s", argv[1]);
1588 return 0;
1589 }
1590
1591 if (IsLocal(user) && IsService(user)) {
1592 /* TODO: rate limit this so silly things don't happen. */
1593 ReintroduceUser(user);
1594 return 1;
1595 }
1596
1597 DelUser(user, NULL, false, argv[2]);
1598 return 1;
1599 }
1600
1601 static CMD_FUNC(cmd_part)
1602 {
1603 struct userNode *user;
1604
1605 if (argc < 2)
1606 return 0;
1607 user = GetUserH(origin);
1608 if (!user)
1609 return 0;
1610 parse_foreach(argv[1], part_helper, NULL, NULL, NULL, user);
1611 return 1;
1612 }
1613
1614 static CMD_FUNC(cmd_kick)
1615 {
1616 if (argc < 3)
1617 return 0;
1618 ChannelUserKicked(GetUserH(origin), GetUserN(argv[2]), GetChannel(argv[1]));
1619 return 1;
1620 }
1621
1622 static CMD_FUNC(cmd_squit)
1623 {
1624 struct server *server;
1625
1626 if (argc < 4)
1627 return 0;
1628 if (!(server = GetServerH(argv[1])))
1629 return 0;
1630
1631 if (server == self->uplink) {
1632 /* Force a reconnect to the currently selected server. */
1633 cManager.uplink->tries = 0;
1634 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", argv[3]);
1635 close_socket();
1636 return 1;
1637 }
1638
1639 DelServer(server, 0, argv[3]);
1640 return 1;
1641 }
1642
1643 static CMD_FUNC(cmd_privmsg)
1644 {
1645 struct privmsg_desc pd;
1646 if (argc != 3)
1647 return 0;
1648 pd.user = GetUserH(origin);
1649 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
1650 return 1;
1651 pd.is_notice = 0;
1652 pd.text = argv[2];
1653 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
1654 return 1;
1655 }
1656
1657 static CMD_FUNC(cmd_notice)
1658 {
1659 struct privmsg_desc pd;
1660 if (argc != 3)
1661 return 0;
1662 pd.user = GetUserH(origin);
1663 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
1664 return 1;
1665 pd.is_notice = 1;
1666 pd.text = argv[2];
1667 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
1668 return 1;
1669 }
1670
1671 static CMD_FUNC(cmd_away)
1672 {
1673 struct userNode *uNode;
1674
1675 uNode = GetUserH(origin);
1676 if (!uNode)
1677 return 1;
1678 if (argc < 2)
1679 uNode->modes &= ~FLAGS_AWAY;
1680 else
1681 uNode->modes |= FLAGS_AWAY;
1682 return 1;
1683 }
1684
1685 static CMD_FUNC(cmd_gline)
1686 {
1687 if (argc < 3)
1688 return 0;
1689 if (argv[2][0] == '+') {
1690 if (argc < 5)
1691 return 0;
1692 gline_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0, 0);
1693 return 1;
1694 } else if (argv[2][0] == '-') {
1695 gline_remove(argv[2]+1, 0);
1696 return 1;
1697 } else
1698 return 0;
1699 }
1700
1701 static CMD_FUNC(cmd_shun)
1702 {
1703 if (argc < 3)
1704 return 0;
1705 if (argv[2][0] == '+') {
1706 if (argc < 5)
1707 return 0;
1708 shun_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0);
1709 return 1;
1710 } else if (argv[2][0] == '-') {
1711 shun_remove(argv[2]+1, 0);
1712 return 1;
1713 } else
1714 return 0;
1715 }
1716
1717 static CMD_FUNC(cmd_svsnick)
1718 {
1719 struct userNode *target, *dest;
1720 if ((argc < 4)
1721 || !(target = GetUserN(argv[1]))
1722 || !IsLocal(target)
1723 || (dest = GetUserH(argv[2])))
1724 return 0;
1725 NickChange(target, argv[2], 0);
1726 return 1;
1727 }
1728
1729 static oper_func_t *of_list;
1730 static unsigned int of_size = 0, of_used = 0;
1731
1732 void
1733 free_user(struct userNode *user)
1734 {
1735 free(user->nick);
1736 free(user);
1737 }
1738
1739 static void
1740 parse_cleanup(void)
1741 {
1742 unsigned int nn;
1743 free(of_list);
1744 free(privmsg_funcs);
1745 free(notice_funcs);
1746 free(mcf_list);
1747 dict_delete(irc_func_dict);
1748 for (nn=0; nn<dead_users.used; nn++)
1749 free_user(dead_users.list[nn]);
1750 userList_clean(&dead_users);
1751 }
1752
1753 static void
1754 p10_conf_reload(void) {
1755 hidden_host_suffix = conf_get_data("server/hidden_host", RECDB_QSTRING);
1756 }
1757
1758 static void
1759 remove_unbursted_channel(struct chanNode *cNode) {
1760 if (unbursted_channels)
1761 dict_remove(unbursted_channels, cNode->name);
1762 }
1763
1764 void
1765 init_parse(void)
1766 {
1767 const char *str, *desc;
1768 int numnick, usermask, max_users;
1769 char numer[COMBO_NUMERIC_LEN+1];
1770
1771 /* read config items */
1772 str = conf_get_data("server/extended_accounts", RECDB_QSTRING);
1773 extended_accounts = str ? enabled_string(str) : 1;
1774 str = conf_get_data("server/ping_freq", RECDB_QSTRING);
1775 ping_freq = str ? ParseInterval(str) : 120;
1776 str = conf_get_data("server/ping_timeout", RECDB_QSTRING);
1777 ping_timeout = str ? ParseInterval(str) : 30;
1778 str = conf_get_data("server/force_n2k", RECDB_QSTRING);
1779 force_n2k = str ? enabled_string(str) : 1;
1780 str = conf_get_data("server/numeric", RECDB_QSTRING);
1781 if (!str) {
1782 log_module(MAIN_LOG, LOG_ERROR, "No server/numeric entry in config file.");
1783 exit(1);
1784 }
1785 numnick = atoi(str);
1786 str = conf_get_data("server/max_users", RECDB_QSTRING);
1787 max_users = str ? atoi(str) : 4096;
1788 for (usermask = 4; usermask < max_users; usermask <<= 1) ;
1789 usermask--;
1790 if ((numnick < 64) && (usermask < 4096) && !force_n2k)
1791 inttobase64(numer, (numnick << 12) + (usermask & 0x00fff), 3);
1792 else
1793 inttobase64(numer, (numnick << 18) + (usermask & 0x3ffff), 5);
1794
1795 str = conf_get_data("server/his_servername", RECDB_QSTRING);
1796 his_servername = str ? strdup(str) : NULL;
1797 str = conf_get_data("server/his_servercomment", RECDB_QSTRING);
1798 his_servercomment = str ? strdup(str) : NULL;
1799
1800 str = conf_get_data("server/hostname", RECDB_QSTRING);
1801 desc = conf_get_data("server/description", RECDB_QSTRING);
1802 if (!str || !desc) {
1803 log_module(MAIN_LOG, LOG_ERROR, "No server/hostname entry in config file.");
1804 exit(1);
1805 }
1806 self = AddServer(NULL, str, 0, boot_time, now, numer, desc);
1807 conf_register_reload(p10_conf_reload);
1808
1809 irc_func_dict = dict_new();
1810 dict_insert(irc_func_dict, CMD_BURST, cmd_burst);
1811 dict_insert(irc_func_dict, TOK_BURST, cmd_burst);
1812 dict_insert(irc_func_dict, CMD_CREATE, cmd_create);
1813 dict_insert(irc_func_dict, TOK_CREATE, cmd_create);
1814 dict_insert(irc_func_dict, CMD_EOB, cmd_eob);
1815 dict_insert(irc_func_dict, TOK_EOB, cmd_eob);
1816 dict_insert(irc_func_dict, CMD_EOB_ACK, cmd_eob_ack);
1817 dict_insert(irc_func_dict, TOK_EOB_ACK, cmd_eob_ack);
1818 dict_insert(irc_func_dict, CMD_MODE, cmd_mode);
1819 dict_insert(irc_func_dict, TOK_MODE, cmd_mode);
1820 dict_insert(irc_func_dict, CMD_NICK, cmd_nick);
1821 dict_insert(irc_func_dict, TOK_NICK, cmd_nick);
1822 dict_insert(irc_func_dict, CMD_ACCOUNT, cmd_account);
1823 dict_insert(irc_func_dict, TOK_ACCOUNT, cmd_account);
1824 dict_insert(irc_func_dict, CMD_FAKEHOST, cmd_fakehost);
1825 dict_insert(irc_func_dict, TOK_FAKEHOST, cmd_fakehost);
1826 dict_insert(irc_func_dict, CMD_PASS, cmd_pass);
1827 dict_insert(irc_func_dict, TOK_PASS, cmd_pass);
1828 dict_insert(irc_func_dict, CMD_PING, cmd_ping);
1829 dict_insert(irc_func_dict, TOK_PING, cmd_ping);
1830 dict_insert(irc_func_dict, CMD_PRIVMSG, cmd_privmsg);
1831 dict_insert(irc_func_dict, TOK_PRIVMSG, cmd_privmsg);
1832 dict_insert(irc_func_dict, CMD_PONG, cmd_pong);
1833 dict_insert(irc_func_dict, TOK_PONG, cmd_pong);
1834 dict_insert(irc_func_dict, CMD_QUIT, cmd_quit);
1835 dict_insert(irc_func_dict, TOK_QUIT, cmd_quit);
1836 dict_insert(irc_func_dict, CMD_SERVER, cmd_server);
1837 dict_insert(irc_func_dict, TOK_SERVER, cmd_server);
1838 dict_insert(irc_func_dict, CMD_JOIN, cmd_join);
1839 dict_insert(irc_func_dict, TOK_JOIN, cmd_join);
1840 dict_insert(irc_func_dict, CMD_PART, cmd_part);
1841 dict_insert(irc_func_dict, TOK_PART, cmd_part);
1842 dict_insert(irc_func_dict, CMD_ERROR, cmd_error);
1843 dict_insert(irc_func_dict, TOK_ERROR, cmd_error);
1844 dict_insert(irc_func_dict, CMD_TOPIC, cmd_topic);
1845 dict_insert(irc_func_dict, TOK_TOPIC, cmd_topic);
1846 dict_insert(irc_func_dict, CMD_AWAY, cmd_away);
1847 dict_insert(irc_func_dict, TOK_AWAY, cmd_away);
1848 dict_insert(irc_func_dict, CMD_SILENCE, cmd_dummy);
1849 dict_insert(irc_func_dict, TOK_SILENCE, cmd_dummy);
1850 dict_insert(irc_func_dict, CMD_KICK, cmd_kick);
1851 dict_insert(irc_func_dict, TOK_KICK, cmd_kick);
1852 dict_insert(irc_func_dict, CMD_SQUIT, cmd_squit);
1853 dict_insert(irc_func_dict, TOK_SQUIT, cmd_squit);
1854 dict_insert(irc_func_dict, CMD_KILL, cmd_kill);
1855 dict_insert(irc_func_dict, TOK_KILL, cmd_kill);
1856 dict_insert(irc_func_dict, CMD_NOTICE, cmd_notice);
1857 dict_insert(irc_func_dict, TOK_NOTICE, cmd_notice);
1858 dict_insert(irc_func_dict, CMD_STATS, cmd_stats);
1859 dict_insert(irc_func_dict, TOK_STATS, cmd_stats);
1860 dict_insert(irc_func_dict, CMD_SVSNICK, cmd_svsnick);
1861 dict_insert(irc_func_dict, TOK_SVSNICK, cmd_svsnick);
1862 dict_insert(irc_func_dict, CMD_WHOIS, cmd_whois);
1863 dict_insert(irc_func_dict, TOK_WHOIS, cmd_whois);
1864 dict_insert(irc_func_dict, CMD_GLINE, cmd_gline);
1865 dict_insert(irc_func_dict, TOK_GLINE, cmd_gline);
1866 dict_insert(irc_func_dict, CMD_SHUN, cmd_shun);
1867 dict_insert(irc_func_dict, TOK_SHUN, cmd_shun);
1868 dict_insert(irc_func_dict, CMD_OPMODE, cmd_opmode);
1869 dict_insert(irc_func_dict, TOK_OPMODE, cmd_opmode);
1870 dict_insert(irc_func_dict, CMD_CLEARMODE, cmd_clearmode);
1871 dict_insert(irc_func_dict, TOK_CLEARMODE, cmd_clearmode);
1872 dict_insert(irc_func_dict, CMD_VERSION, cmd_version);
1873 dict_insert(irc_func_dict, TOK_VERSION, cmd_version);
1874 dict_insert(irc_func_dict, CMD_ADMIN, cmd_admin);
1875 dict_insert(irc_func_dict, TOK_ADMIN, cmd_admin);
1876
1877 /* In P10, DESTRUCT doesn't do anything except be broadcast to servers.
1878 * Apparently to obliterate channels from any servers that think they
1879 * exist?
1880 */
1881 dict_insert(irc_func_dict, CMD_DESTRUCT, cmd_dummy);
1882 dict_insert(irc_func_dict, TOK_DESTRUCT, cmd_dummy);
1883 /* Ignore invites */
1884 dict_insert(irc_func_dict, CMD_INVITE, cmd_dummy);
1885 dict_insert(irc_func_dict, TOK_INVITE, cmd_dummy);
1886 /* DESYNCH is just informational, so ignore it */
1887 dict_insert(irc_func_dict, CMD_DESYNCH, cmd_dummy);
1888 dict_insert(irc_func_dict, TOK_DESYNCH, cmd_dummy);
1889 /* Ignore channel operator notices. */
1890 dict_insert(irc_func_dict, CMD_WALLCHOPS, cmd_dummy);
1891 dict_insert(irc_func_dict, TOK_WALLCHOPS, cmd_dummy);
1892 dict_insert(irc_func_dict, CMD_WALLVOICES, cmd_dummy);
1893 dict_insert(irc_func_dict, TOK_WALLVOICES, cmd_dummy);
1894 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
1895 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
1896 /* Ignore opers being silly. */
1897 dict_insert(irc_func_dict, CMD_WALLOPS, cmd_dummy);
1898 dict_insert(irc_func_dict, TOK_WALLOPS, cmd_dummy);
1899 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
1900 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
1901 dict_insert(irc_func_dict, TOK_WALLUSERS, cmd_dummy);
1902 /* Ignore dnsbl exemptions */
1903 dict_insert(irc_func_dict, TOK_EXEMPT, cmd_dummy);
1904 dict_insert(irc_func_dict, TOK_MARK, cmd_dummy);
1905 /* Ignore privs */
1906 dict_insert(irc_func_dict, TOK_PRIVS, cmd_dummy);
1907 /* Ignore remote luser */
1908 dict_insert(irc_func_dict, TOK_LUSERS, cmd_dummy);
1909 /* We have reliable clock! Always! Wraaa! */
1910 dict_insert(irc_func_dict, CMD_SETTIME, cmd_dummy);
1911 dict_insert(irc_func_dict, TOK_SETTIME, cmd_dummy);
1912
1913 /* ignore /trace and /motd commands targetted at us */
1914 dict_insert(irc_func_dict, TOK_TRACE, cmd_dummy);
1915 dict_insert(irc_func_dict, TOK_MOTD, cmd_dummy);
1916
1917 /* handle topics */
1918 dict_insert(irc_func_dict, "331", cmd_num_topic);
1919 dict_insert(irc_func_dict, "332", cmd_num_topic);
1920 dict_insert(irc_func_dict, "333", cmd_num_topic);
1921 dict_insert(irc_func_dict, "345", cmd_dummy); /* blah has been invited to blah */
1922 dict_insert(irc_func_dict, "432", cmd_error_nick); /* Erroneus [sic] nickname */
1923 /* ban list resetting */
1924 /* "stats g" responses */
1925 dict_insert(irc_func_dict, "247", cmd_num_gline);
1926 dict_insert(irc_func_dict, "542", cmd_num_shun);
1927 dict_insert(irc_func_dict, "219", cmd_dummy); /* "End of /STATS report" */
1928 /* other numeric responses we might get */
1929 dict_insert(irc_func_dict, "401", cmd_dummy); /* target left network */
1930 dict_insert(irc_func_dict, "403", cmd_dummy); /* no such channel */
1931 dict_insert(irc_func_dict, "404", cmd_dummy); /* cannot send to channel */
1932 dict_insert(irc_func_dict, "439", cmd_dummy); /* target change too fast */
1933 dict_insert(irc_func_dict, "441", cmd_dummy); /* target isn't on that channel */
1934 dict_insert(irc_func_dict, "442", cmd_dummy); /* you aren't on that channel */
1935 dict_insert(irc_func_dict, "443", cmd_dummy); /* is already on channel (after invite?) */
1936 dict_insert(irc_func_dict, "461", cmd_dummy); /* Not enough parameters (after TOPIC w/ 0 args) */
1937 dict_insert(irc_func_dict, "467", cmd_dummy); /* Channel key already set */
1938
1939 num_privmsg_funcs = 16;
1940 privmsg_funcs = malloc(sizeof(privmsg_func_t)*num_privmsg_funcs);
1941 memset(privmsg_funcs, 0, sizeof(privmsg_func_t)*num_privmsg_funcs);
1942
1943 num_notice_funcs = 16;
1944 notice_funcs = malloc(sizeof(privmsg_func_t)*num_notice_funcs);
1945 memset(notice_funcs, 0, sizeof(privmsg_func_t)*num_notice_funcs);
1946
1947 userList_init(&dead_users);
1948 reg_del_channel_func(remove_unbursted_channel);
1949 reg_exit_func(parse_cleanup);
1950 }
1951
1952 int
1953 parse_line(char *line, int recursive)
1954 {
1955 char *argv[MAXNUMPARAMS], *origin;
1956 int argc, cmd, res=0;
1957 cmd_func_t *func;
1958
1959 argc = split_line(line, true, MAXNUMPARAMS, argv);
1960 cmd = self->uplink || !argv[0][1] || !argv[0][2];
1961 if (argc > cmd) {
1962 if (cmd) {
1963 if (argv[0][0] == ':') {
1964 origin = argv[0]+1;
1965 } else if (!argv[0][1] || !argv[0][2]) {
1966 struct server *sNode = GetServerN(argv[0]);
1967 origin = sNode ? sNode->name : 0;
1968 } else {
1969 struct userNode *uNode = GetUserN(argv[0]);
1970 origin = uNode ? uNode->nick : 0;
1971 }
1972 } else
1973 origin = 0;
1974 if ((func = dict_find(irc_func_dict, argv[cmd], NULL)))
1975 res = func(origin, argc-cmd, argv+cmd);
1976 }
1977 if (!res) {
1978 log_module(MAIN_LOG, LOG_ERROR, "PARSE ERROR on line: %s", unsplit_string(argv, argc, NULL));
1979 } else if (!recursive) {
1980 unsigned int i;
1981 for (i=0; i<dead_users.used; i++)
1982 free_user(dead_users.list[i]);
1983 dead_users.used = 0;
1984 }
1985 return res;
1986 }
1987
1988 static void
1989 parse_foreach(char *target_list, foreach_chanfunc cf, foreach_nonchan nc, foreach_userfunc uf, foreach_nonuser nu, void *data)
1990 {
1991 char *j, old;
1992
1993 do {
1994 j = target_list;
1995 while (*j != 0 && *j != ',')
1996 j++;
1997 old = *j;
1998 *j = 0;
1999
2000 if (IsChannelName(target_list)
2001 || (target_list[0] == '0' && target_list[1] == '\0')) {
2002 struct chanNode *chan = GetChannel(target_list);
2003
2004 if (chan) {
2005 if (cf)
2006 cf(chan, data);
2007 } else {
2008 if (nc)
2009 nc(target_list, data);
2010 }
2011 } else {
2012 struct userNode *user;
2013 struct privmsg_desc *pd = data;
2014
2015 pd->is_qualified = 0;
2016 if (*target_list == '@') {
2017 user = NULL;
2018 } else if (strchr(target_list, '@')) {
2019 struct server *server;
2020
2021 pd->is_qualified = 1;
2022 user = GetUserH(strtok(target_list, "@"));
2023 server = GetServerH(strtok(NULL, "@"));
2024
2025 if (user && (user->uplink != server)) {
2026 /* Don't attempt to index into any arrays
2027 using a user's numeric on another server. */
2028 user = NULL;
2029 }
2030 } else {
2031 user = GetUserN(target_list);
2032 }
2033
2034 if (user) {
2035 if (uf)
2036 uf(user, data);
2037 } else {
2038 if (nu)
2039 nu(target_list, data);
2040 }
2041 }
2042 target_list = j+1;
2043 } while (old == ',');
2044 }
2045
2046 static int
2047 get_local_numeric(void)
2048 {
2049 static unsigned int next_numeric = 0;
2050 if (self->clients > self->num_mask)
2051 return -1;
2052 while (self->users[next_numeric])
2053 if (++next_numeric > self->num_mask)
2054 next_numeric = 0;
2055 return next_numeric;
2056 }
2057
2058 static void
2059 make_numeric(struct server *svr, int local_num, char *outbuf)
2060 {
2061 int slen, llen;
2062
2063 if (force_n2k || svr->numeric[1]) {
2064 slen = 2;
2065 llen = 3;
2066 } else {
2067 slen = 1;
2068 llen = (local_num < 64*64) ? 2 : 3;
2069 }
2070 strncpy(outbuf, svr->numeric, slen);
2071 inttobase64(outbuf+slen, local_num, llen);
2072 outbuf[slen+llen] = 0;
2073 }
2074
2075 struct server *
2076 AddServer(struct server *uplink, const char *name, int hops, time_t boot, time_t link, const char *numeric, const char *description)
2077 {
2078 struct server* sNode;
2079 int slen, mlen;
2080
2081 if ((sNode = GetServerN(numeric))) {
2082 /* This means we're trying to re-add an existant server.
2083 * To be safe, we should forget the previous incarnation.
2084 * (And all its linked servers.)
2085 *
2086 * It usually only happens in replays when the original
2087 * had a ping timeout and the replay didn't (because
2088 * replaying a ping timeout invariably gets things wrong).
2089 */
2090 DelServer(sNode, 0, NULL);
2091 }
2092
2093 switch (strlen(numeric)) {
2094 case 5: slen = 2; mlen = 3; break;
2095 case 4: slen = 1; mlen = 3; break;
2096 case 3: slen = 1; mlen = 2; break;
2097 default:
2098 log_module(MAIN_LOG, LOG_ERROR, "AddServer(\"%s\", \"%s\", ...): Numeric %s has invalid length.", uplink->name, name, numeric);
2099 return NULL;
2100 }
2101
2102 sNode = calloc(1, sizeof(*sNode));
2103 sNode->uplink = uplink;
2104 safestrncpy(sNode->name, name, sizeof(sNode->name));
2105 sNode->num_mask = base64toint(numeric+slen, mlen);
2106 sNode->hops = hops;
2107 sNode->boot = boot;
2108 sNode->link = link;
2109 strncpy(sNode->numeric, numeric, slen);
2110 safestrncpy(sNode->description, description, sizeof(sNode->description));
2111 sNode->users = calloc(sNode->num_mask+1, sizeof(*sNode->users));
2112 serverList_init(&sNode->children);
2113 if (sNode->uplink) {
2114 /* uplink may be NULL if we're just building ourself */
2115 serverList_append(&sNode->uplink->children, sNode);
2116 }
2117 servers_num[base64toint(numeric, slen)] = sNode;
2118 dict_insert(servers, sNode->name, sNode);
2119 return sNode;
2120 }
2121
2122 void DelServer(struct server* serv, int announce, const char *message)
2123 {
2124 unsigned int i;
2125
2126 /* If we receive an ERROR command before the SERVER
2127 * command a NULL server can be passed */
2128 if (!serv)
2129 return;
2130
2131 /* Hrm, what's the right way to SQUIT some other server?
2132 * (This code is only to handle killing juped servers.) */
2133 if (announce && (serv->uplink == self) && (serv != self->uplink))
2134 irc_squit(serv, message, NULL);
2135
2136 /* must recursively remove servers linked to this one first */
2137 for (i=serv->children.used;i>0;)
2138 if (serv->children.list[--i] != self)
2139 DelServer(serv->children.list[i], false, NULL);
2140
2141 /* clean up server's user hash tables */
2142 for (i=0;i<=serv->num_mask;i++)
2143 if (serv->users[i])
2144 DelUser(serv->users[i], NULL, false, "server delinked");
2145
2146 /* delete server */
2147 if (serv->uplink)
2148 serverList_remove(&serv->uplink->children, serv);
2149 if (serv == self->uplink)
2150 self->uplink = NULL;
2151 servers_num[base64toint(serv->numeric, strlen(serv->numeric))] = NULL;
2152 dict_remove(servers, serv->name);
2153 serverList_clean(&serv->children);
2154 free(serv->users);
2155 free(serv);
2156 }
2157
2158 struct userNode *
2159 AddService(const char *nick, const char *modes, const char *desc, const char *hostname)
2160 {
2161 char numeric[COMBO_NUMERIC_LEN+1];
2162 int local_num = get_local_numeric();
2163 time_t timestamp = now;
2164 struct userNode *old_user = GetUserH(nick);
2165
2166 if (old_user) {
2167 if (IsLocal(old_user))
2168 return old_user;
2169 timestamp = old_user->timestamp - 1;
2170 }
2171 if (local_num == -1) {
2172 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for service %s", nick);
2173 return 0;
2174 }
2175 if (!hostname)
2176 hostname = self->name;
2177 make_numeric(self, local_num, numeric);
2178 /* TODO: Make these modes part of the conf file */
2179 return AddUser(self, nick, nick, hostname, modes ? modes : "+oik", numeric, desc, now, "AAAAAA");
2180 }
2181
2182 struct userNode *
2183 AddClone(const char *nick, const char *ident, const char *hostname, const char *desc)
2184 {
2185 char numeric[COMBO_NUMERIC_LEN+1];
2186 int local_num = get_local_numeric();
2187 time_t timestamp = now;
2188 struct userNode *old_user = GetUserH(nick);
2189
2190 if (old_user) {
2191 if (IsLocal(old_user))
2192 return old_user;
2193 timestamp = old_user->timestamp - 1;
2194 }
2195 if (local_num == -1) {
2196 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for clone %s", nick);
2197 return 0;
2198 }
2199 make_numeric(self, local_num, numeric);
2200 return AddUser(self, nick, ident, hostname, "+i", numeric, desc, timestamp, "AAAAAA");
2201 }
2202
2203 int
2204 is_valid_nick(const char *nick) {
2205 unsigned int ii;
2206 /* IRC has some of The Most Fucked-Up ideas about character sets
2207 * in the world.. */
2208 if (!isalpha(*nick) && !strchr("{|}~[\\]^_`", *nick))
2209 return 0;
2210 for (ii = 0; nick[ii]; ++ii)
2211 if (!isalnum(nick[ii]) && !strchr("{|}~[\\]^-_`", nick[ii]))
2212 return 0;
2213 if (strlen(nick) > nicklen)
2214 return 0;
2215 return 1;
2216 }
2217
2218 static struct userNode*
2219 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)
2220 {
2221 struct userNode *oldUser, *uNode;
2222 unsigned int n, ignore_user;
2223
2224 if ((strlen(numeric) < 3) || (strlen(numeric) > 5)) {
2225 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): numeric %s wrong length!", uplink, nick, numeric);
2226 return NULL;
2227 }
2228
2229 if (!uplink) {
2230 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s doesn't exist!", uplink, nick, numeric);
2231 return NULL;
2232 }
2233
2234 if (uplink != GetServerN(numeric)) {
2235 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s differs from nominal uplink %s.", uplink, nick, numeric, uplink->name);
2236 return NULL;
2237 }
2238
2239 if (!is_valid_nick(nick)) {
2240 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): invalid nickname detected.", uplink, nick);
2241 return NULL;
2242 }
2243
2244 ignore_user = 0;
2245 if ((oldUser = GetUserH(nick))) {
2246 if (IsLocal(oldUser) && (IsService(oldUser) || IsPersistent(oldUser))) {
2247 /* The service should collide the new user off. */
2248 oldUser->timestamp = timestamp - 1;
2249 irc_user(oldUser);
2250 }
2251 if (oldUser->timestamp > timestamp) {
2252 /* "Old" user is really newer; remove them */
2253 DelUser(oldUser, 0, 1, "Overruled by older nick");
2254 } else {
2255 /* User being added is too new; do not add them to
2256 * clients, but do add them to the server's list, since it
2257 * will send a KILL and QUIT soon. */
2258 ignore_user = 1;
2259 }
2260 }
2261
2262 /* create new usernode and set all values */
2263 uNode = calloc(1, sizeof(*uNode));
2264 uNode->nick = strdup(nick);
2265 safestrncpy(uNode->ident, ident, sizeof(uNode->ident));
2266 safestrncpy(uNode->info, userinfo, sizeof(uNode->info));
2267 safestrncpy(uNode->hostname, hostname, sizeof(uNode->hostname));
2268 safestrncpy(uNode->numeric, numeric, sizeof(uNode->numeric));
2269 irc_p10_pton(&uNode->ip, realip);
2270 uNode->timestamp = timestamp;
2271 modeList_init(&uNode->channels);
2272 uNode->uplink = uplink;
2273 if (++uNode->uplink->clients > uNode->uplink->max_clients) {
2274 uNode->uplink->max_clients = uNode->uplink->clients;
2275 }
2276 uNode->num_local = base64toint(numeric+strlen(uNode->uplink->numeric), 3) & uNode->uplink->num_mask;
2277 uNode->uplink->users[uNode->num_local] = uNode;
2278 mod_usermode(uNode, modes);
2279 if (ignore_user)
2280 return uNode;
2281
2282 dict_insert(clients, uNode->nick, uNode);
2283 if (dict_size(clients) > max_clients) {
2284 max_clients = dict_size(clients);
2285 max_clients_time = now;
2286 }
2287 if (IsLocal(uNode))
2288 irc_user(uNode);
2289 for (n=0; n<nuf_used; n++)
2290 if (nuf_list[n](uNode))
2291 break;
2292
2293 if ((uNode->loc == 1) && (uNode->handle_info))
2294 send_func_list(uNode);
2295
2296 return uNode;
2297 }
2298
2299 /* removes user from it's server's hash table and nick hash table */
2300 void
2301 DelUser(struct userNode* user, struct userNode *killer, int announce, const char *why)
2302 {
2303 unsigned int n;
2304
2305 verify(user);
2306
2307 /* mark them as dead, in case anybody cares */
2308 user->dead = 1;
2309
2310 /* remove pending adduser commands */
2311 wipe_adduser_pending(NULL, user);
2312
2313 /* remove user from all channels */
2314 while (user->channels.used > 0)
2315 DelChannelUser(user, user->channels.list[user->channels.used-1]->channel, false, 0);
2316
2317 /* Call these in reverse order so ChanServ can update presence
2318 information before NickServ nukes the handle_info. */
2319 for (n = duf_used; n > 0; )
2320 duf_list[--n](user, killer, why);
2321
2322 user->uplink->clients--;
2323 user->uplink->users[user->num_local] = NULL;
2324 if (IsOper(user))
2325 userList_remove(&curr_opers, user);
2326 /* remove from global dictionary, but not if after a collide */
2327 if (user == dict_find(clients, user->nick, NULL))
2328 dict_remove(clients, user->nick);
2329
2330 if (IsInvisible(user))
2331 invis_clients--;
2332
2333 if (announce) {
2334 if (IsLocal(user))
2335 irc_quit(user, why);
2336 else
2337 irc_kill(killer, user, why);
2338 }
2339
2340 modeList_clean(&user->channels);
2341 /* We don't free them, in case we try to privmsg them or something
2342 * (like when a stupid oper kills themself). We just put them onto
2343 * a list of clients that get freed after processing each line.
2344 */
2345 if (dead_users.size)
2346 userList_append(&dead_users, user);
2347 else
2348 free_user(user);
2349 }
2350
2351 static void call_oper_funcs(struct userNode *user);
2352
2353 void mod_usermode(struct userNode *user, const char *mode_change) {
2354 int add = 1;
2355 const char *word = mode_change;
2356
2357 if (!user || !mode_change)
2358 return;
2359
2360 call_user_mode_funcs(user, mode_change);
2361
2362 while (*word != ' ' && *word) word++;
2363 while (*word == ' ') word++;
2364 while (1) {
2365 #define do_user_mode(FLAG) do { if (add) user->modes |= FLAG; else user->modes &= ~FLAG; } while (0)
2366 switch (*mode_change++) {
2367 case 0: case ' ': return;
2368 case '+': add = 1; break;
2369 case '-': add = 0; break;
2370 case 'o':
2371 if (add) {
2372 if(!IsOper(user)) { /* Dont re-oper an oper */
2373 userList_append(&curr_opers, user);
2374 call_oper_funcs(user);
2375 }
2376 } else {
2377 userList_remove(&curr_opers, user);
2378 }
2379 do_user_mode(FLAGS_OPER);
2380 break;
2381 case 'O': do_user_mode(FLAGS_LOCOP); break;
2382 case 'i': do_user_mode(FLAGS_INVISIBLE);
2383 if (add)
2384 invis_clients++;
2385 else
2386 invis_clients--;
2387 break;
2388 case 'w': do_user_mode(FLAGS_WALLOP); break;
2389 case 's': do_user_mode(FLAGS_SERVNOTICE); break;
2390 case 'd': do_user_mode(FLAGS_DEAF); break;
2391 case 'k': do_user_mode(FLAGS_SERVICE); break;
2392 case 'g': do_user_mode(FLAGS_GLOBAL); break;
2393 // sethost - reed/apples
2394 // case 'h': do_user_mode(FLAGS_HELPER); break;
2395 // I check if there's an 'h' in the first part, and if there,
2396 // then everything after the space becomes their new host.
2397 case 'h': do_user_mode(FLAGS_SETHOST);
2398 if (*word) {
2399 char sethost[MAXLEN];
2400 unsigned int ii;
2401 for (ii=0; (*word != ' ') && (*word != '\0'); )
2402 sethost[ii++] = *word++;
2403 sethost[ii] = 0;
2404 while (*word == ' ')
2405 word++;
2406 safestrncpy(user->sethost, sethost, sizeof(user->sethost));
2407 }
2408 break;
2409 case 'x': do_user_mode(FLAGS_HIDDEN_HOST); break;
2410 case 'r':
2411 if (*word) {
2412 char tag[MAXLEN];
2413 unsigned int ii;
2414 for (ii=0; (*word != ' ') && (*word != '\0'); )
2415 tag[ii++] = *word++;
2416 tag[ii] = 0;
2417 while (*word == ' ')
2418 word++;
2419 call_account_func(user, tag);
2420 }
2421 break;
2422 case 'f':
2423 if (*word) {
2424 char host[MAXLEN];
2425 unsigned int ii;
2426 for (ii=0; (*word != ' ') && (*word != '\0'); )
2427 host[ii++] = *word++;
2428 host[ii] = 0;
2429 while (*word == ' ')
2430 word++;
2431 assign_fakehost(user, host, 0);
2432 }
2433 break;
2434 }
2435 #undef do_user_mode
2436 }
2437 }
2438
2439 struct mod_chanmode *
2440 mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, unsigned int flags, short base_oplevel)
2441 {
2442 struct mod_chanmode *change;
2443 unsigned int ii, in_arg, ch_arg, add;
2444
2445 if (argc == 0)
2446 return NULL;
2447 if (!(change = mod_chanmode_alloc(argc - 1)))
2448 return NULL;
2449
2450 for (ii = ch_arg = 0, in_arg = add = 1;
2451 (modes[0][ii] != '\0') && (modes[0][ii] != ' ');
2452 ++ii) {
2453 switch (modes[0][ii]) {
2454 case '+':
2455 add = 1;
2456 break;
2457 case '-':
2458 add = 0;
2459 break;
2460 #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)
2461 case 'C': do_chan_mode(MODE_NOCTCPS); break;
2462 case 'D': do_chan_mode(MODE_DELAYJOINS); break;
2463 case 'c': do_chan_mode(MODE_NOCOLORS); break;
2464 case 'i': do_chan_mode(MODE_INVITEONLY); break;
2465 case 'm': do_chan_mode(MODE_MODERATED); break;
2466 case 'n': do_chan_mode(MODE_NOPRIVMSGS); break;
2467 case 'p': do_chan_mode(MODE_PRIVATE); break;
2468 case 'r': do_chan_mode(MODE_REGONLY); break;
2469 case 's': do_chan_mode(MODE_SECRET); break;
2470 case 't': do_chan_mode(MODE_TOPICLIMIT); break;
2471 case 'S': do_chan_mode(MODE_STRIPCOLOR); break;
2472 case 'M': do_chan_mode(MODE_MODUNREG); break;
2473 case 'N': do_chan_mode(MODE_NONOTICE); break;
2474 case 'Q': do_chan_mode(MODE_NOQUITMSGS); break;
2475 case 'T': do_chan_mode(MODE_NOAMSG); break;
2476 case 'O': do_chan_mode(MODE_OPERSONLY); break;
2477 case 'Z': do_chan_mode(MODE_SSLONLY); break;
2478 case 'L': do_chan_mode(MODE_HIDEMODE); break;
2479 case 'z':
2480 if (!(flags & MCP_REGISTERED)) {
2481 do_chan_mode(MODE_REGISTERED);
2482 } else {
2483 mod_chanmode_free(change);
2484 return NULL;
2485 }
2486 break;
2487 #undef do_chan_mode
2488 case 'l':
2489 if (add) {
2490 if (in_arg >= argc)
2491 goto error;
2492 change->modes_set |= MODE_LIMIT;
2493 change->new_limit = atoi(modes[in_arg++]);
2494 } else {
2495 change->modes_set &= ~MODE_LIMIT;
2496 change->modes_clear |= MODE_LIMIT;
2497 }
2498 break;
2499 case 'k':
2500 if (add) {
2501 if (in_arg >= argc)
2502 goto error;
2503 change->modes_set |= MODE_KEY;
2504 safestrncpy(change->new_key, modes[in_arg++], sizeof(change->new_key));
2505 } else {
2506 change->modes_clear |= MODE_KEY;
2507 if (!(flags & MCP_KEY_FREE)) {
2508 if (in_arg >= argc)
2509 goto error;
2510 in_arg++;
2511 }
2512 }
2513 break;
2514 case 'U':
2515 if (add)
2516 {
2517 if (in_arg >= argc)
2518 goto error;
2519 change->modes_set |= MODE_UPASS;
2520 safestrncpy(change->new_upass, modes[in_arg++], sizeof(change->new_upass));
2521 } else {
2522 change->modes_clear |= MODE_UPASS;
2523 if (!(flags & MCP_UPASS_FREE)) {
2524 if (in_arg >= argc)
2525 goto error;
2526 in_arg++;
2527 }
2528 }
2529 break;
2530 case 'A':
2531 if (add) {
2532 if (in_arg >= argc)
2533 goto error;
2534 change->modes_set |= MODE_APASS;
2535 safestrncpy(change->new_apass, modes[in_arg++], sizeof(change->new_apass));
2536 } else {
2537 change->modes_clear |= MODE_APASS;
2538 if (!(flags & MCP_APASS_FREE)) {
2539 if (in_arg >= argc)
2540 goto error;
2541 in_arg++;
2542 }
2543 }
2544 break;
2545 case 'b':
2546 if (!(flags & MCP_ALLOW_OVB))
2547 goto error;
2548 if (in_arg >= argc)
2549 goto error;
2550 change->args[ch_arg].mode = MODE_BAN;
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 'e':
2556 if (!(flags & MCP_ALLOW_OVB))
2557 goto error;
2558 if (in_arg >= argc)
2559 goto error;
2560 change->args[ch_arg].mode = MODE_EXEMPT;
2561 if (!add)
2562 change->args[ch_arg].mode |= MODE_REMOVE;
2563 change->args[ch_arg++].u.hostmask = modes[in_arg++];
2564 break;
2565 case 'o': case 'h': case 'v':
2566 {
2567 struct userNode *victim;
2568 char *oplevel_str;
2569 int oplevel;
2570
2571 if (in_arg >= argc)
2572 goto error;
2573 oplevel_str = strchr(modes[in_arg], ':');
2574 if (oplevel_str)
2575 {
2576 /* XXYYY M #channel +o XXYYY:<oplevel> */
2577 *oplevel_str++ = '\0';
2578 oplevel = parse_oplevel(oplevel_str);
2579 if (oplevel <= base_oplevel && !(flags & MCP_FROM_SERVER))
2580 oplevel = base_oplevel + 1;
2581 }
2582 else if (channel->modes & MODE_UPASS)
2583 oplevel = base_oplevel + 1;
2584 else
2585 oplevel = -1;
2586
2587 /* Check that oplevel is within bounds. */
2588 if (oplevel > MAXOPLEVEL)
2589 oplevel = MAXOPLEVEL;
2590
2591 if (!(flags & MCP_ALLOW_OVB))
2592 goto error;
2593 if (in_arg >= argc)
2594 goto error;
2595
2596 if (modes[0][ii] == 'o')
2597 change->args[ch_arg].mode = MODE_CHANOP;
2598 else if (modes[0][ii] == 'h')
2599 change->args[ch_arg].mode = MODE_HALFOP;
2600 else if (modes[0][ii] == 'v')
2601 change->args[ch_arg].mode = MODE_VOICE;
2602
2603 if (!add)
2604 change->args[ch_arg].mode |= MODE_REMOVE;
2605 if (flags & MCP_FROM_SERVER)
2606 victim = GetUserN(modes[in_arg++]);
2607 else
2608 victim = GetUserH(modes[in_arg++]);
2609 if (!victim)
2610 continue;
2611 if ((change->args[ch_arg].u.member = GetUserMode(channel, victim)))
2612 {
2613 /* Apply the oplevel change */
2614 change->args[ch_arg].u.member->oplevel = oplevel;
2615 ch_arg++;
2616 }
2617 break;
2618 }
2619 default:
2620 if (!(flags & MCP_FROM_SERVER))
2621 goto error;
2622 break;
2623 }
2624 }
2625 change->argc = ch_arg; /* in case any turned out to be ignored */
2626 if (change->modes_set & MODE_SECRET) {
2627 change->modes_set &= ~(MODE_PRIVATE);
2628 change->modes_clear |= MODE_PRIVATE;
2629 } else if (change->modes_set & MODE_PRIVATE) {
2630 change->modes_set &= ~(MODE_SECRET);
2631 change->modes_clear |= MODE_SECRET;
2632 }
2633 return change;
2634 error:
2635 mod_chanmode_free(change);
2636 return NULL;
2637 }
2638
2639 struct chanmode_buffer {
2640 char modes[MAXLEN];
2641 char args[MAXLEN];
2642 struct chanNode *channel;
2643 struct userNode *actor;
2644 unsigned int modes_used;
2645 unsigned int args_used;
2646 size_t chname_len;
2647 unsigned int is_add : 1;
2648 unsigned int is_chanop : 1;
2649 };
2650
2651 static void
2652 mod_chanmode_append(struct chanmode_buffer *buf, char ch, const char *arg)
2653 {
2654 size_t arg_len = strlen(arg);
2655 if (buf->modes_used > (MAXMODEPARAMS) ||
2656 buf->modes_used + buf->args_used + buf->chname_len + arg_len > 450) {
2657 memcpy(buf->modes + buf->modes_used, buf->args, buf->args_used);
2658 buf->modes[buf->modes_used + buf->args_used] = '\0';
2659 irc_mode((buf->is_chanop ? buf->actor : NULL), buf->channel, buf->modes);
2660 buf->modes[0] = buf->is_add ? '+' : '-';
2661 buf->modes_used = 1;
2662 buf->args_used = 0;
2663 }
2664 buf->modes[buf->modes_used++] = ch;
2665 buf->args[buf->args_used++] = ' ';
2666 memcpy(buf->args + buf->args_used, arg, arg_len);
2667 buf->args_used += arg_len;
2668 }
2669
2670 void
2671 mod_chanmode_announce(struct userNode *who, struct chanNode *channel, struct mod_chanmode *change)
2672 {
2673 struct chanmode_buffer chbuf;
2674 unsigned int arg;
2675 struct modeNode *mn;
2676 char int_buff[32], mode = '\0';
2677
2678 assert(change->argc <= change->alloc_argc);
2679 memset(&chbuf, 0, sizeof(chbuf));
2680 chbuf.channel = channel;
2681 chbuf.actor = who;
2682 chbuf.chname_len = strlen(channel->name);
2683
2684 mn = GetUserMode(channel, who);
2685 if ((mn && (mn->modes & MODE_CHANOP)) || off_channel)
2686 chbuf.is_chanop = 1;
2687
2688 /* First remove modes */
2689 chbuf.is_add = 0;
2690 if (change->modes_clear) {
2691 if (mode != '-')
2692 chbuf.modes[chbuf.modes_used++] = mode = '-';
2693 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
2694 DO_MODE_CHAR(PRIVATE, 'p');
2695 DO_MODE_CHAR(SECRET, 's');
2696 DO_MODE_CHAR(MODERATED, 'm');
2697 DO_MODE_CHAR(TOPICLIMIT, 't');
2698 DO_MODE_CHAR(INVITEONLY, 'i');
2699 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2700 DO_MODE_CHAR(LIMIT, 'l');
2701 DO_MODE_CHAR(DELAYJOINS, 'D');
2702 DO_MODE_CHAR(REGONLY, 'r');
2703 DO_MODE_CHAR(NOCOLORS, 'c');
2704 DO_MODE_CHAR(NOCTCPS, 'C');
2705 DO_MODE_CHAR(STRIPCOLOR, 'S');
2706 DO_MODE_CHAR(MODUNREG, 'M');
2707 DO_MODE_CHAR(NONOTICE, 'N');
2708 DO_MODE_CHAR(NOQUITMSGS, 'Q');
2709 DO_MODE_CHAR(NOAMSG, 'T');
2710 DO_MODE_CHAR(OPERSONLY, 'O');
2711 DO_MODE_CHAR(REGISTERED, 'z');
2712 DO_MODE_CHAR(SSLONLY, 'Z');
2713 DO_MODE_CHAR(HIDEMODE, 'L');
2714 #undef DO_MODE_CHAR
2715 if (change->modes_clear & channel->modes & MODE_KEY)
2716 mod_chanmode_append(&chbuf, 'k', channel->key);
2717 if (change->modes_clear & channel->modes & MODE_UPASS)
2718 mod_chanmode_append(&chbuf, 'U', channel->upass);
2719 if (change->modes_clear * channel->modes & MODE_APASS)
2720 mod_chanmode_append(&chbuf, 'A', channel->apass);
2721 }
2722 for (arg = 0; arg < change->argc; ++arg) {
2723 if (!(change->args[arg].mode & MODE_REMOVE))
2724 continue;
2725 if (mode != '-')
2726 chbuf.modes[chbuf.modes_used++] = mode = '-';
2727 switch (change->args[arg].mode & ~MODE_REMOVE) {
2728 case MODE_BAN:
2729 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
2730 break;
2731 case MODE_EXEMPT:
2732 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
2733 break;
2734 default:
2735 if (change->args[arg].mode & MODE_CHANOP)
2736 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
2737 if (change->args[arg].mode & MODE_HALFOP)
2738 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
2739 if (change->args[arg].mode & MODE_VOICE)
2740 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
2741 break;
2742 }
2743 }
2744
2745 /* Then set them */
2746 chbuf.is_add = 1;
2747 if (change->modes_set) {
2748 if (mode != '+')
2749 chbuf.modes[chbuf.modes_used++] = mode = '+';
2750 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
2751 DO_MODE_CHAR(PRIVATE, 'p');
2752 DO_MODE_CHAR(SECRET, 's');
2753 DO_MODE_CHAR(MODERATED, 'm');
2754 DO_MODE_CHAR(TOPICLIMIT, 't');
2755 DO_MODE_CHAR(INVITEONLY, 'i');
2756 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2757 DO_MODE_CHAR(DELAYJOINS, 'D');
2758 DO_MODE_CHAR(REGONLY, 'r');
2759 DO_MODE_CHAR(NOCOLORS, 'c');
2760 DO_MODE_CHAR(NOCTCPS, 'C');
2761 DO_MODE_CHAR(STRIPCOLOR, 'S');
2762 DO_MODE_CHAR(MODUNREG, 'M');
2763 DO_MODE_CHAR(NONOTICE, 'N');
2764 DO_MODE_CHAR(NOQUITMSGS, 'Q');
2765 DO_MODE_CHAR(NOAMSG, 'T');
2766 DO_MODE_CHAR(OPERSONLY, 'O');
2767 DO_MODE_CHAR(REGISTERED, 'z');
2768 DO_MODE_CHAR(SSLONLY, 'Z');
2769 DO_MODE_CHAR(HIDEMODE, 'L');
2770 #undef DO_MODE_CHAR
2771 if(change->modes_set & MODE_KEY)
2772 mod_chanmode_append(&chbuf, 'k', change->new_key);
2773 if (change->modes_set & MODE_UPASS)
2774 mod_chanmode_append(&chbuf, 'U', change->new_upass);
2775 if (change->modes_set & MODE_APASS)
2776 mod_chanmode_append(&chbuf, 'A', change->new_apass);
2777 if(change->modes_set & MODE_LIMIT) {
2778 sprintf(int_buff, "%d", change->new_limit);
2779 mod_chanmode_append(&chbuf, 'l', int_buff);
2780 }
2781 }
2782 for (arg = 0; arg < change->argc; ++arg) {
2783 if (change->args[arg].mode & MODE_REMOVE)
2784 continue;
2785 if (mode != '+')
2786 chbuf.modes[chbuf.modes_used++] = mode = '+';
2787 switch (change->args[arg].mode) {
2788 case MODE_BAN:
2789 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
2790 break;
2791 case MODE_EXEMPT:
2792 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
2793 break;
2794 default:
2795 if (change->args[arg].mode & MODE_CHANOP)
2796 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
2797 if (change->args[arg].mode & MODE_HALFOP)
2798 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
2799 if (change->args[arg].mode & MODE_VOICE)
2800 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
2801 break;
2802 }
2803 }
2804
2805 /* Flush the buffer and apply changes locally */
2806 if (chbuf.modes_used > 0) {
2807 memcpy(chbuf.modes + chbuf.modes_used, chbuf.args, chbuf.args_used);
2808 chbuf.modes[chbuf.modes_used + chbuf.args_used] = '\0';
2809 irc_mode((chbuf.is_chanop ? chbuf.actor : NULL), chbuf.channel, chbuf.modes);
2810 }
2811 mod_chanmode_apply(who, channel, change);
2812 }
2813
2814 char *
2815 mod_chanmode_format(struct mod_chanmode *change, char *outbuff)
2816 {
2817 unsigned int used = 0;
2818 assert(change->argc <= change->alloc_argc);
2819 if (change->modes_clear) {
2820 outbuff[used++] = '-';
2821 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) outbuff[used++] = CHAR
2822 DO_MODE_CHAR(PRIVATE, 'p');
2823 DO_MODE_CHAR(SECRET, 's');
2824 DO_MODE_CHAR(MODERATED, 'm');
2825 DO_MODE_CHAR(TOPICLIMIT, 't');
2826 DO_MODE_CHAR(INVITEONLY, 'i');
2827 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2828 DO_MODE_CHAR(LIMIT, 'l');
2829 DO_MODE_CHAR(KEY, 'k');
2830 DO_MODE_CHAR(UPASS, 'U');
2831 DO_MODE_CHAR(APASS, 'A');
2832 DO_MODE_CHAR(DELAYJOINS, 'D');
2833 DO_MODE_CHAR(REGONLY, 'r');
2834 DO_MODE_CHAR(NOCOLORS, 'c');
2835 DO_MODE_CHAR(NOCTCPS, 'C');
2836 DO_MODE_CHAR(STRIPCOLOR, 'S');
2837 DO_MODE_CHAR(MODUNREG, 'M');
2838 DO_MODE_CHAR(NONOTICE, 'N');
2839 DO_MODE_CHAR(NOQUITMSGS, 'Q');
2840 DO_MODE_CHAR(NOAMSG, 'T');
2841 DO_MODE_CHAR(OPERSONLY, 'O');
2842 DO_MODE_CHAR(REGISTERED, 'z');
2843 DO_MODE_CHAR(SSLONLY, 'Z');
2844 DO_MODE_CHAR(HIDEMODE, 'L');
2845 #undef DO_MODE_CHAR
2846 }
2847 if (change->modes_set) {
2848 outbuff[used++] = '+';
2849 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) outbuff[used++] = CHAR
2850 DO_MODE_CHAR(PRIVATE, 'p');
2851 DO_MODE_CHAR(SECRET, 's');
2852 DO_MODE_CHAR(MODERATED, 'm');
2853 DO_MODE_CHAR(TOPICLIMIT, 't');
2854 DO_MODE_CHAR(INVITEONLY, 'i');
2855 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2856 DO_MODE_CHAR(DELAYJOINS, 'D');
2857 DO_MODE_CHAR(REGONLY, 'r');
2858 DO_MODE_CHAR(NOCOLORS, 'c');
2859 DO_MODE_CHAR(NOCTCPS, 'C');
2860 DO_MODE_CHAR(STRIPCOLOR, 'S');
2861 DO_MODE_CHAR(MODUNREG, 'M');
2862 DO_MODE_CHAR(NONOTICE, 'N');
2863 DO_MODE_CHAR(NOQUITMSGS, 'Q');
2864 DO_MODE_CHAR(NOAMSG, 'T');
2865 DO_MODE_CHAR(OPERSONLY, 'O');
2866 DO_MODE_CHAR(REGISTERED, 'z');
2867 DO_MODE_CHAR(SSLONLY, 'Z');
2868 DO_MODE_CHAR(HIDEMODE, 'L');
2869 #undef DO_MODE_CHAR
2870 switch (change->modes_set & (MODE_KEY|MODE_LIMIT|MODE_APASS|MODE_UPASS)) {
2871 /* Doing this implementation has been a pain in the arse, I hope I didn't forget a possible combination */
2872 case MODE_KEY|MODE_LIMIT|MODE_APASS|MODE_UPASS:
2873 used += sprintf(outbuff+used, "lkAU %d %s %s %s", change->new_limit, change->new_key,change->new_apass, change->new_upass);
2874 break;
2875
2876 case MODE_KEY|MODE_LIMIT|MODE_APASS:
2877 used += sprintf(outbuff+used, "lkA %d %s %s", change->new_limit, change->new_key, change->new_apass);
2878 break;
2879 case MODE_KEY|MODE_LIMIT|MODE_UPASS:
2880 used += sprintf(outbuff+used, "lkU %d %s %s", change->new_limit, change->new_key, change->new_upass);
2881 break;
2882 case MODE_KEY|MODE_APASS|MODE_UPASS:
2883 used += sprintf(outbuff+used, "kAU %s %s %s", change->new_key, change->new_apass, change->new_upass);
2884 break;
2885
2886 case MODE_KEY|MODE_APASS:
2887 used += sprintf(outbuff+used, "kA %s %s", change->new_key, change->new_apass);
2888 break;
2889 case MODE_KEY|MODE_UPASS:
2890 used += sprintf(outbuff+used, "kU %s %s", change->new_key, change->new_upass);
2891 break;
2892 case MODE_KEY|MODE_LIMIT:
2893 used += sprintf(outbuff+used, "lk %d %s", change->new_limit, change->new_key);
2894 break;
2895
2896 case MODE_LIMIT|MODE_UPASS:
2897 used += sprintf(outbuff+used, "lU %d %s", change->new_limit, change->new_upass);
2898 break;
2899 case MODE_LIMIT|MODE_APASS:
2900 used += sprintf(outbuff+used, "lA %d %s", change->new_limit, change->new_apass);
2901 break;
2902 case MODE_APASS|MODE_UPASS:
2903 used += sprintf(outbuff+used, "AU %s %s", change->new_apass, change->new_upass);
2904
2905 case MODE_LIMIT|MODE_APASS|MODE_UPASS:
2906 used += sprintf(outbuff+used, "lAU %d %s %s", change->new_limit, change->new_apass, change->new_upass);
2907 break;
2908
2909 case MODE_APASS:
2910 used += sprintf(outbuff+used, "A %s", change->new_apass);
2911 break;
2912 case MODE_UPASS:
2913 used += sprintf(outbuff+used, "U %s", change->new_upass);
2914 break;
2915 case MODE_KEY:
2916 used += sprintf(outbuff+used, "k %s", change->new_key);
2917 break;
2918 case MODE_LIMIT:
2919 used += sprintf(outbuff+used, "l %d", change->new_limit);
2920 break;
2921 }
2922 }
2923 outbuff[used] = 0;
2924 return outbuff;
2925 }
2926
2927 static int
2928 clear_chanmode(struct chanNode *channel, const char *modes)
2929 {
2930 unsigned int remove;
2931
2932 for (remove = 0; *modes; modes++) {
2933 switch (*modes) {
2934 case 'o': remove |= MODE_CHANOP; break;
2935 case 'h': remove |= MODE_HALFOP; break;
2936 case 'v': remove |= MODE_VOICE; break;
2937 case 'p': remove |= MODE_PRIVATE; break;
2938 case 's': remove |= MODE_SECRET; break;
2939 case 'm': remove |= MODE_MODERATED; break;
2940 case 't': remove |= MODE_TOPICLIMIT; break;
2941 case 'i': remove |= MODE_INVITEONLY; break;
2942 case 'n': remove |= MODE_NOPRIVMSGS; break;
2943 case 'k':
2944 remove |= MODE_KEY;
2945 channel->key[0] = '\0';
2946 break;
2947 case 'A':
2948 remove |= MODE_APASS;
2949 channel->apass[0] = '\0';
2950 break;
2951 case 'U':
2952 remove |= MODE_UPASS;
2953 channel->upass[0] = '\0';
2954 break;
2955 case 'l':
2956 remove |= MODE_LIMIT;
2957 channel->limit = 0;
2958 break;
2959 case 'b': remove |= MODE_BAN; break;
2960 case 'e': remove |= MODE_EXEMPT; break;
2961 case 'D': remove |= MODE_DELAYJOINS; break;
2962 case 'r': remove |= MODE_REGONLY; break;
2963 case 'c': remove |= MODE_NOCOLORS; break;
2964 case 'C': remove |= MODE_NOCTCPS; break;
2965 case 'S': remove |= MODE_STRIPCOLOR; break;
2966 case 'M': remove |= MODE_MODUNREG; break;
2967 case 'N': remove |= MODE_NONOTICE; break;
2968 case 'Q': remove |= MODE_NOQUITMSGS; break;
2969 case 'T': remove |= MODE_NOAMSG; break;
2970 case 'O': remove |= MODE_OPERSONLY; break;
2971 case 'z': remove |= MODE_REGISTERED; break;
2972 case 'Z': remove |= MODE_SSLONLY; break;
2973 case 'L': remove |= MODE_HIDEMODE; break;
2974 }
2975 }
2976
2977 if (!remove)
2978 return 1;
2979
2980 /* Remove simple modes. */
2981 channel->modes &= ~remove;
2982
2983 /* If removing bans, kill 'em all. */
2984 if ((remove & MODE_BAN) && channel->banlist.used) {
2985 unsigned int i;
2986 for (i=0; i<channel->banlist.used; i++)
2987 free(channel->banlist.list[i]);
2988 channel->banlist.used = 0;
2989 }
2990
2991 /* If removing exempts, kill 'em all. */
2992 if ((remove & MODE_EXEMPT) && channel->exemptlist.used) {
2993 unsigned int i;
2994 for (i=0; i<channel->exemptlist.used; i++)
2995 free(channel->exemptlist.list[i]);
2996 channel->exemptlist.used = 0;
2997 }
2998
2999 /* Remove member modes. */
3000 if ((remove & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE)) && channel->members.used) {
3001 int mask = ~(remove & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE));
3002 unsigned int i;
3003
3004 for (i = 0; i < channel->members.used; i++)
3005 channel->members.list[i]->modes &= mask;
3006 }
3007
3008 return 1;
3009 }
3010
3011 void
3012 reg_privmsg_func(struct userNode *user, privmsg_func_t handler)
3013 {
3014 unsigned int numeric = user->num_local;
3015 if (numeric >= num_privmsg_funcs) {
3016 int newnum = numeric + 8;
3017 privmsg_funcs = realloc(privmsg_funcs, newnum*sizeof(privmsg_func_t));
3018 memset(privmsg_funcs+num_privmsg_funcs, 0, (newnum-num_privmsg_funcs)*sizeof(privmsg_func_t));
3019 num_privmsg_funcs = newnum;
3020 }
3021 if (privmsg_funcs[numeric])
3022 log_module(MAIN_LOG, LOG_WARNING, "re-registering new privmsg handler for numeric %d", numeric);
3023 privmsg_funcs[numeric] = handler;
3024 }
3025
3026 void
3027 unreg_privmsg_func(struct userNode *user, privmsg_func_t handler)
3028 {
3029 unsigned int x;
3030
3031 if (!user || handler)
3032 return; /* this really only works with users */
3033
3034 memset(privmsg_funcs+user->num_local, 0, sizeof(privmsg_func_t));
3035
3036 for (x = user->num_local+1; x < num_privmsg_funcs; x++)
3037 memmove(privmsg_funcs+x-1, privmsg_funcs+x, sizeof(privmsg_func_t));
3038
3039 privmsg_funcs = realloc(privmsg_funcs, num_privmsg_funcs*sizeof(privmsg_func_t));
3040 num_privmsg_funcs--;
3041 }
3042
3043
3044 void
3045 reg_notice_func(struct userNode *user, privmsg_func_t handler)
3046 {
3047 unsigned int numeric = user->num_local;
3048 if (numeric >= num_notice_funcs) {
3049 int newnum = numeric + 8;
3050 notice_funcs = realloc(notice_funcs, newnum*sizeof(privmsg_func_t));
3051 memset(notice_funcs+num_notice_funcs, 0, (newnum-num_notice_funcs)*sizeof(privmsg_func_t));
3052 num_notice_funcs = newnum;
3053 }
3054 if (notice_funcs[numeric])
3055 log_module(MAIN_LOG, LOG_WARNING, "re-registering new notice handler for numeric %d", numeric);
3056 notice_funcs[numeric] = handler;
3057 }
3058
3059 void
3060 unreg_notice_func(struct userNode *user, privmsg_func_t handler)
3061 {
3062 unsigned int x;
3063
3064 if (!user || handler)
3065 return; /* this really only works with users */
3066
3067 memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t));
3068
3069 for (x = user->num_local+1; x < num_notice_funcs; x++)
3070 memmove(notice_funcs+x-1, notice_funcs+x, sizeof(privmsg_func_t));
3071
3072 memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t));
3073 notice_funcs = realloc(notice_funcs, num_notice_funcs*sizeof(privmsg_func_t));
3074 num_notice_funcs--;
3075 }
3076
3077 void
3078 reg_oper_func(oper_func_t handler)
3079 {
3080 if (of_used == of_size) {
3081 if (of_size) {
3082 of_size <<= 1;
3083 of_list = realloc(of_list, of_size*sizeof(oper_func_t));
3084 } else {
3085 of_size = 8;
3086 of_list = malloc(of_size*sizeof(oper_func_t));
3087 }
3088 }
3089 of_list[of_used++] = handler;
3090 }
3091
3092 static void
3093 call_oper_funcs(struct userNode *user)
3094 {
3095 unsigned int n;
3096 if (IsLocal(user))
3097 return;
3098 for (n=0; n<of_used; n++)
3099 of_list[n](user);
3100 }
3101
3102 static void
3103 send_burst(void)
3104 {
3105 unsigned int i, hop, max_hop=1;
3106 dict_iterator_t it;
3107
3108 /* burst (juped) servers, closest first (except self, which is sent already) */
3109 for (i=0; i<ArrayLength(servers_num); i++)
3110 if (servers_num[i] && servers_num[i]->hops > max_hop)
3111 max_hop = servers_num[i]->hops;
3112 for (hop=1; hop<=max_hop; hop++) {
3113 for (i=0;i<ArrayLength(servers_num);i++) {
3114 if (servers_num[i]
3115 && (servers_num[i]->hops == hop)
3116 && (servers_num[i] != self->uplink))
3117 irc_server(servers_num[i]);
3118 }
3119 }
3120
3121 /* burst local nicks */
3122 for (i=0; i<=self->num_mask; i++)
3123 if (self->users[i])
3124 irc_user(self->users[i]);
3125
3126 /* build dict of unbursted channel names (just copy existing channels) */
3127 unbursted_channels = dict_new();
3128 for (it = dict_first(channels); it; it = iter_next(it))
3129 dict_insert(unbursted_channels, iter_key(it), iter_data(it));
3130 }
3131
3132 /*
3133 * Oplevel parsing
3134 */
3135 static int
3136 parse_oplevel(char *str)
3137 {
3138 int oplevel = 0;
3139 while (isdigit(*str))
3140 oplevel = oplevel * 10 + *str++ - '0';
3141 return oplevel;
3142 }
3143