Anope IRC Services  Version 2.0
hostserv.cpp
Go to the documentation of this file.
1 /* HostServ core functions
2  *
3  * (C) 2003-2014 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 HostServCore : public Module
15 {
17  public:
18  HostServCore(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, PSEUDOCLIENT | VENDOR)
19  {
20  if (!IRCD || !IRCD->CanSetVHost)
21  throw ModuleException("Your IRCd does not support vhosts");
22  }
23 
25  {
26  const Anope::string &hsnick = conf->GetModule(this)->Get<const Anope::string>("client");
27 
28  if (hsnick.empty())
29  throw ConfigException(Module::name + ": <client> must be defined");
30 
31  BotInfo *bi = BotInfo::Find(hsnick, true);
32  if (!bi)
33  throw ConfigException(Module::name + ": no bot named " + hsnick);
34 
35  HostServ = bi;
36  }
37 
39  {
40  if (!IRCD->CanSetVHost)
41  return;
42 
43  const NickAlias *na = NickAlias::Find(u->nick);
44  if (!na || na->nc != u->Account() || !na->HasVhost())
45  na = NickAlias::Find(u->Account()->display);
46  if (!na || !na->HasVhost())
47  return;
48 
49  if (u->vhost.empty() || !u->vhost.equals_cs(na->GetVhostHost()) || (!na->GetVhostIdent().empty() && !u->GetVIdent().equals_cs(na->GetVhostIdent())))
50  {
51  IRCD->SendVhost(u, na->GetVhostIdent(), na->GetVhostHost());
52 
53  u->vhost = na->GetVhostHost();
54  u->UpdateHost();
55 
56  if (IRCD->CanSetVIdent && !na->GetVhostIdent().empty())
57  u->SetVIdent(na->GetVhostIdent());
58 
59  if (HostServ)
60  {
61  if (!na->GetVhostIdent().empty())
62  u->SendMessage(HostServ, _("Your vhost of \002%s\002@\002%s\002 is now activated."), na->GetVhostIdent().c_str(), na->GetVhostHost().c_str());
63  else
64  u->SendMessage(HostServ, _("Your vhost of \002%s\002 is now activated."), na->GetVhostHost().c_str());
65  }
66  }
67  }
68 
70  {
71  this->OnUserLogin(u);
72  }
73 
74  EventReturn OnPreHelp(CommandSource &source, const std::vector<Anope::string> &params) anope_override
75  {
76  if (!params.empty() || source.c || source.service != *HostServ)
77  return EVENT_CONTINUE;
78  source.Reply(_("%s commands:"), HostServ->nick.c_str());
79  return EVENT_CONTINUE;
80  }
81 
83  {
84  if (Config->GetModule(this)->Get<bool>("activate_on_set"))
85  {
86  User *u = User::Find(na->nick);
87 
88  if (u && u->Account() == na->nc)
89  {
90  IRCD->SendVhost(u, na->GetVhostIdent(), na->GetVhostHost());
91 
92  u->vhost = na->GetVhostHost();
93  u->UpdateHost();
94 
95  if (IRCD->CanSetVIdent && !na->GetVhostIdent().empty())
96  u->SetVIdent(na->GetVhostIdent());
97 
98  if (HostServ)
99  {
100  if (!na->GetVhostIdent().empty())
101  u->SendMessage(HostServ, _("Your vhost of \002%s\002@\002%s\002 is now activated."), na->GetVhostIdent().c_str(), na->GetVhostHost().c_str());
102  else
103  u->SendMessage(HostServ, _("Your vhost of \002%s\002 is now activated."), na->GetVhostHost().c_str());
104  }
105  }
106  }
107  }
108 
110  {
111  if (Config->GetModule(this)->Get<bool>("activate_on_set"))
112  {
113  User *u = User::Find(na->nick);
114 
115  if (u && u->Account() == na->nc)
116  IRCD->SendVhostDel(u);
117  }
118  }
119 };
120 
122 
Serialize::Reference< NickCore > nc
Definition: account.h:47
Definition: bots.h:24
static NickAlias * Find(const Anope::string &nick)
Definition: nickalias.cpp:121
void OnNickUpdate(User *u) anope_override
Definition: hostserv.cpp:69
Anope::string name
Definition: modules.h:221
virtual void SendVhost(User *u, const Anope::string &vident, const Anope::string &vhost)
Definition: protocol.h:99
EventReturn OnPreHelp(CommandSource &source, const std::vector< Anope::string > &params) anope_override
Definition: hostserv.cpp:74
void OnReload(Configuration::Conf *conf) anope_override
Definition: hostserv.cpp:24
const Anope::string & GetVhostHost() const
Definition: nickalias.cpp:106
bool HasVhost() const
Definition: nickalias.cpp:96
Definition: users.h:34
void OnDeleteVhost(NickAlias *na) anope_override
Definition: hostserv.cpp:109
bool CanSetVHost
Definition: protocol.h:53
HostServCore(const Anope::string &modname, const Anope::string &creator)
Definition: hostserv.cpp:18
bool CanSetVIdent
Definition: protocol.h:53
Definition: Config.cs:26
void OnSetVhost(NickAlias *na) anope_override
Definition: hostserv.cpp:82
void OnUserLogin(User *u) anope_override
Definition: hostserv.cpp:38
void SetVIdent(const Anope::string &ident)
Definition: users.cpp:238
#define anope_override
Definition: services.h:56
void UpdateHost()
Definition: users.cpp:494
bool empty() const
Definition: anope.h:126
virtual void SendVhostDel(User *)
Definition: protocol.h:100
CoreExport IRCDProto * IRCD
Definition: protocol.cpp:23
EventReturn
Definition: modules.h:129
#define MODULE_INIT(x)
Definition: modules.h:45
Anope::string nick
Definition: users.h:62
static User * Find(const Anope::string &name, bool nick_only=false)
Definition: users.cpp:815
NickCore * Account() const
Definition: users.cpp:422
const char * c_str() const
Definition: anope.h:117
Reference< BotInfo > HostServ
Definition: hostserv.cpp:16
void SendMessage(BotInfo *source, const char *fmt,...)
Definition: users.cpp:320
#define _(x)
Definition: services.h:50
static BotInfo * Find(const Anope::string &nick, bool nick_only=false)
Definition: bots.cpp:249
Anope::string vhost
Definition: users.h:67
const Anope::string & GetVhostIdent() const
Definition: nickalias.cpp:101