]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_namesx.cpp
Wahhhhhhhhhhhh bwahahaha. Mass commit to tidy up tons of messy include lists
[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)
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                                 ServerInstance->Log(DEBUG,"Setting this user as NAMESX capable");
69                                 user->Extend("NAMESX",dummy);
70                                 return 1;
71                         }
72                 }
73                 return 0;
74         }
75
76         virtual int OnUserList(userrec* user, chanrec* Ptr)
77         {
78                 ServerInstance->Log(DEBUG,"NAMESX called for %s %s",user->nick,Ptr->name);
79                 if (user->GetExt("NAMESX"))
80                 {
81                         ServerInstance->Log(DEBUG,"Using NAMESX user list code");
82                         char list[MAXBUF];
83                         size_t dlen, curlen;
84                         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, Ptr->name);
85                         int numusers = 0;
86                         char* ptr = list + dlen;
87                         CUList *ulist= Ptr->GetUsers();
88                         bool has_user = Ptr->HasUser(user);
89                         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
90                         {
91                                 if ((!has_user) && (i->second->modes[UM_INVISIBLE]))
92                                 {
93                                         continue;
94                                 }
95                                 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", Ptr->GetAllPrefixChars(i->second), i->second->nick);
96                                 curlen += ptrlen;
97                                 ptr += ptrlen;
98                                 numusers++;
99                                 if (curlen > (480-NICKMAX))
100                                 {
101                                         /* list overflowed into multiple numerics */
102                                         ServerInstance->Log(DEBUG,"Send list 1");
103                                         user->WriteServ(list);
104                                         /* reset our lengths */
105                                         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, Ptr->name);
106                                         ptr = list + dlen;
107                                         ptrlen = 0;
108                                         numusers = 0;
109                                 }
110                         }
111                         /* if whats left in the list isnt empty, send it */
112                         if (numusers)
113                         {
114                                 ServerInstance->Log(DEBUG,"Send list 2");
115                                 user->WriteServ(list);
116                         }
117                         user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
118                         ServerInstance->Log(DEBUG,"Returning 1");
119                         return 1;
120                 }
121
122                 ServerInstance->Log(DEBUG,"Returning 0");
123                 return 0;               
124         }
125 };
126
127
128 class ModuleNamesXFactory : public ModuleFactory
129 {
130  public:
131         ModuleNamesXFactory()
132         {
133         }
134
135         ~ModuleNamesXFactory()
136         {
137         }
138
139                 virtual Module * CreateModule(InspIRCd* Me)
140         {
141                 return new ModuleNamesX(Me);
142         }
143 };
144
145
146 extern "C" void * init_module( void )
147 {
148         return new ModuleNamesXFactory;
149 }
150