1 /* proto-p10.c - IRC protocol output
2 * Copyright 2000-2004 srvx Development Team
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version. Important limitations are
8 * listed in the COPYING file that accompanies this software.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, email srvx-maintainers@srvx.net.
19 #include "proto-common.c"
22 #define CMD_ACCOUNT "ACCOUNT"
23 #define CMD_ADMIN "ADMIN"
24 #define CMD_ASLL "ASLL"
25 #define CMD_AWAY "AWAY"
26 #define CMD_BURST "BURST"
27 #define CMD_CLEARMODE "CLEARMODE"
28 #define CMD_CLOSE "CLOSE"
29 #define CMD_CNOTICE "CNOTICE"
30 #define CMD_CONNECT "CONNECT"
31 #define CMD_CPRIVMSG "CPRIVMSG"
32 #define CMD_CREATE "CREATE"
33 #define CMD_DESTRUCT "DESTRUCT"
34 #define CMD_DESYNCH "DESYNCH"
37 #define CMD_EOB "END_OF_BURST"
38 #define CMD_EOB_ACK "EOB_ACK"
39 #define CMD_ERROR "ERROR"
41 #define CMD_GLINE "GLINE"
42 #define CMD_HASH "HASH"
43 #define CMD_HELP "HELP"
44 #define CMD_INFO "INFO"
45 #define CMD_INVITE "INVITE"
46 #define CMD_ISON "ISON"
47 #define CMD_JOIN "JOIN"
48 #define CMD_JUPE "JUPE"
49 #define CMD_KICK "KICK"
50 #define CMD_KILL "KILL"
51 #define CMD_LINKS "LINKS"
52 #define CMD_LIST "LIST"
53 #define CMD_LUSERS "LUSERS"
55 #define CMD_MODE "MODE"
56 #define CMD_MOTD "MOTD"
57 #define CMD_NAMES "NAMES"
58 #define CMD_NICK "NICK"
59 #define CMD_NOTICE "NOTICE"
60 #define CMD_OPER "OPER"
61 #define CMD_OPMODE "OPMODE"
62 #define CMD_PART "PART"
63 #define CMD_PASS "PASS"
64 #define CMD_PING "PING"
65 #define CMD_PONG "PONG"
66 #define CMD_POST "POST"
67 #define CMD_PRIVMSG "PRIVMSG"
68 #define CMD_PRIVS "PRIVS"
69 #define CMD_PROTO "PROTO"
70 #define CMD_QUIT "QUIT"
71 #define CMD_REHASH "REHASH"
72 #define CMD_RESET "RESET"
73 #define CMD_RESTART "RESTART"
74 #define CMD_RPING "RPING"
75 #define CMD_RPONG "RPONG"
76 #define CMD_SERVER "SERVER"
77 #define CMD_SERVLIST "SERVLIST"
78 #define CMD_SERVSET "SERVSET"
80 #define CMD_SETTIME "SETTIME"
81 #define CMD_SILENCE "SILENCE"
82 #define CMD_SQUERY "SQUERY"
83 #define CMD_SQUIT "SQUIT"
84 #define CMD_STATS "STATS"
85 #define CMD_SVSNICK "SVSNICK"
86 #define CMD_TIME "TIME"
87 #define CMD_TOPIC "TOPIC"
88 #define CMD_TRACE "TRACE"
89 #define CMD_UPING "UPING"
90 #define CMD_USER "USER"
91 #define CMD_USERHOST "USERHOST"
92 #define CMD_USERIP "USERIP"
93 #define CMD_VERSION "VERSION"
94 #define CMD_WALLCHOPS "WALLCHOPS"
95 #define CMD_WALLOPS "WALLOPS"
96 #define CMD_WALLUSERS "WALLUSERS"
97 #define CMD_WALLVOICES "WALLVOICES"
99 #define CMD_WHOIS "WHOIS"
100 #define CMD_WHOWAS "WHOWAS"
102 /* Tokenized commands. */
103 #define TOK_ACCOUNT "AC"
104 #define TOK_ADMIN "AD"
105 #define TOK_ASLL "LL"
107 #define TOK_BURST "B"
108 #define TOK_CLEARMODE "CM"
109 #define TOK_CLOSE "CLOSE"
110 #define TOK_CNOTICE "CN"
111 #define TOK_CONNECT "CO"
112 #define TOK_CPRIVMSG "CP"
113 #define TOK_CREATE "C"
114 #define TOK_DESTRUCT "DE"
115 #define TOK_DESYNCH "DS"
116 #define TOK_DIE "DIE"
117 #define TOK_DNS "DNS"
119 #define TOK_EOB_ACK "EA"
120 #define TOK_ERROR "Y"
121 #define TOK_GET "GET"
122 #define TOK_GLINE "GL"
123 #define TOK_HASH "HASH"
124 #define TOK_HELP "HELP"
126 #define TOK_INVITE "I"
127 #define TOK_ISON "ISON"
129 #define TOK_JUPE "JU"
132 #define TOK_LINKS "LI"
133 #define TOK_LIST "LIST"
134 #define TOK_LUSERS "LU"
135 #define TOK_MAP "MAP"
137 #define TOK_MOTD "MO"
138 #define TOK_NAMES "E"
140 #define TOK_NOTICE "O"
141 #define TOK_OPER "OPER"
142 #define TOK_OPMODE "OM"
144 #define TOK_PASS "PA"
147 #define TOK_POST "POST"
148 #define TOK_PRIVMSG "P"
149 #define TOK_PRIVS "PRIVS"
150 #define TOK_PROTO "PROTO"
152 #define TOK_REHASH "REHASH"
153 #define TOK_RESET "RESET"
154 #define TOK_RESTART "RESTART"
155 #define TOK_RPING "RI"
156 #define TOK_RPONG "RO"
157 #define TOK_SERVER "S"
158 #define TOK_SERVLIST "SERVSET"
159 #define TOK_SERVSET "SERVSET"
160 #define TOK_SET "SET"
161 #define TOK_SETTIME "SE"
162 #define TOK_SILENCE "U"
163 #define TOK_SQUERY "SQUERY"
164 #define TOK_SQUIT "SQ"
165 #define TOK_STATS "R"
166 #define TOK_SVSNICK "SN"
167 #define TOK_TIME "TI"
168 #define TOK_TOPIC "T"
169 #define TOK_TRACE "TR"
170 #define TOK_UPING "UP"
171 #define TOK_USER "USER"
172 #define TOK_USERHOST "USERHOST"
173 #define TOK_USERIP "USERIP"
174 #define TOK_VERSION "V"
175 #define TOK_WALLCHOPS "WC"
176 #define TOK_WALLOPS "WA"
177 #define TOK_WALLUSERS "WU"
178 #define TOK_WALLVOICES "WV"
180 #define TOK_WHOIS "W"
181 #define TOK_WHOWAS "X"
183 /* Protocol messages; aliased to full commands or tokens depending
184 on compile-time configuration. ircu prefers tokens WITH THE
185 EXCEPTION OF THE SERVER AND PASS COMMANDS, which cannot be
186 tokenized, because clients' (ie. a linking server) commands are
187 only checked against the full command list.
189 #if defined(ENABLE_TOKENS)
190 #define TYPE(NAME) TOK_ ## NAME
191 #else /* !ENABLE_TOKENS */
192 #define TYPE(NAME) CMD_ ## NAME
193 #endif /* ENABLE_TOKENS */
195 #define P10_ACCOUNT TYPE(ACCOUNT)
196 #define P10_ADMIN TYPE(ADMIN)
197 #define P10_ASLL TYPE(ASLL)
198 #define P10_AWAY TYPE(AWAY)
199 #define P10_BURST TYPE(BURST)
200 #define P10_CLEARMODE TYPE(CLEARMODE)
201 #define P10_CLOSE TYPE(CLOSE)
202 #define P10_CNOTICE TYPE(CNOTICE)
203 #define P10_CONNECT TYPE(CONNECT)
204 #define P10_CPRIVMSG TYPE(CPRIVMSG)
205 #define P10_CREATE TYPE(CREATE)
206 #define P10_DESTRUCT TYPE(DESTRUCT)
207 #define P10_DESYNCH TYPE(DESYNCH)
208 #define P10_DIE TYPE(DIE)
209 #define P10_DNS TYPE(DNS)
210 #define P10_EOB TYPE(EOB)
211 #define P10_EOB_ACK TYPE(EOB_ACK)
212 #define P10_ERROR TYPE(ERROR)
213 #define P10_GET TYPE(GET)
214 #define P10_GLINE TYPE(GLINE)
215 #define P10_HASH TYPE(HASH)
216 #define P10_HELP TYPE(HELP)
217 #define P10_INFO TYPE(INFO)
218 #define P10_INVITE TYPE(INVITE)
219 #define P10_ISON TYPE(ISON)
220 #define P10_JOIN TYPE(JOIN)
221 #define P10_JUPE TYPE(JUPE)
222 #define P10_KICK TYPE(KICK)
223 #define P10_KILL TYPE(KILL)
224 #define P10_LINKS TYPE(LINKS)
225 #define P10_LIST TYPE(LIST)
226 #define P10_LUSERS TYPE(LUSERS)
227 #define P10_MAP TYPE(MAP)
228 #define P10_MODE TYPE(MODE)
229 #define P10_MOTD TYPE(MOTD)
230 #define P10_NAMES TYPE(NAMES)
231 #define P10_NICK TYPE(NICK)
232 #define P10_NOTICE TYPE(NOTICE)
233 #define P10_OPER TYPE(OPER)
234 #define P10_OPMODE TYPE(OPMODE)
235 #define P10_PART TYPE(PART)
236 #define P10_PASS CMD_PASS
237 #define P10_PING TYPE(PING)
238 #define P10_PONG TYPE(PONG)
239 #define P10_POST TYPE(POST)
240 #define P10_PRIVMSG TYPE(PRIVMSG)
241 #define P10_PRIVS TYPE(PRIVS)
242 #define P10_PROTO TYPE(PROTO)
243 #define P10_QUIT TYPE(QUIT)
244 #define P10_REHASH TYPE(REHASH)
245 #define P10_RESET TYPE(RESET)
246 #define P10_RESTART TYPE(RESTART)
247 #define P10_RPING TYPE(RPING)
248 #define P10_RPONG TYPE(RPONG)
249 #define P10_SERVER CMD_SERVER
250 #define P10_SERVLIST TYPE(SERVLIST)
251 #define P10_SERVSET TYPE(SERVSET)
252 #define P10_SET TYPE(SET)
253 #define P10_SETTIME TYPE(SETTIME)
254 #define P10_SILENCE TYPE(SILENCE)
255 #define P10_SQUERY TYPE(SQUERY)
256 #define P10_SQUIT TYPE(SQUIT)
257 #define P10_STATS TYPE(STATS)
258 #define P10_SVSNICK TYPE(SVSNICK)
259 #define P10_TIME TYPE(TIME)
260 #define P10_TOPIC TYPE(TOPIC)
261 #define P10_TRACE TYPE(TRACE)
262 #define P10_UPING TYPE(UPING)
263 #define P10_USER TYPE(USER)
264 #define P10_USERHOST TYPE(USERHOST)
265 #define P10_USERIP TYPE(USERIP)
266 #define P10_VERSION TYPE(VERSION)
267 #define P10_WALLCHOPS TYPE(WALLCHOPS)
268 #define P10_WALLOPS TYPE(WALLOPS)
269 #define P10_WALLUSERS TYPE(WALLUSERS)
270 #define P10_WALLVOICES TYPE(WALLVOICES)
271 #define P10_WHO TYPE(WHO)
272 #define P10_WHOIS TYPE(WHOIS)
273 #define P10_WHOWAS TYPE(WHOWAS)
275 /* Servers claiming to have a boot or link time before PREHISTORY
276 * trigger errors to the log. We hope no server has been running
277 * constantly since September 1994. :)
279 #define PREHISTORY 780000000
281 static struct server *servers_num[64*64];
282 static privmsg_func_t *privmsg_funcs;
283 static unsigned int num_privmsg_funcs;
284 static privmsg_func_t *notice_funcs;
285 static unsigned int num_notice_funcs;
286 static struct dict *unbursted_channels;
288 static struct userNode *AddUser(struct server* uplink, const char *nick, const char *ident, const char *hostname, const char *modes, const char *numeric, const char *userinfo, time_t timestamp, const char *realip);
290 /* Numerics can be XYY, XYYY, or XXYYY; with X's identifying the
291 * server and Y's indentifying the client on that server. */
293 GetServerN(const char *numeric)
295 switch (strlen(numeric)) {
297 return servers_num[base64toint(numeric, 2)];
301 return servers_num[base64toint(numeric, 1)];
308 GetUserN(const char *numeric) /* using numeric */
314 switch (strlen(numeric)) {
316 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): numeric too long!", numeric);
318 case 5: slen = 2; ulen = 3; break;
319 case 4: slen = 1; ulen = 3; break;
320 case 3: slen = 1; ulen = 2; break;
321 case 2: case 1: case 0:
322 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): numeric too short!", numeric);
325 if (!(s = servers_num[base64toint(numeric, slen)])) {
326 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): couldn't find server (len=%d)!", numeric, slen);
329 n = base64toint(numeric+slen, ulen) & s->num_mask;
330 if (!(un = s->users[n])) {
331 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s) couldn't find user!", numeric);
337 privmsg_user_helper(struct userNode *un, void *data)
339 struct privmsg_desc *pd = data;
340 unsigned int num = un->num_local;
341 if (!pd->is_notice) {
342 if ((num < num_privmsg_funcs) && privmsg_funcs[num]) {
343 privmsg_funcs[num](pd->user, un, pd->text, pd->is_qualified);
346 if ((num < num_notice_funcs) && notice_funcs[num]) {
347 notice_funcs[num](pd->user, un, pd->text, pd->is_qualified);
353 irc_server(struct server *srv)
355 char extranum[COMBO_NUMERIC_LEN+1];
357 inttobase64(extranum, srv->num_mask, (srv->numeric[1] || (srv->num_mask >= 64*64)) ? 3 : 2);
359 /* The +s, ignored by Run's ircu, means "service" to Undernet's ircu */
360 putsock(P10_SERVER " %s %d %li %li J10 %s%s +s :%s",
361 srv->name, srv->hops+1, srv->boot, srv->link, srv->numeric, extranum, srv->description);
363 putsock("%s " P10_SERVER " %s %d %li %li %c10 %s%s +s :%s",
364 self->numeric, srv->name, srv->hops+1, srv->boot, srv->link, (srv->self_burst ? 'J' : 'P'), srv->numeric, extranum, srv->description);
369 irc_user(struct userNode *user)
374 inttobase64(b64ip, ntohl(user->ip.s_addr), 6);
381 modes[modelen++] = 'o';
382 if (IsInvisible(user))
383 modes[modelen++] = 'i';
385 modes[modelen++] = 'w';
387 modes[modelen++] = 'k';
388 if (IsServNotice(user))
389 modes[modelen++] = 's';
391 modes[modelen++] = 'd';
393 modes[modelen++] = 'g';
394 if (IsHelperIrcu(user))
395 modes[modelen++] = 'h';
396 if (IsHiddenHost(user))
397 modes[modelen++] = 'x';
400 /* we don't need to put the + in modes because it's in the format string. */
401 putsock("%s " P10_NICK " %s %d %li %s %s +%s %s %s :%s",
402 user->uplink->numeric, user->nick, user->uplink->hops+1, user->timestamp, user->ident, user->hostname, modes, b64ip, user->numeric, user->info);
404 putsock("%s " P10_NICK " %s %d %li %s %s %s %s :%s",
405 user->uplink->numeric, user->nick, user->uplink->hops+1, user->timestamp, user->ident, user->hostname, b64ip, user->numeric, user->info);
410 irc_account(struct userNode *user, const char *stamp)
412 putsock("%s " P10_ACCOUNT " %s %s", self->numeric, user->numeric, stamp);
416 irc_regnick(UNUSED_ARG(struct userNode *user))
418 /* no operation here */
422 irc_nick(struct userNode *user, UNUSED_ARG(const char *old_nick))
424 putsock("%s " P10_NICK " %s "FMT_TIME_T, user->numeric, user->nick, now);
428 irc_fetchtopic(struct userNode *from, const char *to)
432 putsock("%s " P10_TOPIC " %s", from->numeric, to);
436 irc_squit(struct server *srv, const char *message, const char *service_message)
438 if (!service_message)
439 service_message = message;
441 /* Are we leaving the network? */
442 if (srv == self && cManager.uplink->state == CONNECTED) {
445 /* Quit all clients linked to me. */
446 for (i = 0; i <= self->num_mask; i++) {
449 irc_quit(self->users[i], service_message);
453 putsock("%s " P10_SQUIT " %s %d :%s", self->numeric, srv->name, 0, message);
456 /* Force a reconnect to the currently selected server. */
457 cManager.uplink->tries = 0;
458 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", message);
464 irc_wallchops(struct userNode *from, const char *to, const char *message)
466 putsock("%s " P10_WALLCHOPS " %s :%s", from->numeric, to, message);
470 irc_notice(struct userNode *from, const char *to, const char *message)
472 putsock("%s " P10_NOTICE " %s :%s", from->numeric, to, message);
476 irc_privmsg(struct userNode *from, const char *to, const char *message)
478 putsock("%s " P10_PRIVMSG " %s :%s", from->numeric, to, message);
484 putsock("%s " P10_EOB, self->numeric);
490 putsock("%s " P10_EOB_ACK, self->numeric);
494 irc_ping(const char *payload)
496 putsock("%s " P10_PING " :%s", self->numeric, payload);
500 irc_pong(const char *who, const char *data)
502 putsock("%s " P10_PONG " %s :%s", self->numeric, who, data);
506 irc_pass(const char *passwd)
508 putsock(P10_PASS " :%s", passwd);
512 irc_introduce(const char *passwd)
514 void timed_send_ping(void *data);
516 self->self_burst = self->burst = 1;
520 timeq_add(now + ping_freq, timed_send_ping, 0);
524 irc_gline(struct server *srv, struct gline *gline)
526 putsock("%s " P10_GLINE " %s +%s %ld :%s",
527 self->numeric, (srv ? srv->numeric : "*"), gline->target, gline->expires-now, gline->reason);
531 irc_settime(const char *srv_name_mask, time_t new_time)
533 ioset_set_time(new_time);
534 if (!strcmp(srv_name_mask, "*"))
536 putsock("%s " P10_SETTIME " " FMT_TIME_T " %s", self->numeric, new_time, srv_name_mask);
540 irc_ungline(const char *mask)
542 putsock("%s " P10_GLINE " * -%s", self->numeric, mask);
546 irc_burst(struct chanNode *chan)
548 char burst_line[512];
549 int pos, base_len, len;
555 if (!chan->members.used)
557 base_len = sprintf(burst_line, "%s " P10_BURST " %s " FMT_TIME_T " ",
558 self->numeric, chan->name, chan->timestamp);
559 len = irc_make_chanmode(chan, burst_line+base_len);
560 pos = base_len + len;
562 burst_line[pos++] = ' ';
565 for (n=0; n<chan->members.used; n++) {
566 mn = chan->members.list[n];
568 burst_line[pos-1] = 0; /* -1 to back up over the space or comma */
569 putsock("%s", burst_line);
573 memcpy(burst_line+pos, mn->user->numeric, strlen(mn->user->numeric));
574 pos += strlen(mn->user->numeric);
575 if (mn->modes && (mn->modes != last_mode)) {
576 last_mode = mn->modes;
577 burst_line[pos++] = ':';
578 if (last_mode & MODE_CHANOP)
579 burst_line[pos++] = 'o';
580 if (last_mode & MODE_VOICE)
581 burst_line[pos++] = 'v';
583 if ((n+1)<chan->members.used)
584 burst_line[pos++] = ',';
586 if (chan->banlist.used) {
588 if (pos+2+strlen(chan->banlist.list[0]->ban) > 505) {
589 burst_line[pos-1] = 0;
590 putsock("%s", burst_line);
593 burst_line[pos++] = ' ';
596 burst_line[pos++] = ':';
597 burst_line[pos++] = '%';
599 for (n=0; n<chan->banlist.used; n++) {
600 bn = chan->banlist.list[n];
601 len = strlen(bn->ban);
602 if (pos+len+1 > 510) {
603 burst_line[pos-1] = 0; /* -1 to back up over the space or comma */
604 putsock("%s", burst_line);
607 memcpy(burst_line+pos, bn->ban, len);
609 burst_line[pos++] = ' ';
612 /* print the last line */
614 putsock("%s", burst_line);
618 irc_quit(struct userNode *user, const char *message)
620 putsock("%s " P10_QUIT " :%s", user->numeric, message);
624 irc_error(const char *to, const char *message)
627 putsock("%s " P10_ERROR " :%s", to, message);
629 putsock(":%s " P10_ERROR " :%s", self->name, message);
634 irc_kill(struct userNode *from, struct userNode *target, const char *message)
637 putsock("%s " P10_KILL " %s :%s!%s (%s)",
638 from->numeric, target->numeric, self->name, from->nick, message);
640 putsock("%s " P10_KILL " %s :%s (%s)",
641 self->numeric, target->numeric, self->name, message);
646 irc_mode(struct userNode *from, struct chanNode *target, const char *modes)
648 putsock("%s " P10_MODE " %s %s "FMT_TIME_T,
649 (from ? from->numeric : self->numeric),
650 target->name, modes, target->timestamp);
654 irc_invite(struct userNode *from, struct userNode *who, struct chanNode *to)
656 putsock("%s " P10_INVITE " %s %s", from->numeric, who->nick, to->name);
660 irc_join(struct userNode *who, struct chanNode *what)
662 if (what->members.used == 1) {
663 putsock("%s " P10_CREATE " %s %lu",
664 who->numeric, what->name, what->timestamp);
666 putsock("%s " P10_JOIN " %s %lu", who->numeric, what->name, what->timestamp);
671 irc_kick(struct userNode *who, struct userNode *target, struct chanNode *channel, const char *msg)
674 struct modeNode *mn = GetUserMode(channel, who);
675 numeric = (mn && (mn->modes & MODE_CHANOP)) ? who->numeric : self->numeric;
676 putsock("%s " P10_KICK " %s %s :%s",
677 numeric, channel->name, target->numeric, msg);
681 irc_stats(struct userNode *from, struct server *target, char type)
683 putsock("%s " P10_STATS " %c :%s", from->numeric, type, target->numeric);
687 irc_svsnick(struct userNode *from, struct userNode *target, const char *newnick)
689 putsock("%s " P10_SVSNICK " %s %s "FMT_TIME_T, from->uplink->numeric, target->numeric, newnick, now);
693 irc_part(struct userNode *who, struct chanNode *what, const char *reason)
696 putsock("%s " P10_PART " %s :%s", who->numeric, what->name, reason);
698 putsock("%s " P10_PART " %s", who->numeric, what->name);
703 irc_topic(struct userNode *who, struct chanNode *what, const char *topic)
705 putsock("%s " P10_TOPIC " %s :%s", who->numeric, what->name, topic);
709 irc_raw(const char *what)
715 irc_numeric(struct userNode *user, unsigned int num, const char *format, ...)
719 va_start(arg_list, format);
720 vsnprintf(buffer, MAXLEN-2, format, arg_list);
721 buffer[MAXLEN-1] = 0;
722 putsock(":%s %03d %s %s", self->name, num, user->nick, buffer);
725 static void send_burst(void);
728 change_nicklen(int new_nicklen)
731 char new_nick[NICKLEN+1];
732 struct userNode *user;
734 nicklen = new_nicklen;
735 /* fix up any users we have here */
736 for (nn=0; nn<=self->num_mask; nn++) {
737 if (!(user = self->users[nn]))
739 safestrncpy(new_nick, user->nick, sizeof(new_nick));
740 new_nick[nicklen] = 0;
741 NickChange(user, new_nick, 1);
745 static CMD_FUNC(cmd_server)
753 /* another server introduced us */
754 srv = AddServer(GetServerH(origin), argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
757 srv->self_burst = argv[5][0] == 'J';
760 /* this must be our uplink */
761 srv = self->uplink = AddServer(self, argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
764 srv->self_burst = argv[5][0] == 'J';
766 if ((argv[7][0] == '+') && !force_n2k) {
767 log_module(MAIN_LOG, LOG_WARNING, "Got Undernet-style SERVER message but \"force_n2k\" not on.");
772 /* Fix up our timestamps if necessary. */
773 if (srv->boot <= PREHISTORY) {
774 /* Server from the mists of time.. */
775 if (srv->hops == 1) {
776 log_module(MAIN_LOG, LOG_ERROR, "Server %s claims to have booted at time "FMT_TIME_T". This is absurd.", srv->name, srv->boot);
778 } else if ((str = conf_get_data("server/reliable_clock", RECDB_QSTRING))
779 && enabled_string(str)) {
780 /* If we have a reliable clock, we just keep our current time. */
782 if (srv->boot <= self->boot) {
783 /* The other server is older than us. Accept their timestamp.
784 * Alternately, we are same age, but we accept their time
785 * since we are linking to them. */
786 self->boot = srv->boot;
787 ioset_set_time(srv->link);
790 if (srv == self->uplink) {
791 extern time_t burst_begin;
797 static CMD_FUNC(cmd_eob)
799 struct server *sender;
803 if (!(sender = GetServerH(origin)))
805 if (sender == self->uplink) {
806 cManager.uplink->state = CONNECTED;
807 for (it = dict_first(unbursted_channels); it; it = iter_next(it))
808 irc_burst(iter_data(it));
809 dict_delete(unbursted_channels);
810 unbursted_channels = NULL;
814 sender->self_burst = 0;
815 recalc_bursts(sender);
816 for (ii=0; ii<slf_used; ii++)
817 slf_list[ii](sender);
821 static CMD_FUNC(cmd_eob_ack)
823 extern time_t burst_begin;
825 if (GetServerH(origin) == self->uplink) {
826 burst_length = now - burst_begin;
827 self->self_burst = self->burst = 0;
829 cManager.uplink->state = CONNECTED;
833 static CMD_FUNC(cmd_ping)
839 irc_pong(argv[2], argv[1]);
840 else if ((srv = GetServerH(origin)))
841 irc_pong(self->name, srv->numeric);
842 else if ((un = GetUserH(origin)))
843 irc_pong(self->name, un->numeric);
845 irc_pong(self->name, origin);
847 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
848 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
849 timeq_add(now + ping_freq, timed_send_ping, 0);
854 static CMD_FUNC(cmd_error_nick)
856 /* Go back to original IRC length .. and try to reconnect :/ */
858 irc_squit(self, "Got erroneous nickname, truncating nicks.", NULL);
863 struct userNode *user;
868 join_helper(struct chanNode *chan, void *data)
870 struct create_desc *cd = data;
871 AddChannelUser(cd->user, chan);
875 create_helper(char *name, void *data)
877 struct create_desc *cd = data;
879 if (!strcmp(name, "0")) {
880 while (cd->user->channels.used > 0)
881 DelChannelUser(cd->user, cd->user->channels.list[0]->channel, 0, 0);
885 AddChannelUser(cd->user, AddChannel(name, cd->when, NULL, NULL));
888 static CMD_FUNC(cmd_create)
890 struct create_desc cd;
891 struct userNode *user;
893 if ((argc < 3) || !(user = GetUserH(origin)))
896 cd.when = atoi(argv[2]);
897 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
901 static CMD_FUNC(cmd_join)
903 struct create_desc cd;
905 if (!(cd.user = GetUserH(origin)))
912 cd.when = atoi(argv[2]);
913 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
917 static CMD_FUNC(cmd_pong)
921 if (!strcmp(argv[2], self->name)) {
922 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
923 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
924 timeq_add(now + ping_freq, timed_send_ping, 0);
930 static CMD_FUNC(cmd_nick)
932 struct userNode *user;
933 if ((user = GetUserH(origin))) {
934 /* nick change (since the source is a user numeric) */
937 NickChange(user, argv[1], 1);
944 serv = GetServerH(origin);
946 unsplit_string(argv+6, argc-9, modes);
949 AddUser(serv, argv[1], argv[4], argv[5], modes, argv[argc-2], argv[argc-1], atoi(argv[3]), argv[argc-3]);
954 static CMD_FUNC(cmd_account)
956 struct userNode *user;
958 if ((argc < 3) || !origin || !GetServerH(origin))
959 return 0; /* Origin must be server. */
960 user = GetUserN(argv[1]);
962 return 1; /* A QUIT probably passed the ACCOUNT. */
963 call_account_func(user, argv[2]);
967 static CMD_FUNC(cmd_burst)
970 char modes[MAXLEN], *members = "", *banlist = NULL;
971 unsigned int next = 3, res = 1;
972 struct chanNode *cNode;
974 struct modeNode *mNode;
976 char *user, *end, sep;
982 while (next < argc) {
983 switch (argv[next][0]) {
987 for (pos=argv[next], n_modes = 1; *pos; pos++)
988 if ((*pos == 'k') || (*pos == 'l'))
990 unsplit_string(argv+next, n_modes, modes);
994 case '%': banlist = argv[next++]+1; break;
995 default: members = argv[next++]; break;
999 in_timestamp = atoi(argv[2]);
1000 if ((cNode = dict_find(unbursted_channels, argv[1], NULL))) {
1001 cNode->timestamp = in_timestamp;
1002 dict_remove(unbursted_channels, cNode->name);
1005 cNode = AddChannel(argv[1], in_timestamp, modes, banlist);
1007 /* Burst channel members in now. */
1008 for (user = members, sep = *members, mode = 0; sep; user = end) {
1009 for (end = user + 3; isalnum(*end) || *end == '[' || *end == ']'; end++) ;
1010 sep = *end++; end[-1] = 0;
1013 while ((sep = *end++)) {
1015 mode |= MODE_CHANOP;
1016 else if (sep == 'v')
1024 if (!(un = GetUserN(user))) {
1028 if ((mNode = AddChannelUser(un, cNode)))
1029 mNode->modes = mode;
1035 static CMD_FUNC(cmd_mode)
1037 struct chanNode *cn;
1038 struct userNode *un;
1042 if (!IsChannelName(argv[1])) {
1043 un = GetUserH(argv[1]);
1045 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose mode is changing.", argv[1]);
1048 mod_usermode(un, argv[2]);
1052 if (!(cn = GetChannel(argv[1]))) {
1053 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1056 if ((un = GetUserH(origin))) {
1057 struct modeNode *mn;
1058 /* Update idle time for person setting the mode */
1059 if ((mn = GetUserMode(cn, un)))
1060 mn->idle_since = now;
1062 /* If it came from a server, reset timestamp to re-sync. */
1063 cn->timestamp = atoi(argv[argc-1]);
1066 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER|(un ? MC_NOTIFY : 0));
1069 static CMD_FUNC(cmd_opmode)
1071 struct chanNode *cn;
1072 struct userNode *un;
1077 if (!(cn = GetChannel(argv[1]))) {
1078 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1081 if (!(un = GetUserH(origin))) {
1082 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting OPMODE.", origin);
1086 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using OPMODE.", un->nick);
1090 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER); /* do NOT announce opmode locally */
1093 static int clear_chanmode(struct chanNode *channel, const char *modes);
1095 static CMD_FUNC(cmd_clearmode)
1097 struct chanNode *cn;
1098 struct userNode *un;
1103 if (!(cn = GetChannel(argv[1]))) {
1104 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1107 if (!(un = GetUserH(origin))) {
1108 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting CLEARMODE.", origin);
1112 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using CLEARMODE.", un->nick);
1116 return clear_chanmode(cn, argv[2]);
1119 static CMD_FUNC(cmd_topic)
1121 struct chanNode *cn;
1122 time_t chan_ts, topic_ts;
1126 if (!(cn = GetChannel(argv[1]))) {
1127 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose topic is being set", argv[1]);
1131 /* Looks like an Asuka style topic burst. */
1132 chan_ts = atoi(argv[2]);
1133 topic_ts = atoi(argv[3]);
1135 chan_ts = cn->timestamp;
1138 SetChannelTopic(cn, GetUserH(origin), argv[argc-1], 0);
1139 cn->topic_time = topic_ts;
1143 static CMD_FUNC(cmd_num_topic)
1145 static struct chanNode *cn;
1148 return 0; /* huh? */
1150 cn = GetChannel(argv[2]);
1152 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s in topic reply", argv[2]);
1158 switch (atoi(argv[0])) {
1161 break; /* no topic */
1165 safestrncpy(cn->topic, unsplit_string(argv+3, argc-3, NULL), sizeof(cn->topic));
1170 safestrncpy(cn->topic_nick, argv[3], sizeof(cn->topic_nick));
1171 cn->topic_time = atoi(argv[4]);
1174 return 0; /* should never happen */
1179 static CMD_FUNC(cmd_num_gline)
1183 gline_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0);
1187 static CMD_FUNC(cmd_quit)
1189 struct userNode *user;
1192 /* Sometimes we get a KILL then a QUIT or the like, so we don't want to
1193 * call DelUser unless we have the user in our grasp. */
1194 if ((user = GetUserH(origin)))
1195 DelUser(user, NULL, false, argv[1]);
1199 static CMD_FUNC(cmd_kill)
1201 struct userNode *user;
1204 user = GetUserN(argv[1]);
1206 /* If we get a KILL for a non-existent user, it could be a
1207 * Ghost response to a KILL we sent out earlier. So we only
1208 * whine if the target is local.
1210 if (!strncmp(argv[1], self->numeric, strlen(self->numeric)))
1211 log_module(MAIN_LOG, LOG_ERROR, "Unable to find kill victim %s", argv[1]);
1215 if (IsLocal(user) && IsService(user)) {
1216 /* TODO: rate limit this so silly things don't happen. */
1217 ReintroduceUser(user);
1221 DelUser(user, NULL, false, argv[2]);
1225 static CMD_FUNC(cmd_part)
1227 struct userNode *user;
1231 user = GetUserH(origin);
1234 parse_foreach(argv[1], part_helper, NULL, NULL, NULL, user);
1238 static CMD_FUNC(cmd_kick)
1242 ChannelUserKicked(GetUserH(origin), GetUserN(argv[2]), GetChannel(argv[1]));
1246 static CMD_FUNC(cmd_squit)
1248 struct server *server;
1252 if (!(server = GetServerH(argv[1])))
1255 if (server == self->uplink) {
1256 /* Force a reconnect to the currently selected server. */
1257 cManager.uplink->tries = 0;
1258 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", argv[3]);
1263 DelServer(server, 0, argv[3]);
1267 static CMD_FUNC(cmd_privmsg)
1269 struct privmsg_desc pd;
1272 pd.user = GetUserH(origin);
1273 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
1277 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
1281 static CMD_FUNC(cmd_notice)
1283 struct privmsg_desc pd;
1286 pd.user = GetUserH(origin);
1287 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
1291 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
1295 static CMD_FUNC(cmd_away)
1297 struct userNode *uNode;
1299 uNode = GetUserH(origin);
1303 uNode->modes &= ~FLAGS_AWAY;
1305 uNode->modes |= FLAGS_AWAY;
1309 static CMD_FUNC(cmd_gline)
1313 if (argv[2][0] == '+') {
1316 gline_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0);
1318 } else if (argv[2][0] == '-') {
1319 gline_remove(argv[2]+1, 0);
1325 static CMD_FUNC(cmd_svsnick)
1327 struct userNode *target, *dest;
1329 || !(target = GetUserN(argv[1]))
1331 || (dest = GetUserH(argv[2])))
1333 NickChange(target, argv[2], 0);
1337 static oper_func_t *of_list;
1338 static unsigned int of_size = 0, of_used = 0;
1341 free_user(struct userNode *user)
1352 free(privmsg_funcs);
1355 dict_delete(irc_func_dict);
1356 for (nn=0; nn<dead_users.used; nn++)
1357 free_user(dead_users.list[nn]);
1358 userList_clean(&dead_users);
1362 p10_conf_reload(void) {
1363 hidden_host_suffix = conf_get_data("server/hidden_host", RECDB_QSTRING);
1367 remove_unbursted_channel(struct chanNode *cNode) {
1368 if (unbursted_channels)
1369 dict_remove(unbursted_channels, cNode->name);
1375 const char *str, *desc;
1376 int numnick, usermask, max_users;
1377 char numer[COMBO_NUMERIC_LEN+1];
1379 /* read config items */
1380 str = conf_get_data("server/ping_freq", RECDB_QSTRING);
1381 ping_freq = str ? ParseInterval(str) : 120;
1382 str = conf_get_data("server/ping_timeout", RECDB_QSTRING);
1383 ping_timeout = str ? ParseInterval(str) : 30;
1384 str = conf_get_data("server/force_n2k", RECDB_QSTRING);
1385 force_n2k = str ? enabled_string(str) : 1;
1386 str = conf_get_data("server/numeric", RECDB_QSTRING);
1388 log_module(MAIN_LOG, LOG_ERROR, "No server/numeric entry in config file.");
1391 numnick = atoi(str);
1392 str = conf_get_data("server/max_users", RECDB_QSTRING);
1393 max_users = str ? atoi(str) : 4096;
1394 for (usermask = 4; usermask < max_users; usermask <<= 1) ;
1396 if ((numnick < 64) && (usermask < 4096) && !force_n2k)
1397 inttobase64(numer, (numnick << 12) + (usermask & 0x00fff), 3);
1399 inttobase64(numer, (numnick << 18) + (usermask & 0x3ffff), 5);
1400 str = conf_get_data("server/hostname", RECDB_QSTRING);
1401 desc = conf_get_data("server/description", RECDB_QSTRING);
1402 if (!str || !desc) {
1403 log_module(MAIN_LOG, LOG_ERROR, "No server/hostname entry in config file.");
1406 self = AddServer(NULL, str, 0, boot_time, now, numer, desc);
1407 conf_register_reload(p10_conf_reload);
1409 irc_func_dict = dict_new();
1410 dict_insert(irc_func_dict, CMD_BURST, cmd_burst);
1411 dict_insert(irc_func_dict, TOK_BURST, cmd_burst);
1412 dict_insert(irc_func_dict, CMD_CREATE, cmd_create);
1413 dict_insert(irc_func_dict, TOK_CREATE, cmd_create);
1414 dict_insert(irc_func_dict, CMD_EOB, cmd_eob);
1415 dict_insert(irc_func_dict, TOK_EOB, cmd_eob);
1416 dict_insert(irc_func_dict, CMD_EOB_ACK, cmd_eob_ack);
1417 dict_insert(irc_func_dict, TOK_EOB_ACK, cmd_eob_ack);
1418 dict_insert(irc_func_dict, CMD_MODE, cmd_mode);
1419 dict_insert(irc_func_dict, TOK_MODE, cmd_mode);
1420 dict_insert(irc_func_dict, CMD_NICK, cmd_nick);
1421 dict_insert(irc_func_dict, TOK_NICK, cmd_nick);
1422 dict_insert(irc_func_dict, CMD_ACCOUNT, cmd_account);
1423 dict_insert(irc_func_dict, TOK_ACCOUNT, cmd_account);
1424 dict_insert(irc_func_dict, CMD_PASS, cmd_pass);
1425 dict_insert(irc_func_dict, TOK_PASS, cmd_pass);
1426 dict_insert(irc_func_dict, CMD_PING, cmd_ping);
1427 dict_insert(irc_func_dict, TOK_PING, cmd_ping);
1428 dict_insert(irc_func_dict, CMD_PRIVMSG, cmd_privmsg);
1429 dict_insert(irc_func_dict, TOK_PRIVMSG, cmd_privmsg);
1430 dict_insert(irc_func_dict, CMD_PONG, cmd_pong);
1431 dict_insert(irc_func_dict, TOK_PONG, cmd_pong);
1432 dict_insert(irc_func_dict, CMD_QUIT, cmd_quit);
1433 dict_insert(irc_func_dict, TOK_QUIT, cmd_quit);
1434 dict_insert(irc_func_dict, CMD_SERVER, cmd_server);
1435 dict_insert(irc_func_dict, TOK_SERVER, cmd_server);
1436 dict_insert(irc_func_dict, CMD_JOIN, cmd_join);
1437 dict_insert(irc_func_dict, TOK_JOIN, cmd_join);
1438 dict_insert(irc_func_dict, CMD_PART, cmd_part);
1439 dict_insert(irc_func_dict, TOK_PART, cmd_part);
1440 dict_insert(irc_func_dict, CMD_ERROR, cmd_error);
1441 dict_insert(irc_func_dict, TOK_ERROR, cmd_error);
1442 dict_insert(irc_func_dict, CMD_TOPIC, cmd_topic);
1443 dict_insert(irc_func_dict, TOK_TOPIC, cmd_topic);
1444 dict_insert(irc_func_dict, CMD_AWAY, cmd_away);
1445 dict_insert(irc_func_dict, TOK_AWAY, cmd_away);
1446 dict_insert(irc_func_dict, CMD_SILENCE, cmd_dummy);
1447 dict_insert(irc_func_dict, TOK_SILENCE, cmd_dummy);
1448 dict_insert(irc_func_dict, CMD_KICK, cmd_kick);
1449 dict_insert(irc_func_dict, TOK_KICK, cmd_kick);
1450 dict_insert(irc_func_dict, CMD_SQUIT, cmd_squit);
1451 dict_insert(irc_func_dict, TOK_SQUIT, cmd_squit);
1452 dict_insert(irc_func_dict, CMD_KILL, cmd_kill);
1453 dict_insert(irc_func_dict, TOK_KILL, cmd_kill);
1454 dict_insert(irc_func_dict, CMD_NOTICE, cmd_notice);
1455 dict_insert(irc_func_dict, TOK_NOTICE, cmd_notice);
1456 dict_insert(irc_func_dict, CMD_STATS, cmd_stats);
1457 dict_insert(irc_func_dict, TOK_STATS, cmd_stats);
1458 dict_insert(irc_func_dict, CMD_SVSNICK, cmd_svsnick);
1459 dict_insert(irc_func_dict, TOK_SVSNICK, cmd_svsnick);
1460 dict_insert(irc_func_dict, CMD_WHOIS, cmd_whois);
1461 dict_insert(irc_func_dict, TOK_WHOIS, cmd_whois);
1462 dict_insert(irc_func_dict, CMD_GLINE, cmd_gline);
1463 dict_insert(irc_func_dict, TOK_GLINE, cmd_gline);
1464 dict_insert(irc_func_dict, CMD_OPMODE, cmd_opmode);
1465 dict_insert(irc_func_dict, TOK_OPMODE, cmd_opmode);
1466 dict_insert(irc_func_dict, CMD_CLEARMODE, cmd_clearmode);
1467 dict_insert(irc_func_dict, TOK_CLEARMODE, cmd_clearmode);
1468 dict_insert(irc_func_dict, CMD_VERSION, cmd_version);
1469 dict_insert(irc_func_dict, TOK_VERSION, cmd_version);
1470 dict_insert(irc_func_dict, CMD_ADMIN, cmd_admin);
1471 dict_insert(irc_func_dict, TOK_ADMIN, cmd_admin);
1473 /* In P10, DESTRUCT doesn't do anything except be broadcast to servers.
1474 * Apparently to obliterate channels from any servers that think they
1477 dict_insert(irc_func_dict, CMD_DESTRUCT, cmd_dummy);
1478 dict_insert(irc_func_dict, TOK_DESTRUCT, cmd_dummy);
1479 /* Ignore invites */
1480 dict_insert(irc_func_dict, CMD_INVITE, cmd_dummy);
1481 dict_insert(irc_func_dict, TOK_INVITE, cmd_dummy);
1482 /* DESYNCH is just informational, so ignore it */
1483 dict_insert(irc_func_dict, CMD_DESYNCH, cmd_dummy);
1484 dict_insert(irc_func_dict, TOK_DESYNCH, cmd_dummy);
1485 /* Ignore channel operator notices. */
1486 dict_insert(irc_func_dict, CMD_WALLCHOPS, cmd_dummy);
1487 dict_insert(irc_func_dict, TOK_WALLCHOPS, cmd_dummy);
1488 dict_insert(irc_func_dict, CMD_WALLVOICES, cmd_dummy);
1489 dict_insert(irc_func_dict, TOK_WALLVOICES, cmd_dummy);
1490 /* Ignore opers being silly. */
1491 dict_insert(irc_func_dict, CMD_WALLOPS, cmd_dummy);
1492 dict_insert(irc_func_dict, TOK_WALLOPS, cmd_dummy);
1493 /* We have reliable clock! Always! Wraaa! */
1494 dict_insert(irc_func_dict, CMD_SETTIME, cmd_dummy);
1495 dict_insert(irc_func_dict, TOK_SETTIME, cmd_dummy);
1497 dict_insert(irc_func_dict, "331", cmd_num_topic);
1498 dict_insert(irc_func_dict, "332", cmd_num_topic);
1499 dict_insert(irc_func_dict, "333", cmd_num_topic);
1500 dict_insert(irc_func_dict, "432", cmd_error_nick); /* Erroneus [sic] nickname */
1501 /* ban list resetting */
1502 /* "stats g" responses */
1503 dict_insert(irc_func_dict, "247", cmd_num_gline);
1504 dict_insert(irc_func_dict, "219", cmd_dummy); /* "End of /STATS report" */
1505 /* other numeric responses we might get */
1506 dict_insert(irc_func_dict, "401", cmd_dummy); /* target left network */
1507 dict_insert(irc_func_dict, "403", cmd_dummy); /* no such channel */
1508 dict_insert(irc_func_dict, "404", cmd_dummy); /* cannot send to channel */
1509 dict_insert(irc_func_dict, "441", cmd_dummy); /* target isn't on that channel */
1510 dict_insert(irc_func_dict, "442", cmd_dummy); /* you aren't on that channel */
1511 dict_insert(irc_func_dict, "443", cmd_dummy); /* is already on channel (after invite?) */
1512 dict_insert(irc_func_dict, "461", cmd_dummy); /* Not enough parameters (after TOPIC w/ 0 args) */
1514 num_privmsg_funcs = 16;
1515 privmsg_funcs = malloc(sizeof(privmsg_func_t)*num_privmsg_funcs);
1516 memset(privmsg_funcs, 0, sizeof(privmsg_func_t)*num_privmsg_funcs);
1518 num_notice_funcs = 16;
1519 notice_funcs = malloc(sizeof(privmsg_func_t)*num_notice_funcs);
1520 memset(notice_funcs, 0, sizeof(privmsg_func_t)*num_notice_funcs);
1522 userList_init(&dead_users);
1523 reg_del_channel_func(remove_unbursted_channel);
1524 reg_exit_func(parse_cleanup);
1528 parse_line(char *line, int recursive)
1530 char *argv[MAXNUMPARAMS], *origin;
1531 int argc, cmd, res=0;
1534 argc = split_line(line, true, MAXNUMPARAMS, argv);
1535 cmd = self->uplink || !argv[0][1] || !argv[0][2];
1538 if (argv[0][0] == ':') {
1540 } else if (!argv[0][1] || !argv[0][2]) {
1541 struct server *sNode = GetServerN(argv[0]);
1542 origin = sNode ? sNode->name : 0;
1544 struct userNode *uNode = GetUserN(argv[0]);
1545 origin = uNode ? uNode->nick : 0;
1549 if ((func = dict_find(irc_func_dict, argv[cmd], NULL)))
1550 res = func(origin, argc-cmd, argv+cmd);
1553 log_module(MAIN_LOG, LOG_ERROR, "PARSE ERROR on line: %s", unsplit_string(argv, argc, NULL));
1554 } else if (!recursive) {
1556 for (i=0; i<dead_users.used; i++)
1557 free_user(dead_users.list[i]);
1558 dead_users.used = 0;
1564 parse_foreach(char *target_list, foreach_chanfunc cf, foreach_nonchan nc, foreach_userfunc uf, foreach_nonuser nu, void *data)
1569 while (*j != 0 && *j != ',')
1573 if (IsChannelName(target_list)
1574 || (target_list[0] == '0' && target_list[1] == '\0')) {
1575 struct chanNode *chan = GetChannel(target_list);
1581 nc(target_list, data);
1584 struct userNode *user;
1585 struct privmsg_desc *pd = data;
1587 pd->is_qualified = 0;
1588 if (*target_list == '@') {
1590 } else if (strchr(target_list, '@')) {
1591 struct server *server;
1593 pd->is_qualified = 1;
1594 user = GetUserH(strtok(target_list, "@"));
1595 server = GetServerH(strtok(NULL, "@"));
1597 if (user && (user->uplink != server)) {
1598 /* Don't attempt to index into any arrays
1599 using a user's numeric on another server. */
1603 user = GetUserN(target_list);
1611 nu(target_list, data);
1615 } while (old == ',');
1619 get_local_numeric(void)
1621 static unsigned int next_numeric = 0;
1622 if (self->clients > self->num_mask)
1624 while (self->users[next_numeric])
1625 if (++next_numeric > self->num_mask)
1627 return next_numeric;
1631 make_numeric(struct server *svr, int local_num, char *outbuf)
1635 if (force_n2k || svr->numeric[1]) {
1640 llen = (local_num < 64*64) ? 2 : 3;
1642 strncpy(outbuf, svr->numeric, slen);
1643 inttobase64(outbuf+slen, local_num, llen);
1644 outbuf[slen+llen] = 0;
1648 AddServer(struct server *uplink, const char *name, int hops, time_t boot, time_t link, const char *numeric, const char *description)
1650 struct server* sNode;
1653 if ((sNode = GetServerN(numeric))) {
1654 /* This means we're trying to re-add an existant server.
1655 * To be safe, we should forget the previous incarnation.
1656 * (And all its linked servers.)
1658 * It usually only happens in replays when the original
1659 * had a ping timeout and the replay didn't (because
1660 * replaying a ping timeout invariably gets things wrong).
1662 DelServer(sNode, 0, NULL);
1665 switch (strlen(numeric)) {
1666 case 5: slen = 2; mlen = 3; break;
1667 case 4: slen = 1; mlen = 3; break;
1668 case 3: slen = 1; mlen = 2; break;
1670 log_module(MAIN_LOG, LOG_ERROR, "AddServer(\"%s\", \"%s\", ...): Numeric %s has invalid length.", uplink->name, name, numeric);
1674 sNode = calloc(1, sizeof(*sNode));
1675 sNode->uplink = uplink;
1676 safestrncpy(sNode->name, name, sizeof(sNode->name));
1677 sNode->num_mask = base64toint(numeric+slen, mlen);
1681 strncpy(sNode->numeric, numeric, slen);
1682 safestrncpy(sNode->description, description, sizeof(sNode->description));
1683 sNode->users = calloc(sNode->num_mask+1, sizeof(*sNode->users));
1684 serverList_init(&sNode->children);
1685 if (sNode->uplink) {
1686 /* uplink may be NULL if we're just building ourself */
1687 serverList_append(&sNode->uplink->children, sNode);
1689 servers_num[base64toint(numeric, slen)] = sNode;
1690 dict_insert(servers, sNode->name, sNode);
1694 void DelServer(struct server* serv, int announce, const char *message)
1698 /* If we receive an ERROR command before the SERVER
1699 * command a NULL server can be passed */
1703 /* Hrm, what's the right way to SQUIT some other server?
1704 * (This code is only to handle killing juped servers.) */
1705 if (announce && (serv->uplink == self) && (serv != self->uplink))
1706 irc_squit(serv, message, NULL);
1708 /* must recursively remove servers linked to this one first */
1709 for (i=serv->children.used;i>0;)
1710 if (serv->children.list[--i] != self)
1711 DelServer(serv->children.list[i], false, NULL);
1713 /* clean up server's user hash tables */
1714 for (i=0;i<=serv->num_mask;i++)
1716 DelUser(serv->users[i], NULL, false, "server delinked");
1720 serverList_remove(&serv->uplink->children, serv);
1721 if (serv == self->uplink)
1722 self->uplink = NULL;
1723 servers_num[base64toint(serv->numeric, strlen(serv->numeric))] = NULL;
1724 dict_remove(servers, serv->name);
1725 serverList_clean(&serv->children);
1731 AddService(const char *nick, const char *desc)
1733 char numeric[COMBO_NUMERIC_LEN+1];
1734 int local_num = get_local_numeric();
1735 time_t timestamp = now;
1736 struct userNode *old_user = GetUserH(nick);
1739 if (IsLocal(old_user))
1741 timestamp = old_user->timestamp - 1;
1743 if (local_num == -1) {
1744 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for service %s", nick);
1747 make_numeric(self, local_num, numeric);
1748 return AddUser(self, nick, nick, self->name, "+oik", numeric, desc, now, "AAAAAA");
1752 AddClone(const char *nick, const char *ident, const char *hostname, const char *desc)
1754 char numeric[COMBO_NUMERIC_LEN+1];
1755 int local_num = get_local_numeric();
1756 time_t timestamp = now;
1757 struct userNode *old_user = GetUserH(nick);
1760 if (IsLocal(old_user))
1762 timestamp = old_user->timestamp - 1;
1764 if (local_num == -1) {
1765 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for clone %s", nick);
1768 make_numeric(self, local_num, numeric);
1769 return AddUser(self, nick, ident, hostname, "+i", numeric, desc, timestamp, "AAAAAA");
1773 is_valid_nick(const char *nick) {
1774 /* IRC has some of The Most Fucked-Up ideas about character sets
1776 if (!isalpha(*nick) && !strchr("{|}~[\\]^_`", *nick))
1778 for (++nick; *nick; ++nick)
1779 if (!isalnum(*nick) && !strchr("{|}~[\\]^-_`", *nick))
1781 if (strlen(nick) > nicklen)
1786 static struct userNode*
1787 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)
1789 struct userNode *oldUser, *uNode;
1790 unsigned int n, ignore_user;
1792 if ((strlen(numeric) < 3) || (strlen(numeric) > 5)) {
1793 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): numeric %s wrong length!", uplink, nick, numeric);
1798 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s doesn't exist!", uplink, nick, numeric);
1802 if (uplink != GetServerN(numeric)) {
1803 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s differs from nominal uplink %s.", uplink, nick, numeric, uplink->name);
1807 if (!is_valid_nick(nick)) {
1808 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): invalid nickname detected.", uplink, nick);
1813 if ((oldUser = GetUserH(nick))) {
1814 if (IsLocal(oldUser) && (IsService(oldUser) || IsPersistent(oldUser))) {
1815 /* The service should collide the new user off. */
1816 oldUser->timestamp = timestamp - 1;
1819 if (oldUser->timestamp > timestamp) {
1820 /* "Old" user is really newer; remove them */
1821 DelUser(oldUser, 0, 1, "Overruled by older nick");
1823 /* User being added is too new; do not add them to
1824 * clients, but do add them to the server's list, since it
1825 * will send a KILL and QUIT soon. */
1830 /* create new usernode and set all values */
1831 uNode = calloc(1, sizeof(*uNode));
1832 uNode->nick = strdup(nick);
1833 safestrncpy(uNode->ident, ident, sizeof(uNode->ident));
1834 safestrncpy(uNode->info, userinfo, sizeof(uNode->info));
1835 safestrncpy(uNode->hostname, hostname, sizeof(uNode->hostname));
1836 safestrncpy(uNode->numeric, numeric, sizeof(uNode->numeric));
1837 uNode->ip.s_addr = htonl(base64toint(realip, 6));
1838 uNode->timestamp = timestamp;
1839 modeList_init(&uNode->channels);
1840 uNode->uplink = uplink;
1841 if (++uNode->uplink->clients > uNode->uplink->max_clients) {
1842 uNode->uplink->max_clients = uNode->uplink->clients;
1844 uNode->num_local = base64toint(numeric+strlen(uNode->uplink->numeric), 3) & uNode->uplink->num_mask;
1845 uNode->uplink->users[uNode->num_local] = uNode;
1846 mod_usermode(uNode, modes);
1850 dict_insert(clients, uNode->nick, uNode);
1851 if (dict_size(clients) > max_clients) {
1852 max_clients = dict_size(clients);
1853 max_clients_time = now;
1857 for (n=0; n<nuf_used; n++)
1858 if (nuf_list[n](uNode))
1863 /* removes user from it's server's hash table and nick hash table */
1865 DelUser(struct userNode* user, struct userNode *killer, int announce, const char *why)
1869 /* mark them as dead, in case anybody cares */
1872 /* remove user from all channels */
1873 while (user->channels.used > 0)
1874 DelChannelUser(user, user->channels.list[user->channels.used-1]->channel, false, 0);
1876 /* Call these in reverse order so ChanServ can update presence
1877 information before NickServ nukes the handle_info. */
1878 for (n = duf_used; n > 0; )
1879 duf_list[--n](user, killer, why);
1881 user->uplink->clients--;
1882 user->uplink->users[user->num_local] = NULL;
1884 userList_remove(&curr_opers, user);
1885 /* remove from global dictionary, but not if after a collide */
1886 if (user == dict_find(clients, user->nick, NULL))
1887 dict_remove(clients, user->nick);
1889 if (IsInvisible(user))
1894 irc_quit(user, why);
1896 irc_kill(killer, user, why);
1899 modeList_clean(&user->channels);
1900 /* We don't free them, in case we try to privmsg them or something
1901 * (like when a stupid oper kills themself). We just put them onto
1902 * a list of clients that get freed after processing each line.
1904 if (dead_users.size)
1905 userList_append(&dead_users, user);
1910 void mod_usermode(struct userNode *user, const char *mode_change) {
1911 static void call_oper_funcs(struct userNode *user);
1913 const char *word = mode_change;
1915 if (!user || !mode_change)
1917 while (*word != ' ' && *word) word++;\
1918 while (*word == ' ') word++; \
1920 #define do_user_mode(FLAG) do { if (add) user->modes |= FLAG; else user->modes &= ~FLAG; } while (0)
1921 switch (*mode_change++) {
1922 case 0: case ' ': return;
1923 case '+': add = 1; break;
1924 case '-': add = 0; break;
1926 do_user_mode(FLAGS_OPER);
1928 userList_append(&curr_opers, user);
1929 call_oper_funcs(user);
1931 userList_remove(&curr_opers, user);
1934 case 'O': do_user_mode(FLAGS_LOCOP); break;
1935 case 'i': do_user_mode(FLAGS_INVISIBLE);
1941 case 'w': do_user_mode(FLAGS_WALLOP); break;
1942 case 's': do_user_mode(FLAGS_SERVNOTICE); break;
1943 case 'd': do_user_mode(FLAGS_DEAF); break;
1944 case 'k': do_user_mode(FLAGS_SERVICE); break;
1945 case 'g': do_user_mode(FLAGS_GLOBAL); break;
1946 case 'h': do_user_mode(FLAGS_HELPER); break;
1947 case 'x': do_user_mode(FLAGS_HIDDEN_HOST); break;
1952 for (ii=0; (*word != ' ') && (*word != '\0'); )
1953 tag[ii++] = *word++;
1955 while (*word == ' ')
1957 call_account_func(user, tag);
1965 struct mod_chanmode *
1966 mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, unsigned int flags)
1968 struct mod_chanmode *change;
1969 unsigned int ii, in_arg, ch_arg, add;
1973 if (!(change = mod_chanmode_alloc(argc - 1)))
1976 for (ii = ch_arg = 0, in_arg = add = 1;
1977 (modes[0][ii] != '\0') && (modes[0][ii] != ' ');
1979 switch (modes[0][ii]) {
1986 #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)
1987 case 'C': do_chan_mode(MODE_NOCTCPS); break;
1988 case 'D': do_chan_mode(MODE_DELAYJOINS); break;
1989 case 'c': do_chan_mode(MODE_NOCOLORS); break;
1990 case 'i': do_chan_mode(MODE_INVITEONLY); break;
1991 case 'm': do_chan_mode(MODE_MODERATED); break;
1992 case 'n': do_chan_mode(MODE_NOPRIVMSGS); break;
1993 case 'p': do_chan_mode(MODE_PRIVATE); break;
1994 case 'r': do_chan_mode(MODE_REGONLY); break;
1995 case 's': do_chan_mode(MODE_SECRET); break;
1996 case 't': do_chan_mode(MODE_TOPICLIMIT); break;
2002 change->modes_set |= MODE_LIMIT;
2003 change->new_limit = atoi(modes[in_arg++]);
2005 change->modes_set &= ~MODE_LIMIT;
2006 change->modes_clear |= MODE_LIMIT;
2013 change->modes_set |= MODE_KEY;
2014 safestrncpy(change->new_key, modes[in_arg++], sizeof(change->new_key));
2016 change->modes_clear |= MODE_KEY;
2017 if (!(flags & MCP_KEY_FREE)) {
2025 if (!(flags & MCP_ALLOW_OVB))
2029 change->args[ch_arg].mode = MODE_BAN;
2031 change->args[ch_arg].mode |= MODE_REMOVE;
2032 change->args[ch_arg++].hostmask = modes[in_arg++];
2036 struct userNode *victim;
2037 if (!(flags & MCP_ALLOW_OVB))
2041 change->args[ch_arg].mode = (modes[0][ii] == 'o') ? MODE_CHANOP : MODE_VOICE;
2043 change->args[ch_arg].mode |= MODE_REMOVE;
2044 if (flags & MCP_FROM_SERVER)
2045 victim = GetUserN(modes[in_arg++]);
2047 victim = GetUserH(modes[in_arg++]);
2050 if ((change->args[ch_arg].member = GetUserMode(channel, victim)))
2056 change->argc = ch_arg; /* in case any turned out to be ignored */
2057 if (change->modes_set & MODE_SECRET) {
2058 change->modes_set &= ~(MODE_PRIVATE);
2059 change->modes_clear |= MODE_PRIVATE;
2060 } else if (change->modes_set & MODE_PRIVATE) {
2061 change->modes_set &= ~(MODE_SECRET);
2062 change->modes_clear |= MODE_SECRET;
2066 mod_chanmode_free(change);
2070 struct chanmode_buffer {
2073 struct chanNode *channel;
2074 struct userNode *actor;
2075 unsigned int modes_used;
2076 unsigned int args_used;
2078 unsigned int is_add : 1;
2079 unsigned int is_chanop : 1;
2083 mod_chanmode_append(struct chanmode_buffer *buf, char ch, const char *arg)
2085 size_t arg_len = strlen(arg);
2086 if (buf->modes_used > (MAXMODEPARAMS) ||
2087 buf->modes_used + buf->args_used + buf->chname_len + arg_len > 450) {
2088 memcpy(buf->modes + buf->modes_used, buf->args, buf->args_used);
2089 buf->modes[buf->modes_used + buf->args_used] = '\0';
2090 irc_mode((buf->is_chanop ? buf->actor : NULL), buf->channel, buf->modes);
2091 buf->modes[0] = buf->is_add ? '+' : '-';
2092 buf->modes_used = 1;
2095 buf->modes[buf->modes_used++] = ch;
2096 buf->args[buf->args_used++] = ' ';
2097 memcpy(buf->args + buf->args_used, arg, arg_len);
2098 buf->args_used += arg_len;
2102 mod_chanmode_announce(struct userNode *who, struct chanNode *channel, struct mod_chanmode *change)
2104 struct chanmode_buffer chbuf;
2106 struct modeNode *mn;
2107 char int_buff[32], mode = '\0';
2109 assert(change->argc <= change->alloc_argc);
2110 memset(&chbuf, 0, sizeof(chbuf));
2111 chbuf.channel = channel;
2113 chbuf.chname_len = strlen(channel->name);
2114 if ((mn = GetUserMode(channel, who)) && (mn->modes & MODE_CHANOP))
2115 chbuf.is_chanop = 1;
2117 /* First remove modes */
2119 if (change->modes_clear) {
2121 chbuf.modes[chbuf.modes_used++] = mode = '-';
2122 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
2123 DO_MODE_CHAR(PRIVATE, 'p');
2124 DO_MODE_CHAR(SECRET, 's');
2125 DO_MODE_CHAR(MODERATED, 'm');
2126 DO_MODE_CHAR(TOPICLIMIT, 't');
2127 DO_MODE_CHAR(INVITEONLY, 'i');
2128 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2129 DO_MODE_CHAR(LIMIT, 'l');
2130 DO_MODE_CHAR(DELAYJOINS, 'D');
2131 DO_MODE_CHAR(REGONLY, 'r');
2132 DO_MODE_CHAR(NOCOLORS, 'c');
2133 DO_MODE_CHAR(NOCTCPS, 'C');
2135 if (change->modes_clear & channel->modes & MODE_KEY)
2136 mod_chanmode_append(&chbuf, 'k', channel->key);
2138 for (arg = 0; arg < change->argc; ++arg) {
2139 if (!(change->args[arg].mode & MODE_REMOVE))
2142 chbuf.modes[chbuf.modes_used++] = mode = '-';
2143 switch (change->args[arg].mode & ~MODE_REMOVE) {
2145 mod_chanmode_append(&chbuf, 'b', change->args[arg].hostmask);
2148 if (change->args[arg].mode & MODE_CHANOP)
2149 mod_chanmode_append(&chbuf, 'o', change->args[arg].member->user->numeric);
2150 if (change->args[arg].mode & MODE_VOICE)
2151 mod_chanmode_append(&chbuf, 'v', change->args[arg].member->user->numeric);
2158 if (change->modes_set) {
2160 chbuf.modes[chbuf.modes_used++] = mode = '+';
2161 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
2162 DO_MODE_CHAR(PRIVATE, 'p');
2163 DO_MODE_CHAR(SECRET, 's');
2164 DO_MODE_CHAR(MODERATED, 'm');
2165 DO_MODE_CHAR(TOPICLIMIT, 't');
2166 DO_MODE_CHAR(INVITEONLY, 'i');
2167 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2168 DO_MODE_CHAR(DELAYJOINS, 'D');
2169 DO_MODE_CHAR(REGONLY, 'r');
2170 DO_MODE_CHAR(NOCOLORS, 'c');
2171 DO_MODE_CHAR(NOCTCPS, 'C');
2173 if(change->modes_set & MODE_KEY)
2174 mod_chanmode_append(&chbuf, 'k', change->new_key);
2175 if(change->modes_set & MODE_LIMIT) {
2176 sprintf(int_buff, "%d", change->new_limit);
2177 mod_chanmode_append(&chbuf, 'l', int_buff);
2180 for (arg = 0; arg < change->argc; ++arg) {
2181 if (change->args[arg].mode & MODE_REMOVE)
2184 chbuf.modes[chbuf.modes_used++] = mode = '+';
2185 switch (change->args[arg].mode) {
2187 mod_chanmode_append(&chbuf, 'b', change->args[arg].hostmask);
2190 if (change->args[arg].mode & MODE_CHANOP)
2191 mod_chanmode_append(&chbuf, 'o', change->args[arg].member->user->numeric);
2192 if (change->args[arg].mode & MODE_VOICE)
2193 mod_chanmode_append(&chbuf, 'v', change->args[arg].member->user->numeric);
2198 /* Flush the buffer and apply changes locally */
2199 if (chbuf.modes_used > 0) {
2200 memcpy(chbuf.modes + chbuf.modes_used, chbuf.args, chbuf.args_used);
2201 chbuf.modes[chbuf.modes_used + chbuf.args_used] = '\0';
2202 irc_mode((chbuf.is_chanop ? chbuf.actor : NULL), chbuf.channel, chbuf.modes);
2204 mod_chanmode_apply(who, channel, change);
2208 mod_chanmode_format(struct mod_chanmode *change, char *outbuff)
2210 unsigned int used = 0;
2211 assert(change->argc <= change->alloc_argc);
2212 if (change->modes_clear) {
2213 outbuff[used++] = '-';
2214 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) outbuff[used++] = CHAR
2215 DO_MODE_CHAR(PRIVATE, 'p');
2216 DO_MODE_CHAR(SECRET, 's');
2217 DO_MODE_CHAR(MODERATED, 'm');
2218 DO_MODE_CHAR(TOPICLIMIT, 't');
2219 DO_MODE_CHAR(INVITEONLY, 'i');
2220 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2221 DO_MODE_CHAR(LIMIT, 'l');
2222 DO_MODE_CHAR(KEY, 'k');
2223 DO_MODE_CHAR(DELAYJOINS, 'D');
2224 DO_MODE_CHAR(REGONLY, 'r');
2225 DO_MODE_CHAR(NOCOLORS, 'c');
2226 DO_MODE_CHAR(NOCTCPS, 'C');
2229 if (change->modes_set) {
2230 outbuff[used++] = '+';
2231 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) outbuff[used++] = CHAR
2232 DO_MODE_CHAR(PRIVATE, 'p');
2233 DO_MODE_CHAR(SECRET, 's');
2234 DO_MODE_CHAR(MODERATED, 'm');
2235 DO_MODE_CHAR(TOPICLIMIT, 't');
2236 DO_MODE_CHAR(INVITEONLY, 'i');
2237 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2238 DO_MODE_CHAR(DELAYJOINS, 'D');
2239 DO_MODE_CHAR(REGONLY, 'r');
2240 DO_MODE_CHAR(NOCOLORS, 'c');
2241 DO_MODE_CHAR(NOCTCPS, 'C');
2243 switch (change->modes_set & (MODE_KEY|MODE_LIMIT)) {
2244 case MODE_KEY|MODE_LIMIT:
2245 used += sprintf(outbuff+used, "lk %d %s", change->new_limit, change->new_key);
2248 used += sprintf(outbuff+used, "k %s", change->new_key);
2251 used += sprintf(outbuff+used, "l %d", change->new_limit);
2260 clear_chanmode(struct chanNode *channel, const char *modes)
2262 unsigned int remove;
2264 for (remove = 0; *modes; modes++) {
2266 case 'o': remove |= MODE_CHANOP; break;
2267 case 'v': remove |= MODE_VOICE; break;
2268 case 'p': remove |= MODE_PRIVATE; break;
2269 case 's': remove |= MODE_SECRET; break;
2270 case 'm': remove |= MODE_MODERATED; break;
2271 case 't': remove |= MODE_TOPICLIMIT; break;
2272 case 'i': remove |= MODE_INVITEONLY; break;
2273 case 'n': remove |= MODE_NOPRIVMSGS; break;
2276 channel->key[0] = '\0';
2279 remove |= MODE_LIMIT;
2282 case 'b': remove |= MODE_BAN; break;
2283 case 'D': remove |= MODE_DELAYJOINS; break;
2284 case 'r': remove |= MODE_REGONLY; break;
2285 case 'c': remove |= MODE_NOCOLORS;
2286 case 'C': remove |= MODE_NOCTCPS; break;
2293 /* Remove simple modes. */
2294 channel->modes &= ~remove;
2296 /* If removing bans, kill 'em all. */
2297 if ((remove & MODE_BAN) && channel->banlist.used) {
2299 for (i=0; i<channel->banlist.used; i++)
2300 free(channel->banlist.list[i]);
2301 channel->banlist.used = 0;
2304 /* Remove member modes. */
2305 if ((remove & (MODE_CHANOP | MODE_VOICE)) && channel->members.used) {
2306 int mask = ~(remove & (MODE_CHANOP | MODE_VOICE));
2309 for (i = 0; i < channel->members.used; i++)
2310 channel->members.list[i]->modes &= mask;
2317 reg_privmsg_func(struct userNode *user, privmsg_func_t handler)
2319 unsigned int numeric = user->num_local;
2320 if (numeric >= num_privmsg_funcs) {
2321 int newnum = numeric + 8;
2322 privmsg_funcs = realloc(privmsg_funcs, newnum*sizeof(privmsg_func_t));
2323 memset(privmsg_funcs+num_privmsg_funcs, 0, (newnum-num_privmsg_funcs)*sizeof(privmsg_func_t));
2324 num_privmsg_funcs = newnum;
2326 if (privmsg_funcs[numeric])
2327 log_module(MAIN_LOG, LOG_WARNING, "re-registering new privmsg handler for numeric %d", numeric);
2328 privmsg_funcs[numeric] = handler;
2332 reg_notice_func(struct userNode *user, privmsg_func_t handler)
2334 unsigned int numeric = user->num_local;
2335 if (numeric >= num_notice_funcs) {
2336 int newnum = numeric + 8;
2337 notice_funcs = realloc(notice_funcs, newnum*sizeof(privmsg_func_t));
2338 memset(notice_funcs+num_notice_funcs, 0, (newnum-num_notice_funcs)*sizeof(privmsg_func_t));
2339 num_notice_funcs = newnum;
2341 if (notice_funcs[numeric])
2342 log_module(MAIN_LOG, LOG_WARNING, "re-registering new notice handler for numeric %d", numeric);
2343 notice_funcs[numeric] = handler;
2347 reg_oper_func(oper_func_t handler)
2349 if (of_used == of_size) {
2352 of_list = realloc(of_list, of_size*sizeof(oper_func_t));
2355 of_list = malloc(of_size*sizeof(oper_func_t));
2358 of_list[of_used++] = handler;
2362 call_oper_funcs(struct userNode *user)
2367 for (n=0; n<of_used; n++)
2374 unsigned int i, hop, max_hop=1;
2377 /* burst (juped) servers, closest first (except self, which is sent already) */
2378 for (i=0; i<ArrayLength(servers_num); i++)
2379 if (servers_num[i] && servers_num[i]->hops > max_hop)
2380 max_hop = servers_num[i]->hops;
2381 for (hop=1; hop<=max_hop; hop++) {
2382 for (i=0;i<ArrayLength(servers_num);i++) {
2384 && (servers_num[i]->hops == hop)
2385 && (servers_num[i] != self->uplink))
2386 irc_server(servers_num[i]);
2390 /* burst local nicks */
2391 for (i=0; i<=self->num_mask; i++)
2393 irc_user(self->users[i]);
2395 /* build dict of unbursted channel names (just copy existing channels) */
2396 unbursted_channels = dict_new();
2397 for (it = dict_first(channels); it; it = iter_next(it))
2398 dict_insert(unbursted_channels, iter_key(it), iter_data(it));