]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_namesx.cpp
db8270cb9a6380f02959010c47653aa19e94d106
[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_OnSyncUserMetaData] = List[I_OnPreCommand] = List[I_OnUserList] = List[I_On005Numeric] = 1;
35         }
36
37         virtual ~ModuleNamesX()
38         {
39         }
40
41         void OnSyncUserMetaData(userrec* user, Module* proto,void* opaque, const std::string &extname, bool displayable)
42         {
43                 if ((displayable) && (extname == "NAMESX"))
44                         proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, "Enabled");
45         }
46
47         virtual Version GetVersion()
48         {
49                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
50         }
51
52         virtual void On005Numeric(std::string &output)
53         {
54                 output.append(" NAMESX");
55         }
56
57         virtual int OnPreCommand(const std::string &command, const char** parameters, int pcnt, userrec *user, bool validated, const std::string &original_line)
58         {
59                 irc::string c = command.c_str();
60                 /* We don't actually create a proper command handler class for PROTOCTL,
61                  * because other modules might want to have PROTOCTL hooks too.
62                  * Therefore, we just hook its as an unvalidated command therefore we
63                  * can capture it even if it doesnt exist! :-)
64                  */
65                 if (c == "PROTOCTL")
66                 {
67                         if ((pcnt) && (!strcasecmp(parameters[0],"NAMESX")))
68                         {
69                                 user->Extend("NAMESX",dummy);
70                                 return 1;
71                         }
72                 }
73                 return 0;
74         }
75
76         virtual int OnUserList(userrec* user, chanrec* Ptr, CUList* &ulist)
77         {
78                 if (user->GetExt("NAMESX"))
79                 {
80                         char list[MAXBUF];
81                         size_t dlen, curlen;
82                         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, Ptr->name);
83                         int numusers = 0;
84                         char* ptr = list + dlen;
85
86                         if (!ulist)
87                                 ulist = Ptr->GetUsers();
88
89                         bool has_user = Ptr->HasUser(user);
90                         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
91                         {
92                                 if ((!has_user) && (i->first->IsModeSet('i')))
93                                         continue;
94
95                                 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
96                                         continue;
97
98                                 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", Ptr->GetAllPrefixChars(i->first), i->second.c_str());
99                                 /* OnUserList can change this - reset it back to normal */
100                                 i->second = i->first->nick;
101                                 curlen += ptrlen;
102                                 ptr += ptrlen;
103                                 numusers++;
104                                 if (curlen > (480-NICKMAX))
105                                 {
106                                         /* list overflowed into multiple numerics */
107                                         user->WriteServ(std::string(list));
108                                         /* reset our lengths */
109                                         dlen = curlen = snprintf(list,MAXBUF,"353 %s = %s :", user->nick, Ptr->name);
110                                         ptr = list + dlen;
111                                         ptrlen = 0;
112                                         numusers = 0;
113                                 }
114                         }
115                         /* if whats left in the list isnt empty, send it */
116                         if (numusers)
117                         {
118                                 user->WriteServ(std::string(list));
119                         }
120                         user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
121                         return 1;
122                 }
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