]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_customtitle.cpp
35992d505bd3ba0d5d283793d2077e70128f2622
[user/henk/code/inspircd.git] / src / modules / m_customtitle.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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 TITLE command which allows setting of CUSTOM WHOIS TITLE line */
17
18 /** Handle /TITLE
19  */
20 class CommandTitle : public Command
21 {
22  public:
23         CommandTitle (InspIRCd* Instance) : Command(Instance,"TITLE",0,2)
24         {
25                 this->source = "m_customtitle.so";
26                 syntax = "<user> <password>";
27                 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
28         }
29
30         bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
31         {
32                 std::stringstream hl(hostlist);
33                 std::string xhost;
34                 while (hl >> xhost)
35                 {
36                         if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
37                         {
38                                 return true;
39                         }
40                 }
41                 return false;
42         }
43
44         CmdResult Handle(const std::vector<std::string> &parameters, User* user)
45         {
46                 char TheHost[MAXBUF];
47                 char TheIP[MAXBUF];
48
49                 snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(), user->host.c_str());
50                 snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(), user->GetIPString());
51
52                 ConfigReader Conf(ServerInstance);
53                 for (int i=0; i<Conf.Enumerate("title"); i++)
54                 {
55                         std::string name = Conf.ReadValue("title", "name", "", i);
56                         std::string pass = Conf.ReadValue("title", "password", "", i);
57                         std::string hash = Conf.ReadValue("title", "hash", "", i);
58                         std::string host = Conf.ReadValue("title", "host", "*@*", i);
59                         std::string title = Conf.ReadValue("title", "title", "", i);
60                         std::string vhost = Conf.ReadValue("title", "vhost", "", i);
61
62                         if (!strcmp(name.c_str(),parameters[0].c_str()) && !ServerInstance->PassCompare(user, pass.c_str(), parameters[1].c_str(), hash.c_str()) && OneOfMatches(TheHost,TheIP,host.c_str()) && !title.empty())
63                         {
64                                 std::string* text;
65                                 if (user->GetExt("ctitle", text))
66                                 {
67                                         user->Shrink("ctitle");
68                                         delete text;
69                                 }
70
71                                 text = new std::string(title);
72                                 user->Extend("ctitle", text);
73
74                                 ServerInstance->PI->SendMetaData(user, TYPE_USER, "ctitle", *text);
75
76                                 if (!vhost.empty())
77                                         user->ChangeDisplayedHost(vhost.c_str());
78
79                                 user->WriteServ("NOTICE %s :Custom title set to '%s'",user->nick.c_str(), title.c_str());
80
81                                 return CMD_LOCALONLY;
82                         }
83                 }
84
85                 user->WriteServ("NOTICE %s :Invalid title credentials",user->nick.c_str());
86                 return CMD_LOCALONLY;
87         }
88
89 };
90
91 class ModuleCustomTitle : public Module
92 {
93         CommandTitle cmd;
94
95  public:
96         ModuleCustomTitle(InspIRCd* Me) : Module(Me), cmd(Me)
97         {
98                 ServerInstance->AddCommand(&cmd);
99                 Implementation eventlist[] = { I_OnDecodeMetaData, I_OnWhoisLine, I_OnSyncUserMetaData, I_OnUserQuit, I_OnCleanup };
100                 ServerInstance->Modules->Attach(eventlist, this, 5);
101         }
102
103
104         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
105         int OnWhoisLine(User* user, User* dest, int &numeric, std::string &text)
106         {
107                 /* We use this and not OnWhois because this triggers for remote, too */
108                 if (numeric == 312)
109                 {
110                         /* Insert our numeric before 312 */
111                         std::string* ctitle;
112                         if (dest->GetExt("ctitle", ctitle))
113                         {
114                                 ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :%s",user->nick.c_str(), dest->nick.c_str(), ctitle->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 User or Channel 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(User* 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 == "ctitle")
130                 {
131                         // check if this user has an ctitle field to send
132                         std::string* ctitle;
133                         if (user->GetExt("ctitle", ctitle))
134                         {
135                                 // call this function in the linking module, let it format the data how it
136                                 // sees fit, and send it on its way. We dont need or want to know how.
137                                 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*ctitle);
138                         }
139                 }
140         }
141
142         // when a user quits, tidy up their metadata
143         virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
144         {
145                 std::string* ctitle;
146                 if (user->GetExt("ctitle", ctitle))
147                 {
148                         user->Shrink("ctitle");
149                         delete ctitle;
150                 }
151         }
152
153         // if the module is unloaded, tidy up all our dangling metadata
154         virtual void OnCleanup(int target_type, void* item)
155         {
156                 if (target_type == TYPE_USER)
157                 {
158                         User* user = (User*)item;
159                         std::string* ctitle;
160                         if (user->GetExt("ctitle", ctitle))
161                         {
162                                 user->Shrink("ctitle");
163                                 delete ctitle;
164                         }
165                 }
166         }
167
168         // Whenever the linking module receives metadata from another server and doesnt know what
169         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
170         // module in turn to figure out if this metadata key belongs to them, and what they want
171         // to do with it.
172         // In our case we're only sending a single string around, so we just construct a std::string.
173         // Some modules will probably get much more complex and format more detailed structs and classes
174         // in a textual way for sending over the link.
175         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
176         {
177                 // check if its our metadata key, and its associated with a user
178                 if ((target_type == TYPE_USER) && (extname == "ctitle"))
179                 {
180                         User* dest = (User*)target;
181                         std::string* text;
182                         if (dest->GetExt("ctitle", text))
183                         {
184                                 dest->Shrink("ctitle");
185                                 delete text;
186                         }
187                         if (!extdata.empty())
188                         {
189                                 text = new std::string(extdata);
190                                 dest->Extend("ctitle", text);
191                         }
192                 }
193         }
194
195         virtual ~ModuleCustomTitle()
196         {
197         }
198
199         virtual Version GetVersion()
200         {
201                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
202         }
203 };
204
205 MODULE_INIT(ModuleCustomTitle)