]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_namesx.cpp
Add support for CAP to this via multi-prefix. NOTE, the OnNamesList for this and...
[user/henk/code/inspircd.git] / src / modules / m_namesx.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 #include "m_cap.h"
16
17 static const char* dummy = "ON";
18
19 /* $ModDesc: Provides aliases of commands. */
20
21 class ModuleNamesX : public Module
22 {
23  public:
24         
25         ModuleNamesX(InspIRCd* Me)
26                 : Module(Me)
27         {
28                 Implementation eventlist[] = { I_OnSyncUserMetaData, I_OnPreCommand, I_OnUserList, I_On005Numeric, I_OnEvent };
29                 ServerInstance->Modules->Attach(eventlist, this, 5);
30         }
31
32
33         virtual ~ModuleNamesX()
34         {
35         }
36
37         void OnSyncUserMetaData(User* user, Module* proto,void* opaque, const std::string &extname, bool displayable)
38         {
39                 if ((displayable) && (extname == "NAMESX"))
40                         proto->ProtoSendMetaData(opaque, TYPE_USER, user, extname, "Enabled");
41         }
42
43         virtual Version GetVersion()
44         {
45                 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
46         }
47
48         virtual void On005Numeric(std::string &output)
49         {
50                 output.append(" NAMESX");
51         }
52
53         virtual int OnPreCommand(const std::string &command, const char* const* parameters, int pcnt, User *user, bool validated, const std::string &original_line)
54         {
55                 irc::string c = command.c_str();
56                 /* We don't actually create a proper command handler class for PROTOCTL,
57                  * because other modules might want to have PROTOCTL hooks too.
58                  * Therefore, we just hook its as an unvalidated command therefore we
59                  * can capture it even if it doesnt exist! :-)
60                  */
61                 if (c == "PROTOCTL")
62                 {
63                         if ((pcnt) && (!strcasecmp(parameters[0],"NAMESX")))
64                         {
65                                 user->Extend("NAMESX",dummy);
66                                 return 1;
67                         }
68                 }
69                 return 0;
70         }
71
72         virtual int OnUserList(User* user, Channel* Ptr, CUList* &ulist)
73         {
74                 if (user->GetExt("NAMESX"))
75                 {
76                         char list[MAXBUF];
77                         size_t dlen, curlen;
78                         dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, Ptr->IsModeSet('s') ? '@' : Ptr->IsModeSet('p') ? '*' : '=', Ptr->name);
79                         int numusers = 0;
80                         char* ptr = list + dlen;
81
82                         if (!ulist)
83                                 ulist = Ptr->GetUsers();
84
85                         bool has_user = Ptr->HasUser(user);
86                         for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
87                         {
88                                 if ((!has_user) && (i->first->IsModeSet('i')))
89                                         continue;
90
91                                 if (i->first->Visibility && !i->first->Visibility->VisibleTo(user))
92                                         continue;
93
94                                 size_t ptrlen = snprintf(ptr, MAXBUF, "%s%s ", Ptr->GetAllPrefixChars(i->first), i->second.c_str());
95                                 /* OnUserList can change this - reset it back to normal */
96                                 i->second = i->first->nick;
97                                 curlen += ptrlen;
98                                 ptr += ptrlen;
99                                 numusers++;
100                                 if (curlen > (480-NICKMAX))
101                                 {
102                                         /* list overflowed into multiple numerics */
103                                         user->WriteServ(std::string(list));
104                                         /* reset our lengths */
105                                         dlen = curlen = snprintf(list,MAXBUF,"353 %s %c %s :", user->nick, Ptr->IsModeSet('s') ? '@' : Ptr->IsModeSet('p') ? '*' : '=', 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                                 user->WriteServ(std::string(list));
115                         }
116                         user->WriteServ("366 %s %s :End of /NAMES list.", user->nick, Ptr->name);
117                         return 1;
118                 }
119                 return 0;               
120         }
121
122         virtual void OnEvent(Event *ev)
123         {
124                 if (ev->GetEventID() == "cap_req")
125                 {
126                         CapData *data = (CapData *) ev->GetData();
127
128                         std::vector<std::string>::iterator it;
129                         if ((it = std::find(data->wanted.begin(), data->wanted.end(), "multi-prefix")) != data->wanted.end())
130                         {
131                                 // we can handle this, so ACK it, and remove it from the wanted list
132                                 data->wanted.erase(it);
133                                 data->ack.push_back(*it);
134                                 data->user->Extend("NAMESX",dummy);
135                         }
136                 }
137
138                 if (ev->GetEventID() == "cap_ls")
139                 {
140                         CapData *data = (CapData *) ev->GetData();
141                         data->wanted.push_back("multi-prefix");
142                 }
143
144                 if (ev->GetEventID() == "cap_list")
145                 {
146                         CapData *data = (CapData *) ev->GetData();
147
148                         if (data->user->GetExt("NAMESX"))
149                                 data->ack.push_back("multi-prefix");
150                 }
151
152                 if (ev->GetEventID() == "cap_clear")
153                 {
154                         CapData *data = (CapData *) ev->GetData();
155                         data->ack.push_back("-multi-prefix");
156                         data->user->Shrink("NAMESX");
157                 }
158         }
159 };
160
161 MODULE_INIT(ModuleNamesX)