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