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