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