]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_namesx.cpp
auto-set +s when +n is set (as +n requires +s) - allow +n to be 'set twice' allowing...
[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 "commands.h"
23 #include "inspircd.h"
24 #include <vector>
25
26 static const char* dummy = "ON";
27
28 /* $ModDesc: Provides aliases of commands. */
29
30 class ModuleNamesX : public Module
31 {
32  public:
33         
34         ModuleNamesX(InspIRCd* Me)
35                 : Module::Module(Me)
36         {
37         }
38
39         void Implements(char* List)
40         {
41                 List[I_OnPreCommand] = List[I_OnUserList] = List[I_On005Numeric] = 1;
42         }
43
44         virtual ~ModuleNamesX()
45         {
46         }
47
48         virtual Version GetVersion()
49         {
50                 return Version(1,0,0,1,VF_VENDOR);
51         }
52
53         virtual void On005Numeric(std::string &output)
54         {
55                 output.append(" NAMESX");
56         }
57
58         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated)
59         {
60                 irc::string c = command.c_str();
61                 /* We don't actually create a proper command handler class for PROTOCTL,
62                  * because other modules might want to have PROTOCTL hooks too.
63                  * Therefore, we just hook its as an unvalidated command therefore we
64                  * can capture it even if it doesnt exist! :-)
65                  */
66                 if (c == "PROTOCTL")
67                 {
68                         if ((pcnt) && (!strcasecmp(parameters[0],"NAMESX")))
69                         {
70                                 ServerInstance->Log(DEBUG,"Setting this user as NAMESX capable");
71                                 user->Extend("NAMESX",dummy);
72                                 return 1;
73                         }
74                 }
75                 return 0;
76         }
77
78         virtual int OnUserList(userrec* user, chanrec* Ptr)
79         {
80                 ServerInstance->Log(DEBUG,"NAMESX called for %s %s",user->nick,Ptr->name);
81                 if (user->GetExt("NAMESX"))
82                 {
83                         ServerInstance->Log(DEBUG,"Using NAMESX user list code");
84                         char list[MAXBUF];
85                         size_t dlen, curlen;
86                         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, Ptr->name);
87                         int numusers = 0;
88                         char* ptr = list + dlen;
89                         CUList *ulist= Ptr->GetUsers();
90                         bool has_user = Ptr->HasUser(user);
91                         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
92                         {
93                                 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
94                                 {
95                                         continue;
96                                 }
97                                 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", Ptr->GetAllPrefixChars(i->second), i->second->nick);
98                                 curlen += ptrlen;
99                                 ptr += ptrlen;
100                                 numusers++;
101                                 if (curlen > (480-NICKMAX))
102                                 {
103                                         /* list overflowed into multiple numerics */
104                                         ServerInstance->Log(DEBUG,"Send list 1");
105                                         user->WriteServ(list);
106                                         /* reset our lengths */
107                                         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, Ptr->name);
108                                         ptr = list + dlen;
109                                         ptrlen = 0;
110                                         numusers = 0;
111                                 }
112                         }
113                         /* if whats left in the list isnt empty, send it */
114                         if (numusers)
115                         {
116                                 ServerInstance->Log(DEBUG,"Send list 2");
117                                 user->WriteServ(list);
118                         }
119                         user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
120                         ServerInstance->Log(DEBUG,"Returning 1");
121                         return 1;
122                 }
123
124                 ServerInstance->Log(DEBUG,"Returning 0");
125                 return 0;               
126         }
127 };
128
129
130 class ModuleNamesXFactory : public ModuleFactory
131 {
132  public:
133         ModuleNamesXFactory()
134         {
135         }
136
137         ~ModuleNamesXFactory()
138         {
139         }
140
141                 virtual Module * CreateModule(InspIRCd* Me)
142         {
143                 return new ModuleNamesX(Me);
144         }
145 };
146
147
148 extern "C" void * init_module( void )
149 {
150         return new ModuleNamesXFactory;
151 }
152