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