ns_regcode.cpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456
  1. #include "module.h"
  2. void SendRegcode(const NickAlias *na, CommandSource &source);
  3. static bool SendRegmail(User *u, const NickAlias *na, BotInfo *bi);
  4. class CommandNSProceed : public Command {
  5. public:
  6. CommandNSProceed(Module *creator) : Command(creator, "nickserv/proceed", 1, 2) {
  7. this->SetDesc(_("Confirm a regcode"));
  8. this->SetSyntax(_("\037regcode\037"));
  9. this->AllowUnregistered(true);
  10. }
  11. void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override {
  12. const Anope::string &regcode = params[0];
  13. NickAlias *na;
  14. if(source.nc && !source.nc->HasExt("UNCONFIRMED") && source.HasPriv("nickserv/proceed")) {
  15. na = NickAlias::Find(regcode);
  16. if(na == NULL)
  17. source.Reply(NICK_X_NOT_REGISTERED, regcode.c_str());
  18. else if(na->nc->HasExt("UNCONFIRMED") == false)
  19. source.Reply(_("Nick \002%s\002 is already confirmed."), na->nick.c_str());
  20. else {
  21. na->nc->Shrink<bool>("UNCONFIRMED");
  22. FOREACH_MOD(OnNickConfirm, (source.GetUser(), na->nc));
  23. Log(LOG_ADMIN, source, this) << "to confirm nick " << na->nick << " (" << na->nc->display << ")";
  24. source.Reply(_("Nick \002%s\002 has been confirmed."), na->nick.c_str());
  25. }
  26. }
  27. else if(source.nc) {
  28. Anope::string *code = source.nc->GetExt<Anope::string>("passcode");
  29. if(code != NULL && *code == regcode) {
  30. NickCore *nc = source.nc;
  31. FOREACH_MOD(OnNickConfirm, (source.GetUser(), nc));
  32. if(source.GetUser()) {
  33. na = NickAlias::Find(source.GetNick());
  34. nc->Shrink<Anope::string>("passcode");
  35. Log(LOG_COMMAND, source, this) << "to confirm their registration via regcode";
  36. source.Reply(_("Your registration for nick \002%s\002 has been confirmed."), na->nick.c_str());
  37. nc->Shrink<bool>("UNCONFIRMED");
  38. if(na) {
  39. IRCD->SendLogin(source.GetUser(), na);
  40. if(!Config->GetModule("nickserv")->Get<bool>("nonicknameownership") && na->nc == source.GetAccount() && !na->nc->HasExt("UNCONFIRMED"))
  41. source.GetUser()->SetMode(source.service, "REGISTERED");
  42. }
  43. }
  44. }
  45. else
  46. source.Reply(_("Invalid regcode."));
  47. }
  48. else
  49. source.Reply(_("Invalid regcode."));
  50. }
  51. bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override {
  52. this->SendSyntax(source);
  53. source.Reply(" ");
  54. source.Reply(_("This command is used to confirm a nick registration\n"
  55. "by means of a regcode sent through IRC instead of email.\n"));
  56. if(source.HasPriv("nickserv/proceed"))
  57. source.Reply(_("Additionally, Services Operators with the \037nickserv/proceed\037 permission can\n"
  58. "replace \037regcode\037 with a users nick to force validate them."));
  59. return true;
  60. }
  61. void OnSyntaxError(CommandSource &source, const Anope::string &subcommand) anope_override {
  62. source.Reply(_("Invalid or no regcode has been entered"));
  63. }
  64. };
  65. class CommandNSConfirm : public Command {
  66. public:
  67. CommandNSConfirm(Module *creator) : Command(creator, "nickserv/confirm", 1, 2) {
  68. this->SetDesc(_("Confirm a passcode"));
  69. this->SetSyntax(_("\037passcode\037"));
  70. this->AllowUnregistered(true);
  71. }
  72. void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override {
  73. const Anope::string &passcode = params[0];
  74. const Anope::string &nsregister = Config->GetModule(this->owner)->Get<const Anope::string>("registration");
  75. if(nsregister.equals_ci("regcode")) {
  76. source.Reply(_("Please confirm your registration with \037PROCEED\037 instead."));
  77. return;
  78. }
  79. if(source.nc && !source.nc->HasExt("UNCONFIRMED") && source.HasPriv("nickserv/confirm")) {
  80. NickAlias *na = NickAlias::Find(passcode);
  81. if(na == NULL)
  82. source.Reply(NICK_X_NOT_REGISTERED, passcode.c_str());
  83. else if(na->nc->HasExt("UNCONFIRMED") == false)
  84. source.Reply(_("Nick \002%s\002 is already confirmed."), na->nick.c_str());
  85. else {
  86. na->nc->Shrink<bool>("UNCONFIRMED");
  87. FOREACH_MOD(OnNickConfirm, (source.GetUser(), na->nc));
  88. Log(LOG_ADMIN, source, this) << "to confirm nick " << na->nick << " (" << na->nc->display << ")";
  89. source.Reply(_("Nick \002%s\002 has been confirmed."), na->nick.c_str());
  90. }
  91. }
  92. else if(source.nc) {
  93. Anope::string *code = source.nc->GetExt<Anope::string>("passcode");
  94. if(code != NULL && *code == passcode) {
  95. NickCore *nc = source.nc;
  96. nc->Shrink<Anope::string>("passcode");
  97. Log(LOG_COMMAND, source, this) << "to confirm their email";
  98. source.Reply(_("Your email address of \002%s\002 has been confirmed."), source.nc->email.c_str());
  99. nc->Shrink<bool>("UNCONFIRMED");
  100. FOREACH_MOD(OnNickConfirm, (source.GetUser(), nc));
  101. if(source.GetUser()) {
  102. NickAlias *na = NickAlias::Find(source.GetNick());
  103. if(na) {
  104. IRCD->SendLogin(source.GetUser(), na);
  105. if(!Config->GetModule("nickserv")->Get<bool>("nonicknameownership") && na->nc == source.GetAccount() && !na->nc->HasExt("UNCONFIRMED"))
  106. source.GetUser()->SetMode(source.service, "REGISTERED");
  107. }
  108. }
  109. }
  110. else
  111. source.Reply(_("Invalid passcode."));
  112. }
  113. else
  114. source.Reply(_("Invalid passcode."));
  115. return;
  116. }
  117. bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override {
  118. this->SendSyntax(source);
  119. source.Reply(" ");
  120. source.Reply(_("This command is used by several commands as a way to confirm\n"
  121. "changes made to your account.\n"
  122. " \n"
  123. "This is most commonly used to confirm your email address once\n"
  124. "you register or change it.\n"
  125. " \n"
  126. "This is also used after the RESETPASS command has been used to\n"
  127. "force identify you to your nick so you may change your password."));
  128. if(source.HasPriv("nickserv/confirm"))
  129. source.Reply(_("Additionally, Services Operators with the \037nickserv/confirm\037 permission can\n"
  130. "replace \037passcode\037 with a users nick to force validate them."));
  131. return true;
  132. }
  133. void OnSyntaxError(CommandSource &source, const Anope::string &subcommand) anope_override {
  134. source.Reply(NICK_CONFIRM_INVALID);
  135. }
  136. };
  137. class CommandNSRegister : public Command {
  138. public:
  139. CommandNSRegister(Module *creator) : Command(creator, "nickserv/register", 1, 2) {
  140. this->SetDesc(_("Register a nickname"));
  141. if(Config->GetModule("nickserv")->Get<bool>("forceemail", "yes"))
  142. this->SetSyntax(_("\037password\037 \037email\037"));
  143. else
  144. this->SetSyntax(_("\037password\037 \037[email]\037"));
  145. this->AllowUnregistered(true);
  146. }
  147. void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override {
  148. User *u = source.GetUser();
  149. Anope::string u_nick = source.GetNick();
  150. size_t nicklen = u_nick.length();
  151. Anope::string pass = params[0];
  152. Anope::string email = params.size() > 1 ? params[1] : "";
  153. const Anope::string &nsregister = Config->GetModule(this->owner)->Get<const Anope::string>("registration");
  154. if(Anope::ReadOnly) {
  155. source.Reply(_("Sorry, nickname registration is temporarily disabled."));
  156. return;
  157. }
  158. if(nsregister.equals_ci("disable")) {
  159. source.Reply(_("Registration is currently disabled."));
  160. return;
  161. }
  162. time_t nickregdelay = Config->GetModule(this->owner)->Get<time_t>("nickregdelay");
  163. time_t reg_delay = Config->GetModule("nickserv")->Get<time_t>("regdelay");
  164. if(u && !u->HasMode("OPER") && nickregdelay && Anope::CurTime - u->timestamp < nickregdelay) {
  165. source.Reply(_("You must have been using this nick for at least %d seconds to register."), nickregdelay);
  166. return;
  167. }
  168. /* Prevent "Guest" nicks from being registered. -TheShadow */
  169. /* Guest nick can now have a series of between 1 and 7 digits.
  170. * --lara
  171. */
  172. const Anope::string &guestnick = Config->GetModule("nickserv")->Get<const Anope::string>("guestnickprefix", "Guest");
  173. if(nicklen <= guestnick.length() + 7 && nicklen >= guestnick.length() + 1 && !u_nick.find_ci(guestnick) && u_nick.substr(guestnick.length()).find_first_not_of("1234567890") == Anope::string::npos) {
  174. source.Reply(NICK_CANNOT_BE_REGISTERED, u_nick.c_str());
  175. return;
  176. }
  177. if(!IRCD->IsNickValid(u_nick)) {
  178. source.Reply(NICK_CANNOT_BE_REGISTERED, u_nick.c_str());
  179. return;
  180. }
  181. if(BotInfo::Find(u_nick, true)) {
  182. source.Reply(NICK_CANNOT_BE_REGISTERED, u_nick.c_str());
  183. return;
  184. }
  185. if(Config->GetModule("nickserv")->Get<bool>("restrictopernicks"))
  186. for (unsigned i = 0; i < Oper::opers.size(); ++i) {
  187. Oper *o = Oper::opers[i];
  188. if(!source.IsOper() && u_nick.find_ci(o->name) != Anope::string::npos) {
  189. source.Reply(NICK_CANNOT_BE_REGISTERED, u_nick.c_str());
  190. return;
  191. }
  192. }
  193. unsigned int passlen = Config->GetModule("nickserv")->Get<unsigned>("passlen", "32");
  194. if(Config->GetModule("nickserv")->Get<bool>("forceemail", "yes") && email.empty())
  195. this->OnSyntaxError(source, "");
  196. else if(u && Anope::CurTime < u->lastnickreg + reg_delay)
  197. source.Reply(_("Please wait %d seconds before using the REGISTER command again."), (u->lastnickreg + reg_delay) - Anope::CurTime);
  198. else if(NickAlias::Find(u_nick) != NULL)
  199. source.Reply(NICK_ALREADY_REGISTERED, u_nick.c_str());
  200. else if(pass.equals_ci(u_nick) || (Config->GetBlock("options")->Get<bool>("strictpasswords") && pass.length() < 5))
  201. source.Reply(MORE_OBSCURE_PASSWORD);
  202. else if(pass.length() > passlen)
  203. source.Reply(PASSWORD_TOO_LONG, passlen);
  204. else if(!email.empty() && !Mail::Validate(email))
  205. source.Reply(MAIL_X_INVALID, email.c_str());
  206. else {
  207. NickCore *nc = new NickCore(u_nick);
  208. NickAlias *na = new NickAlias(u_nick, nc);
  209. Anope::Encrypt(pass, nc->pass);
  210. if(!email.empty())
  211. nc->email = email;
  212. if(u) {
  213. na->last_usermask = u->GetIdent() + "@" + u->GetDisplayedHost();
  214. na->last_realname = u->realname;
  215. }
  216. else
  217. na->last_realname = source.GetNick();
  218. Log(LOG_COMMAND, source, this) << "to register " << na->nick << " (email: " << (!na->nc->email.empty() ? na->nc->email : "none") << ")";
  219. if(na->nc->GetAccessCount())
  220. source.Reply(_("Nickname \002%s\002 registered under your user@host-mask: %s"), u_nick.c_str(), na->nc->GetAccess(0).c_str());
  221. else
  222. source.Reply(_("Nickname \002%s\002 registered."), u_nick.c_str());
  223. Anope::string tmp_pass;
  224. if(Anope::Decrypt(na->nc->pass, tmp_pass) == 1)
  225. source.Reply(_("Your password is \002%s\002 - remember this for later use."), tmp_pass.c_str());
  226. if(nsregister.equals_ci("admin")) {
  227. nc->Extend<bool>("UNCONFIRMED");
  228. // User::Identify() called below will notify the user that their registration is pending
  229. }
  230. else if(nsregister.equals_ci("mail")) {
  231. if(!email.empty()) {
  232. nc->Extend<bool>("UNCONFIRMED");
  233. SendRegmail(NULL, na, source.service);
  234. }
  235. }
  236. else if(nsregister.equals_ci("regcode")) {
  237. nc->Extend<bool>("UNCONFIRMED");
  238. SendRegcode(na, source);
  239. }
  240. FOREACH_MOD(OnNickRegister, (source.GetUser(), na, pass));
  241. if(u) {
  242. u->Identify(na);
  243. u->lastnickreg = Anope::CurTime;
  244. }
  245. }
  246. }
  247. bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override {
  248. this->SendSyntax(source);
  249. source.Reply(" ");
  250. source.Reply(_("Registers your nickname in the %s database. Once\n"
  251. "your nick is registered, you can use the \002SET\002 and \002ACCESS\002\n"
  252. "commands to configure your nick's settings as you like\n"
  253. "them. Make sure you remember the password you use when\n"
  254. "registering - you'll need it to make changes to your nick\n"
  255. "later. (Note that \002case matters!\002 \037ANOPE\037, \037Anope\037, and\n"
  256. "\037anope\037 are all different passwords!)\n"
  257. " \n"
  258. "Guidelines on choosing passwords:\n"
  259. " \n"
  260. "Passwords should not be easily guessable. For example,\n"
  261. "using your real name as a password is a bad idea. Using\n"
  262. "your nickname as a password is a much worse idea ;) and,\n"
  263. "in fact, %s will not allow it. Also, short\n"
  264. "passwords are vulnerable to trial-and-error searches, so\n"
  265. "you should choose a password at least 5 characters long.\n"
  266. "Finally, the space character cannot be used in passwords."),
  267. source.service->nick.c_str(), source.service->nick.c_str());
  268. if(!Config->GetModule("nickserv")->Get<bool>("forceemail", "yes")) {
  269. source.Reply(" ");
  270. source.Reply(_("The \037email\037 parameter is optional and will set the email\n"
  271. "for your nick immediately.\n"
  272. "Your privacy is respected; this e-mail won't be given to\n"
  273. "any third-party person. You may also wish to \002SET HIDE\002 it\n"
  274. "after registering if it isn't the default setting already."));
  275. }
  276. source.Reply(" ");
  277. source.Reply(_("This command also creates a new group for your nickname,\n"
  278. "that will allow you to register other nicks later sharing\n"
  279. "the same configuration, the same set of memos and the\n"
  280. "same channel privileges."));
  281. return true;
  282. }
  283. };
  284. class CommandNSResend : public Command {
  285. public:
  286. CommandNSResend(Module *creator) : Command(creator, "nickserv/resend", 0, 0) {
  287. this->SetDesc(_("Resend registration confirmation email"));
  288. }
  289. void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override {
  290. if(!Config->GetModule(this->owner)->Get<const Anope::string>("registration").equals_ci("mail")) {
  291. source.Reply(ACCESS_DENIED);
  292. return;
  293. }
  294. const NickAlias *na = NickAlias::Find(source.GetNick());
  295. if(na == NULL)
  296. source.Reply(NICK_NOT_REGISTERED);
  297. else if(na->nc != source.GetAccount() || !source.nc->HasExt("UNCONFIRMED"))
  298. source.Reply(_("Your account is already confirmed."));
  299. else if(Config->GetModule(this->owner)->Get<const Anope::string>("registration").equals_ci("regcode"))
  300. source.Reply(_("It's not possible to resend regcodes. If you lost it you'll have to re-register."));
  301. else {
  302. if(Anope::CurTime < source.nc->lastmail + Config->GetModule(this->owner)->Get<time_t>("resenddelay"))
  303. source.Reply(_("Cannot send mail now; please retry a little later."));
  304. else if(SendRegmail(source.GetUser(), na, source.service)) {
  305. na->nc->lastmail = Anope::CurTime;
  306. source.Reply(_("Your passcode has been re-sent to %s."), na->nc->email.c_str());
  307. Log(LOG_COMMAND, source, this) << "to resend registration verification code";
  308. }
  309. else
  310. Log(this->owner) << "Unable to resend registration verification code for " << source.GetNick();
  311. }
  312. return;
  313. }
  314. bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override {
  315. if(!Config->GetModule(this->owner)->Get<const Anope::string>("registration").equals_ci("mail"))
  316. return false;
  317. this->SendSyntax(source);
  318. source.Reply(" ");
  319. source.Reply(_("This command will resend you the registration confirmation email."));
  320. return true;
  321. }
  322. void OnServHelp(CommandSource &source) anope_override {
  323. if(Config->GetModule(this->owner)->Get<const Anope::string>("registration").equals_ci("mail"))
  324. Command::OnServHelp(source);
  325. }
  326. };
  327. class NSRegcode : public Module {
  328. CommandNSRegister commandnsregister;
  329. CommandNSConfirm commandnsconfirm;
  330. CommandNSResend commandnsrsend;
  331. CommandNSProceed commandnsproceed;
  332. SerializableExtensibleItem<bool> unconfirmed;
  333. SerializableExtensibleItem<Anope::string> passcode;
  334. public:
  335. NSRegcode(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, THIRD),
  336. commandnsregister(this), commandnsconfirm(this), commandnsrsend(this), unconfirmed(this, "UNCONFIRMED"),
  337. commandnsproceed(this), passcode(this, "passcode") {
  338. if(Config->GetModule("ns_register") != NULL)
  339. throw ModuleException("Module " + this->name + " will not load if ns_register is already loaded.");
  340. if(Config->GetModule(this)->Get<const Anope::string>("registration").equals_ci("disable"))
  341. throw ModuleException("Module " + this->name + " will not load with registration disabled.");
  342. }
  343. void OnNickIdentify(User *u) anope_override {
  344. BotInfo *NickServ;
  345. if(unconfirmed.HasExt(u->Account()) && (NickServ = Config->GetClient("NickServ"))) {
  346. const Anope::string &nsregister = Config->GetModule(this)->Get<const Anope::string>("registration");
  347. if(nsregister.equals_ci("admin"))
  348. u->SendMessage(NickServ, _("All new accounts must be validated by an administrator. Please wait for your registration to be confirmed."));
  349. else if(nsregister.equals_ci("regcode"))
  350. u->SendMessage(NickServ, _("All new accounts must be validated by means of a regcode."));
  351. else
  352. u->SendMessage(NickServ, _("Your email address is not confirmed. To confirm it, follow the instructions that were emailed to you."));
  353. const NickAlias *this_na = NickAlias::Find(u->Account()->display);
  354. time_t time_registered = Anope::CurTime - this_na->time_registered;
  355. time_t unconfirmed_expire = Config->GetModule(this)->Get<time_t>("unconfirmedexpire", "1d");
  356. if(unconfirmed_expire > time_registered)
  357. u->SendMessage(NickServ, _("Your account will expire, if not confirmed, in %s."), Anope::Duration(unconfirmed_expire - time_registered, u->Account()).c_str());
  358. }
  359. }
  360. void OnPreNickExpire(NickAlias *na, bool &expire) anope_override {
  361. if(unconfirmed.HasExt(na->nc)) {
  362. time_t unconfirmed_expire = Config->GetModule(this)->Get<time_t>("unconfirmedexpire", "1d");
  363. if(unconfirmed_expire && Anope::CurTime - na->time_registered >= unconfirmed_expire)
  364. expire = true;
  365. }
  366. }
  367. };
  368. void SendRegcode(const NickAlias *na, CommandSource &source) {
  369. Anope::string *code = na->nc->GetExt<Anope::string>("passcode");
  370. if(code == NULL) {
  371. code = na->nc->Extend<Anope::string>("passcode");
  372. *code = Anope::Random(9);
  373. }
  374. source.Reply(_("Confirm your nick registration using \002PROCEED %s\002\n"), code->c_str());
  375. }
  376. static bool SendRegmail(User *u, const NickAlias *na, BotInfo *bi) {
  377. NickCore *nc = na->nc;
  378. Anope::string *code = na->nc->GetExt<Anope::string>("passcode");
  379. if(code == NULL) {
  380. code = na->nc->Extend<Anope::string>("passcode");
  381. *code = Anope::Random(9);
  382. }
  383. Anope::string subject = Language::Translate(na->nc, Config->GetBlock("mail")->Get<const Anope::string>("registration_subject").c_str()),
  384. message = Language::Translate(na->nc, Config->GetBlock("mail")->Get<const Anope::string>("registration_message").c_str());
  385. subject = subject.replace_all_cs("%n", na->nick);
  386. subject = subject.replace_all_cs("%N", Config->GetBlock("networkinfo")->Get<const Anope::string>("networkname"));
  387. subject = subject.replace_all_cs("%c", *code);
  388. message = message.replace_all_cs("%n", na->nick);
  389. message = message.replace_all_cs("%N", Config->GetBlock("networkinfo")->Get<const Anope::string>("networkname"));
  390. message = message.replace_all_cs("%c", *code);
  391. return Mail::Send(u, nc, bi, subject, message);
  392. }
  393. MODULE_INIT(NSRegcode)