/* Hybrid protocol module for IRC Services. * * IRC Services is copyright (c) 1996-2003 Andrew Church. * E-mail: * Parts written by Andrew Kempe and others. * This program is free but copyrighted software; see the file COPYING for * details. */ #include "services.h" #include "modules.h" #include "conffile.h" #include "language.h" #include "messages.h" #include "modules/operserv/operserv.h" #include "modules/nickserv/nickserv.h" #include "modules/chanserv/chanserv.h" #include "banexcept.h" #include "sjoin.h" #include "halfop.h" /*************************************************************************/ static Module *module; static Module *module_operserv; static char *NetworkDomain = NULL; static char **p_s_OperServ = &ServerName; #define s_OperServ (*p_s_OperServ) /*************************************************************************/ /************************** User/channel modes ***************************/ /*************************************************************************/ struct modedata_init { uint8 mode; ModeData data; }; static const struct modedata_init new_usermodes[] = { {'z', {0x00000001}}, /* Receive globops */ {'a', {0x00000002}}, /* Server admin */ }; static const struct modedata_init new_chanmodes[] = { {'a', {0x00000100,0,0}}, /* Hidden ops */ {'e', {0x00000001,1,1,0,MI_MULTIPLE}}, /* Ban exceptions */ {'I', {0x00000002,1,1,0,MI_MULTIPLE}}, /* invite exceptions */ }; static const struct modedata_init new_chanusermodes[] = { {'h', {0x00000004,1,1,'%'}}, /* Half-op */ }; static void init_modes(void) { int i; for (i = 0; i < lenof(new_usermodes); i++) usermodes[new_usermodes[i].mode] = new_usermodes[i].data; for (i = 0; i < lenof(new_chanmodes); i++) chanmodes[new_chanmodes[i].mode] = new_chanmodes[i].data; for (i = 0; i < lenof(new_chanusermodes); i++) chanusermodes[new_chanusermodes[i].mode] = new_chanusermodes[i].data; mode_setup(); }; /*************************************************************************/ /************************* IRC message receiving *************************/ /*************************************************************************/ static void m_nick(char *source, int ac, char **av) { char *newmodes; if (*source) { /* Old user changing nicks. */ if (ac != 2) { if (debug) module_log("debug: NICK message: wrong number of parameters" " (%d) for source `%s'", ac, source); } else { do_nick(source, ac, av); } return; } /* New user. */ if (ac != 8) { if (debug) module_log("debug: NICK message: wrong number of parameters (%d)" " for new user", ac); return; } /* With Bahamut, new users get modes in the NICK message. Save the * modes and strip the parameter out. */ newmodes = av[3]; memmove(&av[3], &av[4], sizeof(char *) * (ac-4)); ac--; if (do_nick(source, ac, av)) { av[1] = newmodes; do_umode(av[0], 2, av); } } /*************************************************************************/ static void m_capab(char *source, int ac, char **av) { int i; for (i = 0; i < ac; i++) { if (stricmp(av[i], "NOQUIT") == 0) protocol_features |= PF_NOQUIT; } } /*************************************************************************/ static void m_sjoin(char *source, int ac, char **av) { if (ac == 3 || ac < 2) { if (debug) module_log("debug: SJOIN: expected 2 or >=4 params, got %d", ac); return; } do_sjoin(source, ac, av); } static void m_hybtopic(char *source, int ac, char **av) { Channel *c = get_channel(av[0]); ChannelInfo *ci = get_channelinfo(av[0]); const char *topic; char *s; if (!c || !ci) { log("channel: TOPIC %s for nonexistent channel %s", merge_args(ac-1, av+1), av[0]); return; } if (ac == 2) { s = strchr(source, '!'); if (s) *s = 0; if (ac > 1) topic = av[1]; else topic = ""; /* if (call_callback_4(NULL, cb_topic, c, topic, source, (long)time(NULL)) > 0) return; */ strscpy(c->topic_setter, source, sizeof(c->topic_setter)); strscpy(ci->last_topic_setter, source, NICKMAX); free(ci->last_topic); if (*topic) ci->last_topic = sstrdup(topic); else ci->last_topic = NULL; if (c->topic) { free(c->topic); c->topic = NULL; } if (ac > 1 && *av[1]) c->topic = sstrdup(av[1]); ci->last_topic_time = (long)time(NULL); put_channelinfo(ci); return; } } /*************************************************************************/ static Message hybrid_messages[] = { { "GLINE", NULL }, { "CAPAB", m_capab }, { "OPERWALL", NULL }, { "NICK", m_nick }, { "SJOIN", m_sjoin }, { "SVINFO", NULL }, { "TOPIC", m_hybtopic }, { "WALLOPS", NULL }, { NULL } }; /*************************************************************************/ /************************** IRC message sending **************************/ /*************************************************************************/ /* Send a NICK command for a new user. */ static void do_send_nick(const char *nick, const char *user, const char *host, const char *server, const char *name, const char *modes) { /* NICK * : */ send_cmd(NULL, "NICK %s 1 %ld +%s %s %s %s :%s", nick, (long)time(NULL), modes, user, host, server, name); } /*************************************************************************/ /* Send a NICK command to change an existing user's nick. */ static void do_send_nickchange(const char *nick, const char *newnick) { send_cmd(nick, "NICK %s %ld", newnick, (long)time(NULL)); } /*************************************************************************/ /* Send a command to change a user's "real name". */ static void do_send_namechange(const char *nick, const char *newname) { /* Not supported by this protocol. */ } /*************************************************************************/ /* Send a SERVER command, and anything else needed at the beginning of the * connection. */ static void do_send_server(void) { send_cmd(NULL, "PASS %s :TS", RemotePassword); send_cmd(NULL, "CAPAB TS5 EX IE HOPS HUB AOPS"); send_cmd(NULL, "SERVER %s 1 :%s", ServerName, ServerDesc); send_cmd(NULL, "SVINFO 5 5 0 :%ld", (long)time(NULL)); } /*************************************************************************/ /* Send a SERVER command for a remote (juped) server. */ static void do_send_server_remote(const char *server, const char *reason) { send_cmd(NULL, "SERVER %s 2 :%s", server, reason); } /*************************************************************************/ /* Send a WALLOPS (really a GLOBOPS). */ static void do_wallops(const char *source, const char *fmt, ...) { va_list args; char buf[BUFSIZE]; va_start(args, fmt); vsnprintf(buf, sizeof(buf), fmt, args); va_end(args); send_cmd(source ? source : ServerName, "OPERWALL :%s", buf); } /*************************************************************************/ /* Send a NOTICE to all users on the network. */ static void do_notice_all(const char *source, const char *fmt, ...) { va_list args; char msgbuf[BUFSIZE]; va_start(args, fmt); vsnprintf(msgbuf, sizeof(msgbuf), fmt, args); va_end(args); if (NetworkDomain) { send_cmd(source, "NOTICE $$*.%s :%s", NetworkDomain, msgbuf); } else { /* Go through all common top-level domains. If you have others, * add them here. */ send_cmd(source, "NOTICE $$*.com :%s", msgbuf); send_cmd(source, "NOTICE $$*.net :%s", msgbuf); send_cmd(source, "NOTICE $$*.org :%s", msgbuf); send_cmd(source, "NOTICE $$*.edu :%s", msgbuf); } } /*************************************************************************/ /* Send a command which modifies channel status. */ static void do_send_channel_cmd(const char *source, const char *fmt, ...) { va_list args; va_start(args, fmt); vsend_cmd(ServerName, fmt, args); va_end(args); } /*************************************************************************/ /******************************* Callbacks *******************************/ /*************************************************************************/ static int do_set_topic(Channel *c, const char *topic, const char *setter, time_t t) { if (setter) return 0; c->topic_time = t; send_cmd(ServerName, "TOPIC %s %s %ld :%s", c->name, c->topic_setter, (long)c->topic_time, c->topic ? c->topic : ""); return 1; } /*************************************************************************/ static int do_send_akill(const char *username, const char *host, time_t expires, const char *who, const char *reason) { time_t now = time(NULL); send_cmd(NULL, ":%s KLINE * %ld %s %s :%s", s_OperServ, (long)((expires && expires > now) ? expires - now : 0), username, host, reason); return 1; } /*************************************************************************/ static int do_cancel_akill(const char *username, const char *host) { /* no such command :/ */ return 0; } /*************************************************************************/ /***************************** Module stuff ******************************/ /*************************************************************************/ const int32 module_version = MODULE_VERSION_CODE; ConfigDirective module_config[] = { { "NetworkDomain", { { CD_STRING, 0, &NetworkDomain } } }, SJOIN_CONFIG, { NULL } }; /*************************************************************************/ static int do_load_module(Module *mod, const char *modname) { if (strcmp(modname, "operserv/main") == 0) { module_operserv = mod; p_s_OperServ = get_module_symbol(mod, "s_OperServ"); if (!p_s_OperServ) { module_log("Unable to resolve symbol `s_OperServ' in module" " `operserv/main'"); } } else if (strcmp(modname, "operserv/akill") == 0) { if (!add_callback(mod, "send_akill", do_send_akill)) module_log("Unable to add send_akill callback"); if (!add_callback(mod, "cancel_akill", do_cancel_akill)) module_log("Unable to add cancel_akill callback"); } return 0; } /*************************************************************************/ static int do_unload_module(Module *mod) { if (mod == module_operserv) { module_operserv = NULL; } return 0; } /*************************************************************************/ int init_module(Module *module_) { unsigned char c; module = module_; protocol_name = "Hybrid"; protocol_version = "7.0"; protocol_features = 0; protocol_nickmax = 30; if (!register_messages(hybrid_messages)) { module_log("Unable to register messages"); exit_module(1); return 0; } if (!add_callback(NULL, "load module", do_load_module) || !add_callback(NULL, "unload module", do_unload_module) || !add_callback(NULL, "set topic", do_set_topic) ) { module_log("Unable to add callbacks"); exit_module(1); return 0; } if (!init_banexcept(module) || !init_sjoin(module) || !init_halfop(module) ) { exit_module(1); return 0; } init_modes(); irc_lowertable['['] = '['; irc_lowertable['\\'] = '\\'; irc_lowertable[']'] = ']'; for (c = 'A'; c <= '}'; c++) valid_nick_table[c] = 3; for (c = 0; c < 32; c++) valid_chan_table[c] = 0; valid_chan_table[160] = 0; send_nick = do_send_nick; send_nickchange = do_send_nickchange; send_namechange = do_send_namechange; send_server = do_send_server; send_server_remote = do_send_server_remote; wallops = do_wallops; notice_all = do_notice_all; send_channel_cmd = do_send_channel_cmd; pseudoclient_modes = ""; enforcer_modes = ""; setstring(OPER_BOUNCY_MODES, OPER_BOUNCY_MODES_U_LINE); return 1; } /*************************************************************************/ int exit_module(int shutdown) { if (!shutdown) { /* Do not allow removal */ return 0; } exit_halfop(); exit_sjoin(); exit_banexcept(); remove_callback(NULL, "set topic", do_set_topic); remove_callback(NULL, "unload module", do_unload_module); remove_callback(NULL, "load module", do_load_module); unregister_messages(hybrid_messages); return 1; } /*************************************************************************/