]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_namesx.cpp
Add const std::string &original_command to OnPreCommand and OnPostCommand, which...
[user/henk/code/inspircd.git] / src / modules / m_namesx.cpp
1 /*   +------------------------------------+
2  *   | Inspire Internet Relay Chat Daemon |
3  *   +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *   E-mail:
7  *      <brain@chatspike.net>
8  *      <Craig@chatspike.net>
9  * 
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include "users.h"
20 #include "channels.h"
21 #include "modules.h"
22 #include "inspircd.h"
23
24 static const char* dummy = "ON";
25
26 /* $ModDesc: Provides aliases of commands. */
27
28 class ModuleNamesX : public Module
29 {
30  public:
31         
32         ModuleNamesX(InspIRCd* Me)
33                 : Module::Module(Me)
34         {
35         }
36
37         void Implements(char* List)
38         {
39                 List[I_OnPreCommand] = List[I_OnUserList] = List[I_On005Numeric] = 1;
40         }
41
42         virtual ~ModuleNamesX()
43         {
44         }
45
46         virtual Version GetVersion()
47         {
48                 return Version(1,0,0,1,VF_VENDOR);
49         }
50
51         virtual void On005Numeric(std::string &output)
52         {
53                 output.append(" NAMESX");
54         }
55
56         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
57         {
58                 irc::string c = command.c_str();
59                 /* We don't actually create a proper command handler class for PROTOCTL,
60                  * because other modules might want to have PROTOCTL hooks too.
61                  * Therefore, we just hook its as an unvalidated command therefore we
62                  * can capture it even if it doesnt exist! :-)
63                  */
64                 if (c == "PROTOCTL")
65                 {
66                         if ((pcnt) && (!strcasecmp(parameters[0],"NAMESX")))
67                         {
68                                 user->Extend("NAMESX",dummy);
69                                 return 1;
70                         }
71                 }
72                 return 0;
73         }
74
75         virtual int OnUserList(userrec* user, chanrec* Ptr)
76         {
77                 if (user->GetExt("NAMESX"))
78                 {
79                         char list[MAXBUF];
80                         size_t dlen, curlen;
81                         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, Ptr->name);
82                         int numusers = 0;
83                         char* ptr = list + dlen;
84                         CUList *ulist= Ptr->GetUsers();
85                         bool has_user = Ptr->HasUser(user);
86                         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
87                         {
88                                 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
89                                 {
90                                         continue;
91                                 }
92                                 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", Ptr->GetAllPrefixChars(i->second), i->second->nick);
93                                 curlen += ptrlen;
94                                 ptr += ptrlen;
95                                 numusers++;
96                                 if (curlen > (480-NICKMAX))
97                                 {
98                                         /* list overflowed into multiple numerics */
99                                         user->WriteServ(std::string(list));
100                                         /* reset our lengths */
101                                         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, Ptr->name);
102                                         ptr = list + dlen;
103                                         ptrlen = 0;
104                                         numusers = 0;
105                                 }
106                         }
107                         /* if whats left in the list isnt empty, send it */
108                         if (numusers)
109                         {
110                                 user->WriteServ(std::string(list));
111                         }
112                         user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
113                         return 1;
114                 }
115                 return 0;               
116         }
117 };
118
119
120 class ModuleNamesXFactory : public ModuleFactory
121 {
122  public:
123         ModuleNamesXFactory()
124         {
125         }
126
127         ~ModuleNamesXFactory()
128         {
129         }
130
131                 virtual Module * CreateModule(InspIRCd* Me)
132         {
133                 return new ModuleNamesX(Me);
134         }
135 };
136
137
138 extern "C" void * init_module( void )
139 {
140         return new ModuleNamesXFactory;
141 }
142