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