1 /* proto-bahamut.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"
23 #define CAPAB_TS3 0x01
24 #define CAPAB_NOQUIT 0x02
25 #define CAPAB_SSJOIN 0x04
26 #define CAPAB_BURST 0x08
27 #define CAPAB_UNCONNECT 0x10
28 #define CAPAB_NICKIP 0x20
29 #define CAPAB_TSMODE 0x40
30 #define CAPAB_ZIP 0x80
32 struct service_message_info {
33 privmsg_func_t on_privmsg;
34 privmsg_func_t on_notice;
37 static dict_t service_msginfo_dict; /* holds service_message_info structs */
38 static int uplink_capab;
39 static void privmsg_user_helper(struct userNode *un, void *data);
41 void irc_svsmode(struct userNode *target, char *modes, unsigned long stamp);
44 AddServer(struct server *uplink, const char *name, int hops, time_t boot, time_t link, UNUSED_ARG(const char *numeric), const char *description) {
47 sNode = calloc(1, sizeof(*sNode));
48 sNode->uplink = uplink;
49 safestrncpy(sNode->name, name, sizeof(sNode->name));
53 sNode->users = dict_new();
54 safestrncpy(sNode->description, description, sizeof(sNode->description));
55 serverList_init(&sNode->children);
57 /* uplink may be NULL if we're just building ourself */
58 serverList_append(&sNode->uplink->children, sNode);
60 dict_insert(servers, sNode->name, sNode);
62 if (hops && !self->burst) {
64 for (n=0; n<slf_used; n++) {
73 DelServer(struct server* serv, int announce, const char *message) {
75 dict_iterator_t it, next;
78 if (announce && (serv->uplink == self) && (serv != self->uplink)) {
79 irc_squit(serv, message, NULL);
81 for (nn=serv->children.used; nn>0;) {
82 if (serv->children.list[--nn] != self) {
83 DelServer(serv->children.list[nn], false, "uplink delinking");
86 for (it=dict_first(serv->users); it; it=next) {
88 DelUser(iter_data(it), NULL, false, "server delinking");
90 if (serv->uplink) serverList_remove(&serv->uplink->children, serv);
91 if (serv == self->uplink) self->uplink = NULL;
92 dict_remove(servers, serv->name);
93 serverList_clean(&serv->children);
94 dict_delete(serv->users);
99 is_valid_nick(const char *nick) {
100 /* IRC has some of The Most Fucked-Up ideas about character sets
102 if ((*nick < 'A') || (*nick >= '~')) return 0;
103 for (++nick; *nick; ++nick) {
104 if (!((*nick >= 'A') && (*nick < '~'))
110 if (strlen(nick) > nicklen) return 0;
115 AddUser(struct server* uplink, const char *nick, const char *ident, const char *hostname, const char *modes, const char *userinfo, time_t timestamp, struct in_addr realip, const char *stamp) {
116 struct userNode *uNode, *oldUser;
120 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): server does not exist!", uplink, nick);
124 if (!is_valid_nick(nick)) {
125 log_module(MAIN_LOG, LOG_WARNING, "AddUser(%p, %s, ...): invalid nickname detected.", uplink, nick);
129 if ((oldUser = GetUserH(nick))) {
130 if (IsLocal(oldUser) && IsService(oldUser)) {
131 /* The service should collide the new user off. */
132 oldUser->timestamp = timestamp - 1;
135 if (oldUser->timestamp > timestamp) {
136 /* "Old" user is really newer; remove them */
137 DelUser(oldUser, 0, 1, "Overruled by older nick");
139 /* User being added is too new */
144 uNode = calloc(1, sizeof(*uNode));
145 uNode->nick = strdup(nick);
146 safestrncpy(uNode->ident, ident, sizeof(uNode->ident));
147 safestrncpy(uNode->info, userinfo, sizeof(uNode->info));
148 safestrncpy(uNode->hostname, hostname, sizeof(uNode->hostname));
150 uNode->timestamp = timestamp;
151 modeList_init(&uNode->channels);
152 uNode->uplink = uplink;
153 dict_insert(uplink->users, uNode->nick, uNode);
154 if (++uNode->uplink->clients > uNode->uplink->max_clients) {
155 uNode->uplink->max_clients = uNode->uplink->clients;
158 dict_insert(clients, uNode->nick, uNode);
159 if (dict_size(clients) > max_clients) {
160 max_clients = dict_size(clients);
161 max_clients_time = now;
164 mod_usermode(uNode, modes);
165 if (stamp) call_account_func(uNode, stamp);
166 if (IsLocal(uNode)) irc_user(uNode);
167 for (nn=0; nn<nuf_used; nn++) {
168 if (nuf_list[nn](uNode)) break;
174 AddService(const char *nick, const char *desc, const char *hostname) {
175 time_t timestamp = now;
176 struct userNode *old_user = GetUserH(nick);
177 struct in_addr ipaddr = { INADDR_LOOPBACK };
179 if (IsLocal(old_user))
181 timestamp = old_user->timestamp - 1;
184 hostname = self->name;
185 return AddUser(self, nick, nick, hostname, "+oikr", desc, timestamp, ipaddr, 0);
189 AddClone(const char *nick, const char *ident, const char *hostname, const char *desc) {
190 time_t timestamp = now;
191 struct userNode *old_user = GetUserH(nick);
192 struct in_addr ipaddr = { INADDR_LOOPBACK };
194 if (IsLocal(old_user))
196 timestamp = old_user->timestamp - 1;
198 return AddUser(self, nick, ident, hostname, "+ir", desc, timestamp, ipaddr, 0);
202 free_user(struct userNode *user)
209 DelUser(struct userNode* user, struct userNode *killer, int announce, const char *why) {
212 for (nn=user->channels.used; nn>0;) {
213 DelChannelUser(user, user->channels.list[--nn]->channel, false, 0);
215 for (nn=duf_used; nn>0; ) duf_list[--nn](user, killer, why);
216 user->uplink->clients--;
217 dict_remove(user->uplink->users, user->nick);
218 if (IsOper(user)) userList_remove(&curr_opers, user);
219 if (IsInvisible(user)) invis_clients--;
220 if (user == dict_find(clients, user->nick, NULL)) dict_remove(clients, user->nick);
225 irc_kill(killer, user, why);
228 modeList_clean(&user->channels);
230 if (dead_users.size) {
231 userList_append(&dead_users, user);
238 irc_server(struct server *srv) {
240 putsock("SERVER %s %d :%s", srv->name, srv->hops, srv->description);
242 putsock(":%s SERVER %s %d :%s", self->name, srv->name, srv->hops, srv->description);
247 irc_user(struct userNode *user) {
250 if (IsOper(user)) modes[modelen++] = 'o';
251 if (IsInvisible(user)) modes[modelen++] = 'i';
252 if (IsWallOp(user)) modes[modelen++] = 'w';
253 if (IsService(user)) modes[modelen++] = 'k';
254 if (IsServNotice(user)) modes[modelen++] = 's';
255 if (IsDeaf(user)) modes[modelen++] = 'd';
256 if (IsReggedNick(user)) modes[modelen++] = 'r';
257 if (IsGlobal(user)) modes[modelen++] = 'g';
259 putsock("NICK %s %d "FMT_TIME_T" +%s %s %s %s %d %u :%s",
260 user->nick, user->uplink->hops+2, user->timestamp, modes,
261 user->ident, user->hostname, user->uplink->name, 0, ntohl(user->ip.s_addr), user->info);
265 irc_account(struct userNode *user, const char *stamp)
267 if (IsReggedNick(user)) {
268 irc_svsmode(user, "+rd", base64toint(stamp, IDLEN));
270 irc_svsmode(user, "+d", base64toint(stamp, IDLEN));
275 irc_regnick(struct userNode *user)
277 if (IsReggedNick(user)) {
278 irc_svsmode(user, "+r", 0);
280 irc_svsmode(user, "-r", 0);
285 irc_nick(struct userNode *user, const char *old_nick) {
286 if (user->uplink == self) {
287 /* update entries in PRIVMSG/NOTICE handlers (if they exist) */
288 struct service_message_info *smi = dict_find(service_msginfo_dict, user->nick, NULL);
290 dict_remove2(service_msginfo_dict, old_nick, 1);
291 dict_insert(service_msginfo_dict, user->nick, smi);
294 putsock(":%s NICK %s :"FMT_TIME_T, old_nick, user->nick, user->timestamp);
298 irc_pass(const char *passwd) {
299 putsock("PASS %s :TS", passwd);
304 putsock("CAPAB TS3 NOQUIT SSJOIN BURST UNCONNECT NICKIP TSMODE");
309 putsock("SVINFO 3 3 0 :"FMT_TIME_T, now);
318 irc_introduce(const char *passwd) {
319 extern time_t burst_begin;
327 timeq_add(now + ping_freq, timed_send_ping, 0);
331 irc_ping(const char *something) {
332 putsock("PING :%s", something);
336 irc_pong(const char *who, const char *data) {
337 putsock(":%s PONG %s :%s", self->name, who, data);
341 irc_quit(struct userNode *user, const char *message) {
342 putsock(":%s QUIT :%s", user->nick, message);
346 irc_squit(struct server *srv, const char *message, const char *service_message) {
347 if (!service_message) service_message = message;
348 /* If we're leaving the network, QUIT all our clients. */
349 if ((srv == self) && (cManager.uplink->state == CONNECTED)) {
351 for (it = dict_first(srv->users); it; it = iter_next(it)) {
352 irc_quit(iter_data(it), service_message);
355 putsock(":%s SQUIT %s 0 :%s", self->name, srv->name, message);
357 /* Reconnect to the currently selected server. */
358 cManager.uplink->tries = 0;
359 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", message);
365 irc_privmsg(struct userNode *from, const char *to, const char *message) {
366 putsock(":%s PRIVMSG %s :%s", from->nick, to, message);
370 irc_notice(struct userNode *from, const char *to, const char *message) {
371 putsock(":%s NOTICE %s :%s", from->nick, to, message);
375 irc_notice_user(struct userNode *from, struct userNode *to, const char *message) {
376 putsock(":%s NOTICE %s :%s", from->nick, to->nick, message);
380 irc_wallchops(UNUSED_ARG(struct userNode *from), UNUSED_ARG(const char *to), UNUSED_ARG(const char *message)) {
384 irc_join(struct userNode *who, struct chanNode *what) {
385 if (what->members.used == 1) {
386 putsock(":%s SJOIN "FMT_TIME_T" %s + :@%s", self->name, what->timestamp, what->name, who->nick);
388 putsock(":%s SJOIN "FMT_TIME_T" %s", who->nick, what->timestamp, what->name);
393 irc_invite(struct userNode *from, struct userNode *who, struct chanNode *to) {
394 putsock(":%s INVITE %s %s", from->nick, who->nick, to->name);
398 irc_mode(struct userNode *who, struct chanNode *target, const char *modes) {
399 putsock(":%s MODE %s "FMT_TIME_T" %s", who->nick, target->name, target->timestamp, modes);
403 irc_svsmode(struct userNode *target, char *modes, unsigned long stamp) {
404 extern struct userNode *nickserv;
406 putsock(":%s SVSMODE %s "FMT_TIME_T" %s %lu", nickserv->nick, target->nick, target->timestamp, modes, stamp);
408 putsock(":%s SVSMODE %s "FMT_TIME_T" %s", nickserv->nick, target->nick, target->timestamp, modes);
413 irc_kick(struct userNode *who, struct userNode *target, struct chanNode *from, const char *msg) {
414 putsock(":%s KICK %s %s :%s", who->nick, from->name, target->nick, msg);
415 ChannelUserKicked(who, target, from);
419 irc_part(struct userNode *who, struct chanNode *what, const char *reason) {
421 putsock(":%s PART %s :%s", who->nick, what->name, reason);
423 putsock(":%s PART %s", who->nick, what->name);
428 irc_topic(struct userNode *who, struct chanNode *what, const char *topic) {
429 putsock(":%s TOPIC %s :%s", who->nick, what->name, topic);
433 irc_fetchtopic(struct userNode *from, const char *to) {
434 if (!from || !to) return;
435 putsock(":%s TOPIC %s", from->nick, to);
439 irc_gline(struct server *srv, struct gline *gline) {
440 char host[HOSTLEN+1], ident[USERLEN+1], *sep;
443 log_module(MAIN_LOG, LOG_WARNING, "%s tried to send a targeted G-line for %s (not supported by protocol!)", gline->issuer, gline->target);
446 if (!(sep = strchr(gline->target, '@'))) {
447 log_module(MAIN_LOG, LOG_ERROR, "%s tried to add G-line with bad mask %s", gline->issuer, gline->target);
450 len = sep - gline->target + 1;
451 if (len > ArrayLength(ident)) len = ArrayLength(ident);
452 safestrncpy(ident, gline->target, len);
453 safestrncpy(host, sep+1, ArrayLength(host));
454 putsock(":%s AKILL %s %s "FMT_TIME_T" %s "FMT_TIME_T" :%s", self->name, host, ident, gline->expires-gline->issued, gline->issuer, gline->issued, gline->reason);
458 irc_settime(UNUSED_ARG(const char *srv_name_mask), UNUSED_ARG(time_t new_time))
460 /* Bahamut has nothing like this, so ignore it. */
464 irc_ungline(const char *mask) {
465 char host[HOSTLEN+1], ident[USERLEN+1], *sep;
467 if (!(sep = strchr(mask, '@'))) {
468 log_module(MAIN_LOG, LOG_ERROR, "Tried to remove G-line with bad mask %s", mask);
471 len = sep - mask + 1;
472 if (len > ArrayLength(ident)) len = ArrayLength(ident);
473 safestrncpy(ident, mask, len);
474 safestrncpy(host, sep+1, ArrayLength(host));
475 putsock(":%s RAKILL %s %s", self->name, host, ident);
479 irc_error(const char *to, const char *message) {
481 putsock("%s ERROR :%s", to, message);
483 putsock(":%s ERROR :%s", self->name, message);
488 irc_kill(struct userNode *from, struct userNode *target, const char *message) {
490 putsock(":%s KILL %s :%s!%s (%s)", from->nick, target->nick, self->name, from->nick, message);
492 putsock(":%s KILL %s :%s (%s)", self->name, target->nick, self->name, message);
497 irc_raw(const char *what) {
502 irc_stats(struct userNode *from, struct server *target, char type) {
503 putsock(":%s STATS %c :%s", from->nick, type, target->name);
507 irc_svsnick(struct userNode *from, struct userNode *target, const char *newnick)
509 putsock(":%s SVSNICK %s %s :"FMT_TIME_T, from->nick, target->nick, newnick, now);
513 irc_numeric(struct userNode *user, unsigned int num, const char *format, ...) {
516 va_start(arg_list, format);
517 vsnprintf(buffer, MAXLEN-2, format, arg_list);
518 buffer[MAXLEN-1] = 0;
519 putsock(":%s %03d %s %s", self->name, num, user->nick, buffer);
523 parse_foreach(char *target_list, foreach_chanfunc cf, foreach_nonchan nc, foreach_userfunc uf, foreach_nonuser nu, void *data) {
527 while (*j != 0 && *j != ',') j++;
530 if (IsChannelName(target_list)) {
531 struct chanNode *chan = GetChannel(target_list);
533 if (cf) cf(chan, data);
535 if (nc) nc(target_list, data);
538 struct userNode *user;
539 struct privmsg_desc *pd = data;
541 pd->is_qualified = 0;
542 if (*target_list == '@') {
544 } else if (strchr(target_list, '@')) {
545 struct server *server;
547 pd->is_qualified = 1;
548 user = GetUserH(strtok(target_list, "@"));
549 server = GetServerH(strtok(NULL, "@"));
551 if (user && (user->uplink != server)) {
552 /* Don't attempt to index into any arrays
553 using a user's numeric on another server. */
557 user = GetUserH(target_list);
561 if (uf) uf(user, data);
563 if (nu) nu(target_list, data);
567 } while (old == ',');
570 static CMD_FUNC(cmd_notice) {
571 struct privmsg_desc pd;
572 if ((argc < 3) || !origin) return 0;
573 if (!(pd.user = GetUserH(origin))) return 1;
574 if (IsGagged(pd.user) && !IsOper(pd.user)) return 1;
577 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
581 static CMD_FUNC(cmd_privmsg) {
582 struct privmsg_desc pd;
583 if ((argc < 3) || !origin) return 0;
584 if (!(pd.user = GetUserH(origin))) return 1;
585 if (IsGagged(pd.user) && !IsOper(pd.user)) return 1;
588 parse_foreach(argv[1], privmsg_chan_helper, NULL, privmsg_user_helper, privmsg_invalid, &pd);
592 static CMD_FUNC(cmd_capab) {
593 static const struct {
597 { "TS3", CAPAB_TS3 },
598 { "NOQUIT", CAPAB_NOQUIT },
599 { "SSJOIN", CAPAB_SSJOIN },
600 { "BURST", CAPAB_BURST },
601 { "UNCONNECT", CAPAB_UNCONNECT },
602 { "NICKIP", CAPAB_NICKIP },
603 { "TSMODE", CAPAB_TSMODE },
604 { "ZIP", CAPAB_ZIP },
610 for(nn=1; nn<argc; nn++) {
611 for (mm=0; capabs[mm].name && irccasecmp(capabs[mm].name, argv[nn]); mm++) ;
612 if (capabs[mm].name) {
613 uplink_capab |= capabs[mm].mask;
615 log_module(MAIN_LOG, LOG_INFO, "Saw unrecognized/unhandled capability %s. Please notify srvx developers so they can add it.", argv[nn]);
621 static void burst_channel(struct chanNode *chan) {
623 int pos, base_len, len, queued;
626 if (!chan->members.used) return;
627 /* send list of users in the channel.. */
628 base_len = sprintf(line, ":%s SJOIN "FMT_TIME_T" %s ", self->name, chan->timestamp, chan->name);
629 len = irc_make_chanmode(chan, line+base_len);
630 pos = base_len + len;
633 for (nn=0; nn<chan->members.used; nn++) {
634 struct modeNode *mn = chan->members.list[nn];
635 len = strlen(mn->user->nick);
636 if (pos + len > 500) {
644 if (mn->modes & MODE_CHANOP) line[pos++] = '@';
645 if (mn->modes & MODE_VOICE) line[pos++] = '+';
646 memcpy(line+pos, mn->user->nick, len);
650 /* print the last line */
653 /* now send the bans.. */
654 base_len = sprintf(line, ":%s MODE "FMT_TIME_T" %s +", self->name, chan->timestamp, chan->name);
655 pos = sizeof(line)-1;
657 for (nn=queued=0; nn<chan->banlist.used; nn++) {
658 struct banNode *bn = chan->banlist.list[nn];
659 len = strlen(bn->ban);
660 if (pos < base_len+queued+len+4) {
665 putsock("%s%s", line, line+pos);
666 pos = sizeof(line)-1;
669 memcpy(line+pos, bn->ban, len);
678 putsock("%s%s", line, line+pos);
682 static void send_burst() {
684 for (it = dict_first(servers); it; it = iter_next(it)) {
685 struct server *serv = iter_data(it);
686 if ((serv != self) && (serv != self->uplink)) irc_server(serv);
689 for (it = dict_first(clients); it; it = iter_next(it)) {
690 irc_user(iter_data(it));
692 for (it = dict_first(channels); it; it = iter_next(it)) {
693 burst_channel(iter_data(it));
695 /* Uplink will do AWAY and TOPIC bursts before sending BURST 0, but we don't */
699 static CMD_FUNC(cmd_server) {
700 if (argc < 4) return 0;
702 AddServer(GetServerH(origin), argv[1], atoi(argv[2]), 0, now, 0, argv[3]);
704 self->uplink = AddServer(self, argv[1], atoi(argv[2]), 0, now, 0, argv[3]);
710 static CMD_FUNC(cmd_svinfo) {
711 if (argc < 5) return 0;
712 if ((atoi(argv[1]) < 3) || (atoi(argv[2]) > 3)) return 0;
713 /* TODO: something with the timestamp we get from the other guy */
717 static CMD_FUNC(cmd_ping)
719 irc_pong(self->name, argc > 1 ? argv[1] : origin);
720 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
721 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
722 timeq_add(now + ping_freq, timed_send_ping, 0);
727 static CMD_FUNC(cmd_burst) {
728 struct server *sender = GetServerH(origin);
729 if (!sender) return 0;
730 if (argc == 1) return 1;
731 if (sender == self->uplink) {
732 cManager.uplink->state = CONNECTED;
734 sender->self_burst = 0;
735 recalc_bursts(sender);
739 static CMD_FUNC(cmd_nick) {
741 if ((un = GetUserH(origin))) {
743 if (argc < 2) return 0;
744 NickChange(un, argv[1], 1);
746 /* new nick from a server */
751 if (argc < 10) return 0;
752 stamp = strtoul(argv[8], NULL, 0);
753 if (stamp) inttobase64(id, stamp, IDLEN);
754 ip.s_addr = (argc > 10) ? atoi(argv[9]) : 0;
755 un = AddUser(GetServerH(argv[7]), argv[1], argv[5], argv[6], argv[4], argv[argc-1], atoi(argv[3]), ip, (stamp ? id : 0));
760 static CMD_FUNC(cmd_sjoin) {
761 struct chanNode *cNode;
762 struct userNode *uNode;
763 struct modeNode *mNode;
764 unsigned int next = 4, last;
765 char *nick, *nickend;
767 if ((argc == 3) && (uNode = GetUserH(origin))) {
769 if (!(cNode = GetChannel(argv[2]))) {
770 log_module(MAIN_LOG, LOG_ERROR, "Unable to find SJOIN target %s", argv[2]);
773 AddChannelUser(uNode, cNode);
776 if (argc < 5) return 0;
777 if (argv[3][0] == '+') {
778 char modes[MAXLEN], *pos;
780 for (pos = argv[3], n_modes = 1; *pos; pos++) {
781 if ((*pos == 'k') || (*pos == 'l')) n_modes++;
783 unsplit_string(argv+3, n_modes, modes);
784 cNode = AddChannel(argv[2], atoi(argv[1]), modes, NULL);
785 } else if (argv[3][0] == '0') {
786 cNode = GetChannel(argv[2]);
788 log_module(MAIN_LOG, LOG_ERROR, "Unsure how to handle SJOIN when arg 3 is %s", argv[3]);
792 /* argv[next] is now the space-delimited, @+-prefixed list of
793 * nicks in the channel. Split it and add the users. */
794 for (last = 0, nick = argv[next]; !last; nick = nickend + 1) {
796 for (nickend = nick; *nickend && (*nickend != ' '); nickend++) ;
797 if (!*nickend) last = 1;
799 if (*nick == '@') { mode |= MODE_CHANOP; nick++; }
800 if (*nick == '+') { mode |= MODE_VOICE; nick++; }
801 if ((uNode = GetUserH(nick)) && (mNode = AddChannelUser(uNode, cNode))) {
808 static CMD_FUNC(cmd_mode) {
812 log_module(MAIN_LOG, LOG_ERROR, "Illegal MODE from %s (no arguments).", origin);
814 } else if (IsChannelName(argv[1])) {
818 if (!(cn = GetChannel(argv[1]))) {
819 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose mode is changing", argv[1]);
823 if ((un = GetUserH(origin))) {
824 /* Update idle time for the user */
825 if ((mn = GetUserMode(cn, un)))
826 mn->idle_since = now;
828 /* Must be a server in burst or something. Make sure we're using the right timestamp. */
829 cn->timestamp = atoi(argv[2]);
832 return mod_chanmode(un, cn, argv+3, argc-3, MCP_ALLOW_OVB|MCP_FROM_SERVER|MC_ANNOUNCE);
833 } else if ((un = GetUserH(argv[1]))) {
834 mod_usermode(un, argv[2]);
837 log_module(MAIN_LOG, LOG_ERROR, "Not sure what MODE %s is affecting (not a channel name and no such user)", argv[1]);
842 static CMD_FUNC(cmd_topic) {
844 if (argc < 5) return 0;
845 if (!(cn = GetChannel(argv[1]))) {
846 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s whose topic is being set", argv[1]);
849 if (irccasecmp(origin, argv[2])) {
850 /* coming from a topic burst; the origin is a server */
851 safestrncpy(cn->topic, argv[4], sizeof(cn->topic));
852 safestrncpy(cn->topic_nick, argv[2], sizeof(cn->topic_nick));
853 cn->topic_time = atoi(argv[3]);
855 SetChannelTopic(cn, GetUserH(argv[2]), argv[4], 0);
860 static CMD_FUNC(cmd_part) {
861 if (argc < 2) return 0;
862 parse_foreach(argv[1], part_helper, NULL, NULL, NULL, GetUserH(origin));
866 static CMD_FUNC(cmd_away) {
869 if (!(un = GetUserH(origin))) {
870 log_module(MAIN_LOG, LOG_ERROR, "Unable to find user %s sending AWAY", origin);
874 un->modes |= FLAGS_AWAY;
876 un->modes &= ~FLAGS_AWAY;
881 static CMD_FUNC(cmd_kick) {
882 if (argc < 3) return 0;
883 ChannelUserKicked(GetUserH(origin), GetUserH(argv[2]), GetChannel(argv[1]));
887 static CMD_FUNC(cmd_kill) {
888 struct userNode *user;
889 if (argc < 3) return 0;
890 if (!(user = GetUserH(argv[1]))) {
891 log_module(MAIN_LOG, LOG_ERROR, "Unable to find kill victim %s", argv[1]);
894 if (IsLocal(user) && IsService(user)) {
895 ReintroduceUser(user);
897 DelUser(user, GetUserH(origin), false, ((argc >= 3) ? argv[2] : NULL));
902 static CMD_FUNC(cmd_pong)
904 if (argc < 3) return 0;
905 if (!strcmp(argv[2], self->name)) {
906 timeq_del(0, timed_send_ping, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
907 timeq_del(0, timed_ping_timeout, 0, TIMEQ_IGNORE_WHEN|TIMEQ_IGNORE_DATA);
908 timeq_add(now + ping_freq, timed_send_ping, 0);
914 static CMD_FUNC(cmd_num_topic)
916 static struct chanNode *cn;
921 cn = GetChannel(argv[2]);
923 log_module(MAIN_LOG, LOG_ERROR, "Unable to find channel %s in topic reply", argv[2]);
929 switch (atoi(argv[0])) {
932 break; /* no topic */
936 safestrncpy(cn->topic, unsplit_string(argv+3, argc-3, NULL), sizeof(cn->topic));
941 safestrncpy(cn->topic_nick, argv[3], sizeof(cn->topic_nick));
942 cn->topic_time = atoi(argv[4]);
945 return 0; /* should never happen */
950 static CMD_FUNC(cmd_quit)
952 struct userNode *user;
953 if (argc < 2) return 0;
954 /* Sometimes we get a KILL then a QUIT or the like, so we don't want to
955 * call DelUser unless we have the user in our grasp. */
956 if ((user = GetUserH(origin))) {
957 DelUser(user, NULL, false, argv[1]);
962 static CMD_FUNC(cmd_squit)
964 struct server *server;
967 if (!(server = GetServerH(argv[1])))
969 if (server == self->uplink) {
970 /* Force a reconnect to the currently selected server. */
971 cManager.uplink->tries = 0;
972 log_module(MAIN_LOG, LOG_INFO, "Squitting from uplink: %s", argv[3]);
977 DelServer(server, 0, argv[3]);
981 static CMD_FUNC(cmd_svsnick)
983 struct userNode *target, *dest;
984 if (argc < 4) return 0;
985 if (!(target = GetUserH(argv[1]))) return 0;
986 if (!IsLocal(target)) return 0;
987 if ((dest = GetUserH(argv[2]))) return 0; /* Note: Bahamut will /KILL instead. */
988 NickChange(target, argv[2], 0);
992 static oper_func_t *of_list;
993 static unsigned int of_size = 0, of_used = 0;
995 void parse_cleanup(void) {
997 if (of_list) free(of_list);
998 dict_delete(irc_func_dict);
999 dict_delete(service_msginfo_dict);
1001 for (nn=0; nn<dead_users.used; nn++) free_user(dead_users.list[nn]);
1002 userList_clean(&dead_users);
1005 void init_parse(void) {
1006 const char *str, *desc;
1008 str = conf_get_data("server/ping_freq", RECDB_QSTRING);
1009 ping_freq = str ? ParseInterval(str) : 120;
1010 str = conf_get_data("server/ping_timeout", RECDB_QSTRING);
1011 ping_timeout = str ? ParseInterval(str) : 30;
1012 str = conf_get_data("server/hostname", RECDB_QSTRING);
1013 desc = conf_get_data("server/description", RECDB_QSTRING);
1014 if (!str || !desc) {
1015 log_module(MAIN_LOG, LOG_ERROR, "No server/hostname entry in config file.");
1018 self = AddServer(NULL, str, 0, boot_time, now, NULL, desc);
1020 str = conf_get_data("server/ping_freq", RECDB_QSTRING);
1021 ping_freq = str ? ParseInterval(str) : 120;
1022 str = conf_get_data("server/ping_timeout", RECDB_QSTRING);
1023 ping_timeout = str ? ParseInterval(str) : 30;
1025 service_msginfo_dict = dict_new();
1026 dict_set_free_data(service_msginfo_dict, free);
1027 irc_func_dict = dict_new();
1028 dict_insert(irc_func_dict, "ADMIN", cmd_admin);
1029 dict_insert(irc_func_dict, "AWAY", cmd_away);
1030 dict_insert(irc_func_dict, "BURST", cmd_burst);
1031 dict_insert(irc_func_dict, "CAPAB", cmd_capab);
1032 dict_insert(irc_func_dict, "ERROR", cmd_error);
1033 dict_insert(irc_func_dict, "GNOTICE", cmd_dummy);
1034 dict_insert(irc_func_dict, "INVITE", cmd_dummy);
1035 dict_insert(irc_func_dict, "KICK", cmd_kick);
1036 dict_insert(irc_func_dict, "KILL", cmd_kill);
1037 dict_insert(irc_func_dict, "LUSERSLOCK", cmd_dummy);
1038 dict_insert(irc_func_dict, "MODE", cmd_mode);
1039 dict_insert(irc_func_dict, "NICK", cmd_nick);
1040 dict_insert(irc_func_dict, "NOTICE", cmd_notice);
1041 dict_insert(irc_func_dict, "PART", cmd_part);
1042 dict_insert(irc_func_dict, "PASS", cmd_pass);
1043 dict_insert(irc_func_dict, "PING", cmd_ping);
1044 dict_insert(irc_func_dict, "PONG", cmd_pong);
1045 dict_insert(irc_func_dict, "PRIVMSG", cmd_privmsg);
1046 dict_insert(irc_func_dict, "QUIT", cmd_quit);
1047 dict_insert(irc_func_dict, "SERVER", cmd_server);
1048 dict_insert(irc_func_dict, "SJOIN", cmd_sjoin);
1049 dict_insert(irc_func_dict, "SQUIT", cmd_squit);
1050 dict_insert(irc_func_dict, "STATS", cmd_stats);
1051 dict_insert(irc_func_dict, "SVSNICK", cmd_svsnick);
1052 dict_insert(irc_func_dict, "SVINFO", cmd_svinfo);
1053 dict_insert(irc_func_dict, "TOPIC", cmd_topic);
1054 dict_insert(irc_func_dict, "VERSION", cmd_version);
1055 dict_insert(irc_func_dict, "WHOIS", cmd_whois);
1056 dict_insert(irc_func_dict, "331", cmd_num_topic);
1057 dict_insert(irc_func_dict, "332", cmd_num_topic);
1058 dict_insert(irc_func_dict, "333", cmd_num_topic);
1059 dict_insert(irc_func_dict, "413", cmd_num_topic);
1061 userList_init(&dead_users);
1062 reg_exit_func(parse_cleanup);
1065 int parse_line(char *line, int recursive) {
1066 char *argv[MAXNUMPARAMS];
1070 argc = split_line(line, true, ArrayLength(argv), argv);
1071 cmd = line[0] == ':';
1072 if ((argc > cmd) && (func = dict_find(irc_func_dict, argv[cmd], NULL))) {
1075 origin = argv[0] + 1;
1076 } else if (self->uplink) {
1077 origin = self->uplink->name;
1081 res = func(origin, argc-cmd, argv+cmd);
1086 log_module(MAIN_LOG, LOG_ERROR, "PARSE ERROR on line: %s", unsplit_string(argv, argc, NULL));
1087 } else if (!recursive) {
1089 for (i=0; i<dead_users.used; i++) {
1090 free_user(dead_users.list[i]);
1092 dead_users.used = 0;
1098 privmsg_user_helper(struct userNode *un, void *data)
1100 struct privmsg_desc *pd = data;
1101 struct service_message_info *info = dict_find(service_msginfo_dict, un->nick, 0);
1103 if (pd->is_notice) {
1104 if (info->on_notice) info->on_notice(pd->user, un, pd->text, pd->is_qualified);
1106 if (info->on_privmsg) info->on_privmsg(pd->user, un, pd->text, pd->is_qualified);
1112 reg_privmsg_func(struct userNode *user, privmsg_func_t handler) {
1113 struct service_message_info *info = dict_find(service_msginfo_dict, user->nick, NULL);
1115 info = calloc(1, sizeof(*info));
1116 dict_insert(service_msginfo_dict, user->nick, info);
1118 info->on_privmsg = handler;
1122 reg_notice_func(struct userNode *user, privmsg_func_t handler) {
1123 struct service_message_info *info = dict_find(service_msginfo_dict, user->nick, NULL);
1125 info = calloc(1, sizeof(*info));
1126 dict_insert(service_msginfo_dict, user->nick, info);
1128 info->on_notice = handler;
1132 reg_oper_func(oper_func_t handler)
1134 if (of_used == of_size) {
1137 of_list = realloc(of_list, of_size*sizeof(oper_func_t));
1140 of_list = malloc(of_size*sizeof(oper_func_t));
1143 of_list[of_used++] = handler;
1147 call_oper_funcs(struct userNode *user)
1150 if (IsLocal(user)) return;
1151 for (n=0; n<of_used; n++)
1157 void mod_usermode(struct userNode *user, const char *mode_change) {
1160 if (!user || !mode_change) return;
1162 #define do_user_mode(FLAG) do { if (add) user->modes |= FLAG; else user->modes &= ~FLAG; } while (0)
1163 switch (*mode_change++) {
1165 case '+': add = 1; break;
1166 case '-': add = 0; break;
1168 do_user_mode(FLAGS_OPER);
1170 userList_append(&curr_opers, user);
1171 call_oper_funcs(user);
1173 userList_remove(&curr_opers, user);
1176 case 'O': do_user_mode(FLAGS_LOCOP); break;
1177 case 'i': do_user_mode(FLAGS_INVISIBLE);
1178 if (add) invis_clients++; else invis_clients--;
1180 case 'w': do_user_mode(FLAGS_WALLOP); break;
1181 case 's': do_user_mode(FLAGS_SERVNOTICE); break;
1182 case 'd': do_user_mode(FLAGS_DEAF); break;
1183 case 'r': do_user_mode(FLAGS_REGNICK); break;
1184 case 'k': do_user_mode(FLAGS_SERVICE); break;
1185 case 'g': do_user_mode(FLAGS_GLOBAL); break;
1186 case 'h': do_user_mode(FLAGS_HELPER); break;
1192 struct mod_chanmode *
1193 mod_chanmode_parse(struct chanNode *channel, char **modes, unsigned int argc, unsigned int flags)
1195 struct mod_chanmode *change;
1196 unsigned int ii, in_arg, ch_arg, add;
1200 if (!(change = mod_chanmode_alloc(argc)))
1203 for (ii = ch_arg = 0, in_arg = add = 1;
1204 (modes[0][ii] != '\0') && (modes[0][ii] != ' ');
1206 switch (modes[0][ii]) {
1213 #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)
1214 case 'R': do_chan_mode(MODE_REGONLY); break;
1215 case 'D': do_chan_mode(MODE_DELAYJOINS); break;
1216 case 'c': do_chan_mode(MODE_NOCOLORS); break;
1217 case 'i': do_chan_mode(MODE_INVITEONLY); break;
1218 case 'm': do_chan_mode(MODE_MODERATED); break;
1219 case 'n': do_chan_mode(MODE_NOPRIVMSGS); break;
1220 case 'p': do_chan_mode(MODE_PRIVATE); break;
1221 case 's': do_chan_mode(MODE_SECRET); break;
1222 case 't': do_chan_mode(MODE_TOPICLIMIT); break;
1228 change->modes_set |= MODE_LIMIT;
1229 change->new_limit = atoi(modes[in_arg++]);
1231 change->modes_clear |= MODE_LIMIT;
1238 change->modes_set |= MODE_KEY;
1239 safestrncpy(change->new_key, modes[in_arg++], sizeof(change->new_key));
1241 change->modes_clear |= MODE_KEY;
1242 if (!(flags & MCP_KEY_FREE)) {
1250 if (!(flags & MCP_ALLOW_OVB))
1254 change->args[ch_arg].mode = MODE_BAN;
1256 change->args[ch_arg].mode |= MODE_REMOVE;
1257 change->args[ch_arg++].hostmask = modes[in_arg++];
1261 struct userNode *victim;
1262 if (!(flags & MCP_ALLOW_OVB))
1266 change->args[ch_arg].mode = (modes[0][ii] == 'o') ? MODE_CHANOP : MODE_VOICE;
1268 change->args[ch_arg].mode |= MODE_REMOVE;
1269 victim = GetUserH(modes[in_arg++]);
1272 if ((change->args[ch_arg].member = GetUserMode(channel, victim)))
1277 if (!(flags & MCP_FROM_SERVER))
1282 change->argc = argc; /* in case any turned out to be ignored */
1283 if (change->modes_set & MODE_SECRET) {
1284 change->modes_set &= ~(MODE_PRIVATE);
1285 change->modes_clear |= MODE_PRIVATE;
1286 } else if (change->modes_set & MODE_PRIVATE) {
1287 change->modes_set &= ~(MODE_SECRET);
1288 change->modes_clear |= MODE_SECRET;
1292 mod_chanmode_free(change);
1296 struct chanmode_buffer {
1299 struct chanNode *channel;
1300 struct userNode *actor;
1301 unsigned int modes_used;
1302 unsigned int args_used;
1304 unsigned int is_add : 1;
1308 mod_chanmode_append(struct chanmode_buffer *buf, char ch, const char *arg)
1310 size_t arg_len = strlen(arg);
1311 if (buf->modes_used + buf->args_used + buf->chname_len + arg_len > 450) {
1312 memcpy(buf->modes + buf->modes_used, buf->args, buf->args_used);
1313 buf->modes[buf->modes_used + buf->args_used] = '\0';
1314 irc_mode(buf->actor, buf->channel, buf->modes);
1315 buf->modes[0] = buf->is_add ? '+' : '-';
1316 buf->modes_used = 1;
1319 buf->modes[buf->modes_used++] = ch;
1320 buf->args[buf->args_used++] = ' ';
1321 memcpy(buf->args + buf->args_used, arg, arg_len);
1322 buf->args_used += arg_len;
1326 mod_chanmode_announce(struct userNode *who, struct chanNode *channel, struct mod_chanmode *change)
1328 struct chanmode_buffer chbuf;
1332 assert(change->argc <= change->alloc_argc);
1333 memset(&chbuf, 0, sizeof(chbuf));
1334 chbuf.channel = channel;
1336 chbuf.chname_len = strlen(channel->name);
1338 /* First remove modes */
1340 if (change->modes_clear) {
1341 chbuf.modes[chbuf.modes_used++] = '-';
1342 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR;
1343 DO_MODE_CHAR(PRIVATE, 'p');
1344 DO_MODE_CHAR(SECRET, 's');
1345 DO_MODE_CHAR(MODERATED, 'm');
1346 DO_MODE_CHAR(TOPICLIMIT, 't');
1347 DO_MODE_CHAR(INVITEONLY, 'i');
1348 DO_MODE_CHAR(NOPRIVMSGS, 'n');
1349 DO_MODE_CHAR(LIMIT, 'l');
1350 DO_MODE_CHAR(DELAYJOINS, 'D');
1351 DO_MODE_CHAR(REGONLY, 'R');
1352 DO_MODE_CHAR(NOCOLORS, 'c');
1354 if (change->modes_clear & channel->modes & MODE_KEY)
1355 mod_chanmode_append(&chbuf, 'k', channel->key);
1357 for (arg = 0; arg < change->argc; ++arg) {
1358 if (!(change->args[arg].mode & MODE_REMOVE))
1360 switch (change->args[arg].mode & ~MODE_REMOVE) {
1362 mod_chanmode_append(&chbuf, 'b', change->args[arg].hostmask);
1365 if (change->args[arg].mode & MODE_CHANOP)
1366 mod_chanmode_append(&chbuf, 'o', change->args[arg].member->user->nick);
1367 if (change->args[arg].mode & MODE_VOICE)
1368 mod_chanmode_append(&chbuf, 'v', change->args[arg].member->user->nick);
1375 if (change->modes_set) {
1376 chbuf.modes[chbuf.modes_used++] = '+';
1377 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) chbuf.modes[chbuf.modes_used++] = CHAR;
1378 DO_MODE_CHAR(PRIVATE, 'p');
1379 DO_MODE_CHAR(SECRET, 's');
1380 DO_MODE_CHAR(MODERATED, 'm');
1381 DO_MODE_CHAR(TOPICLIMIT, 't');
1382 DO_MODE_CHAR(INVITEONLY, 'i');
1383 DO_MODE_CHAR(NOPRIVMSGS, 'n');
1384 DO_MODE_CHAR(DELAYJOINS, 'D');
1385 DO_MODE_CHAR(REGONLY, 'R');
1386 DO_MODE_CHAR(NOCOLORS, 'c');
1388 if (change->modes_set & MODE_KEY)
1389 mod_chanmode_append(&chbuf, 'k', change->new_key);
1390 if (change->modes_set & MODE_LIMIT)
1392 sprintf(int_buff, "%d", change->new_limit);
1393 mod_chanmode_append(&chbuf, 'l', int_buff);
1396 for (arg = 0; arg < change->argc; ++arg) {
1397 if (change->args[arg].mode & MODE_REMOVE)
1399 switch (change->args[arg].mode) {
1401 mod_chanmode_append(&chbuf, 'b', change->args[arg].hostmask);
1404 if (change->args[arg].mode & MODE_CHANOP)
1405 mod_chanmode_append(&chbuf, 'o', change->args[arg].member->user->nick);
1406 if (change->args[arg].mode & MODE_VOICE)
1407 mod_chanmode_append(&chbuf, 'v', change->args[arg].member->user->nick);
1412 /* Flush the buffer and apply changes locally */
1413 if (chbuf.modes_used > 0) {
1414 memcpy(chbuf.modes + chbuf.modes_used, chbuf.args, chbuf.args_used);
1415 chbuf.modes[chbuf.modes_used + chbuf.args_used] = '\0';
1416 irc_mode(chbuf.actor, chbuf.channel, chbuf.modes);
1418 mod_chanmode_apply(who, channel, change);
1422 mod_chanmode_format(struct mod_chanmode *change, char *outbuff)
1424 unsigned int used = 0;
1425 assert(change->argc <= change->alloc_argc);
1426 if (change->modes_clear) {
1427 outbuff[used++] = '-';
1428 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_clear & MODE_##BIT) outbuff[used++] = CHAR
1429 DO_MODE_CHAR(PRIVATE, 'p');
1430 DO_MODE_CHAR(SECRET, 's');
1431 DO_MODE_CHAR(MODERATED, 'm');
1432 DO_MODE_CHAR(TOPICLIMIT, 't');
1433 DO_MODE_CHAR(INVITEONLY, 'i');
1434 DO_MODE_CHAR(NOPRIVMSGS, 'n');
1435 DO_MODE_CHAR(LIMIT, 'l');
1436 DO_MODE_CHAR(KEY, 'k');
1437 DO_MODE_CHAR(DELAYJOINS, 'D');
1438 DO_MODE_CHAR(REGONLY, '$');
1439 DO_MODE_CHAR(NOCOLORS, 'c');
1442 if (change->modes_set) {
1443 outbuff[used++] = '+';
1444 #define DO_MODE_CHAR(BIT, CHAR) if (change->modes_set & MODE_##BIT) outbuff[used++] = CHAR
1445 DO_MODE_CHAR(PRIVATE, 'p');
1446 DO_MODE_CHAR(SECRET, 's');
1447 DO_MODE_CHAR(MODERATED, 'm');
1448 DO_MODE_CHAR(TOPICLIMIT, 't');
1449 DO_MODE_CHAR(INVITEONLY, 'i');
1450 DO_MODE_CHAR(NOPRIVMSGS, 'n');
1451 DO_MODE_CHAR(DELAYJOINS, 'D');
1452 DO_MODE_CHAR(REGONLY, 'R');
1453 DO_MODE_CHAR(NOCOLORS, 'c');
1455 switch (change->modes_set & (MODE_KEY|MODE_LIMIT)) {
1456 case MODE_KEY|MODE_LIMIT:
1457 used += sprintf(outbuff+used, "lk %d %s", change->new_limit, change->new_key);
1460 used += sprintf(outbuff+used, "k %s", change->new_key);
1463 used += sprintf(outbuff+used, "l %d", change->new_limit);