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