diff options
Diffstat (limited to 'src/core/ns_set.cpp')
-rw-r--r-- | src/core/ns_set.cpp | 523 |
1 files changed, 109 insertions, 414 deletions
diff --git a/src/core/ns_set.cpp b/src/core/ns_set.cpp index 9cc515700..ecb1355c3 100644 --- a/src/core/ns_set.cpp +++ b/src/core/ns_set.cpp @@ -16,515 +16,207 @@ class CommandNSSet : public Command { - private: - CommandReturn DoSetDisplay(User *u, const std::vector<ci::string> ¶ms, NickCore *nc) + std::map<ci::string, Command *> subcommands; + public: + CommandNSSet(const ci::string &cname) : Command(cname, 1, 3) { - ci::string param = params.size() > 1 ? params[1] : ""; - - if (param.empty()) - { - this->OnSyntaxError(u, "DISPLAY"); - return MOD_CONT; - } - - NickAlias *na = findnick(param); - - if (!na || na->nc != nc) - { - notice_lang(Config.s_NickServ, u, NICK_SET_DISPLAY_INVALID); - return MOD_CONT; - } - - change_core_display(nc, param.c_str()); - notice_lang(Config.s_NickServ, u, NICK_SET_DISPLAY_CHANGED, nc->display); - return MOD_CONT; } - CommandReturn DoSetPassword(User *u, const std::vector<ci::string> ¶ms, NickCore *nc) + ~CommandNSSet() { - ci::string param = params.size() > 1 ? params[1] : ""; - std::string buf, tmp_pass; - - if (param.empty()) + for (std::map<ci::string, Command *>::const_iterator it = this->subcommands.begin(); it != this->subcommands.end(); ++it) { - this->OnSyntaxError(u, "PASSWORD"); - return MOD_CONT; + delete it->second; } - - int len = param.size(); - - if (nc->display == param || (Config.StrictPasswords && len < 5)) - { - notice_lang(Config.s_NickServ, u, MORE_OBSCURE_PASSWORD); - return MOD_CONT; - } - else if (len > Config.PassLen) - { - notice_lang(Config.s_NickServ, u, PASSWORD_TOO_LONG); - return MOD_CONT; - } - - buf = param.c_str(); /* conversion from ci::string to std::string */ - if (enc_encrypt(buf, nc->pass) < 0) - { - Alog() << Config.s_NickServ << ": Failed to encrypt password for " << nc->display << " (set)"; - notice_lang(Config.s_NickServ, u, NICK_SET_PASSWORD_FAILED); - return MOD_CONT; - } - - if (enc_decrypt(nc->pass, tmp_pass) == 1) - notice_lang(Config.s_NickServ, u, NICK_SET_PASSWORD_CHANGED_TO, tmp_pass.c_str()); - else - notice_lang(Config.s_NickServ, u, NICK_SET_PASSWORD_CHANGED); - - Alog() << Config.s_NickServ << ": " << u->GetMask() << " (e-mail: " << (nc->email ? nc->email : "none") << ") changed its password."; - - return MOD_CONT; + this->subcommands.clear(); } - CommandReturn DoSetLanguage(User *u, const std::vector<ci::string> ¶ms, NickCore *nc) + CommandReturn Execute(User *u, const std::vector<ci::string> ¶ms) { - const char *param = params.size() > 1 ? params[1].c_str() : NULL; - - if (!param) + if (readonly) { - this->OnSyntaxError(u, "LANGUAGE"); + notice_lang(Config.s_NickServ, u, NICK_SET_DISABLED); return MOD_CONT; } - int langnum; - - if (param[strspn(param, "0123456789")]) /* i.e. not a number */ - { - syntax_error(Config.s_NickServ, u, "SET LANGUAGE", NICK_SET_LANGUAGE_SYNTAX); - return MOD_CONT; - } - langnum = atoi(param) - 1; - if (langnum < 0 || langnum >= NUM_LANGS || langlist[langnum] < 0) + if (u->Account()->HasFlag(NI_SUSPENDED)) { - notice_lang(Config.s_NickServ, u, NICK_SET_LANGUAGE_UNKNOWN, langnum + 1, Config.s_NickServ); + notice_lang(Config.s_NickServ, u, NICK_X_SUSPENDED, u->Account()->display); return MOD_CONT; } - nc->language = langlist[langnum]; - notice_lang(Config.s_NickServ, u, NICK_SET_LANGUAGE_CHANGED); - return MOD_CONT; - } - CommandReturn DoSetUrl(User *u, const std::vector<ci::string> ¶ms, NickCore *nc) - { - const char *param = params.size() > 1 ? params[1].c_str() : NULL; - - if (nc->url) - delete [] nc->url; + Command *c = this->FindCommand(params[0]); - if (param) + if (c) { - nc->url = sstrdup(param); - notice_lang(Config.s_NickServ, u, NICK_SET_URL_CHANGED, param); + ci::string cmdparams; + for (std::vector<ci::string>::const_iterator it = params.begin() + 1; it != params.end(); ++it) + cmdparams += " " + *it; + if (!cmdparams.empty()) + cmdparams.erase(cmdparams.begin()); + mod_run_cmd(NickServ, u, c, params[0], cmdparams); } else { - nc->url = NULL; - notice_lang(Config.s_NickServ, u, NICK_SET_URL_UNSET); + notice_lang(Config.s_NickServ, u, NICK_SET_UNKNOWN_OPTION, params[0].c_str()); } + return MOD_CONT; } - CommandReturn DoSetEmail(User *u, const std::vector<ci::string> ¶ms, NickCore *nc) + bool OnHelp(User *u, const ci::string &subcommand) { - const char *param = params.size() > 1 ? params[1].c_str() : NULL; - - if (!param && Config.NSForceEmail) - { - notice_lang(Config.s_NickServ, u, NICK_SET_EMAIL_UNSET_IMPOSSIBLE); - return MOD_CONT; - } - else if (param && !MailValidate(param)) - { - notice_lang(Config.s_NickServ, u, MAIL_X_INVALID, param); - return MOD_CONT; - } - - Alog() << Config.s_NickServ << ": " << u->GetMask() << " (e-mail: " << (nc->email ? nc->email : "none") << ") changed its e-mail to " << (param ? param : "none"); - - if (nc->email) - delete [] nc->email; - - if (param) + if (subcommand.empty()) { - nc->email = sstrdup(param); - notice_lang(Config.s_NickServ, u, NICK_SET_EMAIL_CHANGED, param); + notice_help(Config.s_NickServ, u, NICK_HELP_SET_HEAD); + for (std::map<ci::string, Command *>::iterator it = this->subcommands.begin(); it != this->subcommands.end(); ++it) + it->second->OnServHelp(u); + notice_help(Config.s_NickServ, u, NICK_HELP_SET_TAIL); + return true; } else { - nc->email = NULL; - notice_lang(Config.s_NickServ, u, NICK_SET_EMAIL_UNSET); - } - return MOD_CONT; - } - - CommandReturn DoSetICQ(User *u, const std::vector<ci::string> ¶ms, NickCore *nc) - { - const char *param = params.size() > 1 ? params[1].c_str() : NULL; + Command *c = this->FindCommand(subcommand); - if (param) - { - int32 tmp = atol(param); - if (!tmp) - notice_lang(Config.s_NickServ, u, NICK_SET_ICQ_INVALID, param); - else + if (c) { - nc->icq = tmp; - notice_lang(Config.s_NickServ, u, NICK_SET_ICQ_CHANGED, param); + return c->OnHelp(u, subcommand); } } - else - { - nc->icq = 0; - notice_lang(Config.s_NickServ, u, NICK_SET_ICQ_UNSET); - } - return MOD_CONT; + + return false; } - CommandReturn DoSetGreet(User *u, const std::vector<ci::string> ¶ms, NickCore *nc) + void OnSyntaxError(User *u, const ci::string &subcommand) { - const char *param = params.size() > 1 ? params[1].c_str() : NULL; - - if (nc->greet) - delete [] nc->greet; - - if (param) - { - char buf[BUFSIZE]; - const char *rest = params.size() > 2 ? params[2].c_str() : NULL; - - snprintf(buf, sizeof(buf), "%s%s%s", param, rest ? " " : "", rest ? rest : ""); - - nc->greet = sstrdup(buf); - notice_lang(Config.s_NickServ, u, NICK_SET_GREET_CHANGED, buf); - } - else - { - nc->greet = NULL; - notice_lang(Config.s_NickServ, u, NICK_SET_GREET_UNSET); - } - return MOD_CONT; + syntax_error(Config.s_NickServ, u, "SET", NICK_SET_SYNTAX); } - CommandReturn DoSetKill(User *u, const std::vector<ci::string> ¶ms, NickCore *nc) + void OnServHelp(User *u) { - ci::string param = params.size() > 1 ? params[1] : ""; - - if (param.empty()) - { - this->OnSyntaxError(u, "KILL"); - return MOD_CONT; - } - - if (param == "ON") - { - nc->SetFlag(NI_KILLPROTECT); - nc->UnsetFlag(NI_KILL_QUICK); - nc->UnsetFlag(NI_KILL_IMMED); - notice_lang(Config.s_NickServ, u, NICK_SET_KILL_ON); - } - else if (param == "QUICK") - { - nc->SetFlag(NI_KILLPROTECT); - nc->SetFlag(NI_KILL_QUICK); - nc->UnsetFlag(NI_KILL_IMMED); - notice_lang(Config.s_NickServ, u, NICK_SET_KILL_QUICK); - } - else if (param == "IMMED") - { - if (Config.NSAllowKillImmed) - { - nc->SetFlag(NI_KILLPROTECT); - nc->SetFlag(NI_KILL_IMMED); - nc->UnsetFlag(NI_KILL_QUICK); - notice_lang(Config.s_NickServ, u, NICK_SET_KILL_IMMED); - } - else - notice_lang(Config.s_NickServ, u, NICK_SET_KILL_IMMED_DISABLED); - } - else if (param == "OFF") - { - nc->UnsetFlag(NI_KILLPROTECT); - nc->UnsetFlag(NI_KILL_QUICK); - nc->UnsetFlag(NI_KILL_IMMED); - notice_lang(Config.s_NickServ, u, NICK_SET_KILL_OFF); - } - else - syntax_error(Config.s_NickServ, u, "SET KILL", Config.NSAllowKillImmed ? NICK_SET_KILL_IMMED_SYNTAX : NICK_SET_KILL_SYNTAX); - return MOD_CONT; + notice_lang(Config.s_NickServ, u, NICK_HELP_CMD_SET); } - CommandReturn DoSetSecure(User *u, const std::vector<ci::string> ¶ms, NickCore *nc) + bool AddSubcommand(Command *c) { - ci::string param = params.size() > 1 ? params[1] : ""; - - if (param.empty()) - { - this->OnSyntaxError(u, "SECURE"); - return MOD_CONT; - } + return this->subcommands.insert(std::make_pair(c->name, c)).second; + } - if (param == "ON") - { - nc->SetFlag(NI_SECURE); - notice_lang(Config.s_NickServ, u, NICK_SET_SECURE_ON); - } - else if (param == "OFF") - { - nc->UnsetFlag(NI_SECURE); - notice_lang(Config.s_NickServ, u, NICK_SET_SECURE_OFF); - } - else - syntax_error(Config.s_NickServ, u, "SET SECURE", NICK_SET_SECURE_SYNTAX); - return MOD_CONT; + bool DelSubcommand(const ci::string &command) + { + return this->subcommands.erase(command); } - CommandReturn DoSetPrivate(User *u, const std::vector<ci::string> ¶ms, NickCore *nc) + Command *FindCommand(const ci::string &subcommand) { - ci::string param = params.size() > 1 ? params[1] : ""; + std::map<ci::string, Command *>::const_iterator it = this->subcommands.find(subcommand); - if (param.empty()) + if (it != this->subcommands.end()) { - this->OnSyntaxError(u, "PRIVATE"); - return MOD_CONT; + return it->second; } - if (param == "ON") - { - nc->SetFlag(NI_PRIVATE); - notice_lang(Config.s_NickServ, u, NICK_SET_PRIVATE_ON); - } - else if (param == "OFF") - { - nc->UnsetFlag(NI_PRIVATE); - notice_lang(Config.s_NickServ, u, NICK_SET_PRIVATE_OFF); - } - else - syntax_error(Config.s_NickServ, u, "SET PRIVATE", NICK_SET_PRIVATE_SYNTAX); - return MOD_CONT; + return NULL; } +}; - CommandReturn DoSetMsg(User *u, const std::vector<ci::string> ¶ms, NickCore *nc) +class CommandNSSetDisplay : public Command +{ + public: + CommandNSSetDisplay(const ci::string &cname) : Command(cname, 1) { - ci::string param = params.size() > 1 ? params[1] : ""; + } - if (param.empty()) - { - this->OnSyntaxError(u, "MSG"); - return MOD_CONT; - } + CommandReturn Execute(User *u, const std::vector<ci::string> ¶ms) + { + NickAlias *na = findnick(params[0]); - if (!Config.UsePrivmsg) + if (!na || na->nc != u->Account()) { - notice_lang(Config.s_NickServ, u, NICK_SET_OPTION_DISABLED, "MSG"); + notice_lang(Config.s_NickServ, u, NICK_SET_DISPLAY_INVALID); return MOD_CONT; } - if (param == "ON") - { - nc->SetFlag(NI_MSG); - notice_lang(Config.s_NickServ, u, NICK_SET_MSG_ON); - } - else if (param == "OFF") - { - nc->UnsetFlag(NI_MSG); - notice_lang(Config.s_NickServ, u, NICK_SET_MSG_OFF); - } - else - syntax_error(Config.s_NickServ, u, "SET MSG", NICK_SET_MSG_SYNTAX); + change_core_display(u->Account(), params[0].c_str()); + notice_lang(Config.s_NickServ, u, NICK_SET_DISPLAY_CHANGED, u->Account()->display); return MOD_CONT; } - CommandReturn DoSetHide(User *u, const std::vector<ci::string> ¶ms, NickCore *nc) + bool OnHelp(User *u, const ci::string &) { - ci::string param = params.size() > 1 ? params[1] : ""; - - if (param.empty()) - { - this->OnSyntaxError(u, "HIDE"); - return MOD_CONT; - } + notice_help(Config.s_NickServ, u, NICK_HELP_SET_DISPLAY); + return true; + } - int onmsg, offmsg; - NickCoreFlag flag; + void OnSyntaxError(User *u, const ci::string &) + { + // XXX + syntax_error(Config.s_NickServ, u, "SET", NICK_SET_SYNTAX); + } - if (param == "EMAIL") - { - flag = NI_HIDE_EMAIL; - onmsg = NICK_SET_HIDE_EMAIL_ON; - offmsg = NICK_SET_HIDE_EMAIL_OFF; - } - else if (param == "USERMASK") - { - flag = NI_HIDE_MASK; - onmsg = NICK_SET_HIDE_MASK_ON; - offmsg = NICK_SET_HIDE_MASK_OFF; - } - else if (param == "STATUS") - { - flag = NI_HIDE_STATUS; - onmsg = NICK_SET_HIDE_STATUS_ON; - offmsg = NICK_SET_HIDE_STATUS_OFF; - } - else if (param == "QUIT") - { - flag = NI_HIDE_QUIT; - onmsg = NICK_SET_HIDE_QUIT_ON; - offmsg = NICK_SET_HIDE_QUIT_OFF; - } - else - { - syntax_error(Config.s_NickServ, u, "SET HIDE", NICK_SET_HIDE_SYNTAX); - return MOD_CONT; - } + void OnServHelp(User *u) + { + notice_lang(Config.s_NickServ, u, NICK_HELP_CMD_SET_DISPLAY); + } +}; - param = params.size() > 2 ? params[2] : ""; - if (param.empty()) - syntax_error(Config.s_NickServ, u, "SET HIDE", NICK_SET_HIDE_SYNTAX); - else if (param == "ON") - { - nc->SetFlag(flag); - notice_lang(Config.s_NickServ, u, onmsg, Config.s_NickServ); - } - else if (param == "OFF") - { - nc->UnsetFlag(flag); - notice_lang(Config.s_NickServ, u, offmsg, Config.s_NickServ); - } - else - syntax_error(Config.s_NickServ, u, "SET HIDE", NICK_SET_HIDE_SYNTAX); - return MOD_CONT; +class CommandNSSetPassword : public Command +{ + public: + CommandNSSetPassword(const ci::string &cname) : Command(cname, 1) + { } - CommandReturn DoSetAutoOP(User *u, const std::vector<ci::string> ¶ms, NickCore *nc) + CommandReturn Execute(User *u, const std::vector<ci::string> ¶ms) { - ci::string param = params.size() > 1 ? params[1] : ""; + ci::string param = params[0]; - if (param.empty()) - { - this->OnSyntaxError(u, "AUTOOP"); - return MOD_CONT; - } + int len = param.size(); - /** - * This works the other way around, the absence of this flag denotes ON - * This is so when people upgrade, and dont have the flag - * the default is on - **/ - if (param == "ON") + if (u->Account()->display == param || (Config.StrictPasswords && len < 5)) { - nc->SetFlag(NI_AUTOOP); - notice_lang(Config.s_NickServ, u, NICK_SET_AUTOOP_ON); + notice_lang(Config.s_NickServ, u, MORE_OBSCURE_PASSWORD); + return MOD_CONT; } - else if (param == "OFF") + else if (len > Config.PassLen) { - nc->UnsetFlag(NI_AUTOOP); - notice_lang(Config.s_NickServ, u, NICK_SET_AUTOOP_OFF); + notice_lang(Config.s_NickServ, u, PASSWORD_TOO_LONG); + return MOD_CONT; } - else - syntax_error(Config.s_NickServ, u, "SET AUTOOP", NICK_SET_AUTOOP_SYNTAX); - return MOD_CONT; - } - public: - CommandNSSet() : Command("SET", 1, 3) - { - } - - CommandReturn Execute(User *u, const std::vector<ci::string> ¶ms) - { - ci::string cmd = params[0]; - - if (readonly) + std::string buf = param.c_str(); /* conversion from ci::string to std::string */ + if (enc_encrypt(buf, u->Account()->pass) < 0) { - notice_lang(Config.s_NickServ, u, NICK_SET_DISABLED); + Alog() << Config.s_NickServ << ": Failed to encrypt password for " << u->Account()->display << " (set)"; + notice_lang(Config.s_NickServ, u, NICK_SET_PASSWORD_FAILED); return MOD_CONT; } -/* - if (na->HasFlag(NS_FORBIDDEN)) - notice_lang(Config.s_NickServ, u, NICK_X_FORBIDDEN, na->nick); -*/ - if (u->Account()->HasFlag(NI_SUSPENDED)) - notice_lang(Config.s_NickServ, u, NICK_X_SUSPENDED, u->Account()->display); - else if (cmd == "DISPLAY") - return this->DoSetDisplay(u, params, u->Account()); - else if (cmd == "PASSWORD") - return this->DoSetPassword(u, params, u->Account()); - else if (cmd == "LANGUAGE") - return this->DoSetLanguage(u, params, u->Account()); - else if (cmd == "URL") - return this->DoSetUrl(u, params, u->Account()); - else if (cmd == "EMAIL") - return this->DoSetEmail(u, params, u->Account()); - else if (cmd == "ICQ") - return this->DoSetICQ(u, params, u->Account()); - else if (cmd == "GREET") - return this->DoSetGreet(u, params, u->Account()); - else if (cmd == "KILL") - return this->DoSetKill(u, params, u->Account()); - else if (cmd == "SECURE") - return this->DoSetSecure(u, params, u->Account()); - else if (cmd == "PRIVATE") - return this->DoSetPrivate(u, params, u->Account()); - else if (cmd == "MSG") - return this->DoSetMsg(u, params, u->Account()); - else if (cmd == "HIDE") - return this->DoSetHide(u, params, u->Account()); - else if (cmd == "AUTOOP") - return this->DoSetAutoOP(u, params, u->Account()); + std::string tmp_pass; + if (enc_decrypt(u->Account()->pass, tmp_pass) == 1) + notice_lang(Config.s_NickServ, u, NICK_SET_PASSWORD_CHANGED_TO, tmp_pass.c_str()); else - notice_lang(Config.s_NickServ, u, NICK_SET_UNKNOWN_OPTION, cmd.c_str()); + notice_lang(Config.s_NickServ, u, NICK_SET_PASSWORD_CHANGED); + + Alog() << Config.s_NickServ << ": " << u->GetMask() << " (e-mail: " << (u->Account()->email ? u->Account()->email : "none") << ") changed its password."; return MOD_CONT; } - bool OnHelp(User *u, const ci::string &subcommand) + bool OnHelp(User *u, const ci::string &) { - if (subcommand.empty()) - notice_help(Config.s_NickServ, u, NICK_HELP_SET); - else if (subcommand == "DISPLAY") - notice_help(Config.s_NickServ, u, NICK_HELP_SET_DISPLAY); - else if (subcommand == "PASSWORD") - notice_help(Config.s_NickServ, u, NICK_HELP_SET_PASSWORD); - else if (subcommand == "URL") - notice_help(Config.s_NickServ, u, NICK_HELP_SET_URL); - else if (subcommand == "EMAIL") - notice_help(Config.s_NickServ, u, NICK_HELP_SET_EMAIL); - else if (subcommand == "ICQ") - notice_help(Config.s_NickServ, u, NICK_HELP_SET_ICQ); - else if (subcommand == "GREET") - notice_help(Config.s_NickServ, u, NICK_HELP_SET_GREET); - else if (subcommand == "KILL") - notice_help(Config.s_NickServ, u, NICK_HELP_SET_KILL); - else if (subcommand == "SECURE") - notice_help(Config.s_NickServ, u, NICK_HELP_SET_SECURE); - else if (subcommand == "PRIVATE") - notice_help(Config.s_NickServ, u, NICK_HELP_SET_PRIVATE); - else if (subcommand == "MSG") - notice_help(Config.s_NickServ, u, NICK_HELP_SET_MSG); - else if (subcommand == "HIDE") - notice_help(Config.s_NickServ, u, NICK_HELP_SET_HIDE); - else if (subcommand == "AUTOOP") - notice_help(Config.s_NickServ, u, NICK_HELP_SET_AUTOOP); - else - return false; - + notice_help(Config.s_NickServ, u, NICK_HELP_SET_PASSWORD); return true; } - void OnSyntaxError(User *u, const ci::string &subcommand) + void OnSyntaxError(User *u, const ci::string &) { + // XXX syntax_error(Config.s_NickServ, u, "SET", NICK_SET_SYNTAX); } void OnServHelp(User *u) { - notice_lang(Config.s_NickServ, u, NICK_HELP_CMD_SET); + notice_lang(Config.s_NickServ, u, NICK_HELP_CMD_SET_PASSWORD); } }; @@ -537,7 +229,10 @@ class NSSet : public Module this->SetVersion(VERSION_STRING); this->SetType(CORE); - this->AddCommand(NickServ, new CommandNSSet()); + Command *set = new CommandNSSet("SET"); + this->AddCommand(NickServ, set); + set->AddSubcommand(new CommandNSSetDisplay("DISPLAY")); + set->AddSubcommand(new CommandNSSetPassword("PASSWORD")); } }; |