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