]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_swhois.cpp
Always deny invite to users below halfop status, move OnUserPreInvite up to above...
[user/henk/code/inspircd.git] / src / modules / m_swhois.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
16 /* $ModDesc: Provides the SWHOIS command which allows setting of arbitary WHOIS lines */
17
18 /** Handle /SWHOIS
19  */
20 class CommandSwhois : public Command
21 {
22
23  public:
24         CommandSwhois (InspIRCd* Instance) : Command(Instance,"SWHOIS","o",2)
25         {
26                 this->source = "m_swhois.so";
27                 syntax = "<nick> :<swhois>";
28                 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
29         }
30
31         CmdResult Handle(const std::vector<std::string> &parameters, User* user)
32         {
33                 User* dest = ServerInstance->FindNick(parameters[0]);
34
35                 if (!dest)
36                 {
37                         user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel", user->nick.c_str(), parameters[0].c_str());
38                         return CMD_FAILURE;
39                 }
40
41                 std::string* text;
42                 dest->GetExt("swhois", text);
43
44                 if (text)
45                 {
46                         // We already had it set...
47                         if (!ServerInstance->ULine(user->server))
48                                 // Ulines set SWHOISes silently
49                                 ServerInstance->SNO->WriteToSnoMask('A', "%s used SWHOIS to set %s's extra whois from '%s' to '%s'", user->nick.c_str(), dest->nick.c_str(), text->c_str(), parameters[1].c_str());
50
51                         dest->Shrink("swhois");
52                         delete text;
53                 }
54                 else if (!ServerInstance->ULine(user->server))
55                 {
56                         // Ulines set SWHOISes silently
57                         ServerInstance->SNO->WriteToSnoMask('A', "%s used SWHOIS to set %s's extra whois to '%s'", user->nick.c_str(), dest->nick.c_str(), parameters[1].c_str());
58                 }
59
60                 text = new std::string(parameters[1]);
61                 dest->Extend("swhois", text);
62
63                 /* Bug #376 - feature request -
64                  * To cut down on the amount of commands services etc have to recognise, this only sends METADATA across the network now
65                  * not an actual SWHOIS command. Any SWHOIS command sent from services will be automatically translated to METADATA by this.
66                  * Sorry w00t i know this was your fix, but i got bored and wanted to clear down the tracker :)
67                  * -- Brain
68                  */
69                 ServerInstance->PI->SendMetaData(dest, TYPE_USER, "swhois", *text);
70
71                 // If it's an empty swhois, unset it (not ideal, but ok)
72                 if (text->empty())
73                 {
74                         dest->Shrink("swhois");
75                         delete text;
76                 }
77
78                 return CMD_LOCALONLY;
79         }
80
81 };
82
83 class ModuleSWhois : public Module
84 {
85         CommandSwhois* mycommand;
86
87         ConfigReader* Conf;
88
89  public:
90         ModuleSWhois(InspIRCd* Me) : Module(Me)
91         {
92
93                 Conf = new ConfigReader(ServerInstance);
94                 mycommand = new CommandSwhois(ServerInstance);
95                 ServerInstance->AddCommand(mycommand);
96                 Implementation eventlist[] = { I_OnDecodeMetaData, I_OnWhoisLine, I_OnSyncUserMetaData, I_OnUserQuit, I_OnCleanup, I_OnRehash, I_OnPostCommand };
97                 ServerInstance->Modules->Attach(eventlist, this, 7);
98         }
99
100         void OnRehash(User* user, const std::string &parameter)
101         {
102                 delete Conf;
103                 Conf = new ConfigReader(ServerInstance);
104         }
105
106
107         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
108         int OnWhoisLine(User* user, User* dest, int &numeric, std::string &text)
109         {
110                 /* We use this and not OnWhois because this triggers for remote, too */
111                 if (numeric == 312)
112                 {
113                         /* Insert our numeric before 312 */
114                         std::string* swhois;
115                         dest->GetExt("swhois", swhois);
116                         if (swhois)
117                         {
118                                 ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :%s",user->nick.c_str(), dest->nick.c_str(), swhois->c_str());
119                         }
120                 }
121
122                 /* Dont block anything */
123                 return 0;
124         }
125
126         // Whenever the linking module wants to send out data, but doesnt know what the data
127         // represents (e.g. it is metadata, added to a User or Channel by a module) then
128         // this method is called. We should use the ProtoSendMetaData function after we've
129         // corrected decided how the data should look, to send the metadata on its way if
130         // it is ours.
131         virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
132         {
133                 // check if the linking module wants to know about OUR metadata
134                 if (extname == "swhois")
135                 {
136                         // check if this user has an swhois field to send
137                         std::string* swhois;
138                         user->GetExt("swhois", swhois);
139                         if (swhois)
140                         {
141                                 // call this function in the linking module, let it format the data how it
142                                 // sees fit, and send it on its way. We dont need or want to know how.
143                                 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*swhois);
144                         }
145                 }
146         }
147
148         // when a user quits, tidy up their metadata
149         virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
150         {
151                 std::string* swhois;
152                 user->GetExt("swhois", swhois);
153                 if (swhois)
154                 {
155                         user->Shrink("swhois");
156                         delete swhois;
157                 }
158         }
159
160         // if the module is unloaded, tidy up all our dangling metadata
161         virtual void OnCleanup(int target_type, void* item)
162         {
163                 if (target_type == TYPE_USER)
164                 {
165                         User* user = (User*)item;
166                         std::string* swhois;
167                         user->GetExt("swhois", swhois);
168                         if (swhois)
169                         {
170                                 user->Shrink("swhois");
171                                 delete swhois;
172                         }
173                 }
174         }
175
176         // Whenever the linking module receives metadata from another server and doesnt know what
177         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
178         // module in turn to figure out if this metadata key belongs to them, and what they want
179         // to do with it.
180         // In our case we're only sending a single string around, so we just construct a std::string.
181         // Some modules will probably get much more complex and format more detailed structs and classes
182         // in a textual way for sending over the link.
183         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
184         {
185                 // check if its our metadata key, and its associated with a user
186                 if ((target_type == TYPE_USER) && (extname == "swhois"))
187                 {
188                         User* dest = (User*)target;
189
190                         // if they already have an swhois field, trash it and replace it with the remote one.
191                         std::string* text;
192                         if (dest->GetExt("swhois", text))
193                         {
194                                 dest->Shrink("swhois");
195                                 delete text;
196                         }
197
198                         if (extdata.empty())
199                                 return; // XXX does the command parser even allow sending blank mdata? it needs to here! -- w00t
200
201                         text = new std::string(extdata);
202                         dest->Extend("swhois", text);
203                 }
204         }
205
206         virtual void OnPostCommand(const std::string &command, const std::vector<std::string> &params, User *user, CmdResult result, const std::string &original_line)
207         {
208                 if ((command != "OPER") || (result != CMD_SUCCESS))
209                         return;
210
211                 std::string swhois;
212
213                 for (int i = 0; i < Conf->Enumerate("oper"); i++)
214                 {
215                         std::string name = Conf->ReadValue("oper", "name", i);
216
217                         if (name == params[0])
218                         {
219                                 swhois = Conf->ReadValue("oper", "swhois", i);
220                                 break;
221                         }
222                 }
223
224                 if (!swhois.length())
225                 {
226                         for (int i = 0; i < Conf->Enumerate("type"); i++)
227                         {
228                                 std::string type = Conf->ReadValue("type", "name", i);
229
230                                 if (type == user->oper)
231                                 {
232                                         swhois = Conf->ReadValue("type", "swhois", i);
233                                         break;
234                                 }
235                         }
236                 }
237
238                 std::string *old;
239                 if (user->GetExt("swhois", old))
240                 {
241                         user->Shrink("swhois");
242                         delete old;
243                 }
244
245                 if (!swhois.length())
246                         return;
247
248                 std::string *text = new std::string(swhois);
249                 user->Extend("swhois", text);
250                 ServerInstance->PI->SendMetaData(user, TYPE_USER, "swhois", *text);
251         }
252
253         virtual ~ModuleSWhois()
254         {
255                 delete Conf;
256         }
257
258         virtual Version GetVersion()
259         {
260                 return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
261         }
262 };
263
264 MODULE_INIT(ModuleSWhois)