]> jfr.im git - irc/evilnet/x3.git/blob - src/proto-p10.c
d3b5dd0d0bfc82e59551719f3f43934ce264ce62
[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 " " FMT_TIME_T " :<%s> %s",
841 self->numeric, (srv ? srv->numeric : "*"), shun->target, shun->expires-now, 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 " FMT_TIME_T, self->numeric, mask, now);
857 }
858
859 void
860 irc_unshun(const char *mask)
861 {
862 putsock("%s " P10_SHUN " * -%s " FMT_TIME_T, self->numeric, mask, now);
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
913 if (len > 0 && (chan->banlist.used > 0 || chan->exemptlist.used > 0))
914 burst_line[pos++] = ' ';
915
916 if (chan->banlist.used) {
917 /* dump the bans */
918 first_ban = 1;
919
920 for (n=0; n<chan->banlist.used; ) {
921 if (first_ban && (pos < 500)) {
922 burst_line[pos++] = ':';
923 burst_line[pos++] = '%';
924 }
925 bn = chan->banlist.list[n];
926 len = strlen(bn->ban);
927 if (pos + 2 + len < 505) {
928 memcpy(burst_line + pos, bn->ban, len);
929 pos += len;
930 burst_line[pos++] = ' ';
931 first_ban = 0;
932 n++;
933 } else {
934 burst_line[pos-1] = 0;
935 putsock("%s", burst_line);
936 pos = base_len;
937 first_ban = 1;
938 }
939 }
940 }
941
942 if (chan->exemptlist.used) {
943 /* dump the exempts */
944 first_exempt = 1;
945
946 for (n=0; n<chan->exemptlist.used; ) {
947 if (first_exempt && (pos < 500)) {
948 if (chan->banlist.used < 1) {
949 burst_line[pos++] = ':';
950 burst_line[pos++] = '%';
951 burst_line[pos++] = ' ';
952 }
953 burst_line[pos++] = '~';
954 burst_line[pos++] = ' ';
955 }
956 en = chan->exemptlist.list[n];
957 len = strlen(en->exempt);
958 if (pos + 2 + len < 505) {
959 memcpy(burst_line + pos, en->exempt, len);
960 pos += len;
961 burst_line[pos++] = ' ';
962 first_exempt = 0;
963 n++;
964 } else {
965 burst_line[pos-1] = 0;
966 putsock("%s", burst_line);
967 pos = base_len;
968 first_exempt = 1;
969 }
970 }
971 }
972 /* print the last line */
973 burst_line[pos] = 0;
974 putsock("%s", burst_line);
975 }
976
977 void
978 irc_quit(struct userNode *user, const char *message)
979 {
980 putsock("%s " P10_QUIT " :%s", user->numeric, message);
981 }
982
983 void
984 irc_error(const char *to, const char *message)
985 {
986 if (to) {
987 putsock("%s " P10_ERROR " :%s", to, message);
988 } else {
989 putsock(":%s " P10_ERROR " :%s", self->name, message);
990 }
991 }
992
993 void
994 irc_kill(struct userNode *from, struct userNode *target, const char *message)
995 {
996 if (from) {
997 putsock("%s " P10_KILL " %s :%s!%s (%s)",
998 from->numeric, target->numeric, self->name, from->nick, message);
999 } else {
1000 putsock("%s " P10_KILL " %s :%s (%s)",
1001 self->numeric, target->numeric, self->name, message);
1002 }
1003 }
1004
1005 void
1006 irc_mode(struct userNode *from, struct chanNode *target, const char *modes)
1007 {
1008 call_channel_mode_funcs(from, target, (char **)&modes, 0);
1009 putsock("%s " P10_MODE " %s %s "FMT_TIME_T,
1010 (from ? from->numeric : self->numeric),
1011 target->name, modes, target->timestamp);
1012 }
1013
1014 /* Added to allow services to mode users
1015 2005 - 8 - 10 by Life4Christ
1016 */
1017 void
1018 irc_umode(struct userNode *target, const char *modes)
1019 {
1020 putsock("%s " P10_MODE " %s %s ",self->numeric,target->nick, modes);
1021 }
1022
1023
1024 void
1025 irc_invite(struct userNode *from, struct userNode *who, struct chanNode *to)
1026 {
1027 putsock("%s " P10_INVITE " %s %s", from->numeric, who->nick, to->name);
1028 }
1029
1030 void
1031 irc_silence(struct userNode *who, const char *mask, int add)
1032 {
1033 putsock("%s " P10_SILENCE " %s %s%s", self->numeric, who->numeric, add ? "+" : "-", mask);
1034 }
1035
1036 void
1037 irc_join(struct userNode *who, struct chanNode *what)
1038 {
1039 if (what->members.used == 1) {
1040 putsock("%s " P10_CREATE " %s "FMT_TIME_T,
1041 who->numeric, what->name, what->timestamp);
1042 } else {
1043 putsock("%s " P10_JOIN " %s "FMT_TIME_T, who->numeric, what->name, what->timestamp);
1044 }
1045 }
1046
1047 void
1048 irc_svsjoin(struct userNode *from, struct userNode *who, struct chanNode *to)
1049 {
1050 putsock("%s " P10_SVSJOIN " %s %s "FMT_TIME_T, from->uplink->numeric, who->numeric, to->name, now);
1051 }
1052
1053 void
1054 irc_svspart(struct userNode *from, struct userNode *who, struct chanNode *to)
1055 {
1056 putsock("%s " P10_SVSPART " %s %s", from->uplink->numeric, who->numeric, to->name);
1057 }
1058
1059 void
1060 irc_svsquit(struct userNode *from, struct userNode *who, char const *reason)
1061 {
1062 putsock("%s " P10_SVSQUIT " %s :%s", from->uplink->numeric, who->numeric, reason);
1063 }
1064
1065 void
1066 irc_kick(struct userNode *who, struct userNode *target, struct chanNode *channel, const char *msg)
1067 {
1068 const char *numeric;
1069 struct modeNode *mn = GetUserMode(channel, who);
1070 numeric = ((mn && (mn->modes & MODE_CHANOP)) || off_channel) ? who->numeric : self->numeric;
1071 putsock("%s " P10_KICK " %s %s :%s",
1072 numeric, channel->name, target->numeric, msg);
1073 }
1074
1075 void
1076 irc_stats(struct userNode *from, struct server *target, char type)
1077 {
1078 putsock("%s " P10_STATS " %c :%s", from->numeric, type, target->numeric);
1079 }
1080
1081 void
1082 irc_svsnick(struct userNode *from, struct userNode *target, const char *newnick)
1083 {
1084 putsock("%s " P10_SVSNICK " %s %s "FMT_TIME_T, from->uplink->numeric, target->numeric, newnick, now);
1085 }
1086
1087 void
1088 irc_swhois(struct userNode *from, struct userNode *target, const char *message)
1089 {
1090 putsock("%s " P10_SWHOIS " %s %s%s", from->uplink->numeric, target->numeric, message ? ":" : "",
1091 message ? message : "");
1092
1093 }
1094
1095 void
1096 irc_part(struct userNode *who, struct chanNode *what, const char *reason)
1097 {
1098 if (reason) {
1099 putsock("%s " P10_PART " %s :%s", who->numeric, what->name, reason);
1100 } else {
1101 putsock("%s " P10_PART " %s", who->numeric, what->name);
1102 }
1103 }
1104
1105 void
1106 irc_topic(struct userNode *service, struct userNode *who, struct chanNode *what, const char *topic)
1107 {
1108
1109 int type = 4, host_in_topic = 0, hasident = 0, hhtype = 0;
1110 const char *hstr, *tstr, *hhstr, *htstr;
1111 char *host, *hostmask;
1112 char shost[MAXLEN];
1113 char sident[MAXLEN];
1114
1115 tstr = conf_get_data("server/type", RECDB_QSTRING);
1116 hstr = conf_get_data("server/host_in_topic", RECDB_QSTRING);
1117 hhstr = conf_get_data("server/hidden_host", RECDB_QSTRING);
1118 htstr = conf_get_data("server/hidden_host_type", RECDB_QSTRING);
1119 if(tstr)
1120 type = atoi(tstr);
1121 else
1122 type = 4;/* default to 040 style topics */
1123 if (htstr)
1124 hhtype = atoi(htstr);
1125
1126 if (hstr) {
1127 if (IsHiddenHost(who) && IsFakeHost(who))
1128 safestrncpy(shost, who->fakehost, sizeof(shost));
1129 else if (IsHiddenHost(who) && IsSetHost(who)) {
1130 hostmask = strdup(who->sethost);
1131 if ((host = (strrchr(hostmask, '@')))) {
1132 hasident = 1;
1133 *host++ = '\0';
1134 } else
1135 host = hostmask;
1136
1137 if (hasident)
1138 safestrncpy(sident, hostmask, sizeof(shost));
1139 else
1140 safestrncpy(sident, who->ident, sizeof(shost));
1141
1142 safestrncpy(shost, host, sizeof(shost));
1143 } else if (IsHiddenHost(who) && ((hhtype == 1) || (hhtype == 3)) && who->handle_info && hhstr) {
1144 snprintf(shost, sizeof(shost), "%s.%s", who->handle_info->handle, hhstr);
1145 } else if (IsHiddenHost(who) && ((hhtype == 2) || (hhtype == 3)) && who->crypthost[0]) {
1146 safestrncpy(shost, who->crypthost, sizeof(shost));
1147 } else
1148 safestrncpy(shost, who->hostname, sizeof(shost));
1149
1150 host_in_topic = atoi(hstr);
1151 }
1152
1153 if (type >= 5) {
1154 putsock("%s " P10_TOPIC " %s %s%s%s%s%s " FMT_TIME_T " " FMT_TIME_T " :%s", service->numeric, what->name,
1155 who->nick, host_in_topic ? "!" : "", host_in_topic ? (IsSetHost(who) ? sident : who->ident) : "",
1156 host_in_topic ? "@" : "", host_in_topic ? shost : "", what->timestamp, now, topic);
1157 } else {
1158 who = service;
1159 putsock("%s " P10_TOPIC " %s :%s", who->numeric, what->name, topic);
1160 }
1161 }
1162
1163 void
1164 irc_raw(const char *what)
1165 {
1166 putsock("%s", what);
1167 }
1168
1169 void
1170 irc_numeric(struct userNode *user, unsigned int num, const char *format, ...)
1171 {
1172 va_list arg_list;
1173 char buffer[MAXLEN];
1174 va_start(arg_list, format);
1175 vsnprintf(buffer, MAXLEN-2, format, arg_list);
1176 buffer[MAXLEN-1] = 0;
1177 putsock(":%s %03d %s %s", self->name, num, user->nick, buffer);
1178 }
1179
1180 void
1181 irc_mark(struct userNode *user, char *mark)
1182 {
1183 char *host = user->hostname;
1184 int type = 4;
1185 const char *tstr = NULL;
1186
1187 /* 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 */
1188 if(user->mark)
1189 return;
1190
1191 tstr = conf_get_data("server/type", RECDB_QSTRING);
1192 if(tstr)
1193 type = atoi(tstr);
1194 else
1195 type = 4;
1196
1197 if (type >= 9)
1198 {
1199 putsock("%s " CMD_MARK " %s MARK %s", self->numeric, user->nick, mark);
1200 return;
1201 }
1202
1203 /* if the mark will put us over the host length, clip some off the left hand side
1204 * to make room...
1205 */
1206 if(strlen(host) + 1 + strlen(mark) > HOSTLEN)
1207 host += 1 + ( (strlen(host) + 1 + strlen(mark)) - HOSTLEN );
1208 putsock("%s " CMD_MARK " %s DNSBL +m %s.%s", self->numeric, user->nick, mark, host);
1209 putsock("%s " CMD_MARK " %s DNSBL_DATA %s", self->numeric, user->nick, mark);
1210
1211 /* Save it in the user */
1212 user->mark = strdup(mark);
1213
1214 /* If they are not otherwise marked, mark their host with fakehost */
1215 if(!IsFakeHost(user) && !IsSetHost(user) && !(IsHiddenHost(user) && user->handle_info) )
1216 {
1217 struct modeNode *mn = NULL;
1218 char fakehost[HOSTLEN];
1219 unsigned int count = 0;
1220 unsigned int n = 0;
1221
1222 putsock("%s " CMD_FAKEHOST " %s %s.%s", self->numeric, user->numeric, mark, host);
1223 putsock("%s " CMD_MODE " %s +x", self->numeric, user->nick);
1224
1225 snprintf(fakehost, sizeof(fakehost), "%s.%s", mark, host);
1226 safestrncpy(user->fakehost, fakehost, sizeof(user->fakehost));
1227
1228 for (n=count=0; n<user->channels.used; n++) {
1229 mn = user->channels.list[n];
1230 if (strlen(mn->channel->name) >= 1) /* Sanity */
1231 check_bans(user, mn->channel->name);
1232 }
1233 }
1234 }
1235
1236 void irc_sno(unsigned int mask, char const* format, ...) {
1237 va_list arg_list;
1238 char buffer[MAXLEN];
1239 va_start(arg_list, format);
1240 vsnprintf(buffer, MAXLEN-2, format, arg_list);
1241 buffer[MAXLEN-1] = 0;
1242 putsock("%s " CMD_SNO " %d :%s", self->numeric, mask, buffer);
1243 }
1244
1245 void
1246 irc_sasl(struct server* dest, const char *identifier, const char *subcmd, const char *data)
1247 {
1248 putsock("%s " P10_SASL " %s %s %s %s", self->numeric, dest->numeric, identifier, subcmd, data);
1249 }
1250
1251 static void send_burst(void);
1252
1253 static void
1254 change_nicklen(int new_nicklen)
1255 {
1256 unsigned int nn;
1257 char new_nick[NICKLEN+1];
1258 struct userNode *user;
1259
1260 nicklen = new_nicklen;
1261 /* fix up any users we have here */
1262 for (nn=0; nn<=self->num_mask; nn++) {
1263 if (!(user = self->users[nn]))
1264 continue;
1265 safestrncpy(new_nick, user->nick, sizeof(new_nick));
1266 new_nick[nicklen] = 0;
1267 NickChange(user, new_nick, 1);
1268 }
1269 }
1270
1271 static CMD_FUNC(cmd_whois)
1272 {
1273 struct userNode *from;
1274 struct userNode *who;
1275 char buf[MAXLEN];
1276 unsigned int i, mlen, len;
1277
1278 if (argc < 3)
1279 return 0;
1280 if (!(from = GetUserH(origin))) {
1281 log_module(MAIN_LOG, LOG_ERROR, "Could not find WHOIS origin user %s", origin);
1282 return 0;
1283 }
1284 if (!(who = GetUserH(argv[2]))) {
1285 irc_numeric(from, ERR_NOSUCHNICK, "%s@%s :No such nick", argv[2], self->name);
1286 return 1;
1287 }
1288
1289 if (IsFakeHost(who) && IsHiddenHost(who))
1290 irc_numeric(from, RPL_WHOISUSER, "%s %s %s * :%s", who->nick, who->ident, who->fakehost, who->info);
1291 else if (IsHiddenHost(who) && who->handle_info && hidden_host_suffix)
1292 irc_numeric(from, RPL_WHOISUSER, "%s %s %s.%s * :%s", who->nick, who->ident, who->handle_info->handle, hidden_host_suffix, who->info);
1293 else
1294 irc_numeric(from, RPL_WHOISUSER, "%s %s %s * :%s", who->nick, who->ident, who->hostname, who->info);
1295
1296 if (!IsService(who) || (from == who)) {
1297 struct modeNode *mn;
1298 mlen = strlen(self->name) + strlen(from->nick) + 12 + strlen(who->nick);
1299 len = 0;
1300 *buf = '\0';
1301 for (i = who->channels.used; i > 0; )
1302 {
1303 mn = who->channels.list[--i];
1304
1305 if (!IsOper(from) && (mn->channel->modes & (MODE_PRIVATE | MODE_SECRET)) && !GetUserMode(mn->channel, from))
1306 continue;
1307
1308 if (len + strlen(mn->channel->name) + mlen > MAXLEN - 5)
1309 {
1310 irc_numeric(from, RPL_WHOISCHANNELS, "%s :%s", who->nick, buf);
1311 *buf = '\0';
1312 len = 0;
1313 }
1314
1315 if (IsDeaf(who))
1316 *(buf + len++) = '-';
1317 if ((mn->channel->modes & (MODE_PRIVATE | MODE_SECRET)) && !GetUserMode(mn->channel, from))
1318 *(buf + len++) = '*';
1319 if (mn->modes & MODE_CHANOP)
1320 *(buf + len++) = '@';
1321 else if (mn->modes & MODE_HALFOP)
1322 *(buf + len++) = '%';
1323 else if (mn->modes & MODE_VOICE)
1324 *(buf + len++) = '+';
1325
1326 if (len)
1327 *(buf + len) = '\0';
1328 strcpy(buf + len, mn->channel->name);
1329 len += strlen(mn->channel->name);
1330 strcat(buf + len, " ");
1331 len++;
1332 }
1333 if (buf[0] != '\0')
1334 irc_numeric(from, RPL_WHOISCHANNELS, "%s :%s", who->nick, buf);
1335 }
1336
1337 if (his_servername && his_servercomment && !IsOper(from) && from != who)
1338 irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, his_servername, his_servercomment);
1339 else
1340 irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, who->uplink->name, who->uplink->description);
1341
1342 if (IsAway(who))
1343 irc_numeric(from, RPL_AWAY, "%s :Away", who->nick);
1344 if (IsOper(who))
1345 irc_numeric(from, RPL_WHOISOPERATOR, "%s :%s", who->nick, IsLocal(who) ? "is a megalomaniacal power hungry tyrant" : "is an IRC Operator");
1346 if (who->handle_info)
1347 irc_numeric(from, RPL_WHOISACCOUNT, "%s %s :is logged in as", who->nick, who->handle_info->handle);
1348 if (IsHiddenHost(who) && who->handle_info && (IsOper(from) || from == who))
1349 irc_numeric(from, RPL_WHOISACTUALLY, "%s %s@%s %s :Actual user@host, Actual IP", who->nick, who->ident, who->hostname, irc_ntoa(&who->ip));
1350 if (IsLocal(who) && !IsService(who) && (IsOper(from) || from == who))
1351 irc_numeric(from, RPL_WHOISIDLE, "%s %ld %ld :seconds idle, signon time", who->nick, now - who->idle_since, who->timestamp);
1352
1353 irc_numeric(from, RPL_ENDOFWHOIS, "%s :End of /WHOIS list", who->nick);
1354 return 1;
1355 }
1356
1357 static CMD_FUNC(cmd_server)
1358 {
1359 struct server *srv;
1360 const char *str;
1361
1362 if (argc < 8)
1363 return 0;
1364 if (self->uplink) {
1365 /* another server introduced us */
1366 srv = AddServer(GetServerH(origin), argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
1367 if (!srv)
1368 return 0;
1369 srv->self_burst = argv[5][0] == 'J';
1370 srv->burst = 1;
1371 } else {
1372 /* this must be our uplink */
1373 srv = self->uplink = AddServer(self, argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
1374 if (!srv)
1375 return 0;
1376 srv->self_burst = argv[5][0] == 'J';
1377 srv->burst = 1;
1378 if ((argv[7][0] == '+') && !force_n2k) {
1379 log_module(MAIN_LOG, LOG_WARNING, "Got Undernet-style SERVER message but \"force_n2k\" not on.");
1380 }
1381 send_burst();
1382 }
1383
1384 /* Fix up our timestamps if necessary. */
1385 if (srv->boot <= PREHISTORY) {
1386 /* Server from the mists of time.. */
1387 if (srv->hops == 1) {
1388 log_module(MAIN_LOG, LOG_ERROR, "Server %s claims to have booted at time "FMT_TIME_T". This is absurd.", srv->name, srv->boot);
1389 }
1390 } else if ((str = conf_get_data("server/reliable_clock", RECDB_QSTRING))
1391 && enabled_string(str)) {
1392 /* If we have a reliable clock, we just keep our current time. */
1393 } else {
1394 if (srv->boot <= self->boot) {
1395 /* The other server is older than us. Accept their timestamp.
1396 * Alternately, we are same age, but we accept their time
1397 * since we are linking to them. */
1398 self->boot = srv->boot;
1399 ioset_set_time(srv->link_time
1400 );
1401 }
1402 }
1403 if (srv == self->uplink) {
1404 extern time_t burst_begin;
1405 burst_begin = now;
1406 }
1407 return 1;
1408 }
1409
1410
1411 static CMD_FUNC(cmd_eob)
1412 {
1413 struct server *sender;
1414 dict_iterator_t it;
1415
1416 if (!(sender = GetServerH(origin)))
1417 return 0;
1418 if (sender == self->uplink) {
1419 cManager.uplink->state = CONNECTED;
1420 for (it = dict_first(unbursted_channels); it; it = iter_next(it))
1421 irc_burst(iter_data(it));
1422 dict_delete(unbursted_channels);
1423 unbursted_channels = NULL;
1424 irc_eob();
1425 irc_eob_ack();
1426
1427 /* now that we know who our uplink is,
1428 * we can center the routing map and activate auto-routing.
1429 */
1430 //activate_routing(NULL, NULL, NULL);
1431 routing_init();
1432 }
1433 sender->self_burst = 0;
1434 recalc_bursts(sender);
1435 call_server_link_funcs(sender);
1436 /* let auto-routing figure out if we were
1437 * wating on this server to link a child to it */
1438 /* DONT call this if uplink is _US_ */
1439 if(sender->uplink != self)
1440 routing_handle_connect(sender->name, sender->uplink->name);
1441 return 1;
1442 }
1443
1444 static CMD_FUNC(cmd_eob_ack)
1445 {
1446 extern time_t burst_begin;
1447
1448 if (GetServerH(origin) == self->uplink) {
1449 burst_length = now - burst_begin;
1450 self->self_burst = self->burst = 0;
1451 }
1452 cManager.uplink->state = CONNECTED;
1453 return 1;
1454 }
1455
1456 static CMD_FUNC(cmd_rping)
1457 {
1458 struct server *dest;
1459
1460 if (!(dest = GetServerN(argv[1])))
1461 return 1; /* if its a jupe or something, return 1 so its silently ignored */
1462
1463 if (dest == self)
1464 irc_rpong(argv[2], argv[3], argv[4], argv[5]);
1465
1466 return 1;
1467 }
1468
1469 static CMD_FUNC(cmd_sasl)
1470 {
1471 struct server *serv;
1472
1473 if (argc < 5)
1474 return 0;
1475
1476 serv = GetServerH(origin);
1477 if (!serv)
1478 return 0;
1479
1480 call_sasl_input_func(serv, argv[2], argv[3], argv[4], (argc>5 ? argv[5] : NULL));
1481
1482 return 1;
1483 }
1484
1485 static CMD_FUNC(cmd_ping)
1486 {
1487 struct server *srv;
1488 struct userNode *un;
1489
1490 if (argc > 3)
1491 irc_pong_asll(argv[2], argv[3]);
1492 else if ((srv = GetServerH(origin)))
1493 irc_pong(self->name, srv->numeric);
1494 else if ((un = GetUserH(origin)))
1495 irc_pong(self->name, un->numeric);
1496 else
1497 irc_pong(self->name, origin);
1498
1499 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1500 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1501 timeq_add(now + ping_freq, timed_send_ping, 0);
1502 received_ping();
1503 return 1;
1504 }
1505
1506 static CMD_FUNC(cmd_error_nick)
1507 {
1508 /* Go back to original IRC length .. and try to reconnect :/ */
1509 change_nicklen(9);
1510 irc_squit(self, "Got erroneous nickname, truncating nicks.", NULL);
1511 return 1;
1512 }
1513
1514 struct create_desc {
1515 struct userNode *user;
1516 time_t when;
1517 };
1518
1519 static void
1520 join_helper(struct chanNode *chan, void *data)
1521 {
1522 struct create_desc *cd = data;
1523 AddChannelUser(cd->user, chan);
1524 }
1525
1526 static void
1527 create_helper(char *name, void *data)
1528 {
1529 struct create_desc *cd = data;
1530
1531 if (!strcmp(name, "0")) {
1532 while (cd->user->channels.used > 0)
1533 DelChannelUser(cd->user, cd->user->channels.list[0]->channel, 0, 0);
1534 return;
1535 }
1536
1537 AddChannelUser(cd->user, AddChannel(name, cd->when, NULL, NULL, NULL));
1538 }
1539
1540 static CMD_FUNC(cmd_create)
1541 {
1542 struct create_desc cd;
1543 struct userNode *user;
1544
1545 if ((argc < 3) || !(user = GetUserH(origin)))
1546 return 0;
1547 cd.user = user;
1548 cd.when = atoi(argv[2]);
1549 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
1550 return 1;
1551 }
1552
1553 static CMD_FUNC(cmd_join)
1554 {
1555 struct create_desc cd;
1556
1557 if (!(cd.user = GetUserH(origin)))
1558 return 0;
1559 if (argc < 2)
1560 return 0;
1561 else if (argc < 3)
1562 cd.when = now;
1563 else
1564 cd.when = atoi(argv[2]);
1565 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
1566 return 1;
1567 }
1568
1569 static CMD_FUNC(cmd_svsjoin)
1570 {
1571 struct create_desc cd;
1572
1573 if (!(cd.user = GetUserH(argv[1])))
1574 return 0;
1575 if (argc < 3)
1576 return 0;
1577 else if (argc < 4)
1578 cd.when = now;
1579 else
1580 cd.when = atoi(argv[3]);
1581 parse_foreach(argv[2], join_helper, create_helper, NULL, NULL, &cd);
1582 return 1;
1583 }
1584
1585 static CMD_FUNC(cmd_pong)
1586 {
1587 if (argc < 3)
1588 return 0;
1589 if (!strcmp(argv[2], self->name)) {
1590 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1591 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
1592 timeq_add(now + ping_freq, timed_send_ping, 0);
1593 received_ping();
1594 }
1595 return 1;
1596 }
1597
1598 static CMD_FUNC(cmd_nick)
1599 {
1600 struct userNode *user;
1601 if ((user = GetUserH(origin))) {
1602 /* nick change (since the source is a user numeric) */
1603 if (argc < 2)
1604 return 0;
1605 NickChange(user, argv[1], 1);
1606 } else {
1607 struct server *serv;
1608 struct userNode *nuser;
1609 char modes[MAXLEN];
1610 /* new nick */
1611 if (argc < 9)
1612 return 0;
1613 serv = GetServerH(origin);
1614 if (argc > 9)
1615 unsplit_string(argv+6, argc-9, modes);
1616 else
1617 strcpy(modes, "+");
1618 nuser = AddUser(serv, argv[1], argv[4], argv[5], modes, argv[argc-2], argv[argc-1], atoi(argv[3]), argv[argc-3]);
1619 }
1620 return 1;
1621 }
1622
1623 static CMD_FUNC(cmd_account)
1624 {
1625 struct userNode *user;
1626 struct server *server;
1627 struct handle_info *hi;
1628
1629 if ((argc < 3) || !origin || !(server = GetServerH(origin)))
1630 return 0; /* Origin must be server. */
1631
1632 /* This next line appears to tremple origin.. why? */
1633 user = GetUserN(argv[1]);
1634 if (!user)
1635 return 1; /* A QUIT probably passed the ACCOUNT. */
1636
1637 if(!extended_accounts) /* any need for this function without? */
1638 return 1;
1639
1640 if(!strcmp(argv[2],"C"))
1641 {
1642 if((hi = loc_auth(NULL, argv[4], argv[5], NULL)))
1643 {
1644 /* Return a AC A */
1645 putsock("%s " P10_ACCOUNT " %s A %s "FMT_TIME_T, self->numeric, server->numeric , argv[3], hi->registered);
1646
1647 log_module(MAIN_LOG, LOG_DEBUG, "loc_auth: %s\n", user->nick);
1648 }
1649 else
1650 {
1651 /* Return a AC D */
1652 putsock("%s " P10_ACCOUNT " %s D %s", self->numeric, server->numeric , argv[3]);
1653 }
1654 return 1;
1655 }
1656 else if(!strcmp(argv[2],"H")) /* New enhanced (host) version of C */
1657 {
1658 if((hi = loc_auth(NULL, argv[5], argv[6], argv[4] )))
1659 {
1660 /* Return a AC A */
1661 putsock("%s " P10_ACCOUNT " %s A %s "FMT_TIME_T, self->numeric, server->numeric , argv[3], hi->registered);
1662 }
1663 else
1664 {
1665 /* Return a AC D */
1666 putsock("%s " P10_ACCOUNT " %s D %s", self->numeric, server->numeric , argv[3]);
1667 }
1668 return 1;
1669 }
1670 else if(!strcmp(argv[2],"S"))
1671 {
1672 if((hi = loc_auth(argv[5], argv[6], argv[7], argv[4])))
1673 {
1674 /* Return a AC A */
1675 putsock("%s " P10_ACCOUNT " %s A %s "FMT_TIME_T, self->numeric, server->numeric , argv[3], hi->registered);
1676 }
1677 else
1678 {
1679 /* Return a AC D */
1680 putsock("%s " P10_ACCOUNT " %s D %s", self->numeric, server->numeric , argv[3]);
1681 }
1682 return 1;
1683 }
1684 else if(!strcmp(argv[2],"R"))
1685 call_account_func(user, argv[3]);
1686 else
1687 call_account_func(user, argv[2]); /* For backward compatability */
1688 return 1;
1689 }
1690
1691 static CMD_FUNC(cmd_fakehost)
1692 {
1693 struct userNode *user;
1694
1695 if ((argc < 3) || !origin || !GetServerH(origin))
1696 return 0;
1697 if (!(user = GetUserN(argv[1])))
1698 return 1;
1699 assign_fakehost(user, argv[2], 0);
1700 return 1;
1701 }
1702
1703 static struct {
1704 char *name;
1705 unsigned int priv;
1706 } privtab[] = {
1707 #define P(priv) { #priv, PRIV_ ## priv }
1708 P(CHAN_LIMIT), P(MODE_LCHAN), P(WALK_LCHAN), P(DEOP_LCHAN),
1709 P(SHOW_INVIS), P(SHOW_ALL_INVIS), P(UNLIMIT_QUERY), P(KILL),
1710 P(LOCAL_KILL), P(REHASH), P(RESTART), P(DIE),
1711 P(GLINE), P(LOCAL_GLINE), P(JUPE), P(LOCAL_JUPE),
1712 P(OPMODE), P(LOCAL_OPMODE), P(SET), P(WHOX),
1713 P(BADCHAN), P(LOCAL_BADCHAN), P(SEE_CHAN), P(PROPAGATE),
1714 P(DISPLAY), P(SEE_OPERS), P(WIDE_GLINE), P(FORCE_OPMODE),
1715 P(FORCE_LOCAL_OPMODE), P(REMOTEREHASH), P(CHECK), P(SEE_SECRET_CHAN),
1716 P(SHUN), P(LOCAL_SHUN), P(WIDE_SHUN), P(ZLINE),
1717 P(LOCAL_ZLINE), P(WIDE_ZLINE), P(LIST_CHAN), P(WHOIS_NOTICE),
1718 P(HIDE_IDLE), P(XTRAOP), P(HIDE_CHANNELS), P(DISPLAY_MODE),
1719 P(FREEFORM), P(REMOVE), P(SPAMFILTER),
1720 #undef P
1721 { 0, 0 }
1722 };
1723
1724 char *client_report_privs(struct userNode *client)
1725 {
1726 int i;
1727
1728 privbuf[0] = '\0';
1729 for (i = 0; privtab[i].name; i++) {
1730 if (HasPriv(client, privtab[i].priv)) {
1731 strcat(privbuf, privtab[i].name);
1732 strcat(privbuf, " ");
1733 }
1734 }
1735
1736 privbuf[strlen(privbuf)] = 0;
1737
1738 return privbuf;
1739 }
1740
1741 int clear_privs(struct userNode *who) {
1742 int i = 0;
1743 assert(0 != who);
1744
1745 for (i = 0; privtab[i].name; i++)
1746 RevokePriv(who, privtab[i].priv);
1747
1748 return 0;
1749 }
1750
1751 int client_modify_priv_by_name(struct userNode *who, char *priv, int what) {
1752 int i = 0;
1753 assert(0 != priv);
1754 assert(0 != who);
1755
1756 for (i = 0; privtab[i].name; i++) {
1757 if (0 == strcmp(privtab[i].name, priv)) {
1758 if (what == PRIV_ADD)
1759 GrantPriv(who, privtab[i].priv);
1760 else if (what == PRIV_DEL) {
1761 RevokePriv(who, privtab[i].priv);
1762 }
1763 }
1764 }
1765 return 0;
1766 }
1767
1768 int check_priv(char *priv)
1769 {
1770 int i;
1771
1772 for (i = 0; privtab[i].name; i++) {
1773 if (0 == strcmp(privtab[i].name, priv)) {
1774 return 1;
1775 }
1776 }
1777 return 0;
1778 }
1779
1780 void
1781 irc_privs(struct userNode *target, char *flag, int add)
1782 {
1783 client_modify_priv_by_name(target, flag, add);
1784 putsock("%s " P10_PRIVS " %s %s%s", self->numeric, target->numeric, (add == PRIV_ADD) ? "+" : "-", flag);
1785 }
1786
1787 void
1788 irc_raw_privs(struct userNode *target, const char *privs)
1789 {
1790 putsock("%s " P10_PRIVS " %s %s", self->numeric, target->numeric, privs);
1791 }
1792
1793 static CMD_FUNC(cmd_privs)
1794 {
1795 char *tstr = NULL;
1796 char buf[512] = "";
1797 char *p = 0;
1798 char *tmp;
1799
1800 int what = PRIV_ADD;
1801 int type = 0;
1802
1803 unsigned int i;
1804
1805 struct server *sender;
1806 struct userNode *user;
1807
1808
1809 tstr = conf_get_data("server/type", RECDB_QSTRING);
1810 if(tstr)
1811 type = atoi(tstr);
1812
1813
1814 if (!(sender = GetServerH(origin))) { /* from oper */
1815 return 1; /* ignore as no services have privs set */
1816 } else { /* from server */
1817 if (type < 5)
1818 return 1; /* silently ignore */
1819
1820 user = argc > 1 ? GetUserN(argv[1]) : NULL;
1821
1822 if (!user)
1823 return 0;
1824
1825 for (i=1; i<argc; i++) {
1826 strcat(buf, argv[i]);
1827 strcat(buf, " ");
1828 }
1829
1830 for (i = 2; i < argc; i++) {
1831 if (*argv[i] == '+') { what = PRIV_ADD; argv[i]++; }
1832 if (*argv[i] == '-') { what = PRIV_DEL; argv[i]++; }
1833 for (tmp = x3_strtok(&p, argv[i], ","); tmp;
1834 tmp = x3_strtok(&p, NULL, ",")) {
1835 if (!strcmp(tmp, "PRIV_NONE")) {
1836 if (now > user->timestamp+5)
1837 clear_privs(user);
1838 break;
1839 } else
1840 client_modify_priv_by_name(user, tmp, what);
1841 }
1842 }
1843 }
1844 return 1;
1845 }
1846
1847 static CMD_FUNC(cmd_burst)
1848 {
1849 extern int rel_age;
1850 char modes[MAXLEN], *members = "";
1851 static char exemptlist[MAXLEN], banlist[MAXLEN];
1852 unsigned int next = 3, res = 1;
1853 int ctype = 0, echeck = 0, bcheck = 0;
1854 struct chanData *cData;
1855 struct chanNode *cNode;
1856 struct userNode *un;
1857 struct modeNode *mNode;
1858 long mode;
1859 int oplevel = -1;
1860 int type = 0;
1861 char *user, *end, sep;
1862 char *tstr;
1863 time_t in_timestamp;
1864 char* parm = NULL;
1865
1866 if (argc < 3)
1867 return 0;
1868 modes[0] = 0;
1869
1870 tstr = conf_get_data("server/type", RECDB_QSTRING);
1871 if(tstr) {
1872 type = atoi(tstr);
1873 }
1874
1875 exemptlist[0] = 0;
1876 banlist[0] = 0;
1877
1878 while (next < argc) {
1879 switch (argv[next][0]) {
1880 case '+': {
1881 const char *pos;
1882 int n_modes;
1883 for (pos=argv[next], n_modes = 1; *pos; pos++)
1884 if ((*pos == 'k') || (*pos == 'l') || (*pos == 'A')
1885 || (*pos == 'U') || ((type > 7) && (*pos == 'L')))
1886 n_modes++;
1887 if (next + n_modes > argc)
1888 n_modes = argc - next;
1889 unsplit_string(argv+next, n_modes, modes);
1890 next += n_modes;
1891 break;
1892 }
1893 case '%': {
1894 ctype = 1;
1895 for(parm = mysep(&argv[next], " "); /* parm = first param */
1896 parm; /* While param is not null */
1897 parm = mysep(&argv[next], " ") /* parm = next param */
1898 )
1899 {
1900 if (0 == strcmp("~", parm))
1901 ctype = 2;
1902 if (ctype == 1) {
1903 if (bcheck == 0) {
1904 /* strip % char off start of very first ban */
1905 if (strlen(parm) > 1) {
1906 strncat(banlist, strtok(parm, "%"), sizeof(banlist) - 1 - strlen(banlist));
1907 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1908 }
1909 bcheck = 1;
1910 } else {
1911 strncat(banlist, parm, sizeof(banlist) - 1 - strlen(banlist));
1912 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1913 }
1914 } else if (ctype == 2) {
1915 if (echeck == 0) {
1916 echeck = 1;
1917 } else {
1918 strncat(exemptlist, parm, sizeof(exemptlist) - 1 - strlen(exemptlist));
1919 strncat(exemptlist, " ", sizeof(exemptlist) - 1 - strlen(exemptlist));
1920 }
1921 }
1922 }
1923 next++;
1924 break;
1925 }
1926 default: members = argv[next++]; break;
1927 }
1928 }
1929
1930 in_timestamp = atoi(argv[2]);
1931 if ((cNode = dict_find(unbursted_channels, argv[1], NULL))) {
1932 cNode->timestamp = in_timestamp;
1933 dict_remove(unbursted_channels, cNode->name);
1934 irc_burst(cNode);
1935 }
1936 cNode = AddChannel(argv[1], in_timestamp, modes, banlist, exemptlist);
1937 cData = cNode->channel_info;
1938
1939 if (!cData) {
1940 if (cNode->modes & MODE_REGISTERED) {
1941 irc_join(opserv, cNode);
1942 irc_mode(opserv, cNode, "-z");
1943 irc_part(opserv, cNode, "");
1944 }
1945 }
1946
1947 /* Burst channel members in now. */
1948 for (user = members, sep = *members, mode = 0; sep; user = end) {
1949 for (end = user + 3; isalnum(*end) || *end == '[' || *end == ']'; end++) ;
1950 sep = *end++; end[-1] = 0;
1951 if (sep == ':') {
1952 mode = 0;
1953 while ((sep = *end++)) {
1954 if (sep == 'o') {
1955 mode |= MODE_CHANOP;
1956 oplevel = -1;
1957 } else if (sep == 'h') {
1958 mode |= MODE_HALFOP;
1959 oplevel = -1;
1960 } else if (sep == 'v') {
1961 mode |= MODE_VOICE;
1962 oplevel = -1;
1963 } else if (isdigit(sep)) {
1964 mode |= MODE_CHANOP;
1965 if (oplevel >= 0)
1966 oplevel += parse_oplevel(end);
1967 else
1968 oplevel = parse_oplevel(end);
1969 while (isdigit(*end)) end++;
1970 } else
1971 break;
1972 }
1973 if (rel_age < 0)
1974 mode = 0;
1975 }
1976 if (!(un = GetUserN(user))) {
1977 res = 0;
1978 continue;
1979 }
1980 if ((mNode = AddChannelUser(un, cNode))) {
1981 mNode->modes = mode;
1982 mNode->oplevel = oplevel;
1983 }
1984 }
1985
1986 return res;
1987 }
1988
1989 /* TODO:
1990 * This is a stub that doesn't actually do anything. It should be completed
1991 * so that bans on *!*@markname.* match users as it does in nefarious
1992 */
1993 static CMD_FUNC(cmd_mark)
1994 {
1995 struct userNode *target;
1996 /*
1997 * log_module(MAIN_LOG, LOG_ERROR, "DEBUG: mark, user %s, type %s, arg %s", argv[1], argv[2], argv[3]);
1998 */
1999
2000 if(argc < 4)
2001 return 0;
2002 if(!strcasecmp(argv[2], "DNSBL")) {
2003 /* DNSBL <modes> */
2004 return 1;
2005 }
2006 else if(!strcasecmp(argv[2], "DNSBL_DATA")) {
2007 /* DNSBL_DATA name */
2008 target = GetUserH(argv[1]);
2009 if(!target) {
2010 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose dnsbl mark is changing.", argv[1]);
2011 return 0;
2012 }
2013 target->mark = strdup(argv[3]);
2014 return 1;
2015
2016 }
2017 else if(!strcasecmp(argv[2], "CVERSION")) {
2018 /* CTCP VERSION mark */
2019 target = GetUserH(argv[1]);
2020 if(!target) {
2021 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose version mark is changing.", argv[1]);
2022 return 0;
2023 }
2024
2025 char *version = unsplit_string(argv + 3, argc - 3, NULL);
2026 if(!version)
2027 version = "";
2028
2029 target->version_reply = strdup(version);
2030
2031 if(match_ircglob(version, "WebTV;*"))
2032 target->no_notice = true; /* webbies cant see notices */
2033
2034 return 1;
2035 }
2036 else if(!strcasecmp(argv[2], "SSLCLIFP")) {
2037 /* SSL fingerprint mark */
2038 target = GetUserH(argv[1]);
2039 if(!target) {
2040 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose SSL fingerprint mark is changing.", argv[1]);
2041 return 0;
2042 }
2043
2044 char *sslfp = unsplit_string(argv + 3, argc - 3, NULL);
2045 if(!sslfp)
2046 sslfp = "";
2047
2048 target->sslfp = strdup(sslfp);
2049
2050 return 1;
2051 }
2052 /* unknown type of mark */
2053 return 1;
2054 }
2055
2056 static CMD_FUNC(cmd_mode)
2057 {
2058 struct chanNode *cn;
2059 struct userNode *un;
2060 char *sethost; // sethost - reed/apples
2061 int i; // sethost - reed/apples
2062
2063 if (argc < 3)
2064 return 0;
2065 if (!IsChannelName(argv[1])) {
2066 un = GetUserH(argv[1]);
2067 if (!un) {
2068 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose mode is changing.", argv[1]);
2069 return 0;
2070 }
2071 // sethost - reed/apples
2072 if (argc == 3)
2073 mod_usermode(un, argv[2]);
2074 else {
2075 sethost = malloc(strlen(argv[2]) + 1 + strlen(argv[3]) + 1);
2076 i = 0;
2077 while((sethost[i++] = *argv[2]++));
2078 i--;
2079 sethost[i++] = ' ';
2080 while((sethost[i++] = *argv[3]++));
2081 mod_usermode(un, sethost); // sethost - reed/apples
2082 }
2083
2084 return 1;
2085 }
2086
2087 if (!(cn = GetChannel(argv[1]))) {
2088 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
2089 return 0;
2090 }
2091 if ((un = GetUserH(origin))) {
2092 struct modeNode *mn;
2093 /* Update idle time for person setting the mode */
2094 if ((mn = GetUserMode(cn, un)))
2095 mn->idle_since = now;
2096 } else {
2097 /* If it came from a server, reset timestamp to re-sync. */
2098 cn->timestamp = atoi(argv[argc-1]);
2099 }
2100
2101 if (checkDefCon(DEFCON_NO_MODE_CHANGE) && !IsOper(un)) {
2102 const char *str;
2103 str = conf_get_data("services/opserv/nick", RECDB_QSTRING);
2104 if (str) {
2105 char modes[MODELEN];
2106 struct userNode *opserv = GetUserH(str);
2107
2108 send_message_type(4, un, opserv, "Channel modes cannot be changed due to DefCon level %d in effect, please try again soon", DefConLevel);
2109 irc_make_chanmode(cn, modes);
2110 irc_mode(opserv, cn, modes);
2111 irc_mode(opserv, cn, DefConChanModes);
2112 }
2113 return 1;
2114 }
2115
2116
2117 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER|(un ? MC_NOTIFY : 0));
2118 }
2119
2120 static CMD_FUNC(cmd_opmode)
2121 {
2122 struct chanNode *cn;
2123 struct userNode *un;
2124
2125 if (argc < 3)
2126 return 0;
2127
2128 if (!(cn = GetChannel(argv[1]))) {
2129 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
2130 return 0;
2131 }
2132 if (!(un = GetUserH(origin))) {
2133 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting OPMODE.", origin);
2134 return 0;
2135 }
2136 if (!IsOper(un)) {
2137 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using OPMODE.", un->nick);
2138 return 0;
2139 }
2140
2141 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER); /* do NOT announce opmode locally */
2142 }
2143
2144 static int clear_chanmode(struct chanNode *channel, const char *modes);
2145
2146 static CMD_FUNC(cmd_clearmode)
2147 {
2148 struct chanNode *cn;
2149 struct userNode *un;
2150
2151 if (argc < 3)
2152 return 0;
2153
2154 if (!(cn = GetChannel(argv[1]))) {
2155 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
2156 return 0;
2157 }
2158 if (!(un = GetUserH(origin))) {
2159 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting CLEARMODE.", origin);
2160 return 0;
2161 }
2162 if (!IsOper(un)) {
2163 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using CLEARMODE.", un->nick);
2164 return 0;
2165 }
2166
2167 return clear_chanmode(cn, argv[2]);
2168 }
2169
2170 static CMD_FUNC(cmd_topic)
2171 {
2172 struct chanNode *cn;
2173 time_t chan_ts, topic_ts;
2174 struct userNode *user;
2175
2176 if (argc < 3)
2177 return 0;
2178 if (!(cn = GetChannel(argv[1]))) {
2179 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose topic is being set", argv[1]);
2180 return 0;
2181 }
2182
2183
2184 if (argc == 5) { /* Asuka / Topic Bursting IRCu's */
2185 user = GetUserH(origin);
2186 chan_ts = atoi(argv[2]);
2187 topic_ts = atoi(argv[3]);
2188 } else if (argc >= 6) { /* Nefarious 0.5.0 */
2189 user = GetUserH(strtok(argv[2], "!"));
2190 chan_ts = atoi(argv[3]);
2191 topic_ts = atoi(argv[4]);
2192 } else { /* Regular IRCu (No Topic Bursting)*/
2193 user = GetUserH(origin);
2194 chan_ts = cn->timestamp;
2195 topic_ts = now;
2196 }
2197
2198 SetChannelTopic(cn, user, user, argv[argc-1], 0);
2199 cn->topic_time = topic_ts;
2200 return 1;
2201 }
2202
2203 static CMD_FUNC(cmd_num_topic)
2204 {
2205 struct chanNode *cn;
2206
2207 if (!argv[0])
2208 return 0; /* huh? */
2209 if (argv[2]) {
2210 cn = GetChannel(argv[2]);
2211 if (!cn) {
2212 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s in topic reply", argv[2]);
2213 return 0;
2214 }
2215 } else
2216 return 0;
2217
2218 switch (atoi(argv[0])) {
2219 case 331:
2220 cn->topic_time = 0;
2221 break; /* no topic */
2222 case 332:
2223 if (argc < 4)
2224 return 0;
2225 safestrncpy(cn->topic, unsplit_string(argv+3, argc-3, NULL), sizeof(cn->topic));
2226 break;
2227 case 333:
2228 if (argc < 5)
2229 return 0;
2230 safestrncpy(cn->topic_nick, argv[3], sizeof(cn->topic_nick));
2231 cn->topic_time = atoi(argv[4]);
2232 break;
2233 default:
2234 return 0; /* should never happen */
2235 }
2236 return 1;
2237 }
2238
2239 static CMD_FUNC(cmd_num_gline)
2240 {
2241 if (argc < 7) {
2242 if (argc < 6)
2243 return 0;
2244 else
2245 gline_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0, 0);
2246 } else {
2247 if (!strcmp(argv[5], "+"))
2248 gline_add(origin, argv[3], atoi(argv[4])-now, argv[6], now, 0, 0);
2249 }
2250 return 1;
2251 }
2252
2253 static CMD_FUNC(cmd_num_shun)
2254 {
2255 if (argc < 7) {
2256 if (argc < 6)
2257 return 0;
2258 else
2259 shun_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0);
2260 } else {
2261 if (!strcmp(argv[5], "+"))
2262 shun_add(origin, argv[3], atoi(argv[4])-now, argv[6], now, 0);
2263 }
2264 return 1;
2265 }
2266
2267 static CMD_FUNC(cmd_quit)
2268 {
2269 struct userNode *user;
2270 if (argc < 2)
2271 return 0;
2272 /* Sometimes we get a KILL then a QUIT or the like, so we don't want to
2273 * call DelUser unless we have the user in our grasp. */
2274 if ((user = GetUserH(origin)))
2275 DelUser(user, NULL, false, argv[1]);
2276 return 1;
2277 }
2278
2279 static CMD_FUNC(cmd_kill)
2280 {
2281 struct userNode *user;
2282 if (argc < 2)
2283 return 0;
2284 user = GetUserN(argv[1]);
2285 if (!user) {
2286 /* If we get a KILL for a non-existent user, it could be a
2287 * Ghost response to a KILL we sent out earlier. So we only
2288 * whine if the target is local.
2289 */
2290 if (!strncmp(argv[1], self->numeric, strlen(self->numeric))) {
2291 log_module(MAIN_LOG, LOG_ERROR, "Unable to find kill victim %s", argv[1]);
2292 return 0;
2293 }
2294 return 1;
2295 }
2296
2297 if (IsLocal(user) && IsService(user)) {
2298 /* TODO: rate limit this so silly things don't happen. */
2299 ReintroduceUser(user);
2300 return 1;
2301 }
2302
2303 DelUser(user, NULL, false, argv[2]);
2304 return 1;
2305 }
2306
2307 static CMD_FUNC(cmd_svspart)
2308 {
2309 struct userNode *user;
2310
2311 if (argc < 3)
2312 return 0;
2313 user = GetUserN(argv[1]);
2314 if (!user)
2315 return 0;
2316 parse_foreach(argv[2], part_helper, NULL, NULL, NULL, user);
2317 return 1;
2318 }
2319
2320 static CMD_FUNC(cmd_silence)
2321 {
2322 struct userNode *user;
2323 char *mask;
2324 char *new_mask;
2325 unsigned int i;
2326
2327 if (argc < 2)
2328 return 0;
2329
2330 user = GetUserN(argv[1]);
2331
2332 /* Sanity, go nuts if this happens */
2333 if (!user)
2334 return 0;
2335
2336 /* We can safely ignore this if a user adding a silence is not
2337 * ignored. However this brings up a TODO. If a user logs in and
2338 * they have silences on the IRCd then we need to set them here
2339 * somehow
2340 */
2341 if (!user->handle_info)
2342 return 1;
2343
2344 mask = argv[2];
2345
2346 if (*mask == '-') {
2347 for (i=0; i<user->handle_info->ignores->used; i++) {
2348 if (!irccasecmp(mask+1, user->handle_info->ignores->list[i]))
2349 user->handle_info->ignores->list[i] = user->handle_info->ignores->list[--user->handle_info->ignores->used];
2350 }
2351 } else {
2352 for (i=0; i<user->handle_info->ignores->used; i++) {
2353 if (!strcmp(mask+1, user->handle_info->ignores->list[i]))
2354 return 1; /* Already on the users NickServ ignore list, safely ignore */
2355 }
2356
2357 new_mask = strdup(mask+1);
2358 string_list_append(user->handle_info->ignores, new_mask);
2359 }
2360 return 1;
2361 }
2362
2363 static CMD_FUNC(cmd_kick)
2364 {
2365 if (argc < 3)
2366 return 0;
2367 ChannelUserKicked(GetUserH(origin), GetUserN(argv[2]), GetChannel(argv[1]));
2368 return 1;
2369 }
2370
2371 static CMD_FUNC(cmd_squit)
2372 {
2373 struct server *server;
2374 char *uplink;
2375
2376 if (argc < 4)
2377 return 0;
2378 if (!(server = GetServerH(argv[1])))
2379 return 0;
2380
2381 if (server == self->uplink) {
2382 /* Force a reconnect to the currently selected server. */
2383 cManager.uplink->tries = 0;
2384 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", argv[3]);
2385 close_socket();
2386 return 1;
2387 }
2388
2389 uplink = strdup(server->uplink->name);
2390 DelServer(server, 0, argv[3]);
2391 /* if its a pingout and pingout connecting is enabled
2392 or its a read error and readerror connecting is enabled
2393 or were doing a "N" and i need to connect that server somewhere */
2394 routing_handle_squit(argv[1], uplink, argv[3]);
2395 free(uplink);
2396 return 1;
2397 }
2398
2399 static CMD_FUNC(cmd_privmsg)
2400 {
2401 struct privmsg_desc pd;
2402 if (argc < 3)
2403 return 0;
2404 pd.user = GetUserH(origin);
2405 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
2406 return 1;
2407
2408 if (checkDefCon(DEFCON_OPER_ONLY) && !IsOper(pd.user)) {
2409 const char *str;
2410 str = conf_get_data("services/opserv/nick", RECDB_QSTRING);
2411 if (str)
2412 send_message_type(4, pd.user, GetUserH(str), "Services are currently not available, please try again soon");
2413 return 1;
2414 }
2415
2416 if (checkDefCon(DEFCON_SILENT_OPER_ONLY) && !IsOper(pd.user))
2417 return 1; /* Silently Ignore */
2418
2419 pd.is_notice = 0;
2420 pd.text = argv[argc - 1];
2421 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
2422
2423 return 1;
2424 }
2425
2426 static CMD_FUNC(cmd_notice)
2427 {
2428 struct privmsg_desc pd;
2429 struct server *srv;
2430 int nuser = 0;
2431
2432 if (argc != 3)
2433 return 0;
2434
2435 pd.user = GetUserH(origin);
2436 if(!pd.user)
2437 nuser = 1;
2438 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user))) {
2439 }
2440 else {
2441 pd.is_notice = 1;
2442 pd.text = argv[2];
2443 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
2444 }
2445
2446 srv = GetServerH(origin);
2447 if(srv) {
2448 char *sargv[MAXNUMPARAMS];
2449 int sargc;
2450
2451 sargc = split_line(argv[2], true, MAXNUMPARAMS, sargv);
2452
2453 if(!strcasecmp(sargv[0], "Connect:")) {
2454 /* :Connect: Host shoe.loxxin.net not listed in ircd.conf */
2455 if(!strcasecmp(sargv[3], "not") && !strcasecmp(sargv[4], "listed")) {
2456 routing_handle_connect_failure(srv, sargv[2], unsplit_string(sargv+3, sargc-3, NULL));
2457 }
2458 }
2459 else if(!strcasecmp(sargv[0], "Link")) {
2460 /* :Link with mephisto.etheria.cx cancelled: Server mephisto.etheria.cx[216.46.33.71]
2461 *
2462 * :Link with laptop.afternet.org cancelled: Connection refused
2463 */
2464 if(!strcasecmp(sargv[3], "cancelled:")) {
2465 routing_handle_connect_failure(srv, sargv[2], unsplit_string(sargv+4, sargc-4, NULL));
2466 }
2467 }
2468 }
2469 return 1;
2470 }
2471
2472 static CMD_FUNC(cmd_away)
2473 {
2474 struct userNode *uNode;
2475
2476 uNode = GetUserH(origin);
2477 if (!uNode)
2478 return 1;
2479 if (argc < 2)
2480 uNode->modes &= ~FLAGS_AWAY;
2481 else
2482 uNode->modes |= FLAGS_AWAY;
2483 return 1;
2484 }
2485
2486 static CMD_FUNC(cmd_gline)
2487 {
2488 if (argc < 3)
2489 return 0;
2490 if (argv[2][0] == '+') {
2491 if (argc < 5)
2492 return 0;
2493 gline_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0, 0);
2494 return 1;
2495 } else if (argv[2][0] == '-') {
2496 gline_remove(argv[2]+1, 0);
2497 return 1;
2498 } else
2499 return 0;
2500 }
2501
2502 static CMD_FUNC(cmd_sgline)
2503 {
2504 struct server *sender;
2505
2506 if (argc < 4)
2507 return 0;
2508
2509 if (!(sender = GetServerH(origin)))
2510 return 0;
2511
2512 gline_add(origin, argv[1], strtoul(argv[2], NULL, 0), argv[argc-1], now, 1, 0);
2513 return 1;
2514 }
2515
2516 static CMD_FUNC(cmd_sshun)
2517 {
2518 struct server *sender;
2519
2520 if (argc < 4)
2521 return 0;
2522
2523 if (!(sender = GetServerH(origin)))
2524 return 0;
2525
2526 shun_add(origin, argv[1], strtoul(argv[2], NULL, 0), argv[argc-1], now, 1);
2527 return 1;
2528 }
2529
2530 static CMD_FUNC(cmd_shun)
2531 {
2532 if (argc < 3)
2533 return 0;
2534 if (argv[2][0] == '+') {
2535 if (argc < 5)
2536 return 0;
2537 shun_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0);
2538 return 1;
2539 } else if (argv[2][0] == '-') {
2540 shun_remove(argv[2]+1, 0);
2541 return 1;
2542 } else
2543 return 0;
2544 }
2545
2546 static CMD_FUNC(cmd_svsnick)
2547 {
2548 struct userNode *target, *dest;
2549 if ((argc < 4)
2550 || !(target = GetUserN(argv[1]))
2551 || !IsLocal(target)
2552 || (dest = GetUserH(argv[2])))
2553 return 0;
2554 NickChange(target, argv[2], 0);
2555 return 1;
2556 }
2557
2558 void
2559 free_user(struct userNode *user)
2560 {
2561 free(user->nick);
2562 free(user);
2563 }
2564
2565 static void
2566 parse_cleanup(UNUSED_ARG(void *extra))
2567 {
2568 unsigned int nn;
2569 free(of_list);
2570 free(of_list_extra);
2571 free(privmsg_funcs);
2572 num_privmsg_funcs = 0;
2573 free(notice_funcs);
2574 num_notice_funcs = 0;
2575 free(mcf_list);
2576 dict_delete(irc_func_dict);
2577 for (nn=0; nn<dead_users.used; nn++)
2578 free_user(dead_users.list[nn]);
2579 userList_clean(&dead_users);
2580 }
2581
2582 static void
2583 p10_conf_reload(void) {
2584 hidden_host_suffix = conf_get_data("server/hidden_host", RECDB_QSTRING);
2585 his_servername = conf_get_data("server/his_servername", RECDB_QSTRING);
2586 his_servercomment = conf_get_data("server/his_servercomment", RECDB_QSTRING);
2587 }
2588
2589 static void
2590 remove_unbursted_channel(struct chanNode *cNode, UNUSED_ARG(void *extra)) {
2591 if (unbursted_channels)
2592 dict_remove(unbursted_channels, cNode->name);
2593 }
2594
2595 void
2596 init_parse(void)
2597 {
2598 const char *str, *desc;
2599 int numnick, usermask, max_users;
2600 char numer[COMBO_NUMERIC_LEN+1];
2601
2602 /* read config items */
2603 str = conf_get_data("server/extended_accounts", RECDB_QSTRING);
2604 extended_accounts = str ? enabled_string(str) : 1;
2605 str = conf_get_data("server/ping_freq", RECDB_QSTRING);
2606 ping_freq = str ? ParseInterval(str) : 120;
2607 str = conf_get_data("server/ping_timeout", RECDB_QSTRING);
2608 ping_timeout = str ? ParseInterval(str) : 30;
2609 str = conf_get_data("server/force_n2k", RECDB_QSTRING);
2610 force_n2k = str ? enabled_string(str) : 1;
2611 str = conf_get_data("server/numeric", RECDB_QSTRING);
2612 if (!str) {
2613 log_module(MAIN_LOG, LOG_ERROR, "No server/numeric entry in config file.");
2614 exit(1);
2615 }
2616 numnick = atoi(str);
2617 str = conf_get_data("server/max_users", RECDB_QSTRING);
2618 max_users = str ? atoi(str) : 4096;
2619 for (usermask = 4; usermask < max_users; usermask <<= 1) ;
2620 usermask--;
2621 if ((numnick < 64) && (usermask < 4096) && !force_n2k)
2622 inttobase64(numer, (numnick << 12) + (usermask & 0x00fff), 3);
2623 else
2624 inttobase64(numer, (numnick << 18) + (usermask & 0x3ffff), 5);
2625
2626 str = conf_get_data("server/hostname", RECDB_QSTRING);
2627 desc = conf_get_data("server/description", RECDB_QSTRING);
2628 if (!str || !desc) {
2629 log_module(MAIN_LOG, LOG_ERROR, "No server/hostname entry in config file.");
2630 exit(1);
2631 }
2632 self = AddServer(NULL, str, 0, boot_time, now, numer, desc);
2633 conf_register_reload(p10_conf_reload);
2634
2635 irc_func_dict = dict_new();
2636 dict_insert(irc_func_dict, CMD_BURST, cmd_burst);
2637 dict_insert(irc_func_dict, TOK_BURST, cmd_burst);
2638 dict_insert(irc_func_dict, CMD_CREATE, cmd_create);
2639 dict_insert(irc_func_dict, TOK_CREATE, cmd_create);
2640 dict_insert(irc_func_dict, CMD_EOB, cmd_eob);
2641 dict_insert(irc_func_dict, TOK_EOB, cmd_eob);
2642 dict_insert(irc_func_dict, CMD_EOB_ACK, cmd_eob_ack);
2643 dict_insert(irc_func_dict, TOK_EOB_ACK, cmd_eob_ack);
2644 dict_insert(irc_func_dict, CMD_MODE, cmd_mode);
2645 dict_insert(irc_func_dict, TOK_MODE, cmd_mode);
2646 dict_insert(irc_func_dict, CMD_MARK, cmd_mark);
2647 dict_insert(irc_func_dict, TOK_MARK, cmd_mark);
2648 dict_insert(irc_func_dict, CMD_NICK, cmd_nick);
2649 dict_insert(irc_func_dict, TOK_NICK, cmd_nick);
2650 dict_insert(irc_func_dict, CMD_ACCOUNT, cmd_account);
2651 dict_insert(irc_func_dict, TOK_ACCOUNT, cmd_account);
2652 dict_insert(irc_func_dict, CMD_FAKEHOST, cmd_fakehost);
2653 dict_insert(irc_func_dict, TOK_FAKEHOST, cmd_fakehost);
2654 dict_insert(irc_func_dict, CMD_PASS, cmd_pass);
2655 dict_insert(irc_func_dict, TOK_PASS, cmd_pass);
2656 dict_insert(irc_func_dict, CMD_PING, cmd_ping);
2657 dict_insert(irc_func_dict, TOK_PING, cmd_ping);
2658 dict_insert(irc_func_dict, CMD_PRIVMSG, cmd_privmsg);
2659 dict_insert(irc_func_dict, TOK_PRIVMSG, cmd_privmsg);
2660 dict_insert(irc_func_dict, CMD_PONG, cmd_pong);
2661 dict_insert(irc_func_dict, TOK_PONG, cmd_pong);
2662 dict_insert(irc_func_dict, CMD_QUIT, cmd_quit);
2663 dict_insert(irc_func_dict, TOK_QUIT, cmd_quit);
2664 dict_insert(irc_func_dict, CMD_SERVER, cmd_server);
2665 dict_insert(irc_func_dict, TOK_SERVER, cmd_server);
2666 dict_insert(irc_func_dict, CMD_JOIN, cmd_join);
2667 dict_insert(irc_func_dict, TOK_JOIN, cmd_join);
2668 dict_insert(irc_func_dict, CMD_PART, cmd_part);
2669 dict_insert(irc_func_dict, TOK_PART, cmd_part);
2670 dict_insert(irc_func_dict, CMD_ERROR, cmd_error);
2671 dict_insert(irc_func_dict, TOK_ERROR, cmd_error);
2672 dict_insert(irc_func_dict, CMD_TOPIC, cmd_topic);
2673 dict_insert(irc_func_dict, TOK_TOPIC, cmd_topic);
2674 dict_insert(irc_func_dict, CMD_AWAY, cmd_away);
2675 dict_insert(irc_func_dict, TOK_AWAY, cmd_away);
2676 dict_insert(irc_func_dict, CMD_SILENCE, cmd_silence);
2677 dict_insert(irc_func_dict, TOK_SILENCE, cmd_silence);
2678 dict_insert(irc_func_dict, CMD_KICK, cmd_kick);
2679 dict_insert(irc_func_dict, TOK_KICK, cmd_kick);
2680 dict_insert(irc_func_dict, CMD_SQUIT, cmd_squit);
2681 dict_insert(irc_func_dict, TOK_SQUIT, cmd_squit);
2682 dict_insert(irc_func_dict, CMD_KILL, cmd_kill);
2683 dict_insert(irc_func_dict, TOK_KILL, cmd_kill);
2684 dict_insert(irc_func_dict, CMD_NOTICE, cmd_notice);
2685 dict_insert(irc_func_dict, TOK_NOTICE, cmd_notice);
2686 dict_insert(irc_func_dict, CMD_STATS, cmd_stats);
2687 dict_insert(irc_func_dict, TOK_STATS, cmd_stats);
2688 dict_insert(irc_func_dict, CMD_SVSJOIN, cmd_svsjoin);
2689 dict_insert(irc_func_dict, TOK_SVSJOIN, cmd_svsjoin);
2690 dict_insert(irc_func_dict, CMD_SVSNICK, cmd_svsnick);
2691 dict_insert(irc_func_dict, TOK_SVSNICK, cmd_svsnick);
2692 dict_insert(irc_func_dict, CMD_SVSPART, cmd_svspart);
2693 dict_insert(irc_func_dict, TOK_SVSPART, cmd_svspart);
2694 dict_insert(irc_func_dict, CMD_SWHOIS, cmd_dummy);
2695 dict_insert(irc_func_dict, TOK_SWHOIS, cmd_dummy);
2696 dict_insert(irc_func_dict, CMD_WHOIS, cmd_whois);
2697 dict_insert(irc_func_dict, TOK_WHOIS, cmd_whois);
2698 dict_insert(irc_func_dict, CMD_GLINE, cmd_gline);
2699 dict_insert(irc_func_dict, TOK_GLINE, cmd_gline);
2700 dict_insert(irc_func_dict, CMD_SGLINE, cmd_sgline);
2701 dict_insert(irc_func_dict, TOK_SGLINE, cmd_sgline);
2702 dict_insert(irc_func_dict, CMD_SHUN, cmd_shun);
2703 dict_insert(irc_func_dict, TOK_SHUN, cmd_shun);
2704 dict_insert(irc_func_dict, CMD_SSHUN, cmd_sshun);
2705 dict_insert(irc_func_dict, TOK_SSHUN, cmd_sshun);
2706 dict_insert(irc_func_dict, CMD_OPMODE, cmd_opmode);
2707 dict_insert(irc_func_dict, TOK_OPMODE, cmd_opmode);
2708 dict_insert(irc_func_dict, CMD_CLEARMODE, cmd_clearmode);
2709 dict_insert(irc_func_dict, TOK_CLEARMODE, cmd_clearmode);
2710 dict_insert(irc_func_dict, CMD_VERSION, cmd_version);
2711 dict_insert(irc_func_dict, TOK_VERSION, cmd_version);
2712 dict_insert(irc_func_dict, CMD_ADMIN, cmd_admin);
2713 dict_insert(irc_func_dict, TOK_ADMIN, cmd_admin);
2714 dict_insert(irc_func_dict, CMD_SMO, cmd_dummy);
2715 dict_insert(irc_func_dict, TOK_SMO, cmd_dummy);
2716 dict_insert(irc_func_dict, CMD_SNO, cmd_dummy);
2717 dict_insert(irc_func_dict, TOK_SNO, cmd_dummy);
2718
2719 dict_insert(irc_func_dict, CMD_RPING, cmd_rping);
2720 dict_insert(irc_func_dict, TOK_RPING, cmd_rping);
2721 dict_insert(irc_func_dict, CMD_RPONG, cmd_dummy);
2722 dict_insert(irc_func_dict, TOK_RPONG, cmd_dummy);
2723
2724 dict_insert(irc_func_dict, CMD_SASL, cmd_sasl);
2725 dict_insert(irc_func_dict, TOK_SASL, cmd_sasl);
2726
2727 /* In P10, DESTRUCT doesn't do anything except be broadcast to servers.
2728 * Apparently to obliterate channels from any servers that think they
2729 * exist?
2730 */
2731 dict_insert(irc_func_dict, CMD_DESTRUCT, cmd_dummy);
2732 dict_insert(irc_func_dict, TOK_DESTRUCT, cmd_dummy);
2733 /* Ignore invites */
2734 dict_insert(irc_func_dict, CMD_INVITE, cmd_dummy);
2735 dict_insert(irc_func_dict, TOK_INVITE, cmd_dummy);
2736 /* DESYNCH is just informational, so ignore it */
2737 dict_insert(irc_func_dict, CMD_DESYNCH, cmd_dummy);
2738 dict_insert(irc_func_dict, TOK_DESYNCH, cmd_dummy);
2739 /* Ignore channel operator notices. */
2740 dict_insert(irc_func_dict, CMD_WALLCHOPS, cmd_dummy);
2741 dict_insert(irc_func_dict, TOK_WALLCHOPS, cmd_dummy);
2742 dict_insert(irc_func_dict, CMD_WALLVOICES, cmd_dummy);
2743 dict_insert(irc_func_dict, TOK_WALLVOICES, cmd_dummy);
2744 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
2745 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
2746 /* Ignore opers being silly. */
2747 dict_insert(irc_func_dict, CMD_WALLOPS, cmd_dummy);
2748 dict_insert(irc_func_dict, TOK_WALLOPS, cmd_dummy);
2749 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
2750 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
2751 dict_insert(irc_func_dict, TOK_WALLUSERS, cmd_dummy);
2752 /* Ignore dnsbl exemptions */
2753 dict_insert(irc_func_dict, TOK_EXEMPT, cmd_dummy);
2754 dict_insert(irc_func_dict, CMD_PRIVS, cmd_privs);
2755 dict_insert(irc_func_dict, TOK_PRIVS, cmd_privs);
2756 /* ignore ALIST for now */
2757 dict_insert(irc_func_dict, TOK_ALIST, cmd_dummy);
2758 dict_insert(irc_func_dict, CMD_ALIST, cmd_dummy);
2759 /* ignore SPAMFILTER */
2760 dict_insert(irc_func_dict, TOK_SPAMFILTER, cmd_dummy);
2761 dict_insert(irc_func_dict, CMD_SPAMFILTER, cmd_dummy);
2762 /* Ignore remote luser */
2763 dict_insert(irc_func_dict, TOK_LUSERS, cmd_dummy);
2764 /* We have reliable clock! Always! Wraaa! */
2765 dict_insert(irc_func_dict, CMD_SETTIME, cmd_dummy);
2766 dict_insert(irc_func_dict, TOK_SETTIME, cmd_dummy);
2767 /* Ignore ZLINE/ZL/REMOVE/RM */
2768 dict_insert(irc_func_dict, CMD_ZLINE, cmd_dummy);
2769 dict_insert(irc_func_dict, TOK_ZLINE, cmd_dummy);
2770 dict_insert(irc_func_dict, CMD_REMOVE, cmd_dummy);
2771 dict_insert(irc_func_dict, TOK_REMOVE, cmd_dummy);
2772
2773 /* ignore /trace and /motd commands targetted at us */
2774 dict_insert(irc_func_dict, TOK_TRACE, cmd_dummy);
2775 dict_insert(irc_func_dict, TOK_MOTD, cmd_dummy);
2776 dict_insert(irc_func_dict, TOK_UPING, cmd_dummy);
2777
2778 /* handle topics */
2779 dict_insert(irc_func_dict, "331", cmd_num_topic);
2780 dict_insert(irc_func_dict, "332", cmd_num_topic);
2781 dict_insert(irc_func_dict, "333", cmd_num_topic);
2782 dict_insert(irc_func_dict, "345", cmd_dummy); /* blah has been invited to blah */
2783 dict_insert(irc_func_dict, "432", cmd_error_nick); /* Erroneus [sic] nickname */
2784 /* ban list resetting */
2785 /* "stats g" responses */
2786 dict_insert(irc_func_dict, "230", cmd_dummy); /* ignore stats headers */
2787 dict_insert(irc_func_dict, "247", cmd_num_gline);
2788 dict_insert(irc_func_dict, "542", cmd_num_shun);
2789 dict_insert(irc_func_dict, "219", cmd_dummy); /* "End of /STATS report" */
2790 /* other numeric responses we might get */
2791 dict_insert(irc_func_dict, "401", cmd_dummy); /* target left network */
2792 dict_insert(irc_func_dict, "403", cmd_dummy); /* no such channel */
2793 dict_insert(irc_func_dict, "404", cmd_dummy); /* cannot send to channel */
2794 dict_insert(irc_func_dict, "439", cmd_dummy); /* target change too fast */
2795 dict_insert(irc_func_dict, "441", cmd_dummy); /* target isn't on that channel */
2796 dict_insert(irc_func_dict, "442", cmd_dummy); /* you aren't on that channel */
2797 dict_insert(irc_func_dict, "443", cmd_dummy); /* is already on channel (after invite?) */
2798 dict_insert(irc_func_dict, "461", cmd_dummy); /* Not enough parameters (after TOPIC w/ 0 args) */
2799 dict_insert(irc_func_dict, "467", cmd_dummy); /* Channel key already set */
2800
2801 num_privmsg_funcs = 16;
2802 privmsg_funcs = malloc(sizeof(privmsg_func_t)*num_privmsg_funcs);
2803 memset(privmsg_funcs, 0, sizeof(privmsg_func_t)*num_privmsg_funcs);
2804
2805 num_notice_funcs = 16;
2806 notice_funcs = malloc(sizeof(privmsg_func_t)*num_notice_funcs);
2807 memset(notice_funcs, 0, sizeof(privmsg_func_t)*num_notice_funcs);
2808
2809 userList_init(&dead_users);
2810 reg_del_channel_func(remove_unbursted_channel, NULL);
2811 reg_exit_func(parse_cleanup, NULL);
2812 // reg_notice_func(opserv, check_ctcp);
2813 }
2814
2815 int
2816 parse_line(char *line, int recursive)
2817 {
2818 char *argv[MAXNUMPARAMS], *origin;
2819 int argc, cmd, res=0;
2820 cmd_func_t *func;
2821
2822 argc = split_line(line, true, MAXNUMPARAMS, argv);
2823 cmd = self->uplink || !argv[0][1] || !argv[0][2];
2824 if (argc > cmd) {
2825 if (cmd) {
2826 if (argv[0][0] == ':') {
2827 origin = argv[0]+1;
2828 } else if (!argv[0][1] || !argv[0][2]) {
2829 struct server *sNode = GetServerN(argv[0]);
2830 origin = sNode ? sNode->name : 0;
2831 } else {
2832 struct userNode *uNode = GetUserN(argv[0]);
2833 origin = uNode ? uNode->nick : 0;
2834 }
2835 } else
2836 origin = 0;
2837 if ((func = dict_find(irc_func_dict, argv[cmd], NULL)))
2838 res = func(origin, argc-cmd, argv+cmd);
2839 }
2840 if (!res) {
2841 log_module(MAIN_LOG, LOG_ERROR, "PARSE ERROR on line: %s", unsplit_string(argv, argc, NULL));
2842 } else if (!recursive) {
2843 unsigned int i;
2844 for (i=0; i<dead_users.used; i++)
2845 free_user(dead_users.list[i]);
2846 dead_users.used = 0;
2847 }
2848 return res;
2849 }
2850
2851 static void
2852 parse_foreach(char *target_list, foreach_chanfunc cf, foreach_nonchan nc, foreach_userfunc uf, foreach_nonuser nu, void *data)
2853 {
2854 char *j, old;
2855
2856 do {
2857 j = target_list;
2858 while (*j != 0 && *j != ',')
2859 j++;
2860 old = *j;
2861 *j = 0;
2862
2863 if (IsChannelName(target_list)
2864 || (target_list[0] == '0' && target_list[1] == '\0')) {
2865 struct chanNode *chan = GetChannel(target_list);
2866
2867 if (chan) {
2868 if (cf)
2869 cf(chan, data);
2870 } else {
2871 if (nc)
2872 nc(target_list, data);
2873 }
2874 } else {
2875 struct userNode *user;
2876 struct privmsg_desc *pd = data;
2877
2878 pd->is_qualified = 0;
2879 if (*target_list == '@') {
2880 user = NULL;
2881 } else if (strchr(target_list, '@')) {
2882 struct server *server;
2883
2884 pd->is_qualified = 1;
2885 user = GetUserH(strtok(target_list, "@"));
2886 server = GetServerH(strtok(NULL, "@"));
2887
2888 if (user && (user->uplink != server)) {
2889 /* Don't attempt to index into any arrays
2890 using a user's numeric on another server. */
2891 user = NULL;
2892 }
2893 } else {
2894 user = GetUserN(target_list);
2895 }
2896
2897 if (user) {
2898 if (uf)
2899 uf(user, data);
2900 } else {
2901 if (nu)
2902 nu(target_list, data);
2903 }
2904 }
2905 target_list = j+1;
2906 } while (old == ',');
2907 }
2908
2909 static int
2910 get_local_numeric(void)
2911 {
2912 static unsigned int next_numeric = 0;
2913 if (self->clients > self->num_mask)
2914 return -1;
2915 while (self->users[next_numeric])
2916 if (++next_numeric > self->num_mask)
2917 next_numeric = 0;
2918 return next_numeric;
2919 }
2920
2921 static void
2922 make_numeric(struct server *svr, int local_num, char *outbuf)
2923 {
2924 int slen, llen;
2925
2926 if (force_n2k || svr->numeric[1]) {
2927 slen = 2;
2928 llen = 3;
2929 } else {
2930 slen = 1;
2931 llen = (local_num < 64*64) ? 2 : 3;
2932 }
2933 strncpy(outbuf, svr->numeric, slen);
2934 inttobase64(outbuf+slen, local_num, llen);
2935 outbuf[slen+llen] = 0;
2936 }
2937
2938 struct server *
2939 AddServer(struct server *uplink, const char *name, int hops, time_t boot, time_t link_time, const char *numeric, const char *description)
2940 {
2941 struct server* sNode;
2942 int slen, mlen;
2943
2944 if ((sNode = GetServerN(numeric))) {
2945 /* This means we're trying to re-add an existant server.
2946 * To be safe, we should forget the previous incarnation.
2947 * (And all its linked servers.)
2948 *
2949 * It usually only happens in replays when the original
2950 * had a ping timeout and the replay didn't (because
2951 * replaying a ping timeout invariably gets things wrong).
2952 */
2953 DelServer(sNode, 0, NULL);
2954 }
2955
2956 switch (strlen(numeric)) {
2957 case 5: slen = 2; mlen = 3; break;
2958 case 4: slen = 1; mlen = 3; break;
2959 case 3: slen = 1; mlen = 2; break;
2960 default:
2961 log_module(MAIN_LOG, LOG_ERROR, "AddServer(\"%s\", \"%s\", ...): Numeric %s has invalid length.", uplink->name, name, numeric);
2962 return NULL;
2963 }
2964
2965 sNode = calloc(1, sizeof(*sNode));
2966 sNode->uplink = uplink;
2967 safestrncpy(sNode->name, name, sizeof(sNode->name));
2968 sNode->num_mask = base64toint(numeric+slen, mlen);
2969 sNode->hops = hops;
2970 sNode->boot = boot;
2971 sNode->link_time = link_time;
2972 strncpy(sNode->numeric, numeric, slen);
2973 safestrncpy(sNode->description, description, sizeof(sNode->description));
2974 sNode->users = calloc(sNode->num_mask+1, sizeof(*sNode->users));
2975 serverList_init(&sNode->children);
2976 if (sNode->uplink) {
2977 /* uplink may be NULL if we're just building ourself */
2978 serverList_append(&sNode->uplink->children, sNode);
2979 }
2980 servers_num[base64toint(numeric, slen)] = sNode;
2981 dict_insert(servers, sNode->name, sNode);
2982 return sNode;
2983 }
2984
2985 void DelServer(struct server* serv, int announce, const char *message)
2986 {
2987 unsigned int i;
2988
2989 /* If we receive an ERROR command before the SERVER
2990 * command a NULL server can be passed */
2991 if (!serv)
2992 return;
2993
2994 /* Hrm, what's the right way to SQUIT some other server?
2995 * (This code is only to handle killing juped servers.) */
2996 if (announce && (serv->uplink == self) && (serv != self->uplink))
2997 irc_squit(serv, message, NULL);
2998
2999 /* must recursively remove servers linked to this one first */
3000 for (i=serv->children.used;i>0;)
3001 if (serv->children.list[--i] != self)
3002 DelServer(serv->children.list[i], false, NULL);
3003
3004 /* clean up server's user hash tables */
3005 for (i=0;i<=serv->num_mask;i++)
3006 if (serv->users[i])
3007 DelUser(serv->users[i], NULL, false, "server delinked");
3008
3009 /* delete server */
3010 if (serv->uplink)
3011 serverList_remove(&serv->uplink->children, serv);
3012 if (serv == self->uplink)
3013 self->uplink = NULL;
3014 servers_num[base64toint(serv->numeric, strlen(serv->numeric))] = NULL;
3015 dict_remove(servers, serv->name);
3016 serverList_clean(&serv->children);
3017 free(serv->users);
3018 free(serv);
3019 }
3020
3021 struct userNode *
3022 AddLocalUser(const char *nick, const char *ident, const char *hostname, const char *desc, const char *modes)
3023 {
3024 char numeric[COMBO_NUMERIC_LEN+1];
3025 int local_num = get_local_numeric();
3026 time_t timestamp = now;
3027 struct userNode *old_user = GetUserH(nick);
3028
3029 if (!modes)
3030 modes = "+oik";
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 service %s", nick);
3039 return 0;
3040 }
3041 if (!hostname)
3042 hostname = self->name;
3043 make_numeric(self, local_num, numeric);
3044 return AddUser(self, nick, ident, hostname, modes, numeric, desc, timestamp, "AAAAAA");
3045 }
3046
3047 struct userNode *
3048 AddClone(const char *nick, const char *ident, const char *hostname, const char *desc)
3049 {
3050 char numeric[COMBO_NUMERIC_LEN+1];
3051 int local_num = get_local_numeric();
3052 time_t timestamp = now;
3053 struct userNode *old_user = GetUserH(nick);
3054
3055 if (old_user) {
3056 if (IsLocal(old_user))
3057 return old_user;
3058 timestamp = old_user->timestamp - 1;
3059 }
3060 if (local_num == -1) {
3061 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for clone %s", nick);
3062 return 0;
3063 }
3064 make_numeric(self, local_num, numeric);
3065 return AddUser(self, nick, ident, hostname, "+i", numeric, desc, timestamp, "AAAAAA");
3066 }
3067
3068 int
3069 is_valid_nick(const char *nick) {
3070 unsigned int ii;
3071 /* IRC has some of The Most Fucked-Up ideas about character sets
3072 * in the world.. */
3073 if (!isalpha(*nick) && !strchr("{|}~[\\]^_`", *nick))
3074 return 0;
3075 for (ii = 0; nick[ii]; ++ii)
3076 if (!isalnum(nick[ii]) && !strchr("{|}~[\\]^-_`", nick[ii]))
3077 return 0;
3078 if (strlen(nick) > nicklen)
3079 return 0;
3080 return 1;
3081 }
3082
3083 static struct userNode*
3084 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)
3085 {
3086 struct userNode *oldUser, *uNode;
3087 unsigned int ignore_user, dummy;
3088 char *tstr;
3089 int type;
3090
3091 if ((strlen(numeric) < 3) || (strlen(numeric) > 5)) {
3092 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): numeric %s wrong length!", (void*)uplink, nick, numeric);
3093 return NULL;
3094 }
3095
3096 if (!uplink) {
3097 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s doesn't exist!", (void*)uplink, nick, numeric);
3098 return NULL;
3099 }
3100
3101 if (uplink != GetServerN(numeric)) {
3102 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s differs from nominal uplink %s.", (void*)uplink, nick, numeric, uplink->name);
3103 return NULL;
3104 }
3105
3106 dummy = modes && modes[0] == '*';
3107 if (dummy) {
3108 ++modes;
3109 } else if (!is_valid_nick(nick)) {
3110 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): invalid nickname detected.", (void*)uplink, nick);
3111 return NULL;
3112 }
3113
3114 ignore_user = 0;
3115 if ((oldUser = GetUserH(nick))) {
3116 if (IsLocal(oldUser)
3117 && (IsService(oldUser) || IsPersistent(oldUser))) {
3118 /* The service should collide the new user off - but not
3119 * if the new user is coming in during a burst. (During a
3120 * burst, the bursting server will kill either our user --
3121 * triggering a ReintroduceUser() -- or its own.)
3122 */
3123 oldUser->timestamp = timestamp - 1;
3124 ignore_user = 1;
3125 if (!uplink->burst)
3126 irc_user(oldUser);
3127 } else if (oldUser->timestamp > timestamp) {
3128 /* "Old" user is really newer; remove them */
3129 DelUser(oldUser, 0, 1, "Overruled by older nick");
3130 } else {
3131 /* User being added is too new; do not add them to
3132 * clients, but do add them to the server's list, since it
3133 * will send a KILL and QUIT soon. */
3134 ignore_user = 1;
3135 }
3136 }
3137
3138 /* create new usernode and set all values */
3139 uNode = calloc(1, sizeof(*uNode));
3140 uNode->nick = strdup(nick);
3141 safestrncpy(uNode->ident, ident, sizeof(uNode->ident));
3142 safestrncpy(uNode->info, userinfo, sizeof(uNode->info));
3143 safestrncpy(uNode->hostname, hostname, sizeof(uNode->hostname));
3144 safestrncpy(uNode->numeric, numeric, sizeof(uNode->numeric));
3145 irc_p10_pton(&uNode->ip, realip);
3146
3147 tstr = conf_get_data("server/type", RECDB_QSTRING);
3148 type = atoi(tstr);
3149 if (type == 7) {
3150 if (irc_in_addr_is_ipv4(uNode->ip)) {
3151 make_virtip((char*)irc_ntoa(&uNode->ip), (char*)irc_ntoa(&uNode->ip), uNode->cryptip);
3152 make_virthost((char*)irc_ntoa(&uNode->ip), uNode->hostname, uNode->crypthost);
3153 } else if (irc_in_addr_is_ipv6(uNode->ip)) {
3154 make_ipv6virthost((char*)irc_ntoa(&uNode->ip), uNode->hostname, uNode->crypthost);
3155 }
3156 }
3157
3158 if (!uNode->crypthost && uNode->cryptip)
3159 snprintf(uNode->crypthost, sizeof(uNode->crypthost), "%s", strdup(uNode->cryptip));
3160 uNode->idle_since = timestamp;
3161 uNode->timestamp = timestamp;
3162 modeList_init(&uNode->channels);
3163 uNode->uplink = uplink;
3164 if (++uNode->uplink->clients > uNode->uplink->max_clients) {
3165 uNode->uplink->max_clients = uNode->uplink->clients;
3166 }
3167 uNode->num_local = base64toint(numeric+strlen(uNode->uplink->numeric), 3) & uNode->uplink->num_mask;
3168 uNode->uplink->users[uNode->num_local] = uNode;
3169 mod_usermode(uNode, modes);
3170 if (dummy)
3171 uNode->modes |= FLAGS_DUMMY;
3172
3173 set_geoip_info(uNode);
3174
3175 if (ignore_user)
3176 return uNode;
3177
3178 dict_insert(clients, uNode->nick, uNode);
3179 if (dict_size(clients) > max_clients) {
3180 max_clients = dict_size(clients);
3181 max_clients_time = now;
3182 }
3183 if (IsLocal(uNode))
3184 irc_user(uNode);
3185 call_new_user_funcs(uNode);
3186
3187 if ((uNode->loc == 1) && (uNode->handle_info))
3188 send_func_list(uNode);
3189
3190 return uNode;
3191 }
3192
3193 /* removes user from it's server's hash table and nick hash table */
3194 void
3195 DelUser(struct userNode* user, struct userNode *killer, int announce, const char *why)
3196 {
3197
3198 verify(user);
3199
3200 /* mark them as dead, in case anybody cares */
3201 user->dead = 1;
3202
3203 /* remove pending adduser commands */
3204 wipe_adduser_pending(NULL, user);
3205
3206 /* remove user from all channels */
3207 while (user->channels.used > 0)
3208 DelChannelUser(user, user->channels.list[user->channels.used-1]->channel, NULL, 0);
3209
3210 /* Call these in reverse order so ChanServ can update presence
3211 information before NickServ nukes the handle_info. */
3212 call_del_user_funcs(user, killer, why);
3213
3214 user->uplink->clients--;
3215 user->uplink->users[user->num_local] = NULL;
3216 if (IsOper(user))
3217 userList_remove(&curr_opers, user);
3218 /* remove from global dictionary, but not if after a collide */
3219 if (user == dict_find(clients, user->nick, NULL))
3220 dict_remove(clients, user->nick);
3221
3222 if (IsInvisible(user))
3223 invis_clients--;
3224
3225 if (announce) {
3226 if (IsLocal(user))
3227 irc_quit(user, why);
3228 else
3229 irc_kill(killer, user, why);
3230 }
3231
3232 if (IsLocal(user)) {
3233 unsigned int num = user->num_local;
3234 if (num < num_privmsg_funcs)
3235 privmsg_funcs[num] = NULL;
3236 if (num < num_notice_funcs)
3237 notice_funcs[num] = NULL;
3238 }
3239
3240 modeList_clean(&user->channels);
3241
3242 /* Clean up version data */
3243 if(user->version_reply) {
3244 free(user->version_reply);
3245 user->version_reply = NULL;
3246 }
3247
3248 /* Clean up SSL fingerprint data */
3249 if(user->sslfp) {
3250 free(user->sslfp);
3251 user->sslfp = NULL;
3252 }
3253
3254 /* clean up mark */
3255 if(user->mark) {
3256 free(user->mark);
3257 user->mark = NULL;
3258 }
3259
3260 /* clean up geoip data if any */
3261 if(user->country_code) free(user->country_code);
3262 if(user->city) free(user->city);
3263 if(user->region) free(user->region);
3264 if(user->postal_code) free(user->postal_code);
3265
3266 /* We don't free them, in case we try to privmsg them or something
3267 * (like when a stupid oper kills themself). We just put them onto
3268 * a list of clients that get freed after processing each line.
3269 */
3270 if (dead_users.size)
3271 userList_append(&dead_users, user);
3272 else
3273 free_user(user);
3274 }
3275
3276 static void call_oper_funcs(struct userNode *user);
3277
3278 void mod_usermode(struct userNode *user, const char *mode_change) {
3279 int add = 1;
3280 const char *word = mode_change;
3281
3282 if (!user || !mode_change)
3283 return;
3284
3285 call_user_mode_funcs(user, mode_change);
3286
3287 while (*word != ' ' && *word) word++;
3288 while (*word == ' ') word++;
3289 while (1) {
3290 #define do_user_mode(FLAG) do { if (add) user->modes |= FLAG; else user->modes &= ~FLAG; } while (0)
3291 switch (*mode_change++) {
3292 case 0: case ' ': return;
3293 case '+': add = 1; break;
3294 case '-': add = 0; break;
3295 case 'o':
3296 do_user_mode(FLAGS_OPER);
3297 if (!add) {
3298 userList_remove(&curr_opers, user);
3299 } else if (!userList_contains(&curr_opers, user)) {
3300 userList_append(&curr_opers, user);
3301 call_oper_funcs(user);
3302 }
3303 break;
3304 case 'O': do_user_mode(FLAGS_LOCOP); break;
3305 case 'i': do_user_mode(FLAGS_INVISIBLE);
3306 if (add)
3307 invis_clients++;
3308 else
3309 invis_clients--;
3310 break;
3311 case 'w': do_user_mode(FLAGS_WALLOP); break;
3312 case 'd': do_user_mode(FLAGS_DEAF); break;
3313 case 'k': do_user_mode(FLAGS_SERVICE); break;
3314 case 'g': do_user_mode(FLAGS_GLOBAL); break;
3315 case 'B': do_user_mode(FLAGS_BOT); break;
3316 case 'n': do_user_mode(FLAGS_HIDECHANS); break;
3317 case 'I': do_user_mode(FLAGS_HIDEIDLE); break;
3318 case 'X': do_user_mode(FLAGS_XTRAOP); break;
3319 case 'C': do_user_mode(FLAGS_CLOAKHOST);
3320 if (*word) {
3321 char cloakhost[MAXLEN];
3322 unsigned int ii;
3323 for (ii=0; (*word != ' ') && (*word != '\0'); )
3324 cloakhost[ii++] = *word++;
3325 cloakhost[ii] = 0;
3326 while (*word == ' ')
3327 word++;
3328 safestrncpy(user->crypthost, cloakhost, sizeof(user->crypthost));
3329 }
3330 break;
3331 case 'c': do_user_mode(FLAGS_CLOAKIP);
3332 if (*word) {
3333 char cloakip[MAXLEN];
3334 unsigned int ii;
3335 for (ii=0; (*word != ' ') && (*word != '\0'); )
3336 cloakip[ii++] = *word++;
3337 cloakip[ii] = 0;
3338 while (*word == ' ')
3339 word++;
3340 safestrncpy(user->cryptip, cloakip, sizeof(user->cryptip));
3341 }
3342 break;
3343 // sethost - reed/apples
3344 // case 'h': do_user_mode(FLAGS_HELPER); break;
3345 // I check if there's an 'h' in the first part, and if there,
3346 // then everything after the space becomes their new host.
3347 case 'h': do_user_mode(FLAGS_SETHOST);
3348 if (*word) {
3349 char sethost[MAXLEN];
3350 unsigned int ii;
3351 for (ii=0; (*word != ' ') && (*word != '\0'); )
3352 sethost[ii++] = *word++;
3353 sethost[ii] = 0;
3354 while (*word == ' ')
3355 word++;
3356 safestrncpy(user->sethost, sethost, sizeof(user->sethost));
3357 }
3358 break;
3359 case 'x': do_user_mode(FLAGS_HIDDEN_HOST); break;
3360 case 'r':
3361 if (*word) {
3362 char tag[MAXLEN];
3363 unsigned int ii;
3364 for (ii=0; (*word != ' ') && (*word != '\0'); )
3365 tag[ii++] = *word++;
3366 tag[ii] = 0;
3367 while (*word == ' ')
3368 word++;
3369 call_account_func(user, tag);
3370 }
3371 break;
3372 case 'f':
3373 if (*word) {
3374 char host[MAXLEN];
3375 unsigned int ii;
3376 for (ii=0; (*word != ' ') && (*word != '\0'); )
3377 host[ii++] = *word++;
3378 host[ii] = 0;
3379 while (*word == ' ')
3380 word++;
3381 assign_fakehost(user, host, 0);
3382 }
3383 break;
3384 case 'a': do_user_mode(FLAGS_ADMIN); break;
3385 case 'z': do_user_mode(FLAGS_SSL); break;
3386 case 'D': do_user_mode(FLAGS_PRIVDEAF); break;
3387 case 'R': do_user_mode(FLAGS_ACCOUNTONLY); break;
3388 case 'W': do_user_mode(FLAGS_WHOIS); break;
3389 case 'H': do_user_mode(FLAGS_HIDEOPER); break;
3390 case 'L': do_user_mode(FLAGS_NOLINK); break;
3391 case 'q': do_user_mode(FLAGS_COMMONCHANSONLY); break;
3392 }
3393 #undef do_user_mode
3394 }
3395 }
3396
3397 static int
3398 keyncpy(char output[], char input[], size_t output_size)
3399 {
3400 size_t ii;
3401
3402 if (input[0] == ':')
3403 {
3404 output[0] = '\0';
3405 return 1;
3406 }
3407
3408 for (ii = 0; (ii + 1 < output_size) && (input[ii] != '\0'); ++ii)
3409 {
3410 output[ii] = input[ii];
3411 }
3412
3413 output[ii] = '\0';
3414 return 0;
3415 }
3416
3417 struct mod_chanmode *
3418 mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, unsigned int flags, short base_oplevel)
3419 {
3420 struct mod_chanmode *change;
3421 unsigned int ii, in_arg, ch_arg, add;
3422
3423 if (argc == 0)
3424 return NULL;
3425 if (!(change = mod_chanmode_alloc(argc - 1)))
3426 return NULL;
3427
3428 for (ii = ch_arg = 0, in_arg = add = 1;
3429 (modes[0][ii] != '\0') && (modes[0][ii] != ' ');
3430 ++ii) {
3431 switch (modes[0][ii]) {
3432 case '+':
3433 add = 1;
3434 break;
3435 case '-':
3436 add = 0;
3437 break;
3438 #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)
3439 case 'C': do_chan_mode(MODE_NOCTCPS); break;
3440 case 'D': do_chan_mode(MODE_DELAYJOINS); break;
3441 case 'c': do_chan_mode(MODE_NOCOLORS); break;
3442 case 'i': do_chan_mode(MODE_INVITEONLY); break;
3443 case 'm': do_chan_mode(MODE_MODERATED); break;
3444 case 'n': do_chan_mode(MODE_NOPRIVMSGS); break;
3445 case 'p': do_chan_mode(MODE_PRIVATE); break;
3446 case 'r': do_chan_mode(MODE_REGONLY); break;
3447 case 's': do_chan_mode(MODE_SECRET); break;
3448 case 't': do_chan_mode(MODE_TOPICLIMIT); break;
3449 case 'S': do_chan_mode(MODE_STRIPCOLOR); break;
3450 case 'M': do_chan_mode(MODE_MODUNREG); break;
3451 case 'N': do_chan_mode(MODE_NONOTICE); break;
3452 case 'Q': do_chan_mode(MODE_NOQUITMSGS); break;
3453 case 'T': do_chan_mode(MODE_NOAMSG); break;
3454 case 'O': do_chan_mode(MODE_OPERSONLY); break;
3455 case 'a': do_chan_mode(MODE_ADMINSONLY); break;
3456 case 'Z': do_chan_mode(MODE_SSLONLY); break;
3457 case 'L': do_chan_mode(MODE_HIDEMODE); break;
3458 case 'z':
3459 if (!(flags & MCP_REGISTERED)) {
3460 do_chan_mode(MODE_REGISTERED);
3461 } else {
3462 mod_chanmode_free(change);
3463 return NULL;
3464 }
3465 break;
3466 #undef do_chan_mode
3467 case 'l':
3468 if (add) {
3469 if (in_arg >= argc)
3470 goto error;
3471 change->modes_set |= MODE_LIMIT;
3472 change->new_limit = atoi(modes[in_arg++]);
3473 } else {
3474 change->modes_set &= ~MODE_LIMIT;
3475 change->modes_clear |= MODE_LIMIT;
3476 }
3477 break;
3478 case 'k':
3479 if (add) {
3480 if ((in_arg >= argc)
3481 || keyncpy(change->new_key, modes[in_arg++], sizeof(change->new_key)))
3482 goto error;
3483 change->modes_set |= MODE_KEY;
3484 } else {
3485 change->modes_clear |= MODE_KEY;
3486 if (!(flags & MCP_KEY_FREE)) {
3487 if (in_arg >= argc)
3488 goto error;
3489 in_arg++;
3490 }
3491 }
3492 break;
3493 case 'U':
3494 if (add)
3495 {
3496 if ((in_arg >= argc)
3497 || keyncpy(change->new_upass, modes[in_arg++], sizeof(change->new_upass)))
3498 goto error;
3499 change->modes_set |= MODE_UPASS;
3500 } else {
3501 change->modes_clear |= MODE_UPASS;
3502 if (!(flags & MCP_UPASS_FREE)) {
3503 if (in_arg >= argc)
3504 goto error;
3505 in_arg++;
3506 }
3507 }
3508 break;
3509 case 'A':
3510 if (add) {
3511 if ((in_arg >= argc)
3512 || keyncpy(change->new_apass, modes[in_arg++], sizeof(change->new_apass)))
3513 goto error;
3514 change->modes_set |= MODE_APASS;
3515 } else {
3516 change->modes_clear |= MODE_APASS;
3517 if (!(flags & MCP_APASS_FREE)) {
3518 if (in_arg >= argc)
3519 goto error;
3520 in_arg++;
3521 }
3522 }
3523 break;
3524 case 'b':
3525 if (!(flags & MCP_ALLOW_OVB))
3526 goto error;
3527 if (in_arg >= argc)
3528 goto error;
3529 change->args[ch_arg].mode = MODE_BAN;
3530 if (!add)
3531 change->args[ch_arg].mode |= MODE_REMOVE;
3532 change->args[ch_arg++].u.hostmask = modes[in_arg++];
3533 break;
3534 case 'e':
3535 if (!(flags & MCP_ALLOW_OVB))
3536 goto error;
3537 if (in_arg >= argc)
3538 goto error;
3539 change->args[ch_arg].mode = MODE_EXEMPT;
3540 if (!add)
3541 change->args[ch_arg].mode |= MODE_REMOVE;
3542 change->args[ch_arg++].u.hostmask = modes[in_arg++];
3543 break;
3544 case 'o': case 'h': case 'v':
3545 {
3546 struct userNode *victim;
3547 char *oplevel_str;
3548 int oplevel;
3549
3550 if (in_arg >= argc)
3551 goto error;
3552 oplevel_str = strchr(modes[in_arg], ':');
3553 if (oplevel_str)
3554 {
3555 /* XXYYY M #channel +o XXYYY:<oplevel> */
3556 *oplevel_str++ = '\0';
3557 oplevel = parse_oplevel(oplevel_str);
3558 if (oplevel <= base_oplevel && !(flags & MCP_FROM_SERVER))
3559 oplevel = base_oplevel + 1;
3560 }
3561 else if (channel->modes & MODE_UPASS)
3562 oplevel = base_oplevel + 1;
3563 else
3564 oplevel = -1;
3565
3566 /* Check that oplevel is within bounds. */
3567 if (oplevel > MAXOPLEVEL)
3568 oplevel = MAXOPLEVEL;
3569
3570 if (!(flags & MCP_ALLOW_OVB))
3571 goto error;
3572 if (in_arg >= argc)
3573 goto error;
3574
3575 if (modes[0][ii] == 'o')
3576 change->args[ch_arg].mode = MODE_CHANOP;
3577 else if (modes[0][ii] == 'h')
3578 change->args[ch_arg].mode = MODE_HALFOP;
3579 else if (modes[0][ii] == 'v')
3580 change->args[ch_arg].mode = MODE_VOICE;
3581
3582 if (!add)
3583 change->args[ch_arg].mode |= MODE_REMOVE;
3584 if (flags & MCP_FROM_SERVER)
3585 victim = GetUserN(modes[in_arg++]);
3586 else
3587 victim = GetUserH(modes[in_arg++]);
3588 if (!victim)
3589 continue;
3590 if ((change->args[ch_arg].u.member = GetUserMode(channel, victim)))
3591 {
3592 /* Apply the oplevel change */
3593 change->args[ch_arg].u.member->oplevel = oplevel;
3594 ch_arg++;
3595 }
3596 break;
3597 }
3598 default:
3599 if (!(flags & MCP_FROM_SERVER))
3600 goto error;
3601 break;
3602 }
3603 }
3604 change->argc = ch_arg; /* in case any turned out to be ignored */
3605 if (change->modes_set & MODE_SECRET) {
3606 change->modes_set &= ~(MODE_PRIVATE);
3607 change->modes_clear |= MODE_PRIVATE;
3608 } else if (change->modes_set & MODE_PRIVATE) {
3609 change->modes_set &= ~(MODE_SECRET);
3610 change->modes_clear |= MODE_SECRET;
3611 }
3612 return change;
3613 error:
3614 mod_chanmode_free(change);
3615 return NULL;
3616 }
3617
3618 struct chanmode_buffer {
3619 char modes[MAXLEN];
3620 char args[MAXLEN];
3621 struct chanNode *channel;
3622 struct userNode *actor;
3623 unsigned int modes_used;
3624 unsigned int args_used;
3625 size_t chname_len;
3626 unsigned int is_add : 1;
3627 unsigned int is_chanop : 1;
3628 };
3629
3630 static void
3631 mod_chanmode_append(struct chanmode_buffer *buf, char ch, const char *arg)
3632 {
3633 size_t arg_len = strlen(arg);
3634 if (buf->modes_used > (MAXMODEPARAMS) ||
3635 buf->modes_used + buf->args_used + buf->chname_len + arg_len > 450) {
3636 memcpy(buf->modes + buf->modes_used, buf->args, buf->args_used);
3637 buf->modes[buf->modes_used + buf->args_used] = '\0';
3638 irc_mode((buf->is_chanop ? buf->actor : NULL), buf->channel, buf->modes);
3639 buf->modes[0] = buf->is_add ? '+' : '-';
3640 buf->modes_used = 1;
3641 buf->args_used = 0;
3642 }
3643 buf->modes[buf->modes_used++] = ch;
3644 buf->args[buf->args_used++] = ' ';
3645 memcpy(buf->args + buf->args_used, arg, arg_len);
3646 buf->args_used += arg_len;
3647 }
3648
3649 void
3650 mod_chanmode_announce(struct userNode *who, struct chanNode *channel, struct mod_chanmode *change)
3651 {
3652 struct chanmode_buffer chbuf;
3653 unsigned int arg;
3654 struct modeNode *mn;
3655 char int_buff[32], mode = '\0';
3656
3657 assert(change->argc <= change->alloc_argc);
3658 memset(&chbuf, 0, sizeof(chbuf));
3659 chbuf.channel = channel;
3660 chbuf.actor = who;
3661 chbuf.chname_len = strlen(channel->name);
3662
3663 mn = GetUserMode(channel, who);
3664 if ((mn && (mn->modes & MODE_CHANOP)) || off_channel)
3665 chbuf.is_chanop = 1;
3666
3667 /* First remove modes */
3668 chbuf.is_add = 0;
3669 if (change->modes_clear) {
3670 if (mode != '-')
3671 chbuf.modes[chbuf.modes_used++] = mode = '-';
3672 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
3673 DO_MODE_CHAR(PRIVATE, 'p');
3674 DO_MODE_CHAR(SECRET, 's');
3675 DO_MODE_CHAR(MODERATED, 'm');
3676 DO_MODE_CHAR(TOPICLIMIT, 't');
3677 DO_MODE_CHAR(INVITEONLY, 'i');
3678 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3679 DO_MODE_CHAR(LIMIT, 'l');
3680 DO_MODE_CHAR(DELAYJOINS, 'D');
3681 DO_MODE_CHAR(REGONLY, 'r');
3682 DO_MODE_CHAR(NOCOLORS, 'c');
3683 DO_MODE_CHAR(NOCTCPS, 'C');
3684 DO_MODE_CHAR(STRIPCOLOR, 'S');
3685 DO_MODE_CHAR(MODUNREG, 'M');
3686 DO_MODE_CHAR(NONOTICE, 'N');
3687 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3688 DO_MODE_CHAR(NOAMSG, 'T');
3689 DO_MODE_CHAR(OPERSONLY, 'O');
3690 DO_MODE_CHAR(ADMINSONLY, 'a');
3691 DO_MODE_CHAR(REGISTERED, 'z');
3692 DO_MODE_CHAR(SSLONLY, 'Z');
3693 DO_MODE_CHAR(HIDEMODE, 'L');
3694 #undef DO_MODE_CHAR
3695 if (change->modes_clear & channel->modes & MODE_KEY)
3696 mod_chanmode_append(&chbuf, 'k', channel->key);
3697 if (change->modes_clear & channel->modes & MODE_UPASS)
3698 mod_chanmode_append(&chbuf, 'U', channel->upass);
3699 if (change->modes_clear & channel->modes & MODE_APASS)
3700 mod_chanmode_append(&chbuf, 'A', channel->apass);
3701 }
3702 for (arg = 0; arg < change->argc; ++arg) {
3703 if (!(change->args[arg].mode & MODE_REMOVE))
3704 continue;
3705 if (mode != '-')
3706 chbuf.modes[chbuf.modes_used++] = mode = '-';
3707 switch (change->args[arg].mode & ~MODE_REMOVE) {
3708 case MODE_BAN:
3709 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
3710 break;
3711 case MODE_EXEMPT:
3712 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
3713 break;
3714 default:
3715 if (change->args[arg].mode & MODE_CHANOP)
3716 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
3717 if (change->args[arg].mode & MODE_HALFOP)
3718 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
3719 if (change->args[arg].mode & MODE_VOICE)
3720 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
3721 break;
3722 }
3723 }
3724
3725 /* Then set them */
3726 chbuf.is_add = 1;
3727 if (change->modes_set) {
3728 if (mode != '+')
3729 chbuf.modes[chbuf.modes_used++] = mode = '+';
3730 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
3731 DO_MODE_CHAR(PRIVATE, 'p');
3732 DO_MODE_CHAR(SECRET, 's');
3733 DO_MODE_CHAR(MODERATED, 'm');
3734 DO_MODE_CHAR(TOPICLIMIT, 't');
3735 DO_MODE_CHAR(INVITEONLY, 'i');
3736 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3737 DO_MODE_CHAR(DELAYJOINS, 'D');
3738 DO_MODE_CHAR(REGONLY, 'r');
3739 DO_MODE_CHAR(NOCOLORS, 'c');
3740 DO_MODE_CHAR(NOCTCPS, 'C');
3741 DO_MODE_CHAR(STRIPCOLOR, 'S');
3742 DO_MODE_CHAR(MODUNREG, 'M');
3743 DO_MODE_CHAR(NONOTICE, 'N');
3744 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3745 DO_MODE_CHAR(NOAMSG, 'T');
3746 DO_MODE_CHAR(OPERSONLY, 'O');
3747 DO_MODE_CHAR(ADMINSONLY, 'a');
3748 DO_MODE_CHAR(REGISTERED, 'z');
3749 DO_MODE_CHAR(SSLONLY, 'Z');
3750 DO_MODE_CHAR(HIDEMODE, 'L');
3751 #undef DO_MODE_CHAR
3752 if(change->modes_set & MODE_KEY)
3753 mod_chanmode_append(&chbuf, 'k', change->new_key);
3754 if (change->modes_set & MODE_UPASS)
3755 mod_chanmode_append(&chbuf, 'U', change->new_upass);
3756 if (change->modes_set & MODE_APASS)
3757 mod_chanmode_append(&chbuf, 'A', change->new_apass);
3758 if(change->modes_set & MODE_LIMIT) {
3759 sprintf(int_buff, "%d", change->new_limit);
3760 mod_chanmode_append(&chbuf, 'l', int_buff);
3761 }
3762 }
3763 for (arg = 0; arg < change->argc; ++arg) {
3764 if (change->args[arg].mode & MODE_REMOVE)
3765 continue;
3766 if (mode != '+')
3767 chbuf.modes[chbuf.modes_used++] = mode = '+';
3768 switch (change->args[arg].mode) {
3769 case MODE_BAN:
3770 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
3771 break;
3772 case MODE_EXEMPT:
3773 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
3774 break;
3775 default:
3776 if (change->args[arg].mode & MODE_CHANOP)
3777 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
3778 if (change->args[arg].mode & MODE_HALFOP)
3779 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
3780 if (change->args[arg].mode & MODE_VOICE)
3781 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
3782 break;
3783 }
3784 }
3785
3786 /* Flush the buffer and apply changes locally */
3787 if (chbuf.modes_used > 0) {
3788 memcpy(chbuf.modes + chbuf.modes_used, chbuf.args, chbuf.args_used);
3789 chbuf.modes[chbuf.modes_used + chbuf.args_used] = '\0';
3790 irc_mode((chbuf.is_chanop ? chbuf.actor : NULL), chbuf.channel, chbuf.modes);
3791 }
3792 mod_chanmode_apply(who, channel, change);
3793 }
3794
3795 char *
3796 mod_chanmode_format(struct mod_chanmode *change, char *outbuff)
3797 {
3798 unsigned int used = 0;
3799 assert(change->argc <= change->alloc_argc);
3800 if (change->modes_clear) {
3801 outbuff[used++] = '-';
3802 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) outbuff[used++] = CHAR
3803 DO_MODE_CHAR(PRIVATE, 'p');
3804 DO_MODE_CHAR(SECRET, 's');
3805 DO_MODE_CHAR(MODERATED, 'm');
3806 DO_MODE_CHAR(TOPICLIMIT, 't');
3807 DO_MODE_CHAR(INVITEONLY, 'i');
3808 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3809 DO_MODE_CHAR(LIMIT, 'l');
3810 DO_MODE_CHAR(KEY, 'k');
3811 DO_MODE_CHAR(UPASS, 'U');
3812 DO_MODE_CHAR(APASS, 'A');
3813 DO_MODE_CHAR(DELAYJOINS, 'D');
3814 DO_MODE_CHAR(REGONLY, 'r');
3815 DO_MODE_CHAR(NOCOLORS, 'c');
3816 DO_MODE_CHAR(NOCTCPS, 'C');
3817 DO_MODE_CHAR(STRIPCOLOR, 'S');
3818 DO_MODE_CHAR(MODUNREG, 'M');
3819 DO_MODE_CHAR(NONOTICE, 'N');
3820 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3821 DO_MODE_CHAR(NOAMSG, 'T');
3822 DO_MODE_CHAR(OPERSONLY, 'O');
3823 DO_MODE_CHAR(ADMINSONLY, 'a');
3824 DO_MODE_CHAR(REGISTERED, 'z');
3825 DO_MODE_CHAR(SSLONLY, 'Z');
3826 DO_MODE_CHAR(HIDEMODE, 'L');
3827 #undef DO_MODE_CHAR
3828 }
3829 if (change->modes_set) {
3830 outbuff[used++] = '+';
3831 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) outbuff[used++] = CHAR
3832 DO_MODE_CHAR(PRIVATE, 'p');
3833 DO_MODE_CHAR(SECRET, 's');
3834 DO_MODE_CHAR(MODERATED, 'm');
3835 DO_MODE_CHAR(TOPICLIMIT, 't');
3836 DO_MODE_CHAR(INVITEONLY, 'i');
3837 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3838 DO_MODE_CHAR(DELAYJOINS, 'D');
3839 DO_MODE_CHAR(REGONLY, 'r');
3840 DO_MODE_CHAR(NOCOLORS, 'c');
3841 DO_MODE_CHAR(NOCTCPS, 'C');
3842 DO_MODE_CHAR(STRIPCOLOR, 'S');
3843 DO_MODE_CHAR(MODUNREG, 'M');
3844 DO_MODE_CHAR(NONOTICE, 'N');
3845 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3846 DO_MODE_CHAR(NOAMSG, 'T');
3847 DO_MODE_CHAR(OPERSONLY, 'O');
3848 DO_MODE_CHAR(ADMINSONLY, 'a');
3849 DO_MODE_CHAR(REGISTERED, 'z');
3850 DO_MODE_CHAR(SSLONLY, 'Z');
3851 DO_MODE_CHAR(HIDEMODE, 'L');
3852
3853 DO_MODE_CHAR(KEY, 'k');
3854 DO_MODE_CHAR(LIMIT, 'l');
3855 DO_MODE_CHAR(APASS, 'A');
3856 DO_MODE_CHAR(UPASS, 'U');
3857 #undef DO_MODE_CHAR
3858
3859 #define DO_MODE_PARM(BIT, PARM) if (change->modes_set & MODE_##BIT) used += sprintf(outbuff+used, " %s", PARM);
3860 DO_MODE_PARM(KEY, change->new_key);
3861 if (change->modes_set & MODE_LIMIT) used += sprintf(outbuff+used, " %d", change->new_limit);
3862 DO_MODE_PARM(APASS, change->new_apass);
3863 DO_MODE_PARM(UPASS, change->new_upass);
3864 #undef DO_MODE_PARM
3865 }
3866 outbuff[used] = 0;
3867 return outbuff;
3868 }
3869
3870 static int
3871 clear_chanmode(struct chanNode *channel, const char *modes)
3872 {
3873 unsigned int cleared;
3874
3875 for (cleared = 0; *modes; modes++) {
3876 switch (*modes) {
3877 case 'o': cleared |= MODE_CHANOP; break;
3878 case 'h': cleared |= MODE_HALFOP; break;
3879 case 'v': cleared |= MODE_VOICE; break;
3880 case 'p': cleared |= MODE_PRIVATE; break;
3881 case 's': cleared |= MODE_SECRET; break;
3882 case 'm': cleared |= MODE_MODERATED; break;
3883 case 't': cleared |= MODE_TOPICLIMIT; break;
3884 case 'i': cleared |= MODE_INVITEONLY; break;
3885 case 'n': cleared |= MODE_NOPRIVMSGS; break;
3886 case 'k':
3887 cleared |= MODE_KEY;
3888 channel->key[0] = '\0';
3889 break;
3890 case 'A':
3891 cleared |= MODE_APASS;
3892 channel->apass[0] = '\0';
3893 break;
3894 case 'U':
3895 cleared |= MODE_UPASS;
3896 channel->upass[0] = '\0';
3897 break;
3898 case 'l':
3899 cleared |= MODE_LIMIT;
3900 channel->limit = 0;
3901 break;
3902 case 'b': cleared |= MODE_BAN; break;
3903 case 'e': cleared |= MODE_EXEMPT; break;
3904 case 'D': cleared |= MODE_DELAYJOINS; break;
3905 case 'r': cleared |= MODE_REGONLY; break;
3906 case 'c': cleared |= MODE_NOCOLORS; break;
3907 case 'C': cleared |= MODE_NOCTCPS; break;
3908 case 'S': cleared |= MODE_STRIPCOLOR; break;
3909 case 'M': cleared |= MODE_MODUNREG; break;
3910 case 'N': cleared |= MODE_NONOTICE; break;
3911 case 'Q': cleared |= MODE_NOQUITMSGS; break;
3912 case 'T': cleared |= MODE_NOAMSG; break;
3913 case 'O': cleared |= MODE_OPERSONLY; break;
3914 case 'a': cleared |= MODE_ADMINSONLY; break;
3915 case 'z': cleared |= MODE_REGISTERED; break;
3916 case 'Z': cleared |= MODE_SSLONLY; break;
3917 case 'L': cleared |= MODE_HIDEMODE; break;
3918 }
3919 }
3920
3921 if (!cleared)
3922 return 1;
3923
3924 /* Remove simple modes. */
3925 channel->modes &= ~cleared;
3926
3927 /* If removing bans, kill 'em all. */
3928 if ((cleared & MODE_BAN) && channel->banlist.used) {
3929 unsigned int i;
3930 for (i=0; i<channel->banlist.used; i++)
3931 free(channel->banlist.list[i]);
3932 channel->banlist.used = 0;
3933 }
3934
3935 /* If removing exempts, kill 'em all. */
3936 if ((cleared & MODE_EXEMPT) && channel->exemptlist.used) {
3937 unsigned int i;
3938 for (i=0; i<channel->exemptlist.used; i++)
3939 free(channel->exemptlist.list[i]);
3940 channel->exemptlist.used = 0;
3941 }
3942
3943 /* Removed member modes. */
3944 if ((cleared & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE)) && channel->members.used) {
3945 int mask = ~(cleared & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE));
3946 unsigned int i;
3947
3948 for (i = 0; i < channel->members.used; i++)
3949 channel->members.list[i]->modes &= mask;
3950 }
3951
3952 return 1;
3953 }
3954
3955 void
3956 reg_privmsg_func(struct userNode *user, privmsg_func_t handler)
3957 {
3958 unsigned int numeric = user->num_local;
3959 if (numeric >= num_privmsg_funcs) {
3960 int newnum = numeric + 8, ii;
3961 privmsg_funcs = realloc(privmsg_funcs, newnum*sizeof(privmsg_func_t));
3962 for (ii = num_privmsg_funcs; ii < newnum; ++ii)
3963 privmsg_funcs[ii] = NULL;
3964 num_privmsg_funcs = newnum;
3965 }
3966 if (privmsg_funcs[numeric])
3967 log_module(MAIN_LOG, LOG_WARNING, "re-registering new privmsg handler for numeric %d", numeric);
3968 privmsg_funcs[numeric] = handler;
3969 }
3970
3971 void
3972 unreg_privmsg_func(struct userNode *user)
3973 {
3974 if (!IsLocal(user) || user->num_local >= num_privmsg_funcs)
3975 return; /* this really only works with users */
3976
3977 privmsg_funcs[user->num_local] = NULL;
3978 }
3979
3980
3981 void
3982 reg_notice_func(struct userNode *user, privmsg_func_t handler)
3983 {
3984 unsigned int numeric = user->num_local;
3985 if (numeric >= num_notice_funcs) {
3986 int newnum = numeric + 8, ii;
3987 notice_funcs = realloc(notice_funcs, newnum*sizeof(privmsg_func_t));
3988 for (ii = num_privmsg_funcs; ii < newnum; ++ii)
3989 privmsg_funcs[ii] = NULL;
3990 num_notice_funcs = newnum;
3991 }
3992 if (notice_funcs[numeric])
3993 log_module(MAIN_LOG, LOG_WARNING, "re-registering new notice handler for numeric %d", numeric);
3994 notice_funcs[numeric] = handler;
3995 }
3996
3997 void
3998 unreg_notice_func(struct userNode *user)
3999 {
4000 if (!IsLocal(user) || user->num_local >= num_privmsg_funcs)
4001 return; /* this really only works with users */
4002
4003 notice_funcs[user->num_local] = NULL;
4004 }
4005
4006 static void
4007 send_burst(void)
4008 {
4009 unsigned int i, hop, max_hop=1;
4010 dict_iterator_t it;
4011
4012 /* burst (juped) servers, closest first (except self, which is sent already) */
4013 for (i=0; i<ArrayLength(servers_num); i++)
4014 if (servers_num[i] && servers_num[i]->hops > max_hop)
4015 max_hop = servers_num[i]->hops;
4016 for (hop=1; hop<=max_hop; hop++) {
4017 for (i=0;i<ArrayLength(servers_num);i++) {
4018 if (servers_num[i]
4019 && (servers_num[i]->hops == hop)
4020 && (servers_num[i] != self->uplink))
4021 irc_server(servers_num[i]);
4022 }
4023 }
4024
4025 /* burst local nicks */
4026 for (i=0; i<=self->num_mask; i++)
4027 if (self->users[i])
4028 irc_user(self->users[i]);
4029
4030 /* build dict of unbursted channel names (just copy existing channels) */
4031 unbursted_channels = dict_new();
4032 for (it = dict_first(channels); it; it = iter_next(it))
4033 dict_insert(unbursted_channels, iter_key(it), iter_data(it));
4034 }
4035
4036 /*
4037 * Oplevel parsing
4038 */
4039 static int
4040 parse_oplevel(char *str)
4041 {
4042 int oplevel = 0;
4043 while (isdigit(*str))
4044 oplevel = oplevel * 10 + *str++ - '0';
4045 return oplevel;
4046 }
4047