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