]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_namedmodes.cpp
Clean up crash on destruction of statics at exit
[user/henk/code/inspircd.git] / src / modules / m_namedmodes.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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 class ModuleNamedModes : public Module
17 {
18  public:
19         ModuleNamedModes()
20         {
21                 Implementation eventlist[] = { I_OnPreMode, I_On005Numeric };
22                 ServerInstance->Modules->Attach(eventlist, this, 2);
23         }
24
25         Version GetVersion()
26         {
27                 return Version("Provides the ability to manipulate modes via long names.",VF_VENDOR);
28         }
29
30         void Prioritize()
31         {
32                 ServerInstance->Modules->SetPriority(this, I_OnPreMode, PRIORITY_FIRST);
33         }
34
35         void On005Numeric(std::string& line)
36         {
37                 std::string::size_type pos = line.find(" CHANMODES=");
38                 if (pos != std::string::npos)
39                 {
40                         pos += 11;
41                         while (line[pos] > 'A' && line[pos] < 'Z')
42                                 pos++;
43                         line.insert(pos, 1, 'Z');
44                 }
45         }
46
47         void DisplayList(User* user, Channel* channel)
48         {
49                 for(char letter = 'A'; letter <= 'z'; letter++)
50                 {
51                         ModeHandler* mh = ServerInstance->Modes->FindMode(letter, MODETYPE_CHANNEL);
52                         if (!mh || mh->IsListMode())
53                                 continue;
54                         if (!channel->IsModeSet(letter))
55                                 continue;
56                         std::string item = mh->name;
57                         if (mh->GetNumParams(true))
58                                 item += "=" + channel->GetModeParameter(letter);
59                         user->WriteNumeric(961, "%s %s %s", user->nick.c_str(), channel->name.c_str(), item.c_str());
60                 }
61                 user->WriteNumeric(960, "%s %s :End of mode list", user->nick.c_str(), channel->name.c_str());
62         }
63
64         ModResult OnPreMode(User* source, User* dest, Channel* channel, const std::vector<std::string>& parameters)
65         {
66                 if (!channel)
67                         return MOD_RES_PASSTHRU;
68                 if (parameters[1].find('Z') == std::string::npos)
69                         return MOD_RES_PASSTHRU;
70                 if (parameters.size() <= 2)
71                 {
72                         DisplayList(source, channel);
73                         return MOD_RES_DENY;
74                 }
75
76                 std::vector<std::string> newparms;
77                 newparms.push_back(parameters[0]);
78                 newparms.push_back(parameters[1]);
79
80                 std::string modelist = newparms[1];
81                 bool adding = true;
82                 unsigned int param_at = 2;
83                 for(unsigned int i = 0; i < modelist.length(); i++)
84                 {
85                         unsigned char modechar = modelist[i];
86                         if (modechar == '+' || modechar == '-')
87                         {
88                                 adding = (modechar == '+');
89                                 continue;
90                         }
91                         ModeHandler *mh = ServerInstance->Modes->FindMode(modechar, MODETYPE_CHANNEL);
92                         if (modechar == 'Z')
93                         {
94                                 modechar = 0;
95                                 std::string name, value;
96                                 if (param_at < parameters.size())
97                                         name = parameters[param_at++];
98                                 std::string::size_type eq = name.find('=');
99                                 if (eq != std::string::npos)
100                                 {
101                                         value = name.substr(eq + 1);
102                                         name = name.substr(0, eq);
103                                 }
104                                 for(char letter = 'A'; modechar == 0 && letter <= 'z'; letter++)
105                                 {
106                                         mh = ServerInstance->Modes->FindMode(letter, MODETYPE_CHANNEL);
107                                         if (mh && mh->name == name)
108                                         {
109                                                 if (mh->GetNumParams(adding))
110                                                 {
111                                                         if (!value.empty())
112                                                         {
113                                                                 newparms.push_back(value);
114                                                                 modechar = letter;
115                                                                 break;
116                                                         }
117                                                 }
118                                                 else
119                                                 {
120                                                         modechar = letter;
121                                                         break;
122                                                 }
123                                         }
124                                 }
125                                 if (modechar)
126                                         modelist[i] = modechar;
127                                 else
128                                         modelist.erase(i, 1);
129                         }
130                         else if (mh && mh->GetNumParams(adding) && param_at < parameters.size())
131                         {
132                                 newparms.push_back(parameters[param_at++]);
133                         }
134                 }
135                 newparms[1] = modelist;
136                 ServerInstance->Modes->Process(newparms, source, false);
137                 return MOD_RES_DENY;
138         }
139 };
140
141 MODULE_INIT(ModuleNamedModes)