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