1 /* OperServ core functions
2  *
3  * (C) 2003-2020 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  */
11 
12 #include "module.h"
13 
14 class CommandOSLogin : public Command
15 {
16  public:
CommandOSLogin(Module * creator)17 	CommandOSLogin(Module *creator) : Command(creator, "operserv/login", 1, 1)
18 	{
19 		this->SetSyntax(_("\037password\037"));
20 		this->RequireUser(true);
21 	}
22 
Execute(CommandSource & source,const std::vector<Anope::string> & params)23 	void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
24 	{
25 		const Anope::string &password = params[0];
26 
27 		User *u = source.GetUser();
28 		Oper *o = source.nc->o;
29 		if (o == NULL)
30 			source.Reply(_("No oper block for your nick."));
31 		else if (o->password.empty())
32 			source.Reply(_("Your oper block doesn't require logging in."));
33 		else if (u->HasExt("os_login"))
34 			source.Reply(_("You are already identified."));
35 		else if (o->password != password)
36 		{
37 			source.Reply(PASSWORD_INCORRECT);
38 			u->BadPassword();
39 		}
40 		else
41 		{
42 			Log(LOG_ADMIN, source, this) << "and successfully identified to " << source.service->nick;
43 			u->Extend<bool>("os_login");
44 			source.Reply(_("Password accepted."));
45 		}
46 	}
47 
OnHelp(CommandSource & source,const Anope::string & subcommand)48 	bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
49 	{
50 		this->SendSyntax(source);
51 		source.Reply(" ");
52 		source.Reply(_("Logs you in to %s so you gain Services Operator privileges.\n"
53 				"This command may be unnecessary if your oper block is\n"
54 				"configured without a password."), source.service->nick.c_str());
55 		return true;
56 	}
57 
GetDesc(CommandSource & source) const58 	const Anope::string GetDesc(CommandSource &source) const anope_override
59 	{
60 		return Anope::printf(Language::Translate(source.GetAccount(), _("Login to %s")), source.service->nick.c_str());
61 	}
62 };
63 
64 class CommandOSLogout : public Command
65 {
66  public:
CommandOSLogout(Module * creator)67 	CommandOSLogout(Module *creator) : Command(creator, "operserv/logout", 0, 0)
68 	{
69 		this->RequireUser(true);
70 	}
71 
Execute(CommandSource & source,const std::vector<Anope::string> & params)72 	void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
73 	{
74 		User *u = source.GetUser();
75 		Oper *o = source.nc->o;
76 		if (o == NULL)
77 			source.Reply(_("No oper block for your nick."));
78 		else if (o->password.empty())
79 			source.Reply(_("Your oper block doesn't require logging in."));
80 		else if (!u->HasExt("os_login"))
81 			source.Reply(_("You are not identified."));
82 		else
83 		{
84 			Log(LOG_ADMIN, source, this);
85 			u->Shrink<bool>("os_login");
86 			source.Reply(_("You have been logged out."));
87 		}
88 	}
89 
OnHelp(CommandSource & source,const Anope::string & subcommand)90 	bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
91 	{
92 		this->SendSyntax(source);
93 		source.Reply(" ");
94 		source.Reply(_("Logs you out from %s so you lose Services Operator privileges.\n"
95 				"This command is only useful if your oper block is configured\n"
96 				"with a password."), source.service->nick.c_str());
97 		return true;
98 	}
99 
GetDesc(CommandSource & source) const100 	const Anope::string GetDesc(CommandSource &source) const anope_override
101 	{
102 		return Anope::printf(Language::Translate(source.GetAccount(), _("Logout from %s")), source.service->nick.c_str());
103 	}
104 };
105 
106 class OSLogin : public Module
107 {
108 	CommandOSLogin commandoslogin;
109 	CommandOSLogout commandoslogout;
110 	ExtensibleItem<bool> os_login;
111 
112  public:
OSLogin(const Anope::string & modname,const Anope::string & creator)113 	OSLogin(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
114 		commandoslogin(this), commandoslogout(this), os_login(this, "os_login")
115 	{
116 
117 	}
118 
IsServicesOper(User * u)119 	EventReturn IsServicesOper(User *u) anope_override
120 	{
121 		if (!u->Account()->o->password.empty())
122 		{
123 			if (os_login.HasExt(u))
124 				return EVENT_ALLOW;
125 			return EVENT_STOP;
126 		}
127 
128 		return EVENT_CONTINUE;
129 	}
130 };
131 
132 MODULE_INIT(OSLogin)
133