Anope IRC Services  Version 2.0
ms_del.cpp
Go to the documentation of this file.
1 /* MemoServ 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 
15 {
19  public:
20  MemoDelCallback(CommandSource &_source, ChannelInfo *_ci, MemoInfo *_mi, const Anope::string &list) : NumberList(list, true), source(_source), ci(_ci), mi(_mi)
21  {
22  }
23 
24  void HandleNumber(unsigned number) anope_override
25  {
26  if (!number || number > mi->memos->size())
27  return;
28 
29  FOREACH_MOD(OnMemoDel, (ci ? ci->name : source.nc->display, mi, mi->GetMemo(number - 1)));
30 
31  mi->Del(number - 1);
32  source.Reply(_("Memo %d has been deleted."), number);
33  }
34 };
35 
36 class CommandMSDel : public Command
37 {
38  public:
39  CommandMSDel(Module *creator) : Command(creator, "memoserv/del", 0, 2)
40  {
41  this->SetDesc(_("Delete a memo or memos"));
42  this->SetSyntax(_("[\037channel\037] {\037num\037 | \037list\037 | LAST | ALL}"));
43  }
44 
45  void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
46  {
47  if (Anope::ReadOnly)
48  {
49  source.Reply(READ_ONLY_MODE);
50  return;
51  }
52 
53  MemoInfo *mi;
54  ChannelInfo *ci = NULL;
55  Anope::string numstr = !params.empty() ? params[0] : "", chan;
56 
57  if (!numstr.empty() && numstr[0] == '#')
58  {
59  chan = numstr;
60  numstr = params.size() > 1 ? params[1] : "";
61 
62  ci = ChannelInfo::Find(chan);
63  if (!ci)
64  {
65  source.Reply(CHAN_X_NOT_REGISTERED, chan.c_str());
66  return;
67  }
68  else if (!source.AccessFor(ci).HasPriv("MEMO"))
69  {
70  source.Reply(ACCESS_DENIED);
71  return;
72  }
73  mi = &ci->memos;
74  }
75  else
76  mi = &source.nc->memos;
77  if (numstr.empty() || (!isdigit(numstr[0]) && !numstr.equals_ci("ALL") && !numstr.equals_ci("LAST")))
78  this->OnSyntaxError(source, numstr);
79  else if (mi->memos->empty())
80  {
81  if (!chan.empty())
82  source.Reply(MEMO_X_HAS_NO_MEMOS, chan.c_str());
83  else
84  source.Reply(MEMO_HAVE_NO_MEMOS);
85  }
86  else
87  {
88  if (isdigit(numstr[0]))
89  {
90  MemoDelCallback list(source, ci, mi, numstr);
91  list.Process();
92  }
93  else if (numstr.equals_ci("LAST"))
94  {
95  /* Delete last memo. */
96  FOREACH_MOD(OnMemoDel, (ci ? ci->name : source.nc->display, mi, mi->GetMemo(mi->memos->size() - 1)));
97  mi->Del(mi->memos->size() - 1);
98  source.Reply(_("Memo %d has been deleted."), mi->memos->size() + 1);
99  }
100  else
101  {
102  /* Delete all memos. */
103  for (unsigned i = mi->memos->size(); i > 0; --i)
104  {
105  FOREACH_MOD(OnMemoDel, (ci ? ci->name : source.nc->display, mi, mi->GetMemo(i)));
106  mi->Del(i - 1);
107  }
108  if (!chan.empty())
109  source.Reply(_("All memos for channel %s have been deleted."), chan.c_str());
110  else
111  source.Reply(_("All of your memos have been deleted."));
112  }
113  }
114  return;
115  }
116 
117  bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
118  {
119  this->SendSyntax(source);
120  source.Reply(" ");
121  source.Reply(_("Deletes the specified memo or memos. You can supply\n"
122  "multiple memo numbers or ranges of numbers instead of a\n"
123  "single number, as in the second example below.\n"
124  " \n"
125  "If \002LAST\002 is given, the last memo will be deleted.\n"
126  "If \002ALL\002 is given, deletes all of your memos.\n"
127  " \n"
128  "Examples:\n"
129  " \n"
130  " \002DEL 1\002\n"
131  " Deletes your first memo.\n"
132  " \n"
133  " \002DEL 2-5,7-9\002\n"
134  " Deletes memos numbered 2 through 5 and 7 through 9."));
135  return true;
136  }
137 };
138 
139 class MSDel : public Module
140 {
142 
143  public:
144  MSDel(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, VENDOR),
145  commandmsdel(this)
146  {
147 
148  }
149 };
150 
CoreExport bool ReadOnly
Definition: main.cpp:28
ChannelInfo * ci
Definition: ms_del.cpp:17
Definition: hashcomp.h:84
Anope::string name
Definition: regchannel.h:63
#define ACCESS_DENIED
Definition: language.h:73
CommandSource & source
Definition: ms_del.cpp:16
#define READ_ONLY_MODE
Definition: language.h:71
void Execute(CommandSource &source, const std::vector< Anope::string > &params) anope_override
Definition: ms_del.cpp:45
void SetDesc(const Anope::string &d)
Definition: command.cpp:130
bool equals_ci(const char *_str) const
Definition: anope.h:78
#define MEMO_HAVE_NO_MEMOS
Definition: language.h:115
#define FOREACH_MOD(ename, args)
Definition: modules.h:62
MemoInfo * mi
Definition: ms_del.cpp:18
static ChannelInfo * Find(const Anope::string &name)
Definition: regchannel.cpp:630
#define MEMO_X_HAS_NO_MEMOS
Definition: language.h:116
void HandleNumber(unsigned number) anope_override
Definition: ms_del.cpp:24
virtual void OnSyntaxError(CommandSource &source, const Anope::string &subcommand)
Definition: command.cpp:191
Definition: memo.h:40
CommandMSDel(Module *creator)
Definition: ms_del.cpp:39
void Reply(const char *message,...)
Definition: command.cpp:96
Anope::string display
Definition: account.h:113
Memo * GetMemo(unsigned index) const
Definition: memos.cpp:82
#define anope_override
Definition: services.h:56
bool empty() const
Definition: anope.h:126
MemoInfo memos
Definition: regchannel.h:77
#define MODULE_INIT(x)
Definition: modules.h:45
void SetSyntax(const Anope::string &s)
Definition: command.cpp:140
void Process()
Definition: misc.cpp:98
#define CHAN_X_NOT_REGISTERED
Definition: language.h:84
Serialize::Checker< std::vector< Memo * > > memos
Definition: memo.h:43
MSDel(const Anope::string &modname, const Anope::string &creator)
Definition: ms_del.cpp:144
CommandMSDel commandmsdel
Definition: ms_del.cpp:141
Reference< NickCore > nc
Definition: commands.h:61
void SendSyntax(CommandSource &)
Definition: command.cpp:145
bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
Definition: ms_del.cpp:117
void Del(unsigned index)
Definition: memos.cpp:99
MemoDelCallback(CommandSource &_source, ChannelInfo *_ci, MemoInfo *_mi, const Anope::string &list)
Definition: ms_del.cpp:20
#define _(x)
Definition: services.h:50