ms_del.cpp

Go to the documentation of this file.
00001 /* MemoServ core functions
00002  *
00003  * (C) 2003-2013 Anope Team
00004  * Contact us at team@anope.org
00005  *
00006  * Please read COPYING and README for further details.
00007  *
00008  * Based on the original code of Epona by Lara.
00009  * Based on the original code of Services by Andy Church.
00010  */
00011 
00012 /*************************************************************************/
00013 
00014 #include "module.h"
00015 
00016 class MemoDelCallback : public NumberList
00017 {
00018         CommandSource &source;
00019         ChannelInfo *ci;
00020         MemoInfo *mi;
00021  public:
00022         MemoDelCallback(CommandSource &_source, ChannelInfo *_ci, MemoInfo *_mi, const Anope::string &list) : NumberList(list, true), source(_source), ci(_ci), mi(_mi)
00023         {
00024         }
00025 
00026         void HandleNumber(unsigned number) anope_override
00027         {
00028                 if (!number || number > mi->memos->size())
00029                         return;
00030 
00031                 if (ci)
00032                         FOREACH_MOD(I_OnMemoDel, OnMemoDel(ci, mi, mi->GetMemo(number - 1)));
00033                 else
00034                         FOREACH_MOD(I_OnMemoDel, OnMemoDel(source.nc, mi, mi->GetMemo(number - 1)));
00035 
00036                 mi->Del(number - 1);
00037                 source.Reply(_("Memo %d has been deleted."), number);
00038         }
00039 };
00040 
00041 class CommandMSDel : public Command
00042 {
00043  public:
00044         CommandMSDel(Module *creator) : Command(creator, "memoserv/del", 0, 2)
00045         {
00046                 this->SetDesc(_("Delete a memo or memos"));
00047                 this->SetSyntax(_("[\037channel\037] {\037num\037 | \037list\037 | LAST | ALL}"));
00048         }
00049 
00050         void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
00051         {
00052 
00053                 MemoInfo *mi;
00054                 ChannelInfo *ci = NULL;
00055                 Anope::string numstr = !params.empty() ? params[0] : "", chan;
00056 
00057                 if (!numstr.empty() && numstr[0] == '#')
00058                 {
00059                         chan = numstr;
00060                         numstr = params.size() > 1 ? params[1] : "";
00061 
00062                         ci = ChannelInfo::Find(chan);
00063                         if (!ci)
00064                         {
00065                                 source.Reply(CHAN_X_NOT_REGISTERED, chan.c_str());
00066                                 return;
00067                         }
00068                         else if (Anope::ReadOnly)
00069                         {
00070                                 source.Reply(READ_ONLY_MODE);
00071                                 return;
00072                         }
00073                         else if (!source.AccessFor(ci).HasPriv("MEMO"))
00074                         {
00075                                 source.Reply(ACCESS_DENIED);
00076                                 return;
00077                         }
00078                         mi = &ci->memos;
00079                 }
00080                 else
00081                         mi = const_cast<MemoInfo *>(&source.nc->memos);
00082                 if (numstr.empty() || (!isdigit(numstr[0]) && !numstr.equals_ci("ALL") && !numstr.equals_ci("LAST")))
00083                         this->OnSyntaxError(source, numstr);
00084                 else if (mi->memos->empty())
00085                 {
00086                         if (!chan.empty())
00087                                 source.Reply(MEMO_X_HAS_NO_MEMOS, chan.c_str());
00088                         else
00089                                 source.Reply(MEMO_HAVE_NO_MEMOS);
00090                 }
00091                 else
00092                 {
00093                         if (isdigit(numstr[0]))
00094                         {
00095                                 MemoDelCallback list(source, ci, mi, numstr);
00096                                 list.Process();
00097                         }
00098                         else if (numstr.equals_ci("LAST"))
00099                         {
00100                                 /* Delete last memo. */
00101                                 if (ci)
00102                                         FOREACH_MOD(I_OnMemoDel, OnMemoDel(ci, mi, mi->GetMemo(mi->memos->size() - 1)));
00103                                 else
00104                                         FOREACH_MOD(I_OnMemoDel, OnMemoDel(source.nc, mi, mi->GetMemo(mi->memos->size() - 1)));
00105                                 mi->Del(mi->memos->size() - 1);
00106                                 source.Reply(_("Memo %d has been deleted."), mi->memos->size() + 1);
00107                         }
00108                         else
00109                         {
00110                                 /* Delete all memos. */
00111                                 for (unsigned i = 0, end = mi->memos->size(); i < end; ++i)
00112                                 {
00113                                         if (ci)
00114                                                 FOREACH_MOD(I_OnMemoDel, OnMemoDel(ci, mi, mi->GetMemo(i)));
00115                                         else
00116                                                 FOREACH_MOD(I_OnMemoDel, OnMemoDel(source.nc, mi, mi->GetMemo(i)));
00117                                         delete mi->GetMemo(i);
00118                                 }
00119                                 mi->memos->clear();
00120                                 if (!chan.empty())
00121                                         source.Reply(_("All memos for channel %s have been deleted."), chan.c_str());
00122                                 else
00123                                         source.Reply(_("All of your memos have been deleted."));
00124                         }
00125                 }
00126                 return;
00127         }
00128 
00129         bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
00130         {
00131                 this->SendSyntax(source);
00132                 source.Reply(" ");
00133                 source.Reply(_("Deletes the specified memo or memos. You can supply\n"
00134                                 "multiple memo numbers or ranges of numbers instead of a\n"
00135                                 "single number, as in the second example below.\n"
00136                                 " \n"
00137                                 "If \002LAST\002 is given, the last memo will be deleted.\n"
00138                                 "If \002ALL\002 is given, deletes all of your memos.\n"
00139                                 " \n"
00140                                 "Examples:\n"
00141                                 " \n"
00142                                 "   \002DEL 1\002\n"
00143                                 "      Deletes your first memo.\n"
00144                                 " \n"
00145                                 "   \002DEL 2-5,7-9\002\n"
00146                                 "      Deletes memos numbered 2 through 5 and 7 through 9."));
00147                 return true;
00148         }
00149 };
00150 
00151 class MSDel : public Module
00152 {
00153         CommandMSDel commandmsdel;
00154 
00155  public:
00156         MSDel(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE),
00157                 commandmsdel(this)
00158         {
00159                 this->SetAuthor("Anope");
00160 
00161         }
00162 };
00163 
00164 MODULE_INIT(MSDel)