ms_list.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 CommandMSList : public Command
00017 {
00018  public:
00019         CommandMSList(Module *creator) : Command(creator, "memoserv/list", 0, 2)
00020         {
00021                 this->SetDesc(_("List your memos"));
00022                 this->SetSyntax(_("[\037channel\037] [\037list\037 | NEW]"));
00023         }
00024 
00025         void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
00026         {
00027 
00028                 Anope::string param = !params.empty() ? params[0] : "", chan;
00029                 ChannelInfo *ci = NULL;
00030                 const MemoInfo *mi;
00031 
00032                 if (!param.empty() && param[0] == '#')
00033                 {
00034                         chan = param;
00035                         param = params.size() > 1 ? params[1] : "";
00036 
00037                         ci = ChannelInfo::Find(chan);
00038                         if (!ci)
00039                         {
00040                                 source.Reply(CHAN_X_NOT_REGISTERED, chan.c_str());
00041                                 return;
00042                         }
00043                         else if (!source.AccessFor(ci).HasPriv("MEMO"))
00044                         {
00045                                 source.Reply(ACCESS_DENIED);
00046                                 return;
00047                         }
00048                         mi = &ci->memos;
00049                 }
00050                 else
00051                         mi = &source.nc->memos;
00052 
00053                 if (!param.empty() && !isdigit(param[0]) && !param.equals_ci("NEW"))
00054                         this->OnSyntaxError(source, param);
00055                 else if (!mi->memos->size())
00056                 {
00057                         if (!chan.empty())
00058                                 source.Reply(MEMO_X_HAS_NO_MEMOS, chan.c_str());
00059                         else
00060                                 source.Reply(MEMO_HAVE_NO_MEMOS);
00061                 }
00062                 else
00063                 {
00064                         ListFormatter list;
00065 
00066                         list.AddColumn("Number").AddColumn("Sender").AddColumn("Date/Time");
00067 
00068                         if (!param.empty() && isdigit(param[0]))
00069                         {
00070                                 class MemoListCallback : public NumberList
00071                                 {
00072                                         ListFormatter &list;
00073                                         CommandSource &source;
00074                                         const MemoInfo *mi;
00075                                  public:
00076                                         MemoListCallback(ListFormatter &_list, CommandSource &_source, const MemoInfo *_mi, const Anope::string &numlist) : NumberList(numlist, false), list(_list), source(_source), mi(_mi)
00077                                         {
00078                                         }
00079 
00080                                         void HandleNumber(unsigned number) anope_override
00081                                         {
00082                                                 if (!number || number > mi->memos->size())
00083                                                         return;
00084 
00085                                                 const Memo *m = mi->GetMemo(number);
00086 
00087                                                 ListFormatter::ListEntry entry;
00088                                                 entry["Number"] = (m->unread ? "* " : "  ") + stringify(number + 1);
00089                                                 entry["Sender"] = m->sender;
00090                                                 entry["Date/Time"] = Anope::strftime(m->time);
00091                                                 this->list.AddEntry(entry);
00092                                         }
00093                                 }
00094                                 mlc(list, source, mi, param);
00095                                 mlc.Process();
00096                         }
00097                         else
00098                         {
00099                                 if (!param.empty())
00100                                 {
00101                                         unsigned i, end;
00102                                         for (i = 0, end = mi->memos->size(); i < end; ++i)
00103                                                 if (mi->GetMemo(i)->unread)
00104                                                         break;
00105                                         if (i == end)
00106                                         {
00107                                                 if (!chan.empty())
00108                                                         source.Reply(MEMO_X_HAS_NO_NEW_MEMOS, chan.c_str());
00109                                                 else
00110                                                         source.Reply(MEMO_HAVE_NO_NEW_MEMOS);
00111                                                 return;
00112                                         }
00113                                 }
00114 
00115                                 for (unsigned i = 0, end = mi->memos->size(); i < end; ++i)
00116                                 {
00117                                         if (!param.empty() && !mi->GetMemo(i)->unread)
00118                                                 continue;
00119 
00120                                         const Memo *m = mi->GetMemo(i);
00121 
00122                                         ListFormatter::ListEntry entry;
00123                                         entry["Number"] = (m->unread ? "* " : "  ") + stringify(i + 1);
00124                                         entry["Sender"] = m->sender;
00125                                         entry["Date/Time"] = Anope::strftime(m->time);
00126                                         list.AddEntry(entry);
00127                                 }
00128                         }
00129 
00130                         std::vector<Anope::string> replies;
00131                         list.Process(replies);
00132 
00133                         source.Reply(_("Memos for %s."), ci ? ci->name.c_str() : source.GetNick().c_str());
00134                         for (unsigned i = 0; i < replies.size(); ++i)
00135                                 source.Reply(replies[i]);
00136                 }
00137                 return;
00138         }
00139 
00140         bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
00141         {
00142                 this->SendSyntax(source);
00143                 source.Reply(" ");
00144                 source.Reply(_("Lists any memos you currently have.  With \002NEW\002, lists only\n"
00145                                 "new (unread) memos. Unread memos are marked with a \"*\"\n"
00146                                 "to the left of the memo number. You can also specify a list\n"
00147                                 "of numbers, as in the example below:\n"
00148                                 "   \002LIST 2-5,7-9\002\n"
00149                                 "      Lists memos numbered 2 through 5 and 7 through 9."));
00150                 return true;
00151         }
00152 };
00153 
00154 class MSList : public Module
00155 {
00156         CommandMSList commandmslist;
00157 
00158  public:
00159         MSList(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE),
00160                 commandmslist(this)
00161         {
00162                 this->SetAuthor("Anope");
00163 
00164         }
00165 };
00166 
00167 MODULE_INIT(MSList)