os_list.cpp

Go to the documentation of this file.
00001 /* OperServ 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 CommandOSChanList : public Command
00017 {
00018  public:
00019         CommandOSChanList(Module *creator) : Command(creator, "operserv/chanlist", 0, 2)
00020         {
00021                 this->SetDesc(_("Lists all channel records"));
00022                 this->SetSyntax(_("[{\037pattern\037 | \037nick\037} [\037SECRET\037]]"));
00023         }
00024 
00025         void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
00026         {
00027                 const Anope::string &pattern = !params.empty() ? params[0] : "";
00028                 const Anope::string &opt = params.size() > 1 ? params[1] : "";
00029                 std::set<Anope::string> modes;
00030                 User *u2;
00031 
00032                 if (!opt.empty() && opt.equals_ci("SECRET"))
00033                 {
00034                         modes.insert("SECRET");
00035                         modes.insert("PRIVATE");
00036                 }
00037 
00038                 ListFormatter list;
00039                 list.AddColumn("Name").AddColumn("Users").AddColumn("Modes").AddColumn("Topic");
00040 
00041                 if (!pattern.empty() && (u2 = User::Find(pattern, true)))
00042                 {
00043                         source.Reply(_("\002%s\002 channel list:"), u2->nick.c_str());
00044 
00045                         for (User::ChanUserList::iterator uit = u2->chans.begin(), uit_end = u2->chans.end(); uit != uit_end; ++uit)
00046                         {
00047                                 ChanUserContainer *cc = *uit;
00048 
00049                                 if (!modes.empty())
00050                                         for (std::set<Anope::string>::iterator it = modes.begin(), it_end = modes.end(); it != it_end; ++it)
00051                                                 if (!cc->chan->HasMode(*it))
00052                                                         continue;
00053 
00054                                 ListFormatter::ListEntry entry;
00055                                 entry["Name"] = cc->chan->name;
00056                                 entry["Users"] = stringify(cc->chan->users.size());
00057                                 entry["Modes"] = cc->chan->GetModes(true, true);
00058                                 entry["Topic"] = cc->chan->topic;
00059                                 list.AddEntry(entry);
00060                         }
00061                 }
00062                 else
00063                 {
00064                         source.Reply(_("Channel list:"));
00065 
00066                         for (channel_map::const_iterator cit = ChannelList.begin(), cit_end = ChannelList.end(); cit != cit_end; ++cit)
00067                         {
00068                                 Channel *c = cit->second;
00069 
00070                                 if (!pattern.empty() && !Anope::Match(c->name, pattern, false, true))
00071                                         continue;
00072                                 if (!modes.empty())
00073                                         for (std::set<Anope::string>::iterator it = modes.begin(), it_end = modes.end(); it != it_end; ++it)
00074                                                 if (!c->HasMode(*it))
00075                                                         continue;
00076 
00077                                 ListFormatter::ListEntry entry;
00078                                 entry["Name"] = c->name;
00079                                 entry["Users"] = stringify(c->users.size());
00080                                 entry["Modes"] = c->GetModes(true, true);
00081                                 entry["Topic"] = c->topic;
00082                                 list.AddEntry(entry);
00083                         }
00084                 }
00085 
00086                 std::vector<Anope::string> replies;
00087                 list.Process(replies);
00088 
00089                 for (unsigned i = 0; i < replies.size(); ++i)
00090                         source.Reply(replies[i]);
00091 
00092                 source.Reply(_("End of channel list."));
00093         }
00094 
00095         bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
00096         {
00097                 this->SendSyntax(source);
00098                 source.Reply(" ");
00099                 source.Reply(_("Lists all channels currently in use on the IRC network, whether they\n"
00100                                 "are registered or not.\n"
00101                                 "If \002pattern\002 is given, lists only channels that match it. If a nickname\n"
00102                                 "is given, lists only the channels the user using it is on. If SECRET is\n"
00103                                 "specified, lists only channels matching \002pattern\002 that have the +s or\n"
00104                                 "+p mode."));
00105 
00106                 if (!Config->RegexEngine.empty())
00107                 {
00108                         source.Reply(" ");
00109                         source.Reply(_("Regex matches are also supported using the %s engine.\n"
00110                                         "Enclose your pattern in // if this is desired."), Config->RegexEngine.c_str());
00111                 }
00112 
00113                 return true;
00114         }
00115 };
00116 
00117 class CommandOSUserList : public Command
00118 {
00119  public:
00120         CommandOSUserList(Module *creator) : Command(creator, "operserv/userlist", 0, 2)
00121         {
00122                 this->SetDesc(_("Lists all user records"));
00123                 this->SetSyntax(_("[{\037pattern\037 | \037channel\037} [\037INVISIBLE\037]]"));
00124         }
00125 
00126         void Execute(CommandSource &source, const std::vector<Anope::string> &params) anope_override
00127         {
00128                 const Anope::string &pattern = !params.empty() ? params[0] : "";
00129                 const Anope::string &opt = params.size() > 1 ? params[1] : "";
00130                 Channel *c;
00131                 std::set<Anope::string> modes;
00132 
00133                 if (!opt.empty() && opt.equals_ci("INVISIBLE"))
00134                         modes.insert("INVIS");
00135 
00136                 ListFormatter list;
00137                 list.AddColumn("Name").AddColumn("Mask");
00138 
00139                 if (!pattern.empty() && (c = Channel::Find(pattern)))
00140                 {
00141                         source.Reply(_("\002%s\002 users list:"), pattern.c_str());
00142 
00143                         for (Channel::ChanUserList::iterator cuit = c->users.begin(), cuit_end = c->users.end(); cuit != cuit_end; ++cuit)
00144                         {
00145                                 ChanUserContainer *uc = *cuit;
00146 
00147                                 if (!modes.empty())
00148                                         for (std::set<Anope::string>::iterator it = modes.begin(), it_end = modes.end(); it != it_end; ++it)
00149                                                 if (!uc->user->HasMode(*it))
00150                                                         continue;
00151 
00152                                 ListFormatter::ListEntry entry;
00153                                 entry["Name"] = uc->user->nick;
00154                                 entry["Mask"] = uc->user->GetIdent() + "@" + uc->user->GetDisplayedHost();
00155                                 list.AddEntry(entry);
00156                         }
00157                 }
00158                 else
00159                 {
00160                         /* Historically this has been ordered, so... */
00161                         Anope::map<User *> ordered_map;
00162                         for (user_map::const_iterator it = UserListByNick.begin(); it != UserListByNick.end(); ++it)
00163                                 ordered_map[it->first] = it->second;
00164 
00165                         source.Reply(_("Users list:"));
00166 
00167                         for (Anope::map<User *>::const_iterator it = ordered_map.begin(); it != ordered_map.end(); ++it)
00168                         {
00169                                 User *u2 = it->second;
00170 
00171                                 if (!pattern.empty())
00172                                 {
00173                                         Anope::string mask = u2->nick + "!" + u2->GetIdent() + "@" + u2->GetDisplayedHost(), mask2 = u2->nick + "!" + u2->GetIdent() + "@" + u2->host, mask3 = u2->nick + "!" + u2->GetIdent() + "@" + (!u2->ip.empty() ? u2->ip : u2->host);
00174                                         if (!Anope::Match(mask, pattern) && !Anope::Match(mask2, pattern) && !Anope::Match(mask3, pattern))
00175                                                 continue;
00176                                         if (!modes.empty())
00177                                                 for (std::set<Anope::string>::iterator mit = modes.begin(), mit_end = modes.end(); mit != mit_end; ++mit)
00178                                                         if (!u2->HasMode(*mit))
00179                                                                 continue;
00180                                 }
00181 
00182                                 ListFormatter::ListEntry entry;
00183                                 entry["Name"] = u2->nick;
00184                                 entry["Mask"] = u2->GetIdent() + "@" + u2->GetDisplayedHost();
00185                                 list.AddEntry(entry);
00186                         }
00187                 }
00188 
00189                 std::vector<Anope::string> replies;
00190                 list.Process(replies);
00191 
00192                 for (unsigned i = 0; i < replies.size(); ++i)
00193                         source.Reply(replies[i]);
00194 
00195                 source.Reply(_("End of users list."));
00196                 return;
00197         }
00198 
00199         bool OnHelp(CommandSource &source, const Anope::string &subcommand) anope_override
00200         {
00201                 this->SendSyntax(source);
00202                 source.Reply(" ");
00203                 source.Reply(_("Lists all users currently online on the IRC network, whether their\n"
00204                                 "nick is registered or not.\n"
00205                                 " \n"
00206                                 "If \002pattern\002 is given, lists only users that match it (it must be in\n"
00207                                 "the format nick!user@host). If \002channel\002 is given, lists only users\n"
00208                                 "that are on the given channel. If INVISIBLE is specified, only users\n"
00209                                 "with the +i flag will be listed."));
00210 
00211                 if (!Config->RegexEngine.empty())
00212                 {
00213                         source.Reply(" ");
00214                         source.Reply(_("Regex matches are also supported using the %s engine.\n"
00215                                         "Enclose your pattern in // if this is desired."), Config->RegexEngine.c_str());
00216                 }
00217 
00218                 return true;
00219         }
00220 };
00221 
00222 class OSList : public Module
00223 {
00224         CommandOSChanList commandoschanlist;
00225         CommandOSUserList commandosuserlist;
00226 
00227  public:
00228         OSList(const Anope::string &modname, const Anope::string &creator) : Module(modname, creator, CORE),
00229                 commandoschanlist(this), commandosuserlist(this)
00230         {
00231                 this->SetAuthor("Anope");
00232 
00233         }
00234 };
00235 
00236 MODULE_INIT(OSList)