]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_customtitle.cpp
Add VF_COMMON to a lot modules which require it. Reported by danielg in bug #369...
[user/henk/code/inspircd.git] / src / modules / m_customtitle.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 #include "users.h"
16 #include "channels.h"
17 #include "modules.h"
18 #include "wildcard.h"
19
20 /* $ModDesc: Provides the TITLE command which allows setting of CUSTOM WHOIS TITLE line */
21
22 /** Handle /TITLE
23  */
24 class cmd_title : public command_t
25 {
26         
27  public:
28         cmd_title (InspIRCd* Instance) : command_t(Instance,"TITLE",0,2)
29         {
30                 this->source = "m_customtitle.so";
31                 syntax = "<user> <password>";
32         }
33
34 bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
35 {
36     std::stringstream hl(hostlist);
37     std::string xhost;
38     while (hl >> xhost)
39     {
40         if (match(host,xhost.c_str()) || match(ip,xhost.c_str(),true))
41         {
42             return true;
43         }
44     }
45     return false;
46 }
47
48         CmdResult Handle(const char** parameters, int pcnt, userrec* user)
49         {
50                 if (!IS_LOCAL(user))
51                         return CMD_LOCALONLY;
52         
53                 char TheHost[MAXBUF];
54                 char TheIP[MAXBUF];
55
56                 snprintf(TheHost,MAXBUF,"%s@%s",user->ident,user->host);
57                 snprintf(TheIP, MAXBUF,"%s@%s",user->ident,user->GetIPString());
58
59                 ConfigReader Conf(ServerInstance);
60                 for (int i=0; i<Conf.Enumerate("title"); i++)
61                 {
62                         std::string name = Conf.ReadValue("title", "name", "", i);
63                         std::string pass = Conf.ReadValue("title", "password", "", i);
64                         std::string host = Conf.ReadValue("title", "host", "*@*", i);
65                         std::string title = Conf.ReadValue("title", "title", "", i);
66                         std::string vhost = Conf.ReadValue("title", "vhost", "", i);
67
68                         if (!strcmp(name.c_str(),parameters[0]) && !strcmp(pass.c_str(),parameters[1]) && OneOfMatches(TheHost,TheIP,host.c_str()) && !title.empty())
69                         {
70                                 std::string* text;
71                                 user->GetExt("ctitle", text);
72
73                                 if (text)
74                                 {
75                                         user->Shrink("ctitle");
76                                         DELETE(text);
77                                 }
78
79                                 text = new std::string(title);
80                                 user->Extend("ctitle", text);
81
82                                 std::deque<std::string>* metadata = new std::deque<std::string>;
83                                 metadata->push_back(user->nick);
84                                 metadata->push_back("ctitle");      // The metadata id
85                                 metadata->push_back(*text);     // The value to send
86                                 Event event((char*)metadata,(Module*)this,"send_metadata");
87                                 event.Send(ServerInstance);
88                                 delete metadata;
89                                                                                         
90                                 if (!vhost.empty())
91                                         user->ChangeDisplayedHost(vhost.c_str());
92
93                                 if (!ServerInstance->ULine(user->server))
94                                         // Ulines set TITLEs silently
95                                         ServerInstance->WriteOpers("*** %s used TITLE to set custom title '%s'",user->nick,title.c_str());
96
97                                 user->WriteServ("NOTICE %s :Custom title set to '%s'",user->nick, title.c_str());
98
99                                 return CMD_SUCCESS;
100                         }
101                 }
102
103                 if (!ServerInstance->ULine(user->server))
104                         // Ulines also fail TITLEs silently
105                         ServerInstance->WriteOpers("*** Failed TITLE attempt by %s!%s@%s using login '%s'",user->nick,user->ident,user->host,parameters[0]);
106
107                 user->WriteServ("NOTICE %s :Invalid title credentials",user->nick);
108                 return CMD_SUCCESS;
109         }
110
111 };
112
113 class ModuleCustomTitle : public Module
114 {
115         cmd_title* mycommand;
116         
117  public:
118         ModuleCustomTitle(InspIRCd* Me) : Module(Me)
119         {
120                 
121                 mycommand = new cmd_title(ServerInstance);
122                 ServerInstance->AddCommand(mycommand);
123         }
124
125         void Implements(char* List)
126         {
127                 List[I_OnDecodeMetaData] = List[I_OnWhoisLine] = List[I_OnSyncUserMetaData] = List[I_OnUserQuit] = List[I_OnCleanup] = 1;
128         }
129
130         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
131         int OnWhoisLine(userrec* user, userrec* dest, int &numeric, std::string &text)
132         {
133                 /* We use this and not OnWhois because this triggers for remote, too */
134                 if (numeric == 312)
135                 {
136                         /* Insert our numeric before 312 */
137                         std::string* ctitle;
138                         dest->GetExt("ctitle", ctitle);
139                         if (ctitle)
140                         {
141                                 ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :%s",user->nick,dest->nick,ctitle->c_str());
142                         }
143                 }
144                 /* Dont block anything */
145                 return 0;
146         }
147
148         // Whenever the linking module wants to send out data, but doesnt know what the data
149         // represents (e.g. it is metadata, added to a userrec or chanrec by a module) then
150         // this method is called. We should use the ProtoSendMetaData function after we've
151         // corrected decided how the data should look, to send the metadata on its way if
152         // it is ours.
153         virtual void OnSyncUserMetaData(userrec* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
154         {
155                 // check if the linking module wants to know about OUR metadata
156                 if (extname == "ctitle")
157                 {
158                         // check if this user has an ctitle field to send
159                         std::string* ctitle;
160                         user->GetExt("ctitle", ctitle);
161                         if (ctitle)
162                         {
163                                 // call this function in the linking module, let it format the data how it
164                                 // sees fit, and send it on its way. We dont need or want to know how.
165                                 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*ctitle);
166                         }
167                 }
168         }
169
170         // when a user quits, tidy up their metadata
171         virtual void OnUserQuit(userrec* user, const std::string &message, const std::string &oper_message)
172         {
173                 std::string* ctitle;
174                 user->GetExt("ctitle", ctitle);
175                 if (ctitle)
176                 {
177                         user->Shrink("ctitle");
178                         DELETE(ctitle);
179                 }
180         }
181
182         // if the module is unloaded, tidy up all our dangling metadata
183         virtual void OnCleanup(int target_type, void* item)
184         {
185                 if (target_type == TYPE_USER)
186                 {
187                         userrec* user = (userrec*)item;
188                         std::string* ctitle;
189                         user->GetExt("ctitle", ctitle);
190                         if (ctitle)
191                         {
192                                 user->Shrink("ctitle");
193                                 DELETE(ctitle);
194                         }
195                 }
196         }
197
198         // Whenever the linking module receives metadata from another server and doesnt know what
199         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
200         // module in turn to figure out if this metadata key belongs to them, and what they want
201         // to do with it.
202         // In our case we're only sending a single string around, so we just construct a std::string.
203         // Some modules will probably get much more complex and format more detailed structs and classes
204         // in a textual way for sending over the link.
205         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
206         {
207                 // check if its our metadata key, and its associated with a user
208                 if ((target_type == TYPE_USER) && (extname == "ctitle"))
209                 {
210                         userrec* dest = (userrec*)target;
211                         // if they dont already have an ctitle field, accept the remote server's
212                         std::string* text;
213                         if (!dest->GetExt("ctitle", text))
214                         {
215                                 std::string* text = new std::string(extdata);
216                                 dest->Extend("ctitle",text);
217                         }
218                 }
219         }
220         
221         virtual ~ModuleCustomTitle()
222         {
223         }
224         
225         virtual Version GetVersion()
226         {
227                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
228         }
229 };
230
231 MODULE_INIT(ModuleCustomTitle)