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