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