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