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