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