1 /* proto-p10.c - IRC protocol output
2 * Copyright 2000-2004 srvx Development Team
4 * This file is part of srvx.
6 * srvx is free software; you can redistribute it and/or modify
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.
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.
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.
21 #include "proto-common.c"
24 #define CMD_ACCOUNT "ACCOUNT"
25 #define CMD_ADMIN "ADMIN"
26 #define CMD_ASLL "ASLL"
27 #define CMD_AWAY "AWAY"
28 #define CMD_BURST "BURST"
29 #define CMD_CLEARMODE "CLEARMODE"
30 #define CMD_CLOSE "CLOSE"
31 #define CMD_CNOTICE "CNOTICE"
32 #define CMD_CONNECT "CONNECT"
33 #define CMD_CPRIVMSG "CPRIVMSG"
34 #define CMD_CREATE "CREATE"
35 #define CMD_DESTRUCT "DESTRUCT"
36 #define CMD_DESYNCH "DESYNCH"
39 #define CMD_EOB "END_OF_BURST"
40 #define CMD_EOB_ACK "EOB_ACK"
41 #define CMD_ERROR "ERROR"
42 #define CMD_FAKEHOST "FAKE"
44 #define CMD_GLINE "GLINE"
45 #define CMD_HASH "HASH"
46 #define CMD_HELP "HELP"
47 #define CMD_INFO "INFO"
48 #define CMD_INVITE "INVITE"
49 #define CMD_ISON "ISON"
50 #define CMD_JOIN "JOIN"
51 #define CMD_JUPE "JUPE"
52 #define CMD_KICK "KICK"
53 #define CMD_KILL "KILL"
54 #define CMD_LINKS "LINKS"
55 #define CMD_LIST "LIST"
56 #define CMD_LUSERS "LUSERS"
58 #define CMD_MODE "MODE"
59 #define CMD_MOTD "MOTD"
60 #define CMD_NAMES "NAMES"
61 #define CMD_NICK "NICK"
62 #define CMD_NOTICE "NOTICE"
63 #define CMD_OPER "OPER"
64 #define CMD_OPMODE "OPMODE"
65 #define CMD_PART "PART"
66 #define CMD_PASS "PASS"
67 #define CMD_PING "PING"
68 #define CMD_PONG "PONG"
69 #define CMD_POST "POST"
70 #define CMD_PRIVMSG "PRIVMSG"
71 #define CMD_PRIVS "PRIVS"
72 #define CMD_PROTO "PROTO"
73 #define CMD_QUIT "QUIT"
74 #define CMD_REHASH "REHASH"
75 #define CMD_RESET "RESET"
76 #define CMD_RESTART "RESTART"
77 #define CMD_RPING "RPING"
78 #define CMD_RPONG "RPONG"
79 #define CMD_SERVER "SERVER"
80 #define CMD_SERVLIST "SERVLIST"
81 #define CMD_SERVSET "SERVSET"
83 #define CMD_SETTIME "SETTIME"
84 #define CMD_SILENCE "SILENCE"
85 #define CMD_SQUERY "SQUERY"
86 #define CMD_SQUIT "SQUIT"
87 #define CMD_STATS "STATS"
88 #define CMD_SVSNICK "SVSNICK"
89 #define CMD_TIME "TIME"
90 #define CMD_TOPIC "TOPIC"
91 #define CMD_TRACE "TRACE"
92 #define CMD_UPING "UPING"
93 #define CMD_USER "USER"
94 #define CMD_USERHOST "USERHOST"
95 #define CMD_USERIP "USERIP"
96 #define CMD_VERSION "VERSION"
97 #define CMD_WALLCHOPS "WALLCHOPS"
98 #define CMD_WALLOPS "WALLOPS"
99 #define CMD_WALLUSERS "WALLUSERS"
100 #define CMD_WALLVOICES "WALLVOICES"
101 #define CMD_WHO "WHO"
102 #define CMD_WHOIS "WHOIS"
103 #define CMD_WHOWAS "WHOWAS"
105 /* Tokenized commands. */
106 #define TOK_ACCOUNT "AC"
107 #define TOK_ADMIN "AD"
108 #define TOK_ASLL "LL"
110 #define TOK_BURST "B"
111 #define TOK_CLEARMODE "CM"
112 #define TOK_CLOSE "CLOSE"
113 #define TOK_CNOTICE "CN"
114 #define TOK_CONNECT "CO"
115 #define TOK_CPRIVMSG "CP"
116 #define TOK_CREATE "C"
117 #define TOK_DESTRUCT "DE"
118 #define TOK_DESYNCH "DS"
119 #define TOK_DIE "DIE"
120 #define TOK_DNS "DNS"
122 #define TOK_EOB_ACK "EA"
123 #define TOK_ERROR "Y"
124 #define TOK_FAKEHOST "FA"
125 #define TOK_GET "GET"
126 #define TOK_GLINE "GL"
127 #define TOK_HASH "HASH"
128 #define TOK_HELP "HELP"
130 #define TOK_INVITE "I"
131 #define TOK_ISON "ISON"
133 #define TOK_JUPE "JU"
136 #define TOK_LINKS "LI"
137 #define TOK_LIST "LIST"
138 #define TOK_LUSERS "LU"
139 #define TOK_MAP "MAP"
141 #define TOK_MOTD "MO"
142 #define TOK_NAMES "E"
144 #define TOK_NOTICE "O"
145 #define TOK_OPER "OPER"
146 #define TOK_OPMODE "OM"
148 #define TOK_PASS "PA"
151 #define TOK_POST "POST"
152 #define TOK_PRIVMSG "P"
153 #define TOK_PRIVS "PRIVS"
154 #define TOK_PROTO "PROTO"
156 #define TOK_REHASH "REHASH"
157 #define TOK_RESET "RESET"
158 #define TOK_RESTART "RESTART"
159 #define TOK_RPING "RI"
160 #define TOK_RPONG "RO"
161 #define TOK_SERVER "S"
162 #define TOK_SERVLIST "SERVSET"
163 #define TOK_SERVSET "SERVSET"
164 #define TOK_SET "SET"
165 #define TOK_SETTIME "SE"
166 #define TOK_SILENCE "U"
167 #define TOK_SQUERY "SQUERY"
168 #define TOK_SQUIT "SQ"
169 #define TOK_STATS "R"
170 #define TOK_SVSNICK "SN"
171 #define TOK_TIME "TI"
172 #define TOK_TOPIC "T"
173 #define TOK_TRACE "TR"
174 #define TOK_UPING "UP"
175 #define TOK_USER "USER"
176 #define TOK_USERHOST "USERHOST"
177 #define TOK_USERIP "USERIP"
178 #define TOK_VERSION "V"
179 #define TOK_WALLCHOPS "WC"
180 #define TOK_WALLOPS "WA"
181 #define TOK_WALLUSERS "WU"
182 #define TOK_WALLVOICES "WV"
184 #define TOK_WHOIS "W"
185 #define TOK_WHOWAS "X"
187 /* Protocol messages; aliased to full commands or tokens depending
188 on compile-time configuration. ircu prefers tokens WITH THE
189 EXCEPTION OF THE SERVER AND PASS COMMANDS, which cannot be
190 tokenized, because clients' (ie. a linking server) commands are
191 only checked against the full command list.
193 #if defined(ENABLE_TOKENS)
194 #define TYPE(NAME) TOK_ ## NAME
195 #else /* !ENABLE_TOKENS */
196 #define TYPE(NAME) CMD_ ## NAME
197 #endif /* ENABLE_TOKENS */
199 #define P10_ACCOUNT TYPE(ACCOUNT)
200 #define P10_ADMIN TYPE(ADMIN)
201 #define P10_ASLL TYPE(ASLL)
202 #define P10_AWAY TYPE(AWAY)
203 #define P10_BURST TYPE(BURST)
204 #define P10_CLEARMODE TYPE(CLEARMODE)
205 #define P10_CLOSE TYPE(CLOSE)
206 #define P10_CNOTICE TYPE(CNOTICE)
207 #define P10_CONNECT TYPE(CONNECT)
208 #define P10_CPRIVMSG TYPE(CPRIVMSG)
209 #define P10_CREATE TYPE(CREATE)
210 #define P10_DESTRUCT TYPE(DESTRUCT)
211 #define P10_DESYNCH TYPE(DESYNCH)
212 #define P10_DIE TYPE(DIE)
213 #define P10_DNS TYPE(DNS)
214 #define P10_EOB TYPE(EOB)
215 #define P10_EOB_ACK TYPE(EOB_ACK)
216 #define P10_ERROR TYPE(ERROR)
217 #define P10_FAKEHOST TYPE(FAKEHOST)
218 #define P10_GET TYPE(GET)
219 #define P10_GLINE TYPE(GLINE)
220 #define P10_HASH TYPE(HASH)
221 #define P10_HELP TYPE(HELP)
222 #define P10_INFO TYPE(INFO)
223 #define P10_INVITE TYPE(INVITE)
224 #define P10_ISON TYPE(ISON)
225 #define P10_JOIN TYPE(JOIN)
226 #define P10_JUPE TYPE(JUPE)
227 #define P10_KICK TYPE(KICK)
228 #define P10_KILL TYPE(KILL)
229 #define P10_LINKS TYPE(LINKS)
230 #define P10_LIST TYPE(LIST)
231 #define P10_LUSERS TYPE(LUSERS)
232 #define P10_MAP TYPE(MAP)
233 #define P10_MODE TYPE(MODE)
234 #define P10_MOTD TYPE(MOTD)
235 #define P10_NAMES TYPE(NAMES)
236 #define P10_NICK TYPE(NICK)
237 #define P10_NOTICE TYPE(NOTICE)
238 #define P10_OPER TYPE(OPER)
239 #define P10_OPMODE TYPE(OPMODE)
240 #define P10_PART TYPE(PART)
241 #define P10_PASS CMD_PASS
242 #define P10_PING TYPE(PING)
243 #define P10_PONG TYPE(PONG)
244 #define P10_POST TYPE(POST)
245 #define P10_PRIVMSG TYPE(PRIVMSG)
246 #define P10_PRIVS TYPE(PRIVS)
247 #define P10_PROTO TYPE(PROTO)
248 #define P10_QUIT TYPE(QUIT)
249 #define P10_REHASH TYPE(REHASH)
250 #define P10_RESET TYPE(RESET)
251 #define P10_RESTART TYPE(RESTART)
252 #define P10_RPING TYPE(RPING)
253 #define P10_RPONG TYPE(RPONG)
254 #define P10_SERVER CMD_SERVER
255 #define P10_SERVLIST TYPE(SERVLIST)
256 #define P10_SERVSET TYPE(SERVSET)
257 #define P10_SET TYPE(SET)
258 #define P10_SETTIME TYPE(SETTIME)
259 #define P10_SILENCE TYPE(SILENCE)
260 #define P10_SQUERY TYPE(SQUERY)
261 #define P10_SQUIT TYPE(SQUIT)
262 #define P10_STATS TYPE(STATS)
263 #define P10_SVSNICK TYPE(SVSNICK)
264 #define P10_TIME TYPE(TIME)
265 #define P10_TOPIC TYPE(TOPIC)
266 #define P10_TRACE TYPE(TRACE)
267 #define P10_UPING TYPE(UPING)
268 #define P10_USER TYPE(USER)
269 #define P10_USERHOST TYPE(USERHOST)
270 #define P10_USERIP TYPE(USERIP)
271 #define P10_VERSION TYPE(VERSION)
272 #define P10_WALLCHOPS TYPE(WALLCHOPS)
273 #define P10_WALLOPS TYPE(WALLOPS)
274 #define P10_WALLUSERS TYPE(WALLUSERS)
275 #define P10_WALLVOICES TYPE(WALLVOICES)
276 #define P10_WHO TYPE(WHO)
277 #define P10_WHOIS TYPE(WHOIS)
278 #define P10_WHOWAS TYPE(WHOWAS)
280 /* Servers claiming to have a boot or link time before PREHISTORY
281 * trigger errors to the log. We hope no server has been running
282 * constantly since September 1994. :)
284 #define PREHISTORY 780000000
286 static struct server *servers_num[64*64];
287 static privmsg_func_t *privmsg_funcs;
288 static unsigned int num_privmsg_funcs;
289 static privmsg_func_t *notice_funcs;
290 static unsigned int num_notice_funcs;
291 static struct dict *unbursted_channels;
292 static char *his_servername;
293 static char *his_servercomment;
295 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);
297 extern int off_channel;
299 /* Numerics can be XYY, XYYY, or XXYYY; with X's identifying the
300 * server and Y's indentifying the client on that server. */
302 GetServerN(const char *numeric)
304 switch (strlen(numeric)) {
306 return servers_num[base64toint(numeric, 2)];
310 return servers_num[base64toint(numeric, 1)];
317 GetUserN(const char *numeric) /* using numeric */
323 switch (strlen(numeric)) {
325 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): numeric too long!", numeric);
327 case 5: slen = 2; ulen = 3; break;
328 case 4: slen = 1; ulen = 3; break;
329 case 3: slen = 1; ulen = 2; break;
330 case 2: case 1: case 0:
331 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): numeric too short!", numeric);
334 if (!(s = servers_num[base64toint(numeric, slen)])) {
335 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s): couldn't find server (len=%d)!", numeric, slen);
338 n = base64toint(numeric+slen, ulen) & s->num_mask;
339 if (!(un = s->users[n])) {
340 log_module(MAIN_LOG, LOG_WARNING, "GetUserN(%s) couldn't find user!", numeric);
346 privmsg_user_helper(struct userNode *un, void *data)
348 struct privmsg_desc *pd = data;
349 unsigned int num = un->num_local;
350 if (!pd->is_notice) {
351 if ((num < num_privmsg_funcs) && privmsg_funcs[num]) {
352 privmsg_funcs[num](pd->user, un, pd->text, pd->is_qualified);
355 if ((num < num_notice_funcs) && notice_funcs[num]) {
356 notice_funcs[num](pd->user, un, pd->text, pd->is_qualified);
362 irc_server(struct server *srv)
364 char extranum[COMBO_NUMERIC_LEN+1];
366 inttobase64(extranum, srv->num_mask, (srv->numeric[1] || (srv->num_mask >= 64*64)) ? 3 : 2);
368 /* The +s, ignored by Run's ircu, means "service" to Undernet's ircu */
369 putsock(P10_SERVER " %s %d %li %li J10 %s%s +s :%s",
370 srv->name, srv->hops+1, srv->boot, srv->link, srv->numeric, extranum, srv->description);
372 putsock("%s " P10_SERVER " %s %d %li %li %c10 %s%s +s :%s",
373 self->numeric, srv->name, srv->hops+1, srv->boot, srv->link, (srv->self_burst ? 'J' : 'P'), srv->numeric, extranum, srv->description);
378 irc_user(struct userNode *user)
383 inttobase64(b64ip, ntohl(user->ip.s_addr), 6);
390 modes[modelen++] = 'o';
391 if (IsInvisible(user))
392 modes[modelen++] = 'i';
394 modes[modelen++] = 'w';
396 modes[modelen++] = 'k';
397 if (IsServNotice(user))
398 modes[modelen++] = 's';
400 modes[modelen++] = 'd';
402 modes[modelen++] = 'g';
403 if (IsHelperIrcu(user))
404 modes[modelen++] = 'h';
405 if (IsHiddenHost(user))
406 modes[modelen++] = 'x';
409 /* we don't need to put the + in modes because it's in the format string. */
410 putsock("%s " P10_NICK " %s %d %li %s %s +%s %s %s :%s",
411 user->uplink->numeric, user->nick, user->uplink->hops+1, user->timestamp, user->ident, user->hostname, modes, b64ip, user->numeric, user->info);
413 putsock("%s " P10_NICK " %s %d %li %s %s %s %s :%s",
414 user->uplink->numeric, user->nick, user->uplink->hops+1, user->timestamp, user->ident, user->hostname, b64ip, user->numeric, user->info);
419 irc_account(struct userNode *user, const char *stamp)
421 putsock("%s " P10_ACCOUNT " %s %s", self->numeric, user->numeric, stamp);
425 irc_fakehost(struct userNode *user, const char *host)
427 putsock("%s " P10_FAKEHOST " %s %s", self->numeric, user->numeric, host);
431 irc_regnick(UNUSED_ARG(struct userNode *user))
433 /* no operation here */
437 irc_nick(struct userNode *user, UNUSED_ARG(const char *old_nick))
439 putsock("%s " P10_NICK " %s "FMT_TIME_T, user->numeric, user->nick, now);
443 irc_fetchtopic(struct userNode *from, const char *to)
447 putsock("%s " P10_TOPIC " %s", from->numeric, to);
451 irc_squit(struct server *srv, const char *message, const char *service_message)
453 if (!service_message)
454 service_message = message;
456 /* Are we leaving the network? */
457 if (srv == self && cManager.uplink->state == CONNECTED) {
460 /* Quit all clients linked to me. */
461 for (i = 0; i <= self->num_mask; i++) {
464 irc_quit(self->users[i], service_message);
468 putsock("%s " P10_SQUIT " %s %d :%s", self->numeric, srv->name, 0, message);
471 /* Force a reconnect to the currently selected server. */
472 cManager.uplink->tries = 0;
473 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", message);
479 irc_wallchops(struct userNode *from, const char *to, const char *message)
481 putsock("%s " P10_WALLCHOPS " %s :%s", from->numeric, to, message);
485 irc_notice(struct userNode *from, const char *to, const char *message)
487 putsock("%s " P10_NOTICE " %s :%s", from->numeric, to, message);
491 irc_notice_user(struct userNode *from, struct userNode *to, const char *message)
493 putsock("%s " P10_NOTICE " %s :%s", from->numeric, to->numeric, message);
497 irc_privmsg(struct userNode *from, const char *to, const char *message)
499 putsock("%s " P10_PRIVMSG " %s :%s", from->numeric, to, message);
505 putsock("%s " P10_EOB, self->numeric);
511 putsock("%s " P10_EOB_ACK, self->numeric);
515 irc_ping(const char *payload)
517 putsock("%s " P10_PING " :%s", self->numeric, payload);
521 irc_pong(const char *who, const char *data)
523 putsock("%s " P10_PONG " %s :%s", self->numeric, who, data);
527 irc_pass(const char *passwd)
529 putsock(P10_PASS " :%s", passwd);
533 irc_introduce(const char *passwd)
535 void timed_send_ping(void *data);
537 self->self_burst = self->burst = 1;
541 timeq_add(now + ping_freq, timed_send_ping, 0);
545 irc_gline(struct server *srv, struct gline *gline)
547 putsock("%s " P10_GLINE " %s +%s %ld :%s",
548 self->numeric, (srv ? srv->numeric : "*"), gline->target, gline->expires-now, gline->reason);
552 irc_settime(const char *srv_name_mask, time_t new_time)
554 ioset_set_time(new_time);
555 if (!strcmp(srv_name_mask, "*"))
557 putsock("%s " P10_SETTIME " " FMT_TIME_T " %s", self->numeric, new_time, srv_name_mask);
561 irc_ungline(const char *mask)
563 putsock("%s " P10_GLINE " * -%s", self->numeric, mask);
567 irc_burst(struct chanNode *chan)
569 char burst_line[512];
570 int pos, base_len, len;
576 base_len = sprintf(burst_line, "%s " P10_BURST " %s " FMT_TIME_T " ",
577 self->numeric, chan->name, chan->timestamp);
578 len = irc_make_chanmode(chan, burst_line+base_len);
579 pos = base_len + len;
581 burst_line[pos++] = ' ';
584 for (n=0; n<chan->members.used; n++) {
585 mn = chan->members.list[n];
587 burst_line[pos-1] = 0; /* -1 to back up over the space or comma */
588 putsock("%s", burst_line);
592 memcpy(burst_line+pos, mn->user->numeric, strlen(mn->user->numeric));
593 pos += strlen(mn->user->numeric);
594 if (mn->modes && (mn->modes != last_mode)) {
595 last_mode = mn->modes;
596 burst_line[pos++] = ':';
597 if (last_mode & MODE_CHANOP)
598 burst_line[pos++] = 'o';
599 if (last_mode & MODE_VOICE)
600 burst_line[pos++] = 'v';
602 if ((n+1)<chan->members.used)
603 burst_line[pos++] = ',';
605 if (chan->banlist.used) {
607 if (pos+2+strlen(chan->banlist.list[0]->ban) > 505) {
608 burst_line[pos-1] = 0;
609 putsock("%s", burst_line);
612 burst_line[pos++] = ' ';
615 burst_line[pos++] = ':';
616 burst_line[pos++] = '%';
618 for (n=0; n<chan->banlist.used; n++) {
619 bn = chan->banlist.list[n];
620 len = strlen(bn->ban);
621 if (pos+len+1 > 510) {
622 burst_line[pos-1] = 0; /* -1 to back up over the space or comma */
623 putsock("%s", burst_line);
626 memcpy(burst_line+pos, bn->ban, len);
628 burst_line[pos++] = ' ';
631 /* print the last line */
633 putsock("%s", burst_line);
637 irc_quit(struct userNode *user, const char *message)
639 putsock("%s " P10_QUIT " :%s", user->numeric, message);
643 irc_error(const char *to, const char *message)
646 putsock("%s " P10_ERROR " :%s", to, message);
648 putsock(":%s " P10_ERROR " :%s", self->name, message);
653 irc_kill(struct userNode *from, struct userNode *target, const char *message)
656 putsock("%s " P10_KILL " %s :%s!%s (%s)",
657 from->numeric, target->numeric, self->name, from->nick, message);
659 putsock("%s " P10_KILL " %s :%s (%s)",
660 self->numeric, target->numeric, self->name, message);
665 irc_mode(struct userNode *from, struct chanNode *target, const char *modes)
667 putsock("%s " P10_MODE " %s %s "FMT_TIME_T,
668 (from ? from->numeric : self->numeric),
669 target->name, modes, target->timestamp);
673 irc_invite(struct userNode *from, struct userNode *who, struct chanNode *to)
675 putsock("%s " P10_INVITE " %s %s", from->numeric, who->nick, to->name);
679 irc_join(struct userNode *who, struct chanNode *what)
681 if (what->members.used == 1) {
682 putsock("%s " P10_CREATE " %s %lu",
683 who->numeric, what->name, what->timestamp);
685 putsock("%s " P10_JOIN " %s %lu", who->numeric, what->name, what->timestamp);
690 irc_kick(struct userNode *who, struct userNode *target, struct chanNode *channel, const char *msg)
693 struct modeNode *mn = GetUserMode(channel, who);
694 numeric = ((mn && (mn->modes & MODE_CHANOP)) || off_channel) ? who->numeric : self->numeric;
695 putsock("%s " P10_KICK " %s %s :%s",
696 numeric, channel->name, target->numeric, msg);
700 irc_stats(struct userNode *from, struct server *target, char type)
702 putsock("%s " P10_STATS " %c :%s", from->numeric, type, target->numeric);
706 irc_svsnick(struct userNode *from, struct userNode *target, const char *newnick)
708 putsock("%s " P10_SVSNICK " %s %s "FMT_TIME_T, from->uplink->numeric, target->numeric, newnick, now);
712 irc_part(struct userNode *who, struct chanNode *what, const char *reason)
715 putsock("%s " P10_PART " %s :%s", who->numeric, what->name, reason);
717 putsock("%s " P10_PART " %s", who->numeric, what->name);
722 irc_topic(struct userNode *who, struct chanNode *what, const char *topic)
724 putsock("%s " P10_TOPIC " %s :%s", who->numeric, what->name, topic);
728 irc_raw(const char *what)
734 irc_numeric(struct userNode *user, unsigned int num, const char *format, ...)
738 va_start(arg_list, format);
739 vsnprintf(buffer, MAXLEN-2, format, arg_list);
740 buffer[MAXLEN-1] = 0;
741 putsock(":%s %03d %s %s", self->name, num, user->nick, buffer);
744 static void send_burst(void);
747 change_nicklen(int new_nicklen)
750 char new_nick[NICKLEN+1];
751 struct userNode *user;
753 nicklen = new_nicklen;
754 /* fix up any users we have here */
755 for (nn=0; nn<=self->num_mask; nn++) {
756 if (!(user = self->users[nn]))
758 safestrncpy(new_nick, user->nick, sizeof(new_nick));
759 new_nick[nicklen] = 0;
760 NickChange(user, new_nick, 1);
764 static CMD_FUNC(cmd_whois)
766 struct userNode *from;
767 struct userNode *who;
771 if (!(from = GetUserH(origin))) {
772 log_module(MAIN_LOG, LOG_ERROR, "Could not find WHOIS origin user %s", origin);
775 if(!(who = GetUserH(argv[2]))) {
776 irc_numeric(from, ERR_NOSUCHNICK, "%s@%s :No such nick", argv[2], self->name);
779 if (IsHiddenHost(who) && !IsOper(from)) {
780 /* Just stay quiet. */
783 irc_numeric(from, RPL_WHOISUSER, "%s %s %s * :%s", who->nick, who->ident, who->hostname, who->info);
784 if (his_servername && his_servercomment)
785 irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, his_servername, his_servercomment);
787 irc_numeric(from, RPL_WHOISSERVER, "%s %s :%s", who->nick, who->uplink->name, who->uplink->description);
789 irc_numeric(from, RPL_WHOISOPERATOR, "%s :is a megalomaniacal power hungry tyrant", who->nick);
790 irc_numeric(from, RPL_ENDOFWHOIS, "%s :End of /WHOIS list", who->nick);
794 static CMD_FUNC(cmd_server)
802 /* another server introduced us */
803 srv = AddServer(GetServerH(origin), argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
806 srv->self_burst = argv[5][0] == 'J';
809 /* this must be our uplink */
810 srv = self->uplink = AddServer(self, argv[1], atoi(argv[2]), atoi(argv[3]), atoi(argv[4]), argv[6], argv[argc-1]);
813 srv->self_burst = argv[5][0] == 'J';
815 if ((argv[7][0] == '+') && !force_n2k) {
816 log_module(MAIN_LOG, LOG_WARNING, "Got Undernet-style SERVER message but \"force_n2k\" not on.");
821 /* Fix up our timestamps if necessary. */
822 if (srv->boot <= PREHISTORY) {
823 /* Server from the mists of time.. */
824 if (srv->hops == 1) {
825 log_module(MAIN_LOG, LOG_ERROR, "Server %s claims to have booted at time "FMT_TIME_T". This is absurd.", srv->name, srv->boot);
827 } else if ((str = conf_get_data("server/reliable_clock", RECDB_QSTRING))
828 && enabled_string(str)) {
829 /* If we have a reliable clock, we just keep our current time. */
831 if (srv->boot <= self->boot) {
832 /* The other server is older than us. Accept their timestamp.
833 * Alternately, we are same age, but we accept their time
834 * since we are linking to them. */
835 self->boot = srv->boot;
836 ioset_set_time(srv->link);
839 if (srv == self->uplink) {
840 extern time_t burst_begin;
846 static CMD_FUNC(cmd_eob)
848 struct server *sender;
852 if (!(sender = GetServerH(origin)))
854 if (sender == self->uplink) {
855 cManager.uplink->state = CONNECTED;
856 for (it = dict_first(unbursted_channels); it; it = iter_next(it))
857 irc_burst(iter_data(it));
858 dict_delete(unbursted_channels);
859 unbursted_channels = NULL;
863 sender->self_burst = 0;
864 recalc_bursts(sender);
865 for (ii=0; ii<slf_used; ii++)
866 slf_list[ii](sender);
870 static CMD_FUNC(cmd_eob_ack)
872 extern time_t burst_begin;
874 if (GetServerH(origin) == self->uplink) {
875 burst_length = now - burst_begin;
876 self->self_burst = self->burst = 0;
878 cManager.uplink->state = CONNECTED;
882 static CMD_FUNC(cmd_ping)
888 irc_pong(argv[2], argv[1]);
889 else if ((srv = GetServerH(origin)))
890 irc_pong(self->name, srv->numeric);
891 else if ((un = GetUserH(origin)))
892 irc_pong(self->name, un->numeric);
894 irc_pong(self->name, origin);
896 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
897 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
898 timeq_add(now + ping_freq, timed_send_ping, 0);
903 static CMD_FUNC(cmd_error_nick)
905 /* Go back to original IRC length .. and try to reconnect :/ */
907 irc_squit(self, "Got erroneous nickname, truncating nicks.", NULL);
912 struct userNode *user;
917 join_helper(struct chanNode *chan, void *data)
919 struct create_desc *cd = data;
920 AddChannelUser(cd->user, chan);
924 create_helper(char *name, void *data)
926 struct create_desc *cd = data;
928 if (!strcmp(name, "0")) {
929 while (cd->user->channels.used > 0)
930 DelChannelUser(cd->user, cd->user->channels.list[0]->channel, 0, 0);
934 AddChannelUser(cd->user, AddChannel(name, cd->when, NULL, NULL));
937 static CMD_FUNC(cmd_create)
939 struct create_desc cd;
940 struct userNode *user;
942 if ((argc < 3) || !(user = GetUserH(origin)))
945 cd.when = atoi(argv[2]);
946 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
950 static CMD_FUNC(cmd_join)
952 struct create_desc cd;
954 if (!(cd.user = GetUserH(origin)))
961 cd.when = atoi(argv[2]);
962 parse_foreach(argv[1], join_helper, create_helper, NULL, NULL, &cd);
966 static CMD_FUNC(cmd_pong)
970 if (!strcmp(argv[2], self->name)) {
971 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
972 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
973 timeq_add(now + ping_freq, timed_send_ping, 0);
979 static CMD_FUNC(cmd_nick)
981 struct userNode *user;
982 if ((user = GetUserH(origin))) {
983 /* nick change (since the source is a user numeric) */
986 NickChange(user, argv[1], 1);
993 serv = GetServerH(origin);
995 unsplit_string(argv+6, argc-9, modes);
998 AddUser(serv, argv[1], argv[4], argv[5], modes, argv[argc-2], argv[argc-1], atoi(argv[3]), argv[argc-3]);
1003 static CMD_FUNC(cmd_account)
1005 struct userNode *user;
1007 if ((argc < 3) || !origin || !GetServerH(origin))
1008 return 0; /* Origin must be server. */
1009 user = GetUserN(argv[1]);
1011 return 1; /* A QUIT probably passed the ACCOUNT. */
1012 call_account_func(user, argv[2]);
1016 static CMD_FUNC(cmd_fakehost)
1018 struct userNode *user;
1020 if ((argc < 3) || !origin || !GetServerH(origin))
1022 if (!(user = GetUserN(argv[1])))
1024 assign_fakehost(user, argv[2], 0);
1028 static CMD_FUNC(cmd_burst)
1031 char modes[MAXLEN], *members = "", *banlist = NULL;
1032 unsigned int next = 3, res = 1;
1033 struct chanNode *cNode;
1034 struct userNode *un;
1035 struct modeNode *mNode;
1037 char *user, *end, sep;
1038 time_t in_timestamp;
1043 while (next < argc) {
1044 switch (argv[next][0]) {
1048 for (pos=argv[next], n_modes = 1; *pos; pos++)
1049 if ((*pos == 'k') || (*pos == 'l'))
1051 unsplit_string(argv+next, n_modes, modes);
1055 case '%': banlist = argv[next++]+1; break;
1056 default: members = argv[next++]; break;
1060 in_timestamp = atoi(argv[2]);
1061 if ((cNode = dict_find(unbursted_channels, argv[1], NULL))) {
1062 cNode->timestamp = in_timestamp;
1063 dict_remove(unbursted_channels, cNode->name);
1066 cNode = AddChannel(argv[1], in_timestamp, modes, banlist);
1068 /* Burst channel members in now. */
1069 for (user = members, sep = *members, mode = 0; sep; user = end) {
1070 for (end = user + 3; isalnum(*end) || *end == '[' || *end == ']'; end++) ;
1071 sep = *end++; end[-1] = 0;
1074 while ((sep = *end++)) {
1076 mode |= MODE_CHANOP;
1077 else if (sep == 'v')
1079 else if (isdigit(sep)) {
1080 mode |= MODE_CHANOP;
1081 while (isdigit(*end)) end++;
1088 if (!(un = GetUserN(user))) {
1092 if ((mNode = AddChannelUser(un, cNode)))
1093 mNode->modes = mode;
1099 static CMD_FUNC(cmd_mode)
1101 struct chanNode *cn;
1102 struct userNode *un;
1106 if (!IsChannelName(argv[1])) {
1107 un = GetUserH(argv[1]);
1109 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s whose mode is changing.", argv[1]);
1112 mod_usermode(un, argv[2]);
1116 if (!(cn = GetChannel(argv[1]))) {
1117 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1120 if ((un = GetUserH(origin))) {
1121 struct modeNode *mn;
1122 /* Update idle time for person setting the mode */
1123 if ((mn = GetUserMode(cn, un)))
1124 mn->idle_since = now;
1126 /* If it came from a server, reset timestamp to re-sync. */
1127 cn->timestamp = atoi(argv[argc-1]);
1130 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER|(un ? MC_NOTIFY : 0));
1133 static CMD_FUNC(cmd_opmode)
1135 struct chanNode *cn;
1136 struct userNode *un;
1141 if (!(cn = GetChannel(argv[1]))) {
1142 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1145 if (!(un = GetUserH(origin))) {
1146 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting OPMODE.", origin);
1150 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using OPMODE.", un->nick);
1154 return mod_chanmode(un, cn, argv+2, argc-2, MCP_ALLOW_OVB|MCP_FROM_SERVER); /* do NOT announce opmode locally */
1157 static int clear_chanmode(struct chanNode *channel, const char *modes);
1159 static CMD_FUNC(cmd_clearmode)
1161 struct chanNode *cn;
1162 struct userNode *un;
1167 if (!(cn = GetChannel(argv[1]))) {
1168 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing.", argv[1]);
1171 if (!(un = GetUserH(origin))) {
1172 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s requesting CLEARMODE.", origin);
1176 log_module(MAIN_LOG, LOG_ERROR, "Non-privileged user %s using CLEARMODE.", un->nick);
1180 return clear_chanmode(cn, argv[2]);
1183 static CMD_FUNC(cmd_topic)
1185 struct chanNode *cn;
1186 time_t chan_ts, topic_ts;
1190 if (!(cn = GetChannel(argv[1]))) {
1191 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose topic is being set", argv[1]);
1195 /* Looks like an Asuka style topic burst. */
1196 chan_ts = atoi(argv[2]);
1197 topic_ts = atoi(argv[3]);
1199 chan_ts = cn->timestamp;
1202 SetChannelTopic(cn, GetUserH(origin), argv[argc-1], 0);
1203 cn->topic_time = topic_ts;
1207 static CMD_FUNC(cmd_num_topic)
1209 struct chanNode *cn;
1212 return 0; /* huh? */
1214 cn = GetChannel(argv[2]);
1216 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s in topic reply", argv[2]);
1222 switch (atoi(argv[0])) {
1225 break; /* no topic */
1229 safestrncpy(cn->topic, unsplit_string(argv+3, argc-3, NULL), sizeof(cn->topic));
1234 safestrncpy(cn->topic_nick, argv[3], sizeof(cn->topic_nick));
1235 cn->topic_time = atoi(argv[4]);
1238 return 0; /* should never happen */
1243 static CMD_FUNC(cmd_num_gline)
1247 gline_add(origin, argv[3], atoi(argv[4])-now, argv[5], now, 0);
1251 static CMD_FUNC(cmd_quit)
1253 struct userNode *user;
1256 /* Sometimes we get a KILL then a QUIT or the like, so we don't want to
1257 * call DelUser unless we have the user in our grasp. */
1258 if ((user = GetUserH(origin)))
1259 DelUser(user, NULL, false, argv[1]);
1263 static CMD_FUNC(cmd_kill)
1265 struct userNode *user;
1268 user = GetUserN(argv[1]);
1270 /* If we get a KILL for a non-existent user, it could be a
1271 * Ghost response to a KILL we sent out earlier. So we only
1272 * whine if the target is local.
1274 if (!strncmp(argv[1], self->numeric, strlen(self->numeric)))
1275 log_module(MAIN_LOG, LOG_ERROR, "Unable to find kill victim %s", argv[1]);
1279 if (IsLocal(user) && IsService(user)) {
1280 /* TODO: rate limit this so silly things don't happen. */
1281 ReintroduceUser(user);
1285 DelUser(user, NULL, false, argv[2]);
1289 static CMD_FUNC(cmd_part)
1291 struct userNode *user;
1295 user = GetUserH(origin);
1298 parse_foreach(argv[1], part_helper, NULL, NULL, NULL, user);
1302 static CMD_FUNC(cmd_kick)
1306 ChannelUserKicked(GetUserH(origin), GetUserN(argv[2]), GetChannel(argv[1]));
1310 static CMD_FUNC(cmd_squit)
1312 struct server *server;
1316 if (!(server = GetServerH(argv[1])))
1319 if (server == self->uplink) {
1320 /* Force a reconnect to the currently selected server. */
1321 cManager.uplink->tries = 0;
1322 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", argv[3]);
1327 DelServer(server, 0, argv[3]);
1331 static CMD_FUNC(cmd_privmsg)
1333 struct privmsg_desc pd;
1336 pd.user = GetUserH(origin);
1337 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
1341 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
1345 static CMD_FUNC(cmd_notice)
1347 struct privmsg_desc pd;
1350 pd.user = GetUserH(origin);
1351 if (!pd.user || (IsGagged(pd.user) && !IsOper(pd.user)))
1355 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
1359 static CMD_FUNC(cmd_away)
1361 struct userNode *uNode;
1363 uNode = GetUserH(origin);
1367 uNode->modes &= ~FLAGS_AWAY;
1369 uNode->modes |= FLAGS_AWAY;
1373 static CMD_FUNC(cmd_gline)
1377 if (argv[2][0] == '+') {
1380 gline_add(origin, argv[2]+1, strtoul(argv[3], NULL, 0), argv[argc-1], now, 0);
1382 } else if (argv[2][0] == '-') {
1383 gline_remove(argv[2]+1, 0);
1389 static CMD_FUNC(cmd_svsnick)
1391 struct userNode *target, *dest;
1393 || !(target = GetUserN(argv[1]))
1395 || (dest = GetUserH(argv[2])))
1397 NickChange(target, argv[2], 0);
1401 static oper_func_t *of_list;
1402 static unsigned int of_size = 0, of_used = 0;
1405 free_user(struct userNode *user)
1416 free(privmsg_funcs);
1419 dict_delete(irc_func_dict);
1420 for (nn=0; nn<dead_users.used; nn++)
1421 free_user(dead_users.list[nn]);
1422 userList_clean(&dead_users);
1426 p10_conf_reload(void) {
1427 hidden_host_suffix = conf_get_data("server/hidden_host", RECDB_QSTRING);
1431 remove_unbursted_channel(struct chanNode *cNode) {
1432 if (unbursted_channels)
1433 dict_remove(unbursted_channels, cNode->name);
1439 const char *str, *desc;
1440 int numnick, usermask, max_users;
1441 char numer[COMBO_NUMERIC_LEN+1];
1443 /* read config items */
1444 str = conf_get_data("server/ping_freq", RECDB_QSTRING);
1445 ping_freq = str ? ParseInterval(str) : 120;
1446 str = conf_get_data("server/ping_timeout", RECDB_QSTRING);
1447 ping_timeout = str ? ParseInterval(str) : 30;
1448 str = conf_get_data("server/force_n2k", RECDB_QSTRING);
1449 force_n2k = str ? enabled_string(str) : 1;
1450 str = conf_get_data("server/numeric", RECDB_QSTRING);
1452 log_module(MAIN_LOG, LOG_ERROR, "No server/numeric entry in config file.");
1455 numnick = atoi(str);
1456 str = conf_get_data("server/max_users", RECDB_QSTRING);
1457 max_users = str ? atoi(str) : 4096;
1458 for (usermask = 4; usermask < max_users; usermask <<= 1) ;
1460 if ((numnick < 64) && (usermask < 4096) && !force_n2k)
1461 inttobase64(numer, (numnick << 12) + (usermask & 0x00fff), 3);
1463 inttobase64(numer, (numnick << 18) + (usermask & 0x3ffff), 5);
1465 str = conf_get_data("server/his_servername", RECDB_QSTRING);
1466 his_servername = str ? strdup(str) : NULL;
1467 str = conf_get_data("server/his_servercomment", RECDB_QSTRING);
1468 his_servercomment = str ? strdup(str) : NULL;
1470 str = conf_get_data("server/hostname", RECDB_QSTRING);
1471 desc = conf_get_data("server/description", RECDB_QSTRING);
1472 if (!str || !desc) {
1473 log_module(MAIN_LOG, LOG_ERROR, "No server/hostname entry in config file.");
1476 self = AddServer(NULL, str, 0, boot_time, now, numer, desc);
1477 conf_register_reload(p10_conf_reload);
1479 irc_func_dict = dict_new();
1480 dict_insert(irc_func_dict, CMD_BURST, cmd_burst);
1481 dict_insert(irc_func_dict, TOK_BURST, cmd_burst);
1482 dict_insert(irc_func_dict, CMD_CREATE, cmd_create);
1483 dict_insert(irc_func_dict, TOK_CREATE, cmd_create);
1484 dict_insert(irc_func_dict, CMD_EOB, cmd_eob);
1485 dict_insert(irc_func_dict, TOK_EOB, cmd_eob);
1486 dict_insert(irc_func_dict, CMD_EOB_ACK, cmd_eob_ack);
1487 dict_insert(irc_func_dict, TOK_EOB_ACK, cmd_eob_ack);
1488 dict_insert(irc_func_dict, CMD_MODE, cmd_mode);
1489 dict_insert(irc_func_dict, TOK_MODE, cmd_mode);
1490 dict_insert(irc_func_dict, CMD_NICK, cmd_nick);
1491 dict_insert(irc_func_dict, TOK_NICK, cmd_nick);
1492 dict_insert(irc_func_dict, CMD_ACCOUNT, cmd_account);
1493 dict_insert(irc_func_dict, TOK_ACCOUNT, cmd_account);
1494 dict_insert(irc_func_dict, CMD_FAKEHOST, cmd_fakehost);
1495 dict_insert(irc_func_dict, TOK_FAKEHOST, cmd_fakehost);
1496 dict_insert(irc_func_dict, CMD_PASS, cmd_pass);
1497 dict_insert(irc_func_dict, TOK_PASS, cmd_pass);
1498 dict_insert(irc_func_dict, CMD_PING, cmd_ping);
1499 dict_insert(irc_func_dict, TOK_PING, cmd_ping);
1500 dict_insert(irc_func_dict, CMD_PRIVMSG, cmd_privmsg);
1501 dict_insert(irc_func_dict, TOK_PRIVMSG, cmd_privmsg);
1502 dict_insert(irc_func_dict, CMD_PONG, cmd_pong);
1503 dict_insert(irc_func_dict, TOK_PONG, cmd_pong);
1504 dict_insert(irc_func_dict, CMD_QUIT, cmd_quit);
1505 dict_insert(irc_func_dict, TOK_QUIT, cmd_quit);
1506 dict_insert(irc_func_dict, CMD_SERVER, cmd_server);
1507 dict_insert(irc_func_dict, TOK_SERVER, cmd_server);
1508 dict_insert(irc_func_dict, CMD_JOIN, cmd_join);
1509 dict_insert(irc_func_dict, TOK_JOIN, cmd_join);
1510 dict_insert(irc_func_dict, CMD_PART, cmd_part);
1511 dict_insert(irc_func_dict, TOK_PART, cmd_part);
1512 dict_insert(irc_func_dict, CMD_ERROR, cmd_error);
1513 dict_insert(irc_func_dict, TOK_ERROR, cmd_error);
1514 dict_insert(irc_func_dict, CMD_TOPIC, cmd_topic);
1515 dict_insert(irc_func_dict, TOK_TOPIC, cmd_topic);
1516 dict_insert(irc_func_dict, CMD_AWAY, cmd_away);
1517 dict_insert(irc_func_dict, TOK_AWAY, cmd_away);
1518 dict_insert(irc_func_dict, CMD_SILENCE, cmd_dummy);
1519 dict_insert(irc_func_dict, TOK_SILENCE, cmd_dummy);
1520 dict_insert(irc_func_dict, CMD_KICK, cmd_kick);
1521 dict_insert(irc_func_dict, TOK_KICK, cmd_kick);
1522 dict_insert(irc_func_dict, CMD_SQUIT, cmd_squit);
1523 dict_insert(irc_func_dict, TOK_SQUIT, cmd_squit);
1524 dict_insert(irc_func_dict, CMD_KILL, cmd_kill);
1525 dict_insert(irc_func_dict, TOK_KILL, cmd_kill);
1526 dict_insert(irc_func_dict, CMD_NOTICE, cmd_notice);
1527 dict_insert(irc_func_dict, TOK_NOTICE, cmd_notice);
1528 dict_insert(irc_func_dict, CMD_STATS, cmd_stats);
1529 dict_insert(irc_func_dict, TOK_STATS, cmd_stats);
1530 dict_insert(irc_func_dict, CMD_SVSNICK, cmd_svsnick);
1531 dict_insert(irc_func_dict, TOK_SVSNICK, cmd_svsnick);
1532 dict_insert(irc_func_dict, CMD_WHOIS, cmd_whois);
1533 dict_insert(irc_func_dict, TOK_WHOIS, cmd_whois);
1534 dict_insert(irc_func_dict, CMD_GLINE, cmd_gline);
1535 dict_insert(irc_func_dict, TOK_GLINE, cmd_gline);
1536 dict_insert(irc_func_dict, CMD_OPMODE, cmd_opmode);
1537 dict_insert(irc_func_dict, TOK_OPMODE, cmd_opmode);
1538 dict_insert(irc_func_dict, CMD_CLEARMODE, cmd_clearmode);
1539 dict_insert(irc_func_dict, TOK_CLEARMODE, cmd_clearmode);
1540 dict_insert(irc_func_dict, CMD_VERSION, cmd_version);
1541 dict_insert(irc_func_dict, TOK_VERSION, cmd_version);
1542 dict_insert(irc_func_dict, CMD_ADMIN, cmd_admin);
1543 dict_insert(irc_func_dict, TOK_ADMIN, cmd_admin);
1545 /* In P10, DESTRUCT doesn't do anything except be broadcast to servers.
1546 * Apparently to obliterate channels from any servers that think they
1549 dict_insert(irc_func_dict, CMD_DESTRUCT, cmd_dummy);
1550 dict_insert(irc_func_dict, TOK_DESTRUCT, cmd_dummy);
1551 /* Ignore invites */
1552 dict_insert(irc_func_dict, CMD_INVITE, cmd_dummy);
1553 dict_insert(irc_func_dict, TOK_INVITE, cmd_dummy);
1554 /* DESYNCH is just informational, so ignore it */
1555 dict_insert(irc_func_dict, CMD_DESYNCH, cmd_dummy);
1556 dict_insert(irc_func_dict, TOK_DESYNCH, cmd_dummy);
1557 /* Ignore channel operator notices. */
1558 dict_insert(irc_func_dict, CMD_WALLCHOPS, cmd_dummy);
1559 dict_insert(irc_func_dict, TOK_WALLCHOPS, cmd_dummy);
1560 dict_insert(irc_func_dict, CMD_WALLVOICES, cmd_dummy);
1561 dict_insert(irc_func_dict, TOK_WALLVOICES, cmd_dummy);
1562 /* Ignore opers being silly. */
1563 dict_insert(irc_func_dict, CMD_WALLOPS, cmd_dummy);
1564 dict_insert(irc_func_dict, TOK_WALLOPS, cmd_dummy);
1565 /* We have reliable clock! Always! Wraaa! */
1566 dict_insert(irc_func_dict, CMD_SETTIME, cmd_dummy);
1567 dict_insert(irc_func_dict, TOK_SETTIME, cmd_dummy);
1569 dict_insert(irc_func_dict, "331", cmd_num_topic);
1570 dict_insert(irc_func_dict, "332", cmd_num_topic);
1571 dict_insert(irc_func_dict, "333", cmd_num_topic);
1572 dict_insert(irc_func_dict, "345", cmd_dummy); /* blah has been invited to blah */
1573 dict_insert(irc_func_dict, "432", cmd_error_nick); /* Erroneus [sic] nickname */
1574 /* ban list resetting */
1575 /* "stats g" responses */
1576 dict_insert(irc_func_dict, "247", cmd_num_gline);
1577 dict_insert(irc_func_dict, "219", cmd_dummy); /* "End of /STATS report" */
1578 /* other numeric responses we might get */
1579 dict_insert(irc_func_dict, "401", cmd_dummy); /* target left network */
1580 dict_insert(irc_func_dict, "403", cmd_dummy); /* no such channel */
1581 dict_insert(irc_func_dict, "404", cmd_dummy); /* cannot send to channel */
1582 dict_insert(irc_func_dict, "439", cmd_dummy); /* target change too fast */
1583 dict_insert(irc_func_dict, "441", cmd_dummy); /* target isn't on that channel */
1584 dict_insert(irc_func_dict, "442", cmd_dummy); /* you aren't on that channel */
1585 dict_insert(irc_func_dict, "443", cmd_dummy); /* is already on channel (after invite?) */
1586 dict_insert(irc_func_dict, "461", cmd_dummy); /* Not enough parameters (after TOPIC w/ 0 args) */
1587 dict_insert(irc_func_dict, "467", cmd_dummy); /* Channel key already set */
1589 num_privmsg_funcs = 16;
1590 privmsg_funcs = malloc(sizeof(privmsg_func_t)*num_privmsg_funcs);
1591 memset(privmsg_funcs, 0, sizeof(privmsg_func_t)*num_privmsg_funcs);
1593 num_notice_funcs = 16;
1594 notice_funcs = malloc(sizeof(privmsg_func_t)*num_notice_funcs);
1595 memset(notice_funcs, 0, sizeof(privmsg_func_t)*num_notice_funcs);
1597 userList_init(&dead_users);
1598 reg_del_channel_func(remove_unbursted_channel);
1599 reg_exit_func(parse_cleanup);
1603 parse_line(char *line, int recursive)
1605 char *argv[MAXNUMPARAMS], *origin;
1606 int argc, cmd, res=0;
1609 argc = split_line(line, true, MAXNUMPARAMS, argv);
1610 cmd = self->uplink || !argv[0][1] || !argv[0][2];
1613 if (argv[0][0] == ':') {
1615 } else if (!argv[0][1] || !argv[0][2]) {
1616 struct server *sNode = GetServerN(argv[0]);
1617 origin = sNode ? sNode->name : 0;
1619 struct userNode *uNode = GetUserN(argv[0]);
1620 origin = uNode ? uNode->nick : 0;
1624 if ((func = dict_find(irc_func_dict, argv[cmd], NULL)))
1625 res = func(origin, argc-cmd, argv+cmd);
1628 log_module(MAIN_LOG, LOG_ERROR, "PARSE ERROR on line: %s", unsplit_string(argv, argc, NULL));
1629 } else if (!recursive) {
1631 for (i=0; i<dead_users.used; i++)
1632 free_user(dead_users.list[i]);
1633 dead_users.used = 0;
1639 parse_foreach(char *target_list, foreach_chanfunc cf, foreach_nonchan nc, foreach_userfunc uf, foreach_nonuser nu, void *data)
1644 while (*j != 0 && *j != ',')
1648 if (IsChannelName(target_list)
1649 || (target_list[0] == '0' && target_list[1] == '\0')) {
1650 struct chanNode *chan = GetChannel(target_list);
1656 nc(target_list, data);
1659 struct userNode *user;
1660 struct privmsg_desc *pd = data;
1662 pd->is_qualified = 0;
1663 if (*target_list == '@') {
1665 } else if (strchr(target_list, '@')) {
1666 struct server *server;
1668 pd->is_qualified = 1;
1669 user = GetUserH(strtok(target_list, "@"));
1670 server = GetServerH(strtok(NULL, "@"));
1672 if (user && (user->uplink != server)) {
1673 /* Don't attempt to index into any arrays
1674 using a user's numeric on another server. */
1678 user = GetUserN(target_list);
1686 nu(target_list, data);
1690 } while (old == ',');
1694 get_local_numeric(void)
1696 static unsigned int next_numeric = 0;
1697 if (self->clients > self->num_mask)
1699 while (self->users[next_numeric])
1700 if (++next_numeric > self->num_mask)
1702 return next_numeric;
1706 make_numeric(struct server *svr, int local_num, char *outbuf)
1710 if (force_n2k || svr->numeric[1]) {
1715 llen = (local_num < 64*64) ? 2 : 3;
1717 strncpy(outbuf, svr->numeric, slen);
1718 inttobase64(outbuf+slen, local_num, llen);
1719 outbuf[slen+llen] = 0;
1723 AddServer(struct server *uplink, const char *name, int hops, time_t boot, time_t link, const char *numeric, const char *description)
1725 struct server* sNode;
1728 if ((sNode = GetServerN(numeric))) {
1729 /* This means we're trying to re-add an existant server.
1730 * To be safe, we should forget the previous incarnation.
1731 * (And all its linked servers.)
1733 * It usually only happens in replays when the original
1734 * had a ping timeout and the replay didn't (because
1735 * replaying a ping timeout invariably gets things wrong).
1737 DelServer(sNode, 0, NULL);
1740 switch (strlen(numeric)) {
1741 case 5: slen = 2; mlen = 3; break;
1742 case 4: slen = 1; mlen = 3; break;
1743 case 3: slen = 1; mlen = 2; break;
1745 log_module(MAIN_LOG, LOG_ERROR, "AddServer(\"%s\", \"%s\", ...): Numeric %s has invalid length.", uplink->name, name, numeric);
1749 sNode = calloc(1, sizeof(*sNode));
1750 sNode->uplink = uplink;
1751 safestrncpy(sNode->name, name, sizeof(sNode->name));
1752 sNode->num_mask = base64toint(numeric+slen, mlen);
1756 strncpy(sNode->numeric, numeric, slen);
1757 safestrncpy(sNode->description, description, sizeof(sNode->description));
1758 sNode->users = calloc(sNode->num_mask+1, sizeof(*sNode->users));
1759 serverList_init(&sNode->children);
1760 if (sNode->uplink) {
1761 /* uplink may be NULL if we're just building ourself */
1762 serverList_append(&sNode->uplink->children, sNode);
1764 servers_num[base64toint(numeric, slen)] = sNode;
1765 dict_insert(servers, sNode->name, sNode);
1769 void DelServer(struct server* serv, int announce, const char *message)
1773 /* If we receive an ERROR command before the SERVER
1774 * command a NULL server can be passed */
1778 /* Hrm, what's the right way to SQUIT some other server?
1779 * (This code is only to handle killing juped servers.) */
1780 if (announce && (serv->uplink == self) && (serv != self->uplink))
1781 irc_squit(serv, message, NULL);
1783 /* must recursively remove servers linked to this one first */
1784 for (i=serv->children.used;i>0;)
1785 if (serv->children.list[--i] != self)
1786 DelServer(serv->children.list[i], false, NULL);
1788 /* clean up server's user hash tables */
1789 for (i=0;i<=serv->num_mask;i++)
1791 DelUser(serv->users[i], NULL, false, "server delinked");
1795 serverList_remove(&serv->uplink->children, serv);
1796 if (serv == self->uplink)
1797 self->uplink = NULL;
1798 servers_num[base64toint(serv->numeric, strlen(serv->numeric))] = NULL;
1799 dict_remove(servers, serv->name);
1800 serverList_clean(&serv->children);
1806 AddService(const char *nick, const char *modes, const char *desc, const char *hostname)
1808 char numeric[COMBO_NUMERIC_LEN+1];
1809 int local_num = get_local_numeric();
1810 time_t timestamp = now;
1811 struct userNode *old_user = GetUserH(nick);
1814 if (IsLocal(old_user))
1816 timestamp = old_user->timestamp - 1;
1818 if (local_num == -1) {
1819 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for service %s", nick);
1823 hostname = self->name;
1824 make_numeric(self, local_num, numeric);
1825 return AddUser(self, nick, nick, hostname, modes ? modes : "+oik", numeric, desc, now, "AAAAAA");
1829 AddClone(const char *nick, const char *ident, const char *hostname, const char *desc)
1831 char numeric[COMBO_NUMERIC_LEN+1];
1832 int local_num = get_local_numeric();
1833 time_t timestamp = now;
1834 struct userNode *old_user = GetUserH(nick);
1837 if (IsLocal(old_user))
1839 timestamp = old_user->timestamp - 1;
1841 if (local_num == -1) {
1842 log_module(MAIN_LOG, LOG_ERROR, "Unable to allocate numnick for clone %s", nick);
1845 make_numeric(self, local_num, numeric);
1846 return AddUser(self, nick, ident, hostname, "+i", numeric, desc, timestamp, "AAAAAA");
1850 is_valid_nick(const char *nick) {
1852 /* IRC has some of The Most Fucked-Up ideas about character sets
1854 if (!isalpha(*nick) && !strchr("{|}~[\\]^_`", *nick))
1856 for (ii = 0; nick[ii]; ++ii)
1857 if (!isalnum(nick[ii]) && !strchr("{|}~[\\]^-_`", nick[ii]))
1859 if (strlen(nick) > nicklen)
1864 static struct userNode*
1865 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)
1867 struct userNode *oldUser, *uNode;
1868 unsigned int n, ignore_user;
1870 if ((strlen(numeric) < 3) || (strlen(numeric) > 5)) {
1871 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): numeric %s wrong length!", uplink, nick, numeric);
1876 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s doesn't exist!", uplink, nick, numeric);
1880 if (uplink != GetServerN(numeric)) {
1881 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server for numeric %s differs from nominal uplink %s.", uplink, nick, numeric, uplink->name);
1885 if (!is_valid_nick(nick)) {
1886 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): invalid nickname detected.", uplink, nick);
1891 if ((oldUser = GetUserH(nick))) {
1892 if (IsLocal(oldUser) && (IsService(oldUser) || IsPersistent(oldUser))) {
1893 /* The service should collide the new user off. */
1894 oldUser->timestamp = timestamp - 1;
1897 if (oldUser->timestamp > timestamp) {
1898 /* "Old" user is really newer; remove them */
1899 DelUser(oldUser, 0, 1, "Overruled by older nick");
1901 /* User being added is too new; do not add them to
1902 * clients, but do add them to the server's list, since it
1903 * will send a KILL and QUIT soon. */
1908 /* create new usernode and set all values */
1909 uNode = calloc(1, sizeof(*uNode));
1910 uNode->nick = strdup(nick);
1911 safestrncpy(uNode->ident, ident, sizeof(uNode->ident));
1912 safestrncpy(uNode->info, userinfo, sizeof(uNode->info));
1913 safestrncpy(uNode->hostname, hostname, sizeof(uNode->hostname));
1914 safestrncpy(uNode->numeric, numeric, sizeof(uNode->numeric));
1915 uNode->ip.s_addr = htonl(base64toint(realip, 6));
1916 uNode->timestamp = timestamp;
1917 modeList_init(&uNode->channels);
1918 uNode->uplink = uplink;
1919 if (++uNode->uplink->clients > uNode->uplink->max_clients) {
1920 uNode->uplink->max_clients = uNode->uplink->clients;
1922 uNode->num_local = base64toint(numeric+strlen(uNode->uplink->numeric), 3) & uNode->uplink->num_mask;
1923 uNode->uplink->users[uNode->num_local] = uNode;
1924 mod_usermode(uNode, modes);
1928 dict_insert(clients, uNode->nick, uNode);
1929 if (dict_size(clients) > max_clients) {
1930 max_clients = dict_size(clients);
1931 max_clients_time = now;
1935 for (n=0; n<nuf_used; n++)
1936 if (nuf_list[n](uNode))
1941 /* removes user from it's server's hash table and nick hash table */
1943 DelUser(struct userNode* user, struct userNode *killer, int announce, const char *why)
1949 /* mark them as dead, in case anybody cares */
1952 /* remove user from all channels */
1953 while (user->channels.used > 0)
1954 DelChannelUser(user, user->channels.list[user->channels.used-1]->channel, false, 0);
1956 /* Call these in reverse order so ChanServ can update presence
1957 information before NickServ nukes the handle_info. */
1958 for (n = duf_used; n > 0; )
1959 duf_list[--n](user, killer, why);
1961 user->uplink->clients--;
1962 user->uplink->users[user->num_local] = NULL;
1964 userList_remove(&curr_opers, user);
1965 /* remove from global dictionary, but not if after a collide */
1966 if (user == dict_find(clients, user->nick, NULL))
1967 dict_remove(clients, user->nick);
1969 if (IsInvisible(user))
1974 irc_quit(user, why);
1976 irc_kill(killer, user, why);
1979 modeList_clean(&user->channels);
1980 /* We don't free them, in case we try to privmsg them or something
1981 * (like when a stupid oper kills themself). We just put them onto
1982 * a list of clients that get freed after processing each line.
1984 if (dead_users.size)
1985 userList_append(&dead_users, user);
1990 void mod_usermode(struct userNode *user, const char *mode_change) {
1991 static void call_oper_funcs(struct userNode *user);
1993 const char *word = mode_change;
1995 if (!user || !mode_change)
1997 while (*word != ' ' && *word) word++;
1998 while (*word == ' ') word++;
2000 #define do_user_mode(FLAG) do { if (add) user->modes |= FLAG; else user->modes &= ~FLAG; } while (0)
2001 switch (*mode_change++) {
2002 case 0: case ' ': return;
2003 case '+': add = 1; break;
2004 case '-': add = 0; break;
2006 do_user_mode(FLAGS_OPER);
2008 userList_append(&curr_opers, user);
2009 call_oper_funcs(user);
2011 userList_remove(&curr_opers, user);
2014 case 'O': do_user_mode(FLAGS_LOCOP); break;
2015 case 'i': do_user_mode(FLAGS_INVISIBLE);
2021 case 'w': do_user_mode(FLAGS_WALLOP); break;
2022 case 's': do_user_mode(FLAGS_SERVNOTICE); break;
2023 case 'd': do_user_mode(FLAGS_DEAF); break;
2024 case 'k': do_user_mode(FLAGS_SERVICE); break;
2025 case 'g': do_user_mode(FLAGS_GLOBAL); break;
2026 case 'h': do_user_mode(FLAGS_HELPER); break;
2027 case 'x': do_user_mode(FLAGS_HIDDEN_HOST); break;
2032 for (ii=0; (*word != ' ') && (*word != '\0'); )
2033 tag[ii++] = *word++;
2035 while (*word == ' ')
2037 call_account_func(user, tag);
2044 for (ii=0; (*word != ' ') && (*word != '\0'); )
2045 host[ii++] = *word++;
2047 while (*word == ' ')
2049 assign_fakehost(user, host, 0);
2057 struct mod_chanmode *
2058 mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, unsigned int flags)
2060 struct mod_chanmode *change;
2061 unsigned int ii, in_arg, ch_arg, add;
2065 if (!(change = mod_chanmode_alloc(argc - 1)))
2068 for (ii = ch_arg = 0, in_arg = add = 1;
2069 (modes[0][ii] != '\0') && (modes[0][ii] != ' ');
2071 switch (modes[0][ii]) {
2078 #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)
2079 case 'C': do_chan_mode(MODE_NOCTCPS); break;
2080 case 'D': do_chan_mode(MODE_DELAYJOINS); break;
2081 case 'c': do_chan_mode(MODE_NOCOLORS); break;
2082 case 'i': do_chan_mode(MODE_INVITEONLY); break;
2083 case 'm': do_chan_mode(MODE_MODERATED); break;
2084 case 'n': do_chan_mode(MODE_NOPRIVMSGS); break;
2085 case 'p': do_chan_mode(MODE_PRIVATE); break;
2086 case 'r': do_chan_mode(MODE_REGONLY); break;
2087 case 's': do_chan_mode(MODE_SECRET); break;
2088 case 't': do_chan_mode(MODE_TOPICLIMIT); break;
2090 if (!(flags & MCP_REGISTERED)) {
2091 do_chan_mode(MODE_REGISTERED);
2093 mod_chanmode_free(change);
2102 change->modes_set |= MODE_LIMIT;
2103 change->new_limit = atoi(modes[in_arg++]);
2105 change->modes_set &= ~MODE_LIMIT;
2106 change->modes_clear |= MODE_LIMIT;
2113 change->modes_set |= MODE_KEY;
2114 safestrncpy(change->new_key, modes[in_arg++], sizeof(change->new_key));
2116 change->modes_clear |= MODE_KEY;
2117 if (!(flags & MCP_KEY_FREE)) {
2125 if (!(flags & MCP_ALLOW_OVB))
2129 change->args[ch_arg].mode = MODE_BAN;
2131 change->args[ch_arg].mode |= MODE_REMOVE;
2132 change->args[ch_arg++].u.hostmask = modes[in_arg++];
2136 struct userNode *victim;
2137 if (!(flags & MCP_ALLOW_OVB))
2141 change->args[ch_arg].mode = (modes[0][ii] == 'o') ? MODE_CHANOP : MODE_VOICE;
2143 change->args[ch_arg].mode |= MODE_REMOVE;
2144 if (flags & MCP_FROM_SERVER)
2145 victim = GetUserN(modes[in_arg++]);
2147 victim = GetUserH(modes[in_arg++]);
2150 if ((change->args[ch_arg].u.member = GetUserMode(channel, victim)))
2155 if (!(flags & MCP_FROM_SERVER))
2160 change->argc = ch_arg; /* in case any turned out to be ignored */
2161 if (change->modes_set & MODE_SECRET) {
2162 change->modes_set &= ~(MODE_PRIVATE);
2163 change->modes_clear |= MODE_PRIVATE;
2164 } else if (change->modes_set & MODE_PRIVATE) {
2165 change->modes_set &= ~(MODE_SECRET);
2166 change->modes_clear |= MODE_SECRET;
2170 mod_chanmode_free(change);
2174 struct chanmode_buffer {
2177 struct chanNode *channel;
2178 struct userNode *actor;
2179 unsigned int modes_used;
2180 unsigned int args_used;
2182 unsigned int is_add : 1;
2183 unsigned int is_chanop : 1;
2187 mod_chanmode_append(struct chanmode_buffer *buf, char ch, const char *arg)
2189 size_t arg_len = strlen(arg);
2190 if (buf->modes_used > (MAXMODEPARAMS) ||
2191 buf->modes_used + buf->args_used + buf->chname_len + arg_len > 450) {
2192 memcpy(buf->modes + buf->modes_used, buf->args, buf->args_used);
2193 buf->modes[buf->modes_used + buf->args_used] = '\0';
2194 irc_mode((buf->is_chanop ? buf->actor : NULL), buf->channel, buf->modes);
2195 buf->modes[0] = buf->is_add ? '+' : '-';
2196 buf->modes_used = 1;
2199 buf->modes[buf->modes_used++] = ch;
2200 buf->args[buf->args_used++] = ' ';
2201 memcpy(buf->args + buf->args_used, arg, arg_len);
2202 buf->args_used += arg_len;
2206 mod_chanmode_announce(struct userNode *who, struct chanNode *channel, struct mod_chanmode *change)
2208 struct chanmode_buffer chbuf;
2210 struct modeNode *mn;
2211 char int_buff[32], mode = '\0';
2213 assert(change->argc <= change->alloc_argc);
2214 memset(&chbuf, 0, sizeof(chbuf));
2215 chbuf.channel = channel;
2217 chbuf.chname_len = strlen(channel->name);
2219 mn = GetUserMode(channel, who);
2220 if ((mn && (mn->modes & MODE_CHANOP)) || off_channel)
2221 chbuf.is_chanop = 1;
2223 /* First remove modes */
2225 if (change->modes_clear) {
2227 chbuf.modes[chbuf.modes_used++] = mode = '-';
2228 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
2229 DO_MODE_CHAR(PRIVATE, 'p');
2230 DO_MODE_CHAR(SECRET, 's');
2231 DO_MODE_CHAR(MODERATED, 'm');
2232 DO_MODE_CHAR(TOPICLIMIT, 't');
2233 DO_MODE_CHAR(INVITEONLY, 'i');
2234 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2235 DO_MODE_CHAR(LIMIT, 'l');
2236 DO_MODE_CHAR(DELAYJOINS, 'D');
2237 DO_MODE_CHAR(REGONLY, 'r');
2238 DO_MODE_CHAR(NOCOLORS, 'c');
2239 DO_MODE_CHAR(NOCTCPS, 'C');
2240 DO_MODE_CHAR(REGISTERED, 'z');
2242 if (change->modes_clear & channel->modes & MODE_KEY)
2243 mod_chanmode_append(&chbuf, 'k', channel->key);
2245 for (arg = 0; arg < change->argc; ++arg) {
2246 if (!(change->args[arg].mode & MODE_REMOVE))
2249 chbuf.modes[chbuf.modes_used++] = mode = '-';
2250 switch (change->args[arg].mode & ~MODE_REMOVE) {
2252 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
2255 if (change->args[arg].mode & MODE_CHANOP)
2256 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
2257 if (change->args[arg].mode & MODE_VOICE)
2258 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
2265 if (change->modes_set) {
2267 chbuf.modes[chbuf.modes_used++] = mode = '+';
2268 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR
2269 DO_MODE_CHAR(PRIVATE, 'p');
2270 DO_MODE_CHAR(SECRET, 's');
2271 DO_MODE_CHAR(MODERATED, 'm');
2272 DO_MODE_CHAR(TOPICLIMIT, 't');
2273 DO_MODE_CHAR(INVITEONLY, 'i');
2274 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2275 DO_MODE_CHAR(DELAYJOINS, 'D');
2276 DO_MODE_CHAR(REGONLY, 'r');
2277 DO_MODE_CHAR(NOCOLORS, 'c');
2278 DO_MODE_CHAR(NOCTCPS, 'C');
2279 DO_MODE_CHAR(REGISTERED, 'z');
2281 if(change->modes_set & MODE_KEY)
2282 mod_chanmode_append(&chbuf, 'k', change->new_key);
2283 if(change->modes_set & MODE_LIMIT) {
2284 sprintf(int_buff, "%d", change->new_limit);
2285 mod_chanmode_append(&chbuf, 'l', int_buff);
2288 for (arg = 0; arg < change->argc; ++arg) {
2289 if (change->args[arg].mode & MODE_REMOVE)
2292 chbuf.modes[chbuf.modes_used++] = mode = '+';
2293 switch (change->args[arg].mode) {
2295 mod_chanmode_append(&chbuf, 'b', change->args[arg].u.hostmask);
2298 if (change->args[arg].mode & MODE_CHANOP)
2299 mod_chanmode_append(&chbuf, 'o', change->args[arg].u.member->user->numeric);
2300 if (change->args[arg].mode & MODE_VOICE)
2301 mod_chanmode_append(&chbuf, 'v', change->args[arg].u.member->user->numeric);
2306 /* Flush the buffer and apply changes locally */
2307 if (chbuf.modes_used > 0) {
2308 memcpy(chbuf.modes + chbuf.modes_used, chbuf.args, chbuf.args_used);
2309 chbuf.modes[chbuf.modes_used + chbuf.args_used] = '\0';
2310 irc_mode((chbuf.is_chanop ? chbuf.actor : NULL), chbuf.channel, chbuf.modes);
2312 mod_chanmode_apply(who, channel, change);
2316 mod_chanmode_format(struct mod_chanmode *change, char *outbuff)
2318 unsigned int used = 0;
2319 assert(change->argc <= change->alloc_argc);
2320 if (change->modes_clear) {
2321 outbuff[used++] = '-';
2322 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) outbuff[used++] = CHAR
2323 DO_MODE_CHAR(PRIVATE, 'p');
2324 DO_MODE_CHAR(SECRET, 's');
2325 DO_MODE_CHAR(MODERATED, 'm');
2326 DO_MODE_CHAR(TOPICLIMIT, 't');
2327 DO_MODE_CHAR(INVITEONLY, 'i');
2328 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2329 DO_MODE_CHAR(LIMIT, 'l');
2330 DO_MODE_CHAR(KEY, 'k');
2331 DO_MODE_CHAR(DELAYJOINS, 'D');
2332 DO_MODE_CHAR(REGONLY, 'r');
2333 DO_MODE_CHAR(NOCOLORS, 'c');
2334 DO_MODE_CHAR(NOCTCPS, 'C');
2335 DO_MODE_CHAR(REGISTERED, 'z');
2338 if (change->modes_set) {
2339 outbuff[used++] = '+';
2340 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) outbuff[used++] = CHAR
2341 DO_MODE_CHAR(PRIVATE, 'p');
2342 DO_MODE_CHAR(SECRET, 's');
2343 DO_MODE_CHAR(MODERATED, 'm');
2344 DO_MODE_CHAR(TOPICLIMIT, 't');
2345 DO_MODE_CHAR(INVITEONLY, 'i');
2346 DO_MODE_CHAR(NOPRIVMSGS, 'n');
2347 DO_MODE_CHAR(DELAYJOINS, 'D');
2348 DO_MODE_CHAR(REGONLY, 'r');
2349 DO_MODE_CHAR(NOCOLORS, 'c');
2350 DO_MODE_CHAR(NOCTCPS, 'C');
2351 DO_MODE_CHAR(REGISTERED, 'z');
2353 switch (change->modes_set & (MODE_KEY|MODE_LIMIT)) {
2354 case MODE_KEY|MODE_LIMIT:
2355 used += sprintf(outbuff+used, "lk %d %s", change->new_limit, change->new_key);
2358 used += sprintf(outbuff+used, "k %s", change->new_key);
2361 used += sprintf(outbuff+used, "l %d", change->new_limit);
2370 clear_chanmode(struct chanNode *channel, const char *modes)
2372 unsigned int remove;
2374 for (remove = 0; *modes; modes++) {
2376 case 'o': remove |= MODE_CHANOP; break;
2377 case 'v': remove |= MODE_VOICE; break;
2378 case 'p': remove |= MODE_PRIVATE; break;
2379 case 's': remove |= MODE_SECRET; break;
2380 case 'm': remove |= MODE_MODERATED; break;
2381 case 't': remove |= MODE_TOPICLIMIT; break;
2382 case 'i': remove |= MODE_INVITEONLY; break;
2383 case 'n': remove |= MODE_NOPRIVMSGS; break;
2386 channel->key[0] = '\0';
2389 remove |= MODE_LIMIT;
2392 case 'b': remove |= MODE_BAN; break;
2393 case 'D': remove |= MODE_DELAYJOINS; break;
2394 case 'r': remove |= MODE_REGONLY; break;
2395 case 'c': remove |= MODE_NOCOLORS; break;
2396 case 'C': remove |= MODE_NOCTCPS; break;
2397 case 'z': remove |= MODE_REGISTERED; break;
2404 /* Remove simple modes. */
2405 channel->modes &= ~remove;
2407 /* If removing bans, kill 'em all. */
2408 if ((remove & MODE_BAN) && channel->banlist.used) {
2410 for (i=0; i<channel->banlist.used; i++)
2411 free(channel->banlist.list[i]);
2412 channel->banlist.used = 0;
2415 /* Remove member modes. */
2416 if ((remove & (MODE_CHANOP | MODE_VOICE)) && channel->members.used) {
2417 int mask = ~(remove & (MODE_CHANOP | MODE_VOICE));
2420 for (i = 0; i < channel->members.used; i++)
2421 channel->members.list[i]->modes &= mask;
2428 reg_privmsg_func(struct userNode *user, privmsg_func_t handler)
2430 unsigned int numeric = user->num_local;
2431 if (numeric >= num_privmsg_funcs) {
2432 int newnum = numeric + 8;
2433 privmsg_funcs = realloc(privmsg_funcs, newnum*sizeof(privmsg_func_t));
2434 memset(privmsg_funcs+num_privmsg_funcs, 0, (newnum-num_privmsg_funcs)*sizeof(privmsg_func_t));
2435 num_privmsg_funcs = newnum;
2437 if (privmsg_funcs[numeric])
2438 log_module(MAIN_LOG, LOG_WARNING, "re-registering new privmsg handler for numeric %d", numeric);
2439 privmsg_funcs[numeric] = handler;
2443 unreg_privmsg_func(struct userNode *user, privmsg_func_t handler)
2447 if (!user || handler)
2448 return; /* this really only works with users */
2450 memset(privmsg_funcs+user->num_local, 0, sizeof(privmsg_func_t));
2452 for (x = user->num_local+1; x < num_privmsg_funcs; x++)
2453 memmove(privmsg_funcs+x-1, privmsg_funcs+x, sizeof(privmsg_func_t));
2455 privmsg_funcs = realloc(privmsg_funcs, num_privmsg_funcs*sizeof(privmsg_func_t));
2456 num_privmsg_funcs--;
2461 reg_notice_func(struct userNode *user, privmsg_func_t handler)
2463 unsigned int numeric = user->num_local;
2464 if (numeric >= num_notice_funcs) {
2465 int newnum = numeric + 8;
2466 notice_funcs = realloc(notice_funcs, newnum*sizeof(privmsg_func_t));
2467 memset(notice_funcs+num_notice_funcs, 0, (newnum-num_notice_funcs)*sizeof(privmsg_func_t));
2468 num_notice_funcs = newnum;
2470 if (notice_funcs[numeric])
2471 log_module(MAIN_LOG, LOG_WARNING, "re-registering new notice handler for numeric %d", numeric);
2472 notice_funcs[numeric] = handler;
2476 unreg_notice_func(struct userNode *user, privmsg_func_t handler)
2480 if (!user || handler)
2481 return; /* this really only works with users */
2483 memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t));
2485 for (x = user->num_local+1; x < num_notice_funcs; x++)
2486 memmove(notice_funcs+x-1, notice_funcs+x, sizeof(privmsg_func_t));
2488 memset(notice_funcs+user->num_local, 0, sizeof(privmsg_func_t));
2489 notice_funcs = realloc(notice_funcs, num_notice_funcs*sizeof(privmsg_func_t));
2494 reg_oper_func(oper_func_t handler)
2496 if (of_used == of_size) {
2499 of_list = realloc(of_list, of_size*sizeof(oper_func_t));
2502 of_list = malloc(of_size*sizeof(oper_func_t));
2505 of_list[of_used++] = handler;
2509 call_oper_funcs(struct userNode *user)
2514 for (n=0; n<of_used; n++)
2521 unsigned int i, hop, max_hop=1;
2524 /* burst (juped) servers, closest first (except self, which is sent already) */
2525 for (i=0; i<ArrayLength(servers_num); i++)
2526 if (servers_num[i] && servers_num[i]->hops > max_hop)
2527 max_hop = servers_num[i]->hops;
2528 for (hop=1; hop<=max_hop; hop++) {
2529 for (i=0;i<ArrayLength(servers_num);i++) {
2531 && (servers_num[i]->hops == hop)
2532 && (servers_num[i] != self->uplink))
2533 irc_server(servers_num[i]);
2537 /* burst local nicks */
2538 for (i=0; i<=self->num_mask; i++)
2540 irc_user(self->users[i]);
2542 /* build dict of unbursted channel names (just copy existing channels) */
2543 unbursted_channels = dict_new();
2544 for (it = dict_first(channels); it; it = iter_next(it))
2545 dict_insert(unbursted_channels, iter_key(it), iter_data(it));