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