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