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