Anope IRC Services  Version 2.0
os_mode.cpp
Go to the documentation of this file.
1 /* OperServ 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 CommandOSMode : public Command
15 {
16  public:
17  CommandOSMode(Module *creator) : Command(creator, "operserv/mode", 2, 3)
18  {
19  this->SetDesc(_("Change channel modes"));
20  this->SetSyntax(_("\037channel\037 \037modes\037"));
21  this->SetSyntax(_("\037channel\037 CLEAR [ALL]"));
22  }
23 
24  void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
25  {
26  const Anope::string &target = params[0];
27  const Anope::string &modes = params[1];
28 
29  Channel *c = Channel::Find(target);
30  if (!c)
31  source.Reply(CHAN_X_NOT_IN_USE, target.c_str());
32  else if (c->bouncy_modes)
33  source.Reply(_("Services is unable to change modes. Are your servers' U:lines configured correctly?"));
34  else if (modes.equals_ci("CLEAR"))
35  {
36  bool all = params.size() > 2 && params[2].equals_ci("ALL");
37 
38  const Channel::ModeList chmodes = c->GetModes();
39  for (Channel::ModeList::const_iterator it = chmodes.begin(), it_end = chmodes.end(); it != it_end; ++it)
40  c->RemoveMode(c->ci->WhoSends(), it->first, it->second, false);
41 
42  if (all)
43  {
44  for (Channel::ChanUserList::iterator it = c->users.begin(), it_end = c->users.end(); it != it_end; ++it)
45  {
46  ChanUserContainer *uc = it->second;
47 
48  if (uc->user->HasMode("OPER"))
49  continue;
50 
51  for (size_t i = uc->status.Modes().length(); i > 0; --i)
52  c->RemoveMode(c->ci->WhoSends(), ModeManager::FindChannelModeByChar(uc->status.Modes()[i - 1]), uc->user->GetUID(), false);
53  }
54 
55  source.Reply(_("All modes cleared on %s."), c->name.c_str());
56  }
57  else
58  source.Reply(_("Non-status modes cleared on %s."), c->name.c_str());
59  }
60  else
61  {
62  spacesepstream sep(modes + (params.size() > 2 ? " " + params[2] : ""));
63  Anope::string mode;
64  int add = 1;
65  Anope::string log_modes, log_params;
66 
67  sep.GetToken(mode);
68  for (unsigned i = 0; i < mode.length(); ++i)
69  {
70  char ch = mode[i];
71 
72  if (ch == '+')
73  {
74  add = 1;
75  log_modes += "+";
76  continue;
77  }
78  else if (ch == '-')
79  {
80  add = 0;
81  log_modes += "-";
82  continue;
83  }
84 
86  if (!cm)
87  continue;
88 
89  Anope::string param, param_log;
90  if (cm->type != MODE_REGULAR)
91  {
92  if (cm->type == MODE_PARAM && !add && anope_dynamic_static_cast<ChannelModeParam *>(cm)->minus_no_arg)
93  ;
94  else if (!sep.GetToken(param))
95  continue;
96 
97  param_log = param;
98 
99  if (cm->type == MODE_STATUS)
100  {
101  User *targ = User::Find(param, true);
102  if (targ == NULL || c->FindUser(targ) == NULL)
103  continue;
104  param = targ->GetUID();
105  }
106  }
107 
108  log_modes += cm->mchar;
109  if (!param.empty())
110  log_params += " " + param_log;
111 
112  if (add)
113  c->SetMode(source.service, cm, param, false);
114  else
115  c->RemoveMode(source.service, cm, param, false);
116  }
117 
118  if (!log_modes.replace_all_cs("+", "").replace_all_cs("-", "").empty())
119  Log(LOG_ADMIN, source, this) << log_modes << log_params << " on " << c->name;
120  }
121  }
122 
123  bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
124  {
125  this->SendSyntax(source);
126  source.Reply(" ");
127  source.Reply(_("Allows Services Operators to change modes for any channel.\n"
128  "Parameters are the same as for the standard /MODE command.\n"
129  "Alternatively, CLEAR may be given to clear all modes on the channel.\n"
130  "If CLEAR ALL is given then all modes, including user status, is removed."));
131  return true;
132  }
133 };
134 
135 class CommandOSUMode : public Command
136 {
137  public:
138  CommandOSUMode(Module *creator) : Command(creator, "operserv/umode", 2, 2)
139  {
140  this->SetDesc(_("Change user modes"));
141  this->SetSyntax(_("\037user\037 \037modes\037"));
142  }
143 
144  void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
145  {
146  const Anope::string &target = params[0];
147  const Anope::string &modes = params[1];
148 
149  User *u2 = User::Find(target, true);
150  if (!u2)
151  source.Reply(NICK_X_NOT_IN_USE, target.c_str());
152  else
153  {
154  u2->SetModes(source.service, "%s", modes.c_str());
155  source.Reply(_("Changed usermodes of \002%s\002 to %s."), u2->nick.c_str(), modes.c_str());
156 
157  u2->SendMessage(source.service, _("\002%s\002 changed your usermodes to %s."), source.GetNick().c_str(), modes.c_str());
158 
159  Log(LOG_ADMIN, source, this) << modes << " on " << target;
160  }
161  }
162 
163  bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
164  {
165  this->SendSyntax(source);
166  source.Reply(" ");
167  source.Reply(_("Allows Services Operators to change modes for any user.\n"
168  "Parameters are the same as for the standard /MODE command."));
169  return true;
170  }
171 };
172 
173 class OSMode : public Module
174 {
177 
178  public:
179  OSMode(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
180  commandosmode(this), commandosumode(this)
181  {
182 
183  }
184 };
185 
bool HasMode(const Anope::string &name) const
Definition: users.cpp:513
const ModeList & GetModes() const
Definition: channels.cpp:238
#define CHAN_X_NOT_IN_USE
Definition: language.h:85
void Execute(CommandSource &source, const std::vector< Anope::string > &params) anope_override
Definition: os_mode.cpp:144
CommandOSMode(Module *creator)
Definition: os_mode.cpp:17
CommandOSUMode commandosumode
Definition: os_mode.cpp:176
Definition: users.h:34
void SetDesc(const Anope::string &d)
Definition: command.cpp:130
bool equals_ci(const char *_str) const
Definition: anope.h:78
Serialize::Reference< ChannelInfo > ci
Definition: channels.h:46
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
Definition: os_mode.cpp:163
Anope::string name
Definition: channels.h:44
#define NICK_X_NOT_IN_USE
Definition: language.h:80
std::multimap< Anope::string, Anope::string > ModeList
Definition: channels.h:36
size_type length() const
Definition: anope.h:131
ModeType type
Definition: modes.h:49
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
Definition: os_mode.cpp:123
const Anope::string & GetUID() const
Definition: users.cpp:230
string replace_all_cs(const string &_orig, const string &_repl) const
Definition: anope.h:229
int16_t bouncy_modes
Definition: channels.h:74
char mchar
Definition: modes.h:47
static ChannelMode * FindChannelModeByChar(char mode)
Definition: modes.cpp:524
static Anope::map< std::pair< bool, Anope::string > > modes
Definition: cs_mode.cpp:745
#define anope_override
Definition: services.h:56
bool empty() const
Definition: anope.h:126
#define MODULE_INIT(x)
Definition: modules.h:45
ChanUserList users
Definition: channels.h:56
ChanUserContainer * FindUser(User *u) const
Definition: channels.cpp:173
void SetSyntax(const Anope::string &s)
Definition: command.cpp:140
ChannelStatus status
Definition: channels.h:28
void SetModes(BotInfo *bi, const char *umodes,...)
Definition: users.cpp:601
Anope::string nick
Definition: users.h:62
static User * Find(const Anope::string &name, bool nick_only=false)
Definition: users.cpp:815
static Channel * Find(const Anope::string &name)
Definition: channels.cpp:920
bool GetToken(Anope::string &token)
Definition: hashcomp.cpp:99
void SetMode(BotInfo *bi, ChannelMode *cm, const Anope::string &param="", bool enforce_mlock=true)
Definition: channels.cpp:401
void SendSyntax(CommandSource &)
Definition: command.cpp:145
const Anope::string & Modes() const
Definition: modes.cpp:96
void Execute(CommandSource &source, const std::vector< Anope::string > &params) anope_override
Definition: os_mode.cpp:24
const char * c_str() const
Definition: anope.h:117
Definition: logger.h:53
CommandOSMode commandosmode
Definition: os_mode.cpp:175
void SendMessage(BotInfo *source, const char *fmt,...)
Definition: users.cpp:320
#define _(x)
Definition: services.h:50
CommandOSUMode(Module *creator)
Definition: os_mode.cpp:138
OSMode(const Anope::string &modname, const Anope::string &creator)
Definition: os_mode.cpp:179
void RemoveMode(BotInfo *bi, ChannelMode *cm, const Anope::string &param="", bool enforce_mlock=true)
Definition: channels.cpp:459