]> jfr.im git - irc/evilnet/x3.git/blob - src/proto-p10.c
Updated ChanServ.netinfo oper count to only include opers who do not have user modes...
[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), P(ADMIN),
1734 P(APASS_OPMODE), P(HIDE_OPER), P(REMOTE), P(SERVICE),
1735 #undef P
1736 { 0, 0 }
1737 };
1738
1739 char *client_report_privs(struct userNode *client)
1740 {
1741 int i;
1742
1743 privbuf[0] = '\0';
1744 for (i = 0; privtab[i].name; i++) {
1745 if (HasPriv(client, privtab[i].priv)) {
1746 strcat(privbuf, privtab[i].name);
1747 strcat(privbuf, " ");
1748 }
1749 }
1750
1751 privbuf[strlen(privbuf)] = 0;
1752
1753 return privbuf;
1754 }
1755
1756 int clear_privs(struct userNode *who) {
1757 int i = 0;
1758 assert(0 != who);
1759
1760 for (i = 0; privtab[i].name; i++)
1761 RevokePriv(who, privtab[i].priv);
1762
1763 return 0;
1764 }
1765
1766 int client_modify_priv_by_name(struct userNode *who, char *priv, int what) {
1767 int i = 0;
1768 assert(0 != priv);
1769 assert(0 != who);
1770
1771 for (i = 0; privtab[i].name; i++) {
1772 if (0 == strcmp(privtab[i].name, priv)) {
1773 if (what == PRIV_ADD)
1774 GrantPriv(who, privtab[i].priv);
1775 else if (what == PRIV_DEL) {
1776 RevokePriv(who, privtab[i].priv);
1777 }
1778 }
1779 }
1780 return 0;
1781 }
1782
1783 int check_priv(char *priv)
1784 {
1785 int i;
1786
1787 for (i = 0; privtab[i].name; i++) {
1788 if (0 == strcmp(privtab[i].name, priv)) {
1789 return 1;
1790 }
1791 }
1792 return 0;
1793 }
1794
1795 void
1796 irc_privs(struct userNode *target, char *flag, int add)
1797 {
1798 client_modify_priv_by_name(target, flag, add);
1799 putsock("%s " P10_PRIVS " %s %s%s", self->numeric, target->numeric, (add == PRIV_ADD) ? "+" : "-", flag);
1800 }
1801
1802 void
1803 irc_raw_privs(struct userNode *target, const char *privs)
1804 {
1805 putsock("%s " P10_PRIVS " %s %s", self->numeric, target->numeric, privs);
1806 }
1807
1808 static CMD_FUNC(cmd_privs)
1809 {
1810 char *tstr = NULL;
1811 char buf[512] = "";
1812 char *p = 0;
1813 char *tmp;
1814
1815 int what = PRIV_ADD;
1816 int type = 0;
1817
1818 unsigned int i;
1819
1820 struct server *sender;
1821 struct userNode *user;
1822
1823
1824 tstr = conf_get_data("server/type", RECDB_QSTRING);
1825 if(tstr)
1826 type = atoi(tstr);
1827
1828
1829 if (!(sender = GetServerH(origin))) { /* from oper */
1830 return 1; /* ignore as no services have privs set */
1831 } else { /* from server */
1832 if (type < 5)
1833 return 1; /* silently ignore */
1834
1835 user = argc > 1 ? GetUserN(argv[1]) : NULL;
1836
1837 if (!user)
1838 return 0;
1839
1840 for (i=1; i<argc; i++) {
1841 strcat(buf, argv[i]);
1842 strcat(buf, " ");
1843 }
1844
1845 for (i = 2; i < argc; i++) {
1846 if (*argv[i] == '+') { what = PRIV_ADD; argv[i]++; }
1847 if (*argv[i] == '-') { what = PRIV_DEL; argv[i]++; }
1848 for (tmp = x3_strtok(&p, argv[i], ","); tmp;
1849 tmp = x3_strtok(&p, NULL, ",")) {
1850 if (!strcmp(tmp, "PRIV_NONE")) {
1851 if (now > user->timestamp+5)
1852 clear_privs(user);
1853 break;
1854 } else
1855 client_modify_priv_by_name(user, tmp, what);
1856 }
1857 }
1858 }
1859 return 1;
1860 }
1861
1862 static CMD_FUNC(cmd_burst)
1863 {
1864 extern int rel_age;
1865 char modes[MAXLEN], *members = "";
1866 static char exemptlist[MAXLEN], banlist[MAXLEN];
1867 unsigned int next = 3, res = 1;
1868 int ctype = 0, echeck = 0, bcheck = 0;
1869 struct chanData *cData;
1870 struct chanNode *cNode;
1871 struct userNode *un;
1872 struct modeNode *mNode;
1873 long mode;
1874 int oplevel = -1;
1875 int type = 0;
1876 char *user, *end, sep;
1877 char *tstr;
1878 time_t in_timestamp;
1879 char* parm = NULL;
1880
1881 if (argc < 3)
1882 return 0;
1883 modes[0] = 0;
1884
1885 tstr = conf_get_data("server/type", RECDB_QSTRING);
1886 if(tstr) {
1887 type = atoi(tstr);
1888 }
1889
1890 exemptlist[0] = 0;
1891 banlist[0] = 0;
1892
1893 while (next < argc) {
1894 switch (argv[next][0]) {
1895 case '+': {
1896 const char *pos;
1897 int n_modes;
1898 for (pos=argv[next], n_modes = 1; *pos; pos++)
1899 if ((*pos == 'k') || (*pos == 'l') || (*pos == 'A')
1900 || (*pos == 'U') || ((type > 7) && (*pos == 'L')))
1901 n_modes++;
1902 if (next + n_modes > argc)
1903 n_modes = argc - next;
1904 unsplit_string(argv+next, n_modes, modes);
1905 next += n_modes;
1906 break;
1907 }
1908 case '%': {
1909 ctype = 1;
1910 for(parm = mysep(&argv[next], " "); /* parm = first param */
1911 parm; /* While param is not null */
1912 parm = mysep(&argv[next], " ") /* parm = next param */
1913 )
1914 {
1915 if (0 == strcmp("~", parm))
1916 ctype = 2;
1917 if (ctype == 1) {
1918 if (bcheck == 0) {
1919 /* strip % char off start of very first ban */
1920 if (strlen(parm) > 1) {
1921 strncat(banlist, strtok(parm, "%"), sizeof(banlist) - 1 - strlen(banlist));
1922 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1923 }
1924 bcheck = 1;
1925 } else {
1926 strncat(banlist, parm, sizeof(banlist) - 1 - strlen(banlist));
1927 strncat(banlist, " ", sizeof(banlist) - 1 - strlen(banlist));
1928 }
1929 } else if (ctype == 2) {
1930 if (echeck == 0) {
1931 echeck = 1;
1932 } else {
1933 strncat(exemptlist, parm, sizeof(exemptlist) - 1 - strlen(exemptlist));
1934 strncat(exemptlist, " ", sizeof(exemptlist) - 1 - strlen(exemptlist));
1935 }
1936 }
1937 }
1938 next++;
1939 break;
1940 }
1941 default: members = argv[next++]; break;
1942 }
1943 }
1944
1945 in_timestamp = atoi(argv[2]);
1946 if ((cNode = dict_find(unbursted_channels, argv[1], NULL))) {
1947 cNode->timestamp = in_timestamp;
1948 dict_remove(unbursted_channels, cNode->name);
1949 irc_burst(cNode);
1950 }
1951 cNode = AddChannel(argv[1], in_timestamp, modes, banlist, exemptlist);
1952 cData = cNode->channel_info;
1953
1954 if (!cData) {
1955 if (cNode->modes & MODE_REGISTERED) {
1956 irc_join(opserv, cNode);
1957 irc_mode(opserv, cNode, "-z");
1958 irc_part(opserv, cNode, "");
1959 }
1960 }
1961
1962 /* Burst channel members in now. */
1963 for (user = members, sep = *members, mode = 0; sep; user = end) {
1964 for (end = user + 3; isalnum(*end) || *end == '[' || *end == ']'; end++) ;
1965 sep = *end++; end[-1] = 0;
1966 if (sep == ':') {
1967 mode = 0;
1968 while ((sep = *end++)) {
1969 if (sep == 'o') {
1970 mode |= MODE_CHANOP;
1971 oplevel = -1;
1972 } else if (sep == 'h') {
1973 mode |= MODE_HALFOP;
1974 oplevel = -1;
1975 } else if (sep == 'v') {
1976 mode |= MODE_VOICE;
1977 oplevel = -1;
1978 } else if (isdigit(sep)) {
1979 mode |= MODE_CHANOP;
1980 if (oplevel >= 0)
1981 oplevel += parse_oplevel(end);
1982 else
1983 oplevel = parse_oplevel(end);
1984 while (isdigit(*end)) end++;
1985 } else
1986 break;
1987 }
1988 if (rel_age < 0)
1989 mode = 0;
1990 }
1991 if (!(un = GetUserN(user))) {
1992 res = 0;
1993 continue;
1994 }
1995 if ((mNode = AddChannelUser(un, cNode))) {
1996 mNode->modes = mode;
1997 mNode->oplevel = oplevel;
1998 }
1999 }
2000
2001 return res;
2002 }
2003
2004 /* TODO:
2005 * This is a stub that doesn't actually do anything. It should be completed
2006 * so that bans on *!*@markname.* match users as it does in nefarious
2007 */
2008 static CMD_FUNC(cmd_mark)
2009 {
2010 const char *tstr;
2011 int type = 4;
2012 struct userNode *target;
2013 /*
2014 * log_module(MAIN_LOG, LOG_ERROR, "DEBUG: mark, user %s, type %s, arg %s", argv[1], argv[2], argv[3]);
2015 */
2016
2017 tstr = conf_get_data("server/type", RECDB_QSTRING);
2018 if(tstr)
2019 type = atoi(tstr);
2020 else
2021 type = 4;/* default to 040 style topics */
2022
2023 if(argc < 4)
2024 return 0;
2025 if(!strcasecmp(argv[2], "DNSBL")) {
2026 /* DNSBL <modes> */
2027 return 1;
2028 }
2029 else if(!strcasecmp(argv[2], "DNSBL_DATA") || !strcasecmp(argv[2], "MARK")) {
2030 int markfound = 0;
2031 unsigned int ii = 0;
2032 /* DNSBL_DATA name */
2033 target = GetUserH(argv[1]);
2034 if(!target) {
2035 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose dnsbl mark is changing.", argv[1]);
2036 return 0;
2037 }
2038 if (type >= 9) {
2039 if (target->marks)
2040 for (ii=0; ii<target->marks->used; ii++)
2041 if (!irccasecmp(target->marks->list[ii], argv[3]))
2042 markfound = 1;
2043 if (!markfound)
2044 {
2045 if (!target->marks)
2046 target->marks = alloc_string_list(1);
2047 string_list_append(target->marks, strdup(argv[3]));
2048 }
2049 } else
2050 target->mark = strdup(argv[3]);
2051 return 1;
2052
2053 }
2054 else if(!strcasecmp(argv[2], "CVERSION")) {
2055 /* CTCP VERSION mark */
2056 target = GetUserH(argv[1]);
2057 if(!target) {
2058 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose version mark is changing.", argv[1]);
2059 return 0;
2060 }
2061
2062 char *version = unsplit_string(argv + 3, argc - 3, NULL);
2063 if(!version)
2064 version = "";
2065
2066 target->version_reply = strdup(version);
2067
2068 if(match_ircglob(version, "WebTV;*"))
2069 target->no_notice = true; /* webbies cant see notices */
2070
2071 return 1;
2072 }
2073 else if(!strcasecmp(argv[2], "SSLCLIFP")) {
2074 /* SSL fingerprint mark */
2075 target = GetUserH(argv[1]);
2076 if(!target) {
2077 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose SSL fingerprint mark is changing.", argv[1]);
2078 return 0;
2079 }
2080
2081 char *sslfp = unsplit_string(argv + 3, argc - 3, NULL);
2082 if(!sslfp)
2083 sslfp = "";
2084
2085 target->sslfp = strdup(sslfp);
2086
2087 return 1;
2088 }
2089 /* unknown type of mark */
2090 return 1;
2091 }
2092
2093 static CMD_FUNC(cmd_mode)
2094 {
2095 struct chanNode *cn;
2096 struct userNode *un;
2097 char *sethost; // sethost - reed/apples
2098 int i; // sethost - reed/apples
2099
2100 if (argc < 3)
2101 return 0;
2102 if (!IsChannelName(argv[1])) {
2103 un = GetUserH(argv[1]);
2104 if (!un) {
2105 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose mode is changing.", argv[1]);
2106 return 0;
2107 }
2108 // sethost - reed/apples
2109 if (argc == 3)
2110 mod_usermode(un, argv[2]);
2111 else {
2112 sethost = malloc(strlen(argv[2]) + 1 + strlen(argv[3]) + 1);
2113 i = 0;
2114 while((sethost[i++] = *argv[2]++));
2115 i--;
2116 sethost[i++] = ' ';
2117 while((sethost[i++] = *argv[3]++));
2118 mod_usermode(un, sethost); // sethost - reed/apples
2119 }
2120
2121 return 1;
2122 }
2123
2124 if (!(cn = GetChannel(argv[1]))) {
2125 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
2126 return 0;
2127 }
2128 if ((un = GetUserH(origin))) {
2129 struct modeNode *mn;
2130 /* Update idle time for person setting the mode */
2131 if ((mn = GetUserMode(cn, un)))
2132 mn->idle_since = now;
2133 } else {
2134 /* If it came from a server, reset timestamp to re-sync. */
2135 cn->timestamp = atoi(argv[argc-1]);
2136 }
2137
2138 if (checkDefCon(DEFCON_NO_MODE_CHANGE) && !IsOper(un)) {
2139 const char *str;
2140 str = conf_get_data("services/opserv/nick", RECDB_QSTRING);
2141 if (str) {
2142 char modes[MODELEN];
2143 struct userNode *opserv = GetUserH(str);
2144
2145 send_message_type(4, un, opserv, "Channel modes cannot be changed due to DefCon level %d in effect, please try again soon", DefConLevel);
2146 irc_make_chanmode(cn, modes);
2147 irc_mode(opserv, cn, modes);
2148 irc_mode(opserv, cn, DefConChanModes);
2149 }
2150 return 1;
2151 }
2152
2153
2154 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER|(un ? MC_NOTIFY : 0));
2155 }
2156
2157 static CMD_FUNC(cmd_opmode)
2158 {
2159 struct chanNode *cn;
2160 struct userNode *un;
2161
2162 if (argc < 3)
2163 return 0;
2164
2165 if (!(cn = GetChannel(argv[1]))) {
2166 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
2167 return 0;
2168 }
2169 if (!(un = GetUserH(origin))) {
2170 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting OPMODE.", origin);
2171 return 0;
2172 }
2173 if (!IsOper(un)) {
2174 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using OPMODE.", un->nick);
2175 return 0;
2176 }
2177
2178 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER); /* do NOT announce opmode locally */
2179 }
2180
2181 static int clear_chanmode(struct chanNode *channel, const char *modes);
2182
2183 static CMD_FUNC(cmd_clearmode)
2184 {
2185 struct chanNode *cn;
2186 struct userNode *un;
2187
2188 if (argc < 3)
2189 return 0;
2190
2191 if (!(cn = GetChannel(argv[1]))) {
2192 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
2193 return 0;
2194 }
2195 if (!(un = GetUserH(origin))) {
2196 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting CLEARMODE.", origin);
2197 return 0;
2198 }
2199 if (!IsOper(un)) {
2200 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using CLEARMODE.", un->nick);
2201 return 0;
2202 }
2203
2204 return clear_chanmode(cn, argv[2]);
2205 }
2206
2207 static CMD_FUNC(cmd_topic)
2208 {
2209 struct chanNode *cn;
2210 time_t chan_ts, topic_ts;
2211 struct userNode *user;
2212
2213 if (argc < 3)
2214 return 0;
2215 if (!(cn = GetChannel(argv[1]))) {
2216 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose topic is being set", argv[1]);
2217 return 0;
2218 }
2219
2220
2221 if (argc == 5) { /* Asuka / Topic Bursting IRCu's */
2222 user = GetUserH(origin);
2223 chan_ts = atoi(argv[2]);
2224 topic_ts = atoi(argv[3]);
2225 } else if (argc >= 6) { /* Nefarious 0.5.0 */
2226 user = GetUserH(strtok(argv[2], "!"));
2227 chan_ts = atoi(argv[3]);
2228 topic_ts = atoi(argv[4]);
2229 } else { /* Regular IRCu (No Topic Bursting)*/
2230 user = GetUserH(origin);
2231 chan_ts = cn->timestamp;
2232 topic_ts = now;
2233 }
2234
2235 SetChannelTopic(cn, user, user, argv[argc-1], 0);
2236 cn->topic_time = topic_ts;
2237 return 1;
2238 }
2239
2240 static CMD_FUNC(cmd_num_topic)
2241 {
2242 struct chanNode *cn;
2243
2244 if (!argv[0])
2245 return 0; /* huh? */
2246 if (argv[2]) {
2247 cn = GetChannel(argv[2]);
2248 if (!cn) {
2249 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s in topic reply", argv[2]);
2250 return 0;
2251 }
2252 } else
2253 return 0;
2254
2255 switch (atoi(argv[0])) {
2256 case 331:
2257 cn->topic_time = 0;
2258 break; /* no topic */
2259 case 332:
2260 if (argc < 4)
2261 return 0;
2262 safestrncpy(cn->topic, unsplit_string(argv+3, argc-3, NULL), sizeof(cn->topic));
2263 break;
2264 case 333:
2265 if (argc < 5)
2266 return 0;
2267 safestrncpy(cn->topic_nick, argv[3], sizeof(cn->topic_nick));
2268 cn->topic_time = atoi(argv[4]);
2269 break;
2270 default:
2271 return 0; /* should never happen */
2272 }
2273 return 1;
2274 }
2275
2276 static CMD_FUNC(cmd_num_gline)
2277 {
2278 if (argc < 7) {
2279 if (argc < 6)
2280 return 0;
2281 else
2282 gline_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0, 0);
2283 } else {
2284 if (!strcmp(argv[5], "+"))
2285 gline_add(origin, argv[3], atoi(argv[4])-now, argv[6], now, 0, 0);
2286 }
2287 return 1;
2288 }
2289
2290 static CMD_FUNC(cmd_num_shun)
2291 {
2292 if (argc < 7) {
2293 if (argc < 6)
2294 return 0;
2295 else
2296 shun_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0);
2297 } else {
2298 if (!strcmp(argv[5], "+"))
2299 shun_add(origin, argv[3], atoi(argv[4])-now, argv[6], now, 0);
2300 }
2301 return 1;
2302 }
2303
2304 static CMD_FUNC(cmd_quit)
2305 {
2306 struct userNode *user;
2307 if (argc < 2)
2308 return 0;
2309 /* Sometimes we get a KILL then a QUIT or the like, so we don't want to
2310 * call DelUser unless we have the user in our grasp. */
2311 if ((user = GetUserH(origin)))
2312 DelUser(user, NULL, false, argv[1]);
2313 return 1;
2314 }
2315
2316 static CMD_FUNC(cmd_kill)
2317 {
2318 struct userNode *user;
2319 if (argc < 2)
2320 return 0;
2321 user = GetUserN(argv[1]);
2322 if (!user) {
2323 /* If we get a KILL for a non-existent user, it could be a
2324 * Ghost response to a KILL we sent out earlier. So we only
2325 * whine if the target is local.
2326 */
2327 if (!strncmp(argv[1], self->numeric, strlen(self->numeric))) {
2328 log_module(MAIN_LOG, LOG_ERROR, "Unable to find kill victim %s", argv[1]);
2329 return 0;
2330 }
2331 return 1;
2332 }
2333
2334 if (IsLocal(user) && IsService(user)) {
2335 /* TODO: rate limit this so silly things don't happen. */
2336 ReintroduceUser(user);
2337 return 1;
2338 }
2339
2340 DelUser(user, NULL, false, argv[2]);
2341 return 1;
2342 }
2343
2344 static CMD_FUNC(cmd_svspart)
2345 {
2346 struct userNode *user;
2347
2348 if (argc < 3)
2349 return 0;
2350 user = GetUserN(argv[1]);
2351 if (!user)
2352 return 0;
2353 parse_foreach(argv[2], part_helper, NULL, NULL, NULL, user);
2354 return 1;
2355 }
2356
2357 static CMD_FUNC(cmd_silence)
2358 {
2359 struct userNode *user;
2360 char *mask;
2361 char *new_mask;
2362 unsigned int i;
2363
2364 if (argc < 2)
2365 return 0;
2366
2367 user = GetUserN(argv[1]);
2368
2369 /* Sanity, go nuts if this happens */
2370 if (!user)
2371 return 0;
2372
2373 /* We can safely ignore this if a user adding a silence is not
2374 * ignored. However this brings up a TODO. If a user logs in and
2375 * they have silences on the IRCd then we need to set them here
2376 * somehow
2377 */
2378 if (!user->handle_info)
2379 return 1;
2380
2381 mask = argv[2];
2382
2383 if (*mask == '-') {
2384 for (i=0; i<user->handle_info->ignores->used; i++) {
2385 if (!irccasecmp(mask+1, user->handle_info->ignores->list[i]))
2386 user->handle_info->ignores->list[i] = user->handle_info->ignores->list[--user->handle_info->ignores->used];
2387 }
2388 } else {
2389 for (i=0; i<user->handle_info->ignores->used; i++) {
2390 if (!strcmp(mask+1, user->handle_info->ignores->list[i]))
2391 return 1; /* Already on the users NickServ ignore list, safely ignore */
2392 }
2393
2394 new_mask = strdup(mask+1);
2395 string_list_append(user->handle_info->ignores, new_mask);
2396 }
2397 return 1;
2398 }
2399
2400 static CMD_FUNC(cmd_kick)
2401 {
2402 if (argc < 3)
2403 return 0;
2404 ChannelUserKicked(GetUserH(origin), GetUserN(argv[2]), GetChannel(argv[1]));
2405 return 1;
2406 }
2407
2408 static CMD_FUNC(cmd_squit)
2409 {
2410 struct server *server;
2411 char *uplink;
2412
2413 if (argc < 4)
2414 return 0;
2415 if (!(server = GetServerH(argv[1])))
2416 return 0;
2417
2418 if (server == self->uplink) {
2419 /* Force a reconnect to the currently selected server. */
2420 cManager.uplink->tries = 0;
2421 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", argv[3]);
2422 close_socket();
2423 return 1;
2424 }
2425
2426 uplink = strdup(server->uplink->name);
2427 DelServer(server, 0, argv[3]);
2428 /* if its a pingout and pingout connecting is enabled
2429 or its a read error and readerror connecting is enabled
2430 or were doing a "N" and i need to connect that server somewhere */
2431 routing_handle_squit(argv[1], uplink, argv[3]);
2432 free(uplink);
2433 return 1;
2434 }
2435
2436 static CMD_FUNC(cmd_privmsg)
2437 {
2438 struct privmsg_desc pd;
2439 if (argc < 3)
2440 return 0;
2441 pd.user = GetUserH(origin);
2442 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
2443 return 1;
2444
2445 if (checkDefCon(DEFCON_OPER_ONLY) && !IsOper(pd.user)) {
2446 const char *str;
2447 str = conf_get_data("services/opserv/nick", RECDB_QSTRING);
2448 if (str)
2449 send_message_type(4, pd.user, GetUserH(str), "Services are currently not available, please try again soon");
2450 return 1;
2451 }
2452
2453 if (checkDefCon(DEFCON_SILENT_OPER_ONLY) && !IsOper(pd.user))
2454 return 1; /* Silently Ignore */
2455
2456 pd.is_notice = 0;
2457 pd.text = argv[argc - 1];
2458 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
2459
2460 return 1;
2461 }
2462
2463 static CMD_FUNC(cmd_notice)
2464 {
2465 struct privmsg_desc pd;
2466 struct server *srv;
2467 int nuser = 0;
2468
2469 if (argc != 3)
2470 return 0;
2471
2472 pd.user = GetUserH(origin);
2473 if(!pd.user)
2474 nuser = 1;
2475 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user))) {
2476 }
2477 else {
2478 pd.is_notice = 1;
2479 pd.text = argv[2];
2480 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
2481 }
2482
2483 srv = GetServerH(origin);
2484 if(srv) {
2485 char *sargv[MAXNUMPARAMS];
2486 int sargc;
2487
2488 sargc = split_line(argv[2], true, MAXNUMPARAMS, sargv);
2489
2490 if(!strcasecmp(sargv[0], "Connect:")) {
2491 /* :Connect: Host shoe.loxxin.net not listed in ircd.conf */
2492 if(!strcasecmp(sargv[3], "not") && !strcasecmp(sargv[4], "listed")) {
2493 routing_handle_connect_failure(srv, sargv[2], unsplit_string(sargv+3, sargc-3, NULL));
2494 }
2495 }
2496 else if(!strcasecmp(sargv[0], "Link")) {
2497 /* :Link with mephisto.etheria.cx cancelled: Server mephisto.etheria.cx[216.46.33.71]
2498 *
2499 * :Link with laptop.afternet.org cancelled: Connection refused
2500 */
2501 if(!strcasecmp(sargv[3], "cancelled:")) {
2502 routing_handle_connect_failure(srv, sargv[2], unsplit_string(sargv+4, sargc-4, NULL));
2503 }
2504 }
2505 }
2506 return 1;
2507 }
2508
2509 static CMD_FUNC(cmd_away)
2510 {
2511 struct userNode *uNode;
2512
2513 uNode = GetUserH(origin);
2514 if (!uNode)
2515 return 1;
2516 if (argc < 2)
2517 uNode->modes &= ~FLAGS_AWAY;
2518 else
2519 uNode->modes |= FLAGS_AWAY;
2520 return 1;
2521 }
2522
2523 static CMD_FUNC(cmd_gline)
2524 {
2525 if (argc < 3)
2526 return 0;
2527 if (argv[2][0] == '+') {
2528 if (argc < 5)
2529 return 0;
2530 gline_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0, 0);
2531 return 1;
2532 } else if (argv[2][0] == '-') {
2533 gline_remove(argv[2]+1, 0);
2534 return 1;
2535 } else
2536 return 0;
2537 }
2538
2539 static CMD_FUNC(cmd_sgline)
2540 {
2541 struct server *sender;
2542
2543 if (argc < 4)
2544 return 0;
2545
2546 if (!(sender = GetServerH(origin)))
2547 return 0;
2548
2549 gline_add(origin, argv[1], strtoul(argv[2], NULL, 0), argv[argc-1], now, 1, 0);
2550 return 1;
2551 }
2552
2553 static CMD_FUNC(cmd_sshun)
2554 {
2555 struct server *sender;
2556
2557 if (argc < 4)
2558 return 0;
2559
2560 if (!(sender = GetServerH(origin)))
2561 return 0;
2562
2563 shun_add(origin, argv[1], strtoul(argv[2], NULL, 0), argv[argc-1], now, 1);
2564 return 1;
2565 }
2566
2567 static CMD_FUNC(cmd_shun)
2568 {
2569 if (argc < 3)
2570 return 0;
2571 if (argv[2][0] == '+') {
2572 if (argc < 5)
2573 return 0;
2574 shun_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0);
2575 return 1;
2576 } else if (argv[2][0] == '-') {
2577 shun_remove(argv[2]+1, 0);
2578 return 1;
2579 } else
2580 return 0;
2581 }
2582
2583 static CMD_FUNC(cmd_svsnick)
2584 {
2585 struct userNode *target, *dest;
2586 if ((argc < 4)
2587 || !(target = GetUserN(argv[1]))
2588 || !IsLocal(target)
2589 || (dest = GetUserH(argv[2])))
2590 return 0;
2591 NickChange(target, argv[2], 0);
2592 return 1;
2593 }
2594
2595 void
2596 free_user(struct userNode *user)
2597 {
2598 free(user->nick);
2599 free(user);
2600 }
2601
2602 static void
2603 parse_cleanup(UNUSED_ARG(void *extra))
2604 {
2605 unsigned int nn;
2606 free(of_list);
2607 free(of_list_extra);
2608 free(privmsg_funcs);
2609 num_privmsg_funcs = 0;
2610 free(notice_funcs);
2611 num_notice_funcs = 0;
2612 free(mcf_list);
2613 dict_delete(irc_func_dict);
2614 for (nn=0; nn<dead_users.used; nn++)
2615 free_user(dead_users.list[nn]);
2616 userList_clean(&dead_users);
2617 }
2618
2619 static void
2620 p10_conf_reload(void) {
2621 hidden_host_suffix = conf_get_data("server/hidden_host", RECDB_QSTRING);
2622 his_servername = conf_get_data("server/his_servername", RECDB_QSTRING);
2623 his_servercomment = conf_get_data("server/his_servercomment", RECDB_QSTRING);
2624 }
2625
2626 static void
2627 remove_unbursted_channel(struct chanNode *cNode, UNUSED_ARG(void *extra)) {
2628 if (unbursted_channels)
2629 dict_remove(unbursted_channels, cNode->name);
2630 }
2631
2632 void
2633 init_parse(void)
2634 {
2635 const char *str, *desc;
2636 int numnick, usermask, max_users;
2637 char numer[COMBO_NUMERIC_LEN+1];
2638
2639 /* read config items */
2640 str = conf_get_data("server/extended_accounts", RECDB_QSTRING);
2641 extended_accounts = str ? enabled_string(str) : 1;
2642 str = conf_get_data("server/ping_freq", RECDB_QSTRING);
2643 ping_freq = str ? ParseInterval(str) : 120;
2644 str = conf_get_data("server/ping_timeout", RECDB_QSTRING);
2645 ping_timeout = str ? ParseInterval(str) : 30;
2646 str = conf_get_data("server/force_n2k", RECDB_QSTRING);
2647 force_n2k = str ? enabled_string(str) : 1;
2648 str = conf_get_data("server/numeric", RECDB_QSTRING);
2649 if (!str) {
2650 log_module(MAIN_LOG, LOG_ERROR, "No server/numeric entry in config file.");
2651 exit(1);
2652 }
2653 numnick = atoi(str);
2654 str = conf_get_data("server/max_users", RECDB_QSTRING);
2655 max_users = str ? atoi(str) : 4096;
2656 for (usermask = 4; usermask < max_users; usermask <<= 1) ;
2657 usermask--;
2658 if ((numnick < 64) && (usermask < 4096) && !force_n2k)
2659 inttobase64(numer, (numnick << 12) + (usermask & 0x00fff), 3);
2660 else
2661 inttobase64(numer, (numnick << 18) + (usermask & 0x3ffff), 5);
2662
2663 str = conf_get_data("server/hostname", RECDB_QSTRING);
2664 desc = conf_get_data("server/description", RECDB_QSTRING);
2665 if (!str || !desc) {
2666 log_module(MAIN_LOG, LOG_ERROR, "No server/hostname entry in config file.");
2667 exit(1);
2668 }
2669 self = AddServer(NULL, str, 0, boot_time, now, numer, desc);
2670 conf_register_reload(p10_conf_reload);
2671
2672 irc_func_dict = dict_new();
2673 dict_insert(irc_func_dict, CMD_BURST, cmd_burst);
2674 dict_insert(irc_func_dict, TOK_BURST, cmd_burst);
2675 dict_insert(irc_func_dict, CMD_CREATE, cmd_create);
2676 dict_insert(irc_func_dict, TOK_CREATE, cmd_create);
2677 dict_insert(irc_func_dict, CMD_EOB, cmd_eob);
2678 dict_insert(irc_func_dict, TOK_EOB, cmd_eob);
2679 dict_insert(irc_func_dict, CMD_EOB_ACK, cmd_eob_ack);
2680 dict_insert(irc_func_dict, TOK_EOB_ACK, cmd_eob_ack);
2681 dict_insert(irc_func_dict, CMD_MODE, cmd_mode);
2682 dict_insert(irc_func_dict, TOK_MODE, cmd_mode);
2683 dict_insert(irc_func_dict, CMD_MARK, cmd_mark);
2684 dict_insert(irc_func_dict, TOK_MARK, cmd_mark);
2685 dict_insert(irc_func_dict, CMD_NICK, cmd_nick);
2686 dict_insert(irc_func_dict, TOK_NICK, cmd_nick);
2687 dict_insert(irc_func_dict, CMD_ACCOUNT, cmd_account);
2688 dict_insert(irc_func_dict, TOK_ACCOUNT, cmd_account);
2689 dict_insert(irc_func_dict, CMD_FAKEHOST, cmd_fakehost);
2690 dict_insert(irc_func_dict, TOK_FAKEHOST, cmd_fakehost);
2691 dict_insert(irc_func_dict, CMD_PASS, cmd_pass);
2692 dict_insert(irc_func_dict, TOK_PASS, cmd_pass);
2693 dict_insert(irc_func_dict, CMD_PING, cmd_ping);
2694 dict_insert(irc_func_dict, TOK_PING, cmd_ping);
2695 dict_insert(irc_func_dict, CMD_PRIVMSG, cmd_privmsg);
2696 dict_insert(irc_func_dict, TOK_PRIVMSG, cmd_privmsg);
2697 dict_insert(irc_func_dict, CMD_PONG, cmd_pong);
2698 dict_insert(irc_func_dict, TOK_PONG, cmd_pong);
2699 dict_insert(irc_func_dict, CMD_QUIT, cmd_quit);
2700 dict_insert(irc_func_dict, TOK_QUIT, cmd_quit);
2701 dict_insert(irc_func_dict, CMD_SERVER, cmd_server);
2702 dict_insert(irc_func_dict, TOK_SERVER, cmd_server);
2703 dict_insert(irc_func_dict, CMD_JOIN, cmd_join);
2704 dict_insert(irc_func_dict, TOK_JOIN, cmd_join);
2705 dict_insert(irc_func_dict, CMD_PART, cmd_part);
2706 dict_insert(irc_func_dict, TOK_PART, cmd_part);
2707 dict_insert(irc_func_dict, CMD_ERROR, cmd_error);
2708 dict_insert(irc_func_dict, TOK_ERROR, cmd_error);
2709 dict_insert(irc_func_dict, CMD_TOPIC, cmd_topic);
2710 dict_insert(irc_func_dict, TOK_TOPIC, cmd_topic);
2711 dict_insert(irc_func_dict, CMD_AWAY, cmd_away);
2712 dict_insert(irc_func_dict, TOK_AWAY, cmd_away);
2713 dict_insert(irc_func_dict, CMD_SILENCE, cmd_silence);
2714 dict_insert(irc_func_dict, TOK_SILENCE, cmd_silence);
2715 dict_insert(irc_func_dict, CMD_KICK, cmd_kick);
2716 dict_insert(irc_func_dict, TOK_KICK, cmd_kick);
2717 dict_insert(irc_func_dict, CMD_SQUIT, cmd_squit);
2718 dict_insert(irc_func_dict, TOK_SQUIT, cmd_squit);
2719 dict_insert(irc_func_dict, CMD_KILL, cmd_kill);
2720 dict_insert(irc_func_dict, TOK_KILL, cmd_kill);
2721 dict_insert(irc_func_dict, CMD_NOTICE, cmd_notice);
2722 dict_insert(irc_func_dict, TOK_NOTICE, cmd_notice);
2723 dict_insert(irc_func_dict, CMD_STATS, cmd_stats);
2724 dict_insert(irc_func_dict, TOK_STATS, cmd_stats);
2725 dict_insert(irc_func_dict, CMD_SVSJOIN, cmd_svsjoin);
2726 dict_insert(irc_func_dict, TOK_SVSJOIN, cmd_svsjoin);
2727 dict_insert(irc_func_dict, CMD_SVSNICK, cmd_svsnick);
2728 dict_insert(irc_func_dict, TOK_SVSNICK, cmd_svsnick);
2729 dict_insert(irc_func_dict, CMD_SVSPART, cmd_svspart);
2730 dict_insert(irc_func_dict, TOK_SVSPART, cmd_svspart);
2731 dict_insert(irc_func_dict, CMD_SWHOIS, cmd_dummy);
2732 dict_insert(irc_func_dict, TOK_SWHOIS, cmd_dummy);
2733 dict_insert(irc_func_dict, CMD_WHOIS, cmd_whois);
2734 dict_insert(irc_func_dict, TOK_WHOIS, cmd_whois);
2735 dict_insert(irc_func_dict, CMD_GLINE, cmd_gline);
2736 dict_insert(irc_func_dict, TOK_GLINE, cmd_gline);
2737 dict_insert(irc_func_dict, CMD_SGLINE, cmd_sgline);
2738 dict_insert(irc_func_dict, TOK_SGLINE, cmd_sgline);
2739 dict_insert(irc_func_dict, CMD_SHUN, cmd_shun);
2740 dict_insert(irc_func_dict, TOK_SHUN, cmd_shun);
2741 dict_insert(irc_func_dict, CMD_SSHUN, cmd_sshun);
2742 dict_insert(irc_func_dict, TOK_SSHUN, cmd_sshun);
2743 dict_insert(irc_func_dict, CMD_OPMODE, cmd_opmode);
2744 dict_insert(irc_func_dict, TOK_OPMODE, cmd_opmode);
2745 dict_insert(irc_func_dict, CMD_CLEARMODE, cmd_clearmode);
2746 dict_insert(irc_func_dict, TOK_CLEARMODE, cmd_clearmode);
2747 dict_insert(irc_func_dict, CMD_VERSION, cmd_version);
2748 dict_insert(irc_func_dict, TOK_VERSION, cmd_version);
2749 dict_insert(irc_func_dict, CMD_ADMIN, cmd_admin);
2750 dict_insert(irc_func_dict, TOK_ADMIN, cmd_admin);
2751 dict_insert(irc_func_dict, CMD_SMO, cmd_dummy);
2752 dict_insert(irc_func_dict, TOK_SMO, cmd_dummy);
2753 dict_insert(irc_func_dict, CMD_SNO, cmd_dummy);
2754 dict_insert(irc_func_dict, TOK_SNO, cmd_dummy);
2755
2756 dict_insert(irc_func_dict, CMD_RPING, cmd_rping);
2757 dict_insert(irc_func_dict, TOK_RPING, cmd_rping);
2758 dict_insert(irc_func_dict, CMD_RPONG, cmd_dummy);
2759 dict_insert(irc_func_dict, TOK_RPONG, cmd_dummy);
2760
2761 dict_insert(irc_func_dict, CMD_SASL, cmd_sasl);
2762 dict_insert(irc_func_dict, TOK_SASL, cmd_sasl);
2763
2764 /* In P10, DESTRUCT doesn't do anything except be broadcast to servers.
2765 * Apparently to obliterate channels from any servers that think they
2766 * exist?
2767 */
2768 dict_insert(irc_func_dict, CMD_DESTRUCT, cmd_dummy);
2769 dict_insert(irc_func_dict, TOK_DESTRUCT, cmd_dummy);
2770 /* Ignore invites */
2771 dict_insert(irc_func_dict, CMD_INVITE, cmd_dummy);
2772 dict_insert(irc_func_dict, TOK_INVITE, cmd_dummy);
2773 /* DESYNCH is just informational, so ignore it */
2774 dict_insert(irc_func_dict, CMD_DESYNCH, cmd_dummy);
2775 dict_insert(irc_func_dict, TOK_DESYNCH, cmd_dummy);
2776 /* Ignore channel operator notices. */
2777 dict_insert(irc_func_dict, CMD_WALLCHOPS, cmd_dummy);
2778 dict_insert(irc_func_dict, TOK_WALLCHOPS, cmd_dummy);
2779 dict_insert(irc_func_dict, CMD_WALLVOICES, cmd_dummy);
2780 dict_insert(irc_func_dict, TOK_WALLVOICES, cmd_dummy);
2781 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
2782 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
2783 /* Ignore opers being silly. */
2784 dict_insert(irc_func_dict, CMD_WALLOPS, cmd_dummy);
2785 dict_insert(irc_func_dict, TOK_WALLOPS, cmd_dummy);
2786 dict_insert(irc_func_dict, CMD_WALLHOPS, cmd_dummy);
2787 dict_insert(irc_func_dict, TOK_WALLHOPS, cmd_dummy);
2788 dict_insert(irc_func_dict, TOK_WALLUSERS, cmd_dummy);
2789 /* Ignore dnsbl exemptions */
2790 dict_insert(irc_func_dict, TOK_EXEMPT, cmd_dummy);
2791 dict_insert(irc_func_dict, CMD_PRIVS, cmd_privs);
2792 dict_insert(irc_func_dict, TOK_PRIVS, cmd_privs);
2793 /* ignore ALIST for now */
2794 dict_insert(irc_func_dict, TOK_ALIST, cmd_dummy);
2795 dict_insert(irc_func_dict, CMD_ALIST, cmd_dummy);
2796 /* ignore SPAMFILTER */
2797 dict_insert(irc_func_dict, TOK_SPAMFILTER, cmd_dummy);
2798 dict_insert(irc_func_dict, CMD_SPAMFILTER, cmd_dummy);
2799 /* Ignore remote luser */
2800 dict_insert(irc_func_dict, TOK_LUSERS, cmd_dummy);
2801 /* We have reliable clock! Always! Wraaa! */
2802 dict_insert(irc_func_dict, CMD_SETTIME, cmd_dummy);
2803 dict_insert(irc_func_dict, TOK_SETTIME, cmd_dummy);
2804 /* Ignore ZLINE/ZL/REMOVE/RM */
2805 dict_insert(irc_func_dict, CMD_ZLINE, cmd_dummy);
2806 dict_insert(irc_func_dict, TOK_ZLINE, cmd_dummy);
2807 dict_insert(irc_func_dict, CMD_REMOVE, cmd_dummy);
2808 dict_insert(irc_func_dict, TOK_REMOVE, cmd_dummy);
2809
2810 /* ignore /trace and /motd commands targetted at us */
2811 dict_insert(irc_func_dict, TOK_TRACE, cmd_dummy);
2812 dict_insert(irc_func_dict, TOK_MOTD, cmd_dummy);
2813 dict_insert(irc_func_dict, TOK_UPING, cmd_dummy);
2814
2815 /* handle topics */
2816 dict_insert(irc_func_dict, "331", cmd_num_topic);
2817 dict_insert(irc_func_dict, "332", cmd_num_topic);
2818 dict_insert(irc_func_dict, "333", cmd_num_topic);
2819 dict_insert(irc_func_dict, "345", cmd_dummy); /* blah has been invited to blah */
2820 dict_insert(irc_func_dict, "432", cmd_error_nick); /* Erroneus [sic] nickname */
2821 /* ban list resetting */
2822 /* "stats g" responses */
2823 dict_insert(irc_func_dict, "230", cmd_dummy); /* ignore stats headers */
2824 dict_insert(irc_func_dict, "247", cmd_num_gline);
2825 dict_insert(irc_func_dict, "542", cmd_num_shun);
2826 dict_insert(irc_func_dict, "219", cmd_dummy); /* "End of /STATS report" */
2827 /* other numeric responses we might get */
2828 dict_insert(irc_func_dict, "401", cmd_dummy); /* target left network */
2829 dict_insert(irc_func_dict, "403", cmd_dummy); /* no such channel */
2830 dict_insert(irc_func_dict, "404", cmd_dummy); /* cannot send to channel */
2831 dict_insert(irc_func_dict, "439", cmd_dummy); /* target change too fast */
2832 dict_insert(irc_func_dict, "441", cmd_dummy); /* target isn't on that channel */
2833 dict_insert(irc_func_dict, "442", cmd_dummy); /* you aren't on that channel */
2834 dict_insert(irc_func_dict, "443", cmd_dummy); /* is already on channel (after invite?) */
2835 dict_insert(irc_func_dict, "461", cmd_dummy); /* Not enough parameters (after TOPIC w/ 0 args) */
2836 dict_insert(irc_func_dict, "467", cmd_dummy); /* Channel key already set */
2837
2838 num_privmsg_funcs = 16;
2839 privmsg_funcs = malloc(sizeof(privmsg_func_t)*num_privmsg_funcs);
2840 memset(privmsg_funcs, 0, sizeof(privmsg_func_t)*num_privmsg_funcs);
2841
2842 num_notice_funcs = 16;
2843 notice_funcs = malloc(sizeof(privmsg_func_t)*num_notice_funcs);
2844 memset(notice_funcs, 0, sizeof(privmsg_func_t)*num_notice_funcs);
2845
2846 userList_init(&dead_users);
2847 reg_del_channel_func(remove_unbursted_channel, NULL);
2848 reg_exit_func(parse_cleanup, NULL);
2849 // reg_notice_func(opserv, check_ctcp);
2850 }
2851
2852 int
2853 parse_line(char *line, int recursive)
2854 {
2855 char *argv[MAXNUMPARAMS], *origin;
2856 int argc, cmd, res=0;
2857 cmd_func_t *func;
2858
2859 argc = split_line(line, true, MAXNUMPARAMS, argv);
2860 cmd = self->uplink || !argv[0][1] || !argv[0][2];
2861 if (argc > cmd) {
2862 if (cmd) {
2863 if (argv[0][0] == ':') {
2864 origin = argv[0]+1;
2865 } else if (!argv[0][1] || !argv[0][2]) {
2866 struct server *sNode = GetServerN(argv[0]);
2867 origin = sNode ? sNode->name : 0;
2868 } else {
2869 struct userNode *uNode = GetUserN(argv[0]);
2870 origin = uNode ? uNode->nick : 0;
2871 }
2872 } else
2873 origin = 0;
2874 if ((func = dict_find(irc_func_dict, argv[cmd], NULL)))
2875 res = func(origin, argc-cmd, argv+cmd);
2876 }
2877 if (!res) {
2878 log_module(MAIN_LOG, LOG_ERROR, "PARSE ERROR on line: %s", unsplit_string(argv, argc, NULL));
2879 } else if (!recursive) {
2880 unsigned int i;
2881 for (i=0; i<dead_users.used; i++)
2882 free_user(dead_users.list[i]);
2883 dead_users.used = 0;
2884 }
2885 return res;
2886 }
2887
2888 static void
2889 parse_foreach(char *target_list, foreach_chanfunc cf, foreach_nonchan nc, foreach_userfunc uf, foreach_nonuser nu, void *data)
2890 {
2891 char *j, old;
2892
2893 do {
2894 j = target_list;
2895 while (*j != 0 && *j != ',')
2896 j++;
2897 old = *j;
2898 *j = 0;
2899
2900 if (IsChannelName(target_list)
2901 || (target_list[0] == '0' && target_list[1] == '\0')) {
2902 struct chanNode *chan = GetChannel(target_list);
2903
2904 if (chan) {
2905 if (cf)
2906 cf(chan, data);
2907 } else {
2908 if (nc)
2909 nc(target_list, data);
2910 }
2911 } else {
2912 struct userNode *user;
2913 struct privmsg_desc *pd = data;
2914
2915 pd->is_qualified = 0;
2916 if (*target_list == '@') {
2917 user = NULL;
2918 } else if (strchr(target_list, '@')) {
2919 struct server *server;
2920
2921 pd->is_qualified = 1;
2922 user = GetUserH(strtok(target_list, "@"));
2923 server = GetServerH(strtok(NULL, "@"));
2924
2925 if (user && (user->uplink != server)) {
2926 /* Don't attempt to index into any arrays
2927 using a user's numeric on another server. */
2928 user = NULL;
2929 }
2930 } else {
2931 user = GetUserN(target_list);
2932 }
2933
2934 if (user) {
2935 if (uf)
2936 uf(user, data);
2937 } else {
2938 if (nu)
2939 nu(target_list, data);
2940 }
2941 }
2942 target_list = j+1;
2943 } while (old == ',');
2944 }
2945
2946 static int
2947 get_local_numeric(void)
2948 {
2949 static unsigned int next_numeric = 0;
2950 if (self->clients > self->num_mask)
2951 return -1;
2952 while (self->users[next_numeric])
2953 if (++next_numeric > self->num_mask)
2954 next_numeric = 0;
2955 return next_numeric;
2956 }
2957
2958 static void
2959 make_numeric(struct server *svr, int local_num, char *outbuf)
2960 {
2961 int slen, llen;
2962
2963 if (force_n2k || svr->numeric[1]) {
2964 slen = 2;
2965 llen = 3;
2966 } else {
2967 slen = 1;
2968 llen = (local_num < 64*64) ? 2 : 3;
2969 }
2970 strncpy(outbuf, svr->numeric, slen);
2971 inttobase64(outbuf+slen, local_num, llen);
2972 outbuf[slen+llen] = 0;
2973 }
2974
2975 struct server *
2976 AddServer(struct server *uplink, const char *name, int hops, time_t boot, time_t link_time, const char *numeric, const char *description)
2977 {
2978 struct server* sNode;
2979 int slen, mlen;
2980
2981 if ((sNode = GetServerN(numeric))) {
2982 /* This means we're trying to re-add an existant server.
2983 * To be safe, we should forget the previous incarnation.
2984 * (And all its linked servers.)
2985 *
2986 * It usually only happens in replays when the original
2987 * had a ping timeout and the replay didn't (because
2988 * replaying a ping timeout invariably gets things wrong).
2989 */
2990 DelServer(sNode, 0, NULL);
2991 }
2992
2993 switch (strlen(numeric)) {
2994 case 5: slen = 2; mlen = 3; break;
2995 case 4: slen = 1; mlen = 3; break;
2996 case 3: slen = 1; mlen = 2; break;
2997 default:
2998 log_module(MAIN_LOG, LOG_ERROR, "AddServer(\"%s\", \"%s\", ...): Numeric %s has invalid length.", uplink->name, name, numeric);
2999 return NULL;
3000 }
3001
3002 sNode = calloc(1, sizeof(*sNode));
3003 sNode->uplink = uplink;
3004 safestrncpy(sNode->name, name, sizeof(sNode->name));
3005 sNode->num_mask = base64toint(numeric+slen, mlen);
3006 sNode->hops = hops;
3007 sNode->boot = boot;
3008 sNode->link_time = link_time;
3009 strncpy(sNode->numeric, numeric, slen);
3010 safestrncpy(sNode->description, description, sizeof(sNode->description));
3011 sNode->users = calloc(sNode->num_mask+1, sizeof(*sNode->users));
3012 serverList_init(&sNode->children);
3013 if (sNode->uplink) {
3014 /* uplink may be NULL if we're just building ourself */
3015 serverList_append(&sNode->uplink->children, sNode);
3016 }
3017 servers_num[base64toint(numeric, slen)] = sNode;
3018 dict_insert(servers, sNode->name, sNode);
3019 return sNode;
3020 }
3021
3022 void DelServer(struct server* serv, int announce, const char *message)
3023 {
3024 unsigned int i;
3025
3026 /* If we receive an ERROR command before the SERVER
3027 * command a NULL server can be passed */
3028 if (!serv)
3029 return;
3030
3031 /* Hrm, what's the right way to SQUIT some other server?
3032 * (This code is only to handle killing juped servers.) */
3033 if (announce && (serv->uplink == self) && (serv != self->uplink))
3034 irc_squit(serv, message, NULL);
3035
3036 /* must recursively remove servers linked to this one first */
3037 for (i=serv->children.used;i>0;)
3038 if (serv->children.list[--i] != self)
3039 DelServer(serv->children.list[i], false, NULL);
3040
3041 /* clean up server's user hash tables */
3042 for (i=0;i<=serv->num_mask;i++)
3043 if (serv->users[i])
3044 DelUser(serv->users[i], NULL, false, "server delinked");
3045
3046 /* delete server */
3047 if (serv->uplink)
3048 serverList_remove(&serv->uplink->children, serv);
3049 if (serv == self->uplink)
3050 self->uplink = NULL;
3051 servers_num[base64toint(serv->numeric, strlen(serv->numeric))] = NULL;
3052 dict_remove(servers, serv->name);
3053 serverList_clean(&serv->children);
3054 free(serv->users);
3055 free(serv);
3056 }
3057
3058 struct userNode *
3059 AddLocalUser(const char *nick, const char *ident, const char *hostname, const char *desc, const char *modes)
3060 {
3061 char numeric[COMBO_NUMERIC_LEN+1];
3062 int local_num = get_local_numeric();
3063 time_t timestamp = now;
3064 struct userNode *old_user = GetUserH(nick);
3065
3066 if (!modes)
3067 modes = "+oik";
3068
3069 if (old_user) {
3070 if (IsLocal(old_user))
3071 return old_user;
3072 timestamp = old_user->timestamp - 1;
3073 }
3074 if (local_num == -1) {
3075 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for service %s", nick);
3076 return 0;
3077 }
3078 if (!hostname)
3079 hostname = self->name;
3080 make_numeric(self, local_num, numeric);
3081 return AddUser(self, nick, ident, hostname, modes, numeric, desc, timestamp, "AAAAAA");
3082 }
3083
3084 struct userNode *
3085 AddClone(const char *nick, const char *ident, const char *hostname, const char *desc)
3086 {
3087 char numeric[COMBO_NUMERIC_LEN+1];
3088 int local_num = get_local_numeric();
3089 time_t timestamp = now;
3090 struct userNode *old_user = GetUserH(nick);
3091
3092 if (old_user) {
3093 if (IsLocal(old_user))
3094 return old_user;
3095 timestamp = old_user->timestamp - 1;
3096 }
3097 if (local_num == -1) {
3098 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for clone %s", nick);
3099 return 0;
3100 }
3101 make_numeric(self, local_num, numeric);
3102 return AddUser(self, nick, ident, hostname, "+i", numeric, desc, timestamp, "AAAAAA");
3103 }
3104
3105 int
3106 is_valid_nick(const char *nick) {
3107 unsigned int ii;
3108 /* IRC has some of The Most Fucked-Up ideas about character sets
3109 * in the world.. */
3110 if (!isalpha(*nick) && !strchr("{|}~[\\]^_`", *nick))
3111 return 0;
3112 for (ii = 0; nick[ii]; ++ii)
3113 if (!isalnum(nick[ii]) && !strchr("{|}~[\\]^-_`", nick[ii]))
3114 return 0;
3115 if (strlen(nick) > nicklen)
3116 return 0;
3117 return 1;
3118 }
3119
3120 static struct userNode*
3121 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)
3122 {
3123 struct userNode *oldUser, *uNode;
3124 unsigned int ignore_user, dummy;
3125 char *tstr;
3126 int type;
3127
3128 if ((strlen(numeric) < 3) || (strlen(numeric) > 5)) {
3129 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): numeric %s wrong length!", (void*)uplink, nick, numeric);
3130 return NULL;
3131 }
3132
3133 if (!uplink) {
3134 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s doesn't exist!", (void*)uplink, nick, numeric);
3135 return NULL;
3136 }
3137
3138 if (uplink != GetServerN(numeric)) {
3139 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s differs from nominal uplink %s.", (void*)uplink, nick, numeric, uplink->name);
3140 return NULL;
3141 }
3142
3143 dummy = modes && modes[0] == '*';
3144 if (dummy) {
3145 ++modes;
3146 } else if (!is_valid_nick(nick)) {
3147 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): invalid nickname detected.", (void*)uplink, nick);
3148 return NULL;
3149 }
3150
3151 ignore_user = 0;
3152 if ((oldUser = GetUserH(nick))) {
3153 if (IsLocal(oldUser)
3154 && (IsService(oldUser) || IsPersistent(oldUser))) {
3155 /* The service should collide the new user off - but not
3156 * if the new user is coming in during a burst. (During a
3157 * burst, the bursting server will kill either our user --
3158 * triggering a ReintroduceUser() -- or its own.)
3159 */
3160 oldUser->timestamp = timestamp - 1;
3161 ignore_user = 1;
3162 if (!uplink->burst)
3163 irc_user(oldUser);
3164 } else if (oldUser->timestamp > timestamp) {
3165 /* "Old" user is really newer; remove them */
3166 DelUser(oldUser, 0, 1, "Overruled by older nick");
3167 } else {
3168 /* User being added is too new; do not add them to
3169 * clients, but do add them to the server's list, since it
3170 * will send a KILL and QUIT soon. */
3171 ignore_user = 1;
3172 }
3173 }
3174
3175 /* create new usernode and set all values */
3176 uNode = calloc(1, sizeof(*uNode));
3177 uNode->nick = strdup(nick);
3178 safestrncpy(uNode->ident, ident, sizeof(uNode->ident));
3179 safestrncpy(uNode->info, userinfo, sizeof(uNode->info));
3180 safestrncpy(uNode->hostname, hostname, sizeof(uNode->hostname));
3181 safestrncpy(uNode->numeric, numeric, sizeof(uNode->numeric));
3182 irc_p10_pton(&uNode->ip, realip);
3183
3184 tstr = conf_get_data("server/type", RECDB_QSTRING);
3185 type = atoi(tstr);
3186 if (type == 7) {
3187 if (irc_in_addr_is_ipv4(uNode->ip)) {
3188 make_virtip((char*)irc_ntoa(&uNode->ip), (char*)irc_ntoa(&uNode->ip), uNode->cryptip);
3189 make_virthost((char*)irc_ntoa(&uNode->ip), uNode->hostname, uNode->crypthost);
3190 } else if (irc_in_addr_is_ipv6(uNode->ip)) {
3191 make_ipv6virthost((char*)irc_ntoa(&uNode->ip), uNode->hostname, uNode->crypthost);
3192 }
3193 }
3194
3195 if (!uNode->crypthost && uNode->cryptip)
3196 snprintf(uNode->crypthost, sizeof(uNode->crypthost), "%s", strdup(uNode->cryptip));
3197 uNode->idle_since = timestamp;
3198 uNode->timestamp = timestamp;
3199 modeList_init(&uNode->channels);
3200 uNode->uplink = uplink;
3201 if (++uNode->uplink->clients > uNode->uplink->max_clients) {
3202 uNode->uplink->max_clients = uNode->uplink->clients;
3203 }
3204 uNode->num_local = base64toint(numeric+strlen(uNode->uplink->numeric), 3) & uNode->uplink->num_mask;
3205 uNode->uplink->users[uNode->num_local] = uNode;
3206 mod_usermode(uNode, modes);
3207 if (dummy)
3208 uNode->modes |= FLAGS_DUMMY;
3209
3210 set_geoip_info(uNode);
3211
3212 if (ignore_user)
3213 return uNode;
3214
3215 dict_insert(clients, uNode->nick, uNode);
3216 if (dict_size(clients) > max_clients) {
3217 max_clients = dict_size(clients);
3218 max_clients_time = now;
3219 }
3220 if (IsLocal(uNode))
3221 irc_user(uNode);
3222 call_new_user_funcs(uNode);
3223
3224 if ((uNode->loc == 1) && (uNode->handle_info))
3225 send_func_list(uNode);
3226
3227 return uNode;
3228 }
3229
3230 /* removes user from it's server's hash table and nick hash table */
3231 void
3232 DelUser(struct userNode* user, struct userNode *killer, int announce, const char *why)
3233 {
3234
3235 verify(user);
3236
3237 /* mark them as dead, in case anybody cares */
3238 user->dead = 1;
3239
3240 /* remove pending adduser commands */
3241 wipe_adduser_pending(NULL, user);
3242
3243 /* remove user from all channels */
3244 while (user->channels.used > 0)
3245 DelChannelUser(user, user->channels.list[user->channels.used-1]->channel, NULL, 0);
3246
3247 /* Call these in reverse order so ChanServ can update presence
3248 information before NickServ nukes the handle_info. */
3249 call_del_user_funcs(user, killer, why);
3250
3251 user->uplink->clients--;
3252 user->uplink->users[user->num_local] = NULL;
3253 if (IsOper(user)) {
3254 userList_remove(&curr_opers, user);
3255 if (count_opers > 0 && !IsBotM(user) && !IsService(user) && !IsHideOper(user))
3256 count_opers--;
3257 }
3258 /* remove from global dictionary, but not if after a collide */
3259 if (user == dict_find(clients, user->nick, NULL))
3260 dict_remove(clients, user->nick);
3261
3262 if (IsInvisible(user))
3263 invis_clients--;
3264
3265 if (announce) {
3266 if (IsLocal(user))
3267 irc_quit(user, why);
3268 else
3269 irc_kill(killer, user, why);
3270 }
3271
3272 if (IsLocal(user)) {
3273 unsigned int num = user->num_local;
3274 if (num < num_privmsg_funcs)
3275 privmsg_funcs[num] = NULL;
3276 if (num < num_notice_funcs)
3277 notice_funcs[num] = NULL;
3278 }
3279
3280 modeList_clean(&user->channels);
3281
3282 /* Clean up version data */
3283 if(user->version_reply) {
3284 free(user->version_reply);
3285 user->version_reply = NULL;
3286 }
3287
3288 /* Clean up SSL fingerprint data */
3289 if(user->sslfp) {
3290 free(user->sslfp);
3291 user->sslfp = NULL;
3292 }
3293
3294 /* clean up mark */
3295 if(user->mark) {
3296 free(user->mark);
3297 user->mark = NULL;
3298 }
3299 free_string_list(user->marks);
3300 user->marks = NULL;
3301
3302 /* clean up geoip data if any */
3303 if(user->country_code) free(user->country_code);
3304 if(user->city) free(user->city);
3305 if(user->region) free(user->region);
3306 if(user->postal_code) free(user->postal_code);
3307
3308 /* We don't free them, in case we try to privmsg them or something
3309 * (like when a stupid oper kills themself). We just put them onto
3310 * a list of clients that get freed after processing each line.
3311 */
3312 if (dead_users.size)
3313 userList_append(&dead_users, user);
3314 else
3315 free_user(user);
3316 }
3317
3318 static void call_oper_funcs(struct userNode *user);
3319
3320 void mod_usermode(struct userNode *user, const char *mode_change) {
3321 int add = 1;
3322 long setmodes = 0;
3323 int donemodes = 0;
3324 const char *word = mode_change;
3325
3326 if (!user || !mode_change)
3327 return;
3328
3329 call_user_mode_funcs(user, mode_change);
3330
3331 setmodes = user->modes;
3332
3333 while (*word != ' ' && *word) word++;
3334 while (*word == ' ') word++;
3335 while (!donemodes) {
3336 #define do_user_mode(FLAG) do { if (add) user->modes |= FLAG; else user->modes &= ~FLAG; } while (0)
3337 switch (*mode_change++) {
3338 case 0: case ' ':
3339 donemodes = 1;
3340 break;
3341 case '+': add = 1; break;
3342 case '-': add = 0; break;
3343 case 'o':
3344 do_user_mode(FLAGS_OPER);
3345 if (!add) {
3346 userList_remove(&curr_opers, user);
3347 } else if (!userList_contains(&curr_opers, user)) {
3348 userList_append(&curr_opers, user);
3349 call_oper_funcs(user);
3350 }
3351 break;
3352 case 'O': do_user_mode(FLAGS_LOCOP); break;
3353 case 'i': do_user_mode(FLAGS_INVISIBLE);
3354 if (add)
3355 invis_clients++;
3356 else
3357 invis_clients--;
3358 break;
3359 case 'w': do_user_mode(FLAGS_WALLOP); break;
3360 case 'd': do_user_mode(FLAGS_DEAF); break;
3361 case 'k': do_user_mode(FLAGS_SERVICE); break;
3362 case 'g': do_user_mode(FLAGS_GLOBAL); break;
3363 case 'B': do_user_mode(FLAGS_BOT); break;
3364 case 'n': do_user_mode(FLAGS_HIDECHANS); break;
3365 case 'I': do_user_mode(FLAGS_HIDEIDLE); break;
3366 case 'X': do_user_mode(FLAGS_XTRAOP); break;
3367 case 'C': do_user_mode(FLAGS_CLOAKHOST);
3368 if (*word) {
3369 char cloakhost[MAXLEN];
3370 unsigned int ii;
3371 for (ii=0; (*word != ' ') && (*word != '\0'); )
3372 cloakhost[ii++] = *word++;
3373 cloakhost[ii] = 0;
3374 while (*word == ' ')
3375 word++;
3376 safestrncpy(user->crypthost, cloakhost, sizeof(user->crypthost));
3377 }
3378 break;
3379 case 'c': do_user_mode(FLAGS_CLOAKIP);
3380 if (*word) {
3381 char cloakip[MAXLEN];
3382 unsigned int ii;
3383 for (ii=0; (*word != ' ') && (*word != '\0'); )
3384 cloakip[ii++] = *word++;
3385 cloakip[ii] = 0;
3386 while (*word == ' ')
3387 word++;
3388 safestrncpy(user->cryptip, cloakip, sizeof(user->cryptip));
3389 }
3390 break;
3391 // sethost - reed/apples
3392 // case 'h': do_user_mode(FLAGS_HELPER); break;
3393 // I check if there's an 'h' in the first part, and if there,
3394 // then everything after the space becomes their new host.
3395 case 'h': do_user_mode(FLAGS_SETHOST);
3396 if (*word) {
3397 char sethost[MAXLEN];
3398 unsigned int ii;
3399 for (ii=0; (*word != ' ') && (*word != '\0'); )
3400 sethost[ii++] = *word++;
3401 sethost[ii] = 0;
3402 while (*word == ' ')
3403 word++;
3404 safestrncpy(user->sethost, sethost, sizeof(user->sethost));
3405 }
3406 break;
3407 case 'x': do_user_mode(FLAGS_HIDDEN_HOST); break;
3408 case 'r':
3409 if (*word) {
3410 char tag[MAXLEN];
3411 unsigned int ii;
3412 for (ii=0; (*word != ' ') && (*word != '\0'); )
3413 tag[ii++] = *word++;
3414 tag[ii] = 0;
3415 while (*word == ' ')
3416 word++;
3417 call_account_func(user, tag);
3418 }
3419 break;
3420 case 'f':
3421 if (*word) {
3422 char host[MAXLEN];
3423 unsigned int ii;
3424 for (ii=0; (*word != ' ') && (*word != '\0'); )
3425 host[ii++] = *word++;
3426 host[ii] = 0;
3427 while (*word == ' ')
3428 word++;
3429 assign_fakehost(user, host, 0);
3430 }
3431 break;
3432 case 'a': do_user_mode(FLAGS_ADMIN); break;
3433 case 'z': do_user_mode(FLAGS_SSL); break;
3434 case 'D': do_user_mode(FLAGS_PRIVDEAF); break;
3435 case 'R': do_user_mode(FLAGS_ACCOUNTONLY); break;
3436 case 'W': do_user_mode(FLAGS_WHOIS); break;
3437 case 'H': do_user_mode(FLAGS_HIDEOPER); break;
3438 case 'L': do_user_mode(FLAGS_NOLINK); break;
3439 case 'q': do_user_mode(FLAGS_COMMONCHANSONLY); break;
3440 }
3441 #undef do_user_mode
3442 }
3443
3444 // Set user mode +o
3445 if (!(setmodes & FLAGS_OPER) && IsOper(user)) {
3446 if (!IsBotM(user) && !IsService(user) && !IsHideOper(user))
3447 count_opers++;
3448 }
3449
3450 // Set user mode -o
3451 if ((setmodes & FLAGS_OPER) && !IsOper(user)) {
3452 if (count_opers > 1 && !(setmodes & FLAGS_BOT) &&
3453 !(setmodes & FLAGS_SERVICE) && !(setmodes & FLAGS_HIDEOPER))
3454 count_opers--;
3455 }
3456
3457 // Set +H, +k or +B
3458 if (!(setmodes & FLAGS_HIDEOPER) &&
3459 !(setmodes & FLAGS_SERVICE) &&
3460 !(setmodes & FLAGS_BOT) &&
3461 (IsHideOper(user) || IsService(user) || IsBotM(user))) {
3462 if ((setmodes & FLAGS_OPER) && IsOper(user) && count_opers > 0)
3463 count_opers--;
3464 }
3465
3466 // Set -H, -k or -B
3467 if (((setmodes & FLAGS_HIDEOPER) ||
3468 (setmodes & FLAGS_SERVICE) ||
3469 (setmodes & FLAGS_BOT)) &&
3470 !IsHideOper(user) && !IsService(user) && !IsBotM(user)) {
3471 if ((setmodes & FLAGS_OPER) && IsOper(user))
3472 count_opers++;
3473 }
3474 }
3475
3476 static int
3477 keyncpy(char output[], char input[], size_t output_size)
3478 {
3479 size_t ii;
3480
3481 if (input[0] == ':')
3482 {
3483 output[0] = '\0';
3484 return 1;
3485 }
3486
3487 for (ii = 0; (ii + 1 < output_size) && (input[ii] != '\0'); ++ii)
3488 {
3489 output[ii] = input[ii];
3490 }
3491
3492 output[ii] = '\0';
3493 return 0;
3494 }
3495
3496 struct mod_chanmode *
3497 mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, unsigned int flags, short base_oplevel)
3498 {
3499 struct mod_chanmode *change;
3500 unsigned int ii, in_arg, ch_arg, add;
3501
3502 if (argc == 0)
3503 return NULL;
3504 if (!(change = mod_chanmode_alloc(argc - 1)))
3505 return NULL;
3506
3507 for (ii = ch_arg = 0, in_arg = add = 1;
3508 (modes[0][ii] != '\0') && (modes[0][ii] != ' ');
3509 ++ii) {
3510 switch (modes[0][ii]) {
3511 case '+':
3512 add = 1;
3513 break;
3514 case '-':
3515 add = 0;
3516 break;
3517 #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)
3518 case 'C': do_chan_mode(MODE_NOCTCPS); break;
3519 case 'D': do_chan_mode(MODE_DELAYJOINS); break;
3520 case 'c': do_chan_mode(MODE_NOCOLORS); break;
3521 case 'i': do_chan_mode(MODE_INVITEONLY); break;
3522 case 'm': do_chan_mode(MODE_MODERATED); break;
3523 case 'n': do_chan_mode(MODE_NOPRIVMSGS); break;
3524 case 'p': do_chan_mode(MODE_PRIVATE); break;
3525 case 'r': do_chan_mode(MODE_REGONLY); break;
3526 case 's': do_chan_mode(MODE_SECRET); break;
3527 case 't': do_chan_mode(MODE_TOPICLIMIT); break;
3528 case 'S': do_chan_mode(MODE_STRIPCOLOR); break;
3529 case 'M': do_chan_mode(MODE_MODUNREG); break;
3530 case 'N': do_chan_mode(MODE_NONOTICE); break;
3531 case 'Q': do_chan_mode(MODE_NOQUITMSGS); break;
3532 case 'T': do_chan_mode(MODE_NOAMSG); break;
3533 case 'O': do_chan_mode(MODE_OPERSONLY); break;
3534 case 'a': do_chan_mode(MODE_ADMINSONLY); break;
3535 case 'Z': do_chan_mode(MODE_SSLONLY); break;
3536 case 'L': do_chan_mode(MODE_HIDEMODE); break;
3537 case 'z':
3538 if (!(flags & MCP_REGISTERED)) {
3539 do_chan_mode(MODE_REGISTERED);
3540 } else {
3541 mod_chanmode_free(change);
3542 return NULL;
3543 }
3544 break;
3545 #undef do_chan_mode
3546 case 'l':
3547 if (add) {
3548 if (in_arg >= argc)
3549 goto error;
3550 change->modes_set |= MODE_LIMIT;
3551 change->new_limit = atoi(modes[in_arg++]);
3552 } else {
3553 change->modes_set &= ~MODE_LIMIT;
3554 change->modes_clear |= MODE_LIMIT;
3555 }
3556 break;
3557 case 'k':
3558 if (add) {
3559 if ((in_arg >= argc)
3560 || keyncpy(change->new_key, modes[in_arg++], sizeof(change->new_key)))
3561 goto error;
3562 change->modes_set |= MODE_KEY;
3563 } else {
3564 change->modes_clear |= MODE_KEY;
3565 if (!(flags & MCP_KEY_FREE)) {
3566 if (in_arg >= argc)
3567 goto error;
3568 in_arg++;
3569 }
3570 }
3571 break;
3572 case 'U':
3573 if (add)
3574 {
3575 if ((in_arg >= argc)
3576 || keyncpy(change->new_upass, modes[in_arg++], sizeof(change->new_upass)))
3577 goto error;
3578 change->modes_set |= MODE_UPASS;
3579 } else {
3580 change->modes_clear |= MODE_UPASS;
3581 if (!(flags & MCP_UPASS_FREE)) {
3582 if (in_arg >= argc)
3583 goto error;
3584 in_arg++;
3585 }
3586 }
3587 break;
3588 case 'A':
3589 if (add) {
3590 if ((in_arg >= argc)
3591 || keyncpy(change->new_apass, modes[in_arg++], sizeof(change->new_apass)))
3592 goto error;
3593 change->modes_set |= MODE_APASS;
3594 } else {
3595 change->modes_clear |= MODE_APASS;
3596 if (!(flags & MCP_APASS_FREE)) {
3597 if (in_arg >= argc)
3598 goto error;
3599 in_arg++;
3600 }
3601 }
3602 break;
3603 case 'b':
3604 if (!(flags & MCP_ALLOW_OVB))
3605 goto error;
3606 if (in_arg >= argc)
3607 goto error;
3608 change->args[ch_arg].mode = MODE_BAN;
3609 if (!add)
3610 change->args[ch_arg].mode |= MODE_REMOVE;
3611 change->args[ch_arg++].u.hostmask = modes[in_arg++];
3612 break;
3613 case 'e':
3614 if (!(flags & MCP_ALLOW_OVB))
3615 goto error;
3616 if (in_arg >= argc)
3617 goto error;
3618 change->args[ch_arg].mode = MODE_EXEMPT;
3619 if (!add)
3620 change->args[ch_arg].mode |= MODE_REMOVE;
3621 change->args[ch_arg++].u.hostmask = modes[in_arg++];
3622 break;
3623 case 'o': case 'h': case 'v':
3624 {
3625 struct userNode *victim;
3626 char *oplevel_str;
3627 int oplevel;
3628
3629 if (in_arg >= argc)
3630 goto error;
3631 oplevel_str = strchr(modes[in_arg], ':');
3632 if (oplevel_str)
3633 {
3634 /* XXYYY M #channel +o XXYYY:<oplevel> */
3635 *oplevel_str++ = '\0';
3636 oplevel = parse_oplevel(oplevel_str);
3637 if (oplevel <= base_oplevel && !(flags & MCP_FROM_SERVER))
3638 oplevel = base_oplevel + 1;
3639 }
3640 else if (channel->modes & MODE_UPASS)
3641 oplevel = base_oplevel + 1;
3642 else
3643 oplevel = -1;
3644
3645 /* Check that oplevel is within bounds. */
3646 if (oplevel > MAXOPLEVEL)
3647 oplevel = MAXOPLEVEL;
3648
3649 if (!(flags & MCP_ALLOW_OVB))
3650 goto error;
3651 if (in_arg >= argc)
3652 goto error;
3653
3654 if (modes[0][ii] == 'o')
3655 change->args[ch_arg].mode = MODE_CHANOP;
3656 else if (modes[0][ii] == 'h')
3657 change->args[ch_arg].mode = MODE_HALFOP;
3658 else if (modes[0][ii] == 'v')
3659 change->args[ch_arg].mode = MODE_VOICE;
3660
3661 if (!add)
3662 change->args[ch_arg].mode |= MODE_REMOVE;
3663 if (flags & MCP_FROM_SERVER)
3664 victim = GetUserN(modes[in_arg++]);
3665 else
3666 victim = GetUserH(modes[in_arg++]);
3667 if (!victim)
3668 continue;
3669 if ((change->args[ch_arg].u.member = GetUserMode(channel, victim)))
3670 {
3671 /* Apply the oplevel change */
3672 change->args[ch_arg].u.member->oplevel = oplevel;
3673 ch_arg++;
3674 }
3675 break;
3676 }
3677 default:
3678 if (!(flags & MCP_FROM_SERVER))
3679 goto error;
3680 break;
3681 }
3682 }
3683 change->argc = ch_arg; /* in case any turned out to be ignored */
3684 if (change->modes_set & MODE_SECRET) {
3685 change->modes_set &= ~(MODE_PRIVATE);
3686 change->modes_clear |= MODE_PRIVATE;
3687 } else if (change->modes_set & MODE_PRIVATE) {
3688 change->modes_set &= ~(MODE_SECRET);
3689 change->modes_clear |= MODE_SECRET;
3690 }
3691 return change;
3692 error:
3693 mod_chanmode_free(change);
3694 return NULL;
3695 }
3696
3697 struct chanmode_buffer {
3698 char modes[MAXLEN];
3699 char args[MAXLEN];
3700 struct chanNode *channel;
3701 struct userNode *actor;
3702 unsigned int modes_used;
3703 unsigned int args_used;
3704 size_t chname_len;
3705 unsigned int is_add : 1;
3706 unsigned int is_chanop : 1;
3707 };
3708
3709 static void
3710 mod_chanmode_append(struct chanmode_buffer *buf, char ch, const char *arg)
3711 {
3712 size_t arg_len = strlen(arg);
3713 if (buf->modes_used > (MAXMODEPARAMS) ||
3714 buf->modes_used + buf->args_used + buf->chname_len + arg_len > 450) {
3715 memcpy(buf->modes + buf->modes_used, buf->args, buf->args_used);
3716 buf->modes[buf->modes_used + buf->args_used] = '\0';
3717 irc_mode((buf->is_chanop ? buf->actor : NULL), buf->channel, buf->modes);
3718 buf->modes[0] = buf->is_add ? '+' : '-';
3719 buf->modes_used = 1;
3720 buf->args_used = 0;
3721 }
3722 buf->modes[buf->modes_used++] = ch;
3723 buf->args[buf->args_used++] = ' ';
3724 memcpy(buf->args + buf->args_used, arg, arg_len);
3725 buf->args_used += arg_len;
3726 }
3727
3728 void
3729 mod_chanmode_announce(struct userNode *who, struct chanNode *channel, struct mod_chanmode *change)
3730 {
3731 struct chanmode_buffer chbuf;
3732 unsigned int arg;
3733 struct modeNode *mn;
3734 char int_buff[32], mode = '\0';
3735
3736 assert(change->argc <= change->alloc_argc);
3737 memset(&chbuf, 0, sizeof(chbuf));
3738 chbuf.channel = channel;
3739 chbuf.actor = who;
3740 chbuf.chname_len = strlen(channel->name);
3741
3742 mn = GetUserMode(channel, who);
3743 if ((mn && (mn->modes & MODE_CHANOP)) || off_channel)
3744 chbuf.is_chanop = 1;
3745
3746 /* First remove modes */
3747 chbuf.is_add = 0;
3748 if (change->modes_clear) {
3749 if (mode != '-')
3750 chbuf.modes[chbuf.modes_used++] = mode = '-';
3751 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
3752 DO_MODE_CHAR(PRIVATE, 'p');
3753 DO_MODE_CHAR(SECRET, 's');
3754 DO_MODE_CHAR(MODERATED, 'm');
3755 DO_MODE_CHAR(TOPICLIMIT, 't');
3756 DO_MODE_CHAR(INVITEONLY, 'i');
3757 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3758 DO_MODE_CHAR(LIMIT, 'l');
3759 DO_MODE_CHAR(DELAYJOINS, 'D');
3760 DO_MODE_CHAR(REGONLY, 'r');
3761 DO_MODE_CHAR(NOCOLORS, 'c');
3762 DO_MODE_CHAR(NOCTCPS, 'C');
3763 DO_MODE_CHAR(STRIPCOLOR, 'S');
3764 DO_MODE_CHAR(MODUNREG, 'M');
3765 DO_MODE_CHAR(NONOTICE, 'N');
3766 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3767 DO_MODE_CHAR(NOAMSG, 'T');
3768 DO_MODE_CHAR(OPERSONLY, 'O');
3769 DO_MODE_CHAR(ADMINSONLY, 'a');
3770 DO_MODE_CHAR(REGISTERED, 'z');
3771 DO_MODE_CHAR(SSLONLY, 'Z');
3772 DO_MODE_CHAR(HIDEMODE, 'L');
3773 #undef DO_MODE_CHAR
3774 if (change->modes_clear & channel->modes & MODE_KEY)
3775 mod_chanmode_append(&chbuf, 'k', channel->key);
3776 if (change->modes_clear & channel->modes & MODE_UPASS)
3777 mod_chanmode_append(&chbuf, 'U', channel->upass);
3778 if (change->modes_clear & channel->modes & MODE_APASS)
3779 mod_chanmode_append(&chbuf, 'A', channel->apass);
3780 }
3781 for (arg = 0; arg < change->argc; ++arg) {
3782 if (!(change->args[arg].mode & MODE_REMOVE))
3783 continue;
3784 if (mode != '-')
3785 chbuf.modes[chbuf.modes_used++] = mode = '-';
3786 switch (change->args[arg].mode & ~MODE_REMOVE) {
3787 case MODE_BAN:
3788 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
3789 break;
3790 case MODE_EXEMPT:
3791 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
3792 break;
3793 default:
3794 if (change->args[arg].mode & MODE_CHANOP)
3795 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
3796 if (change->args[arg].mode & MODE_HALFOP)
3797 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
3798 if (change->args[arg].mode & MODE_VOICE)
3799 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
3800 break;
3801 }
3802 }
3803
3804 /* Then set them */
3805 chbuf.is_add = 1;
3806 if (change->modes_set) {
3807 if (mode != '+')
3808 chbuf.modes[chbuf.modes_used++] = mode = '+';
3809 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
3810 DO_MODE_CHAR(PRIVATE, 'p');
3811 DO_MODE_CHAR(SECRET, 's');
3812 DO_MODE_CHAR(MODERATED, 'm');
3813 DO_MODE_CHAR(TOPICLIMIT, 't');
3814 DO_MODE_CHAR(INVITEONLY, 'i');
3815 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3816 DO_MODE_CHAR(DELAYJOINS, 'D');
3817 DO_MODE_CHAR(REGONLY, 'r');
3818 DO_MODE_CHAR(NOCOLORS, 'c');
3819 DO_MODE_CHAR(NOCTCPS, 'C');
3820 DO_MODE_CHAR(STRIPCOLOR, 'S');
3821 DO_MODE_CHAR(MODUNREG, 'M');
3822 DO_MODE_CHAR(NONOTICE, 'N');
3823 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3824 DO_MODE_CHAR(NOAMSG, 'T');
3825 DO_MODE_CHAR(OPERSONLY, 'O');
3826 DO_MODE_CHAR(ADMINSONLY, 'a');
3827 DO_MODE_CHAR(REGISTERED, 'z');
3828 DO_MODE_CHAR(SSLONLY, 'Z');
3829 DO_MODE_CHAR(HIDEMODE, 'L');
3830 #undef DO_MODE_CHAR
3831 if(change->modes_set & MODE_KEY)
3832 mod_chanmode_append(&chbuf, 'k', change->new_key);
3833 if (change->modes_set & MODE_UPASS)
3834 mod_chanmode_append(&chbuf, 'U', change->new_upass);
3835 if (change->modes_set & MODE_APASS)
3836 mod_chanmode_append(&chbuf, 'A', change->new_apass);
3837 if(change->modes_set & MODE_LIMIT) {
3838 sprintf(int_buff, "%d", change->new_limit);
3839 mod_chanmode_append(&chbuf, 'l', int_buff);
3840 }
3841 }
3842 for (arg = 0; arg < change->argc; ++arg) {
3843 if (change->args[arg].mode & MODE_REMOVE)
3844 continue;
3845 if (mode != '+')
3846 chbuf.modes[chbuf.modes_used++] = mode = '+';
3847 switch (change->args[arg].mode) {
3848 case MODE_BAN:
3849 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
3850 break;
3851 case MODE_EXEMPT:
3852 mod_chanmode_append(&chbuf, 'e', change->args[arg].u.hostmask);
3853 break;
3854 default:
3855 if (change->args[arg].mode & MODE_CHANOP)
3856 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
3857 if (change->args[arg].mode & MODE_HALFOP)
3858 mod_chanmode_append(&chbuf, 'h', change->args[arg].u.member->user->numeric);
3859 if (change->args[arg].mode & MODE_VOICE)
3860 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
3861 break;
3862 }
3863 }
3864
3865 /* Flush the buffer and apply changes locally */
3866 if (chbuf.modes_used > 0) {
3867 memcpy(chbuf.modes + chbuf.modes_used, chbuf.args, chbuf.args_used);
3868 chbuf.modes[chbuf.modes_used + chbuf.args_used] = '\0';
3869 irc_mode((chbuf.is_chanop ? chbuf.actor : NULL), chbuf.channel, chbuf.modes);
3870 }
3871 mod_chanmode_apply(who, channel, change);
3872 }
3873
3874 char *
3875 mod_chanmode_format(struct mod_chanmode *change, char *outbuff)
3876 {
3877 unsigned int used = 0;
3878 assert(change->argc <= change->alloc_argc);
3879 if (change->modes_clear) {
3880 outbuff[used++] = '-';
3881 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) outbuff[used++] = CHAR
3882 DO_MODE_CHAR(PRIVATE, 'p');
3883 DO_MODE_CHAR(SECRET, 's');
3884 DO_MODE_CHAR(MODERATED, 'm');
3885 DO_MODE_CHAR(TOPICLIMIT, 't');
3886 DO_MODE_CHAR(INVITEONLY, 'i');
3887 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3888 DO_MODE_CHAR(LIMIT, 'l');
3889 DO_MODE_CHAR(KEY, 'k');
3890 DO_MODE_CHAR(UPASS, 'U');
3891 DO_MODE_CHAR(APASS, 'A');
3892 DO_MODE_CHAR(DELAYJOINS, 'D');
3893 DO_MODE_CHAR(REGONLY, 'r');
3894 DO_MODE_CHAR(NOCOLORS, 'c');
3895 DO_MODE_CHAR(NOCTCPS, 'C');
3896 DO_MODE_CHAR(STRIPCOLOR, 'S');
3897 DO_MODE_CHAR(MODUNREG, 'M');
3898 DO_MODE_CHAR(NONOTICE, 'N');
3899 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3900 DO_MODE_CHAR(NOAMSG, 'T');
3901 DO_MODE_CHAR(OPERSONLY, 'O');
3902 DO_MODE_CHAR(ADMINSONLY, 'a');
3903 DO_MODE_CHAR(REGISTERED, 'z');
3904 DO_MODE_CHAR(SSLONLY, 'Z');
3905 DO_MODE_CHAR(HIDEMODE, 'L');
3906 #undef DO_MODE_CHAR
3907 }
3908 if (change->modes_set) {
3909 outbuff[used++] = '+';
3910 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) outbuff[used++] = CHAR
3911 DO_MODE_CHAR(PRIVATE, 'p');
3912 DO_MODE_CHAR(SECRET, 's');
3913 DO_MODE_CHAR(MODERATED, 'm');
3914 DO_MODE_CHAR(TOPICLIMIT, 't');
3915 DO_MODE_CHAR(INVITEONLY, 'i');
3916 DO_MODE_CHAR(NOPRIVMSGS, 'n');
3917 DO_MODE_CHAR(DELAYJOINS, 'D');
3918 DO_MODE_CHAR(REGONLY, 'r');
3919 DO_MODE_CHAR(NOCOLORS, 'c');
3920 DO_MODE_CHAR(NOCTCPS, 'C');
3921 DO_MODE_CHAR(STRIPCOLOR, 'S');
3922 DO_MODE_CHAR(MODUNREG, 'M');
3923 DO_MODE_CHAR(NONOTICE, 'N');
3924 DO_MODE_CHAR(NOQUITMSGS, 'Q');
3925 DO_MODE_CHAR(NOAMSG, 'T');
3926 DO_MODE_CHAR(OPERSONLY, 'O');
3927 DO_MODE_CHAR(ADMINSONLY, 'a');
3928 DO_MODE_CHAR(REGISTERED, 'z');
3929 DO_MODE_CHAR(SSLONLY, 'Z');
3930 DO_MODE_CHAR(HIDEMODE, 'L');
3931
3932 DO_MODE_CHAR(KEY, 'k');
3933 DO_MODE_CHAR(LIMIT, 'l');
3934 DO_MODE_CHAR(APASS, 'A');
3935 DO_MODE_CHAR(UPASS, 'U');
3936 #undef DO_MODE_CHAR
3937
3938 #define DO_MODE_PARM(BIT, PARM) if (change->modes_set & MODE_##BIT) used += sprintf(outbuff+used, " %s", PARM);
3939 DO_MODE_PARM(KEY, change->new_key);
3940 if (change->modes_set & MODE_LIMIT) used += sprintf(outbuff+used, " %d", change->new_limit);
3941 DO_MODE_PARM(APASS, change->new_apass);
3942 DO_MODE_PARM(UPASS, change->new_upass);
3943 #undef DO_MODE_PARM
3944 }
3945 outbuff[used] = 0;
3946 return outbuff;
3947 }
3948
3949 static int
3950 clear_chanmode(struct chanNode *channel, const char *modes)
3951 {
3952 unsigned int cleared;
3953
3954 for (cleared = 0; *modes; modes++) {
3955 switch (*modes) {
3956 case 'o': cleared |= MODE_CHANOP; break;
3957 case 'h': cleared |= MODE_HALFOP; break;
3958 case 'v': cleared |= MODE_VOICE; break;
3959 case 'p': cleared |= MODE_PRIVATE; break;
3960 case 's': cleared |= MODE_SECRET; break;
3961 case 'm': cleared |= MODE_MODERATED; break;
3962 case 't': cleared |= MODE_TOPICLIMIT; break;
3963 case 'i': cleared |= MODE_INVITEONLY; break;
3964 case 'n': cleared |= MODE_NOPRIVMSGS; break;
3965 case 'k':
3966 cleared |= MODE_KEY;
3967 channel->key[0] = '\0';
3968 break;
3969 case 'A':
3970 cleared |= MODE_APASS;
3971 channel->apass[0] = '\0';
3972 break;
3973 case 'U':
3974 cleared |= MODE_UPASS;
3975 channel->upass[0] = '\0';
3976 break;
3977 case 'l':
3978 cleared |= MODE_LIMIT;
3979 channel->limit = 0;
3980 break;
3981 case 'b': cleared |= MODE_BAN; break;
3982 case 'e': cleared |= MODE_EXEMPT; break;
3983 case 'D': cleared |= MODE_DELAYJOINS; break;
3984 case 'r': cleared |= MODE_REGONLY; break;
3985 case 'c': cleared |= MODE_NOCOLORS; break;
3986 case 'C': cleared |= MODE_NOCTCPS; break;
3987 case 'S': cleared |= MODE_STRIPCOLOR; break;
3988 case 'M': cleared |= MODE_MODUNREG; break;
3989 case 'N': cleared |= MODE_NONOTICE; break;
3990 case 'Q': cleared |= MODE_NOQUITMSGS; break;
3991 case 'T': cleared |= MODE_NOAMSG; break;
3992 case 'O': cleared |= MODE_OPERSONLY; break;
3993 case 'a': cleared |= MODE_ADMINSONLY; break;
3994 case 'z': cleared |= MODE_REGISTERED; break;
3995 case 'Z': cleared |= MODE_SSLONLY; break;
3996 case 'L': cleared |= MODE_HIDEMODE; break;
3997 }
3998 }
3999
4000 if (!cleared)
4001 return 1;
4002
4003 /* Remove simple modes. */
4004 channel->modes &= ~cleared;
4005
4006 /* If removing bans, kill 'em all. */
4007 if ((cleared & MODE_BAN) && channel->banlist.used) {
4008 unsigned int i;
4009 for (i=0; i<channel->banlist.used; i++)
4010 free(channel->banlist.list[i]);
4011 channel->banlist.used = 0;
4012 }
4013
4014 /* If removing exempts, kill 'em all. */
4015 if ((cleared & MODE_EXEMPT) && channel->exemptlist.used) {
4016 unsigned int i;
4017 for (i=0; i<channel->exemptlist.used; i++)
4018 free(channel->exemptlist.list[i]);
4019 channel->exemptlist.used = 0;
4020 }
4021
4022 /* Removed member modes. */
4023 if ((cleared & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE)) && channel->members.used) {
4024 int mask = ~(cleared & (MODE_CHANOP | MODE_HALFOP | MODE_VOICE));
4025 unsigned int i;
4026
4027 for (i = 0; i < channel->members.used; i++)
4028 channel->members.list[i]->modes &= mask;
4029 }
4030
4031 return 1;
4032 }
4033
4034 void
4035 reg_privmsg_func(struct userNode *user, privmsg_func_t handler)
4036 {
4037 unsigned int numeric = user->num_local;
4038 if (numeric >= num_privmsg_funcs) {
4039 int newnum = numeric + 8, ii;
4040 privmsg_funcs = realloc(privmsg_funcs, newnum*sizeof(privmsg_func_t));
4041 for (ii = num_privmsg_funcs; ii < newnum; ++ii)
4042 privmsg_funcs[ii] = NULL;
4043 num_privmsg_funcs = newnum;
4044 }
4045 if (privmsg_funcs[numeric])
4046 log_module(MAIN_LOG, LOG_WARNING, "re-registering new privmsg handler for numeric %d", numeric);
4047 privmsg_funcs[numeric] = handler;
4048 }
4049
4050 void
4051 unreg_privmsg_func(struct userNode *user)
4052 {
4053 if (!IsLocal(user) || user->num_local >= num_privmsg_funcs)
4054 return; /* this really only works with users */
4055
4056 privmsg_funcs[user->num_local] = NULL;
4057 }
4058
4059
4060 void
4061 reg_notice_func(struct userNode *user, privmsg_func_t handler)
4062 {
4063 unsigned int numeric = user->num_local;
4064 if (numeric >= num_notice_funcs) {
4065 int newnum = numeric + 8, ii;
4066 notice_funcs = realloc(notice_funcs, newnum*sizeof(privmsg_func_t));
4067 for (ii = num_privmsg_funcs; ii < newnum; ++ii)
4068 privmsg_funcs[ii] = NULL;
4069 num_notice_funcs = newnum;
4070 }
4071 if (notice_funcs[numeric])
4072 log_module(MAIN_LOG, LOG_WARNING, "re-registering new notice handler for numeric %d", numeric);
4073 notice_funcs[numeric] = handler;
4074 }
4075
4076 void
4077 unreg_notice_func(struct userNode *user)
4078 {
4079 if (!IsLocal(user) || user->num_local >= num_privmsg_funcs)
4080 return; /* this really only works with users */
4081
4082 notice_funcs[user->num_local] = NULL;
4083 }
4084
4085 static void
4086 send_burst(void)
4087 {
4088 unsigned int i, hop, max_hop=1;
4089 dict_iterator_t it;
4090
4091 /* burst (juped) servers, closest first (except self, which is sent already) */
4092 for (i=0; i<ArrayLength(servers_num); i++)
4093 if (servers_num[i] && servers_num[i]->hops > max_hop)
4094 max_hop = servers_num[i]->hops;
4095 for (hop=1; hop<=max_hop; hop++) {
4096 for (i=0;i<ArrayLength(servers_num);i++) {
4097 if (servers_num[i]
4098 && (servers_num[i]->hops == hop)
4099 && (servers_num[i] != self->uplink))
4100 irc_server(servers_num[i]);
4101 }
4102 }
4103
4104 /* burst local nicks */
4105 for (i=0; i<=self->num_mask; i++)
4106 if (self->users[i])
4107 irc_user(self->users[i]);
4108
4109 /* build dict of unbursted channel names (just copy existing channels) */
4110 unbursted_channels = dict_new();
4111 for (it = dict_first(channels); it; it = iter_next(it))
4112 dict_insert(unbursted_channels, iter_key(it), iter_data(it));
4113 }
4114
4115 /*
4116 * Oplevel parsing
4117 */
4118 static int
4119 parse_oplevel(char *str)
4120 {
4121 int oplevel = 0;
4122 while (isdigit(*str))
4123 oplevel = oplevel * 10 + *str++ - '0';
4124 return oplevel;
4125 }
4126