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