]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_customtitle.cpp
Add support for blacklists and whitelists, just http password auth to go (the most...
[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 std::vector<std::string> &parameters, 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 hash = Conf.ReadValue("title", "hash", "", i);
63                         std::string host = Conf.ReadValue("title", "host", "*@*", i);
64                         std::string title = Conf.ReadValue("title", "title", "", i);
65                         std::string vhost = Conf.ReadValue("title", "vhost", "", i);
66
67                         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())
68                         {
69                                 std::string* text;
70                                 user->GetExt("ctitle", text);
71
72                                 if (text)
73                                 {
74                                         user->Shrink("ctitle");
75                                         delete text;
76                                 }
77
78                                 text = new std::string(title);
79                                 user->Extend("ctitle", text);
80
81                                 ServerInstance->PI->SendMetaData(user, TYPE_USER, "ctitle", *text);
82                                                                                         
83                                 if (!vhost.empty())
84                                         user->ChangeDisplayedHost(vhost.c_str());
85
86                                 if (!ServerInstance->ULine(user->server))
87                                         // Ulines set TITLEs silently
88                                         ServerInstance->SNO->WriteToSnoMask('A', "%s used TITLE to set custom title '%s'",user->nick,title.c_str());
89
90                                 user->WriteServ("NOTICE %s :Custom title set to '%s'",user->nick, title.c_str());
91
92                                 return CMD_SUCCESS;
93                         }
94                 }
95
96                 if (!ServerInstance->ULine(user->server))
97                         // Ulines also fail TITLEs silently
98                         ServerInstance->SNO->WriteToSnoMask('A', "Failed TITLE attempt by %s!%s@%s using login '%s'",user->nick,user->ident,user->host,parameters[0].c_str());
99
100                 user->WriteServ("NOTICE %s :Invalid title credentials",user->nick);
101                 return CMD_SUCCESS;
102         }
103
104 };
105
106 class ModuleCustomTitle : public Module
107 {
108         CommandTitle* mycommand;
109         
110  public:
111         ModuleCustomTitle(InspIRCd* Me) : Module(Me)
112         {
113                 
114                 mycommand = new CommandTitle(ServerInstance);
115                 ServerInstance->AddCommand(mycommand);
116                 Implementation eventlist[] = { I_OnDecodeMetaData, I_OnWhoisLine, I_OnSyncUserMetaData, I_OnUserQuit, I_OnCleanup };
117                 ServerInstance->Modules->Attach(eventlist, this, 5);
118         }
119
120
121         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
122         int OnWhoisLine(User* user, User* dest, int &numeric, std::string &text)
123         {
124                 /* We use this and not OnWhois because this triggers for remote, too */
125                 if (numeric == 312)
126                 {
127                         /* Insert our numeric before 312 */
128                         std::string* ctitle;
129                         dest->GetExt("ctitle", ctitle);
130                         if (ctitle)
131                         {
132                                 ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :%s",user->nick,dest->nick,ctitle->c_str());
133                         }
134                 }
135                 /* Dont block anything */
136                 return 0;
137         }
138
139         // Whenever the linking module wants to send out data, but doesnt know what the data
140         // represents (e.g. it is metadata, added to a User or Channel by a module) then
141         // this method is called. We should use the ProtoSendMetaData function after we've
142         // corrected decided how the data should look, to send the metadata on its way if
143         // it is ours.
144         virtual void OnSyncUserMetaData(User* user, Module* proto, void* opaque, const std::string &extname, bool displayable)
145         {
146                 // check if the linking module wants to know about OUR metadata
147                 if (extname == "ctitle")
148                 {
149                         // check if this user has an ctitle field to send
150                         std::string* ctitle;
151                         user->GetExt("ctitle", ctitle);
152                         if (ctitle)
153                         {
154                                 // call this function in the linking module, let it format the data how it
155                                 // sees fit, and send it on its way. We dont need or want to know how.
156                                 proto->ProtoSendMetaData(opaque,TYPE_USER,user,extname,*ctitle);
157                         }
158                 }
159         }
160
161         // when a user quits, tidy up their metadata
162         virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
163         {
164                 std::string* ctitle;
165                 user->GetExt("ctitle", ctitle);
166                 if (ctitle)
167                 {
168                         user->Shrink("ctitle");
169                         delete ctitle;
170                 }
171         }
172
173         // if the module is unloaded, tidy up all our dangling metadata
174         virtual void OnCleanup(int target_type, void* item)
175         {
176                 if (target_type == TYPE_USER)
177                 {
178                         User* user = (User*)item;
179                         std::string* ctitle;
180                         user->GetExt("ctitle", ctitle);
181                         if (ctitle)
182                         {
183                                 user->Shrink("ctitle");
184                                 delete ctitle;
185                         }
186                 }
187         }
188
189         // Whenever the linking module receives metadata from another server and doesnt know what
190         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
191         // module in turn to figure out if this metadata key belongs to them, and what they want
192         // to do with it.
193         // In our case we're only sending a single string around, so we just construct a std::string.
194         // Some modules will probably get much more complex and format more detailed structs and classes
195         // in a textual way for sending over the link.
196         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
197         {
198                 // check if its our metadata key, and its associated with a user
199                 if ((target_type == TYPE_USER) && (extname == "ctitle"))
200                 {
201                         User* dest = (User*)target;
202                         // if they dont already have an ctitle field, accept the remote server's
203                         std::string* text;
204                         if (!dest->GetExt("ctitle", text))
205                         {
206                                 std::string* ntext = new std::string(extdata);
207                                 dest->Extend("ctitle",ntext);
208                         }
209                 }
210         }
211         
212         virtual ~ModuleCustomTitle()
213         {
214         }
215         
216         virtual Version GetVersion()
217         {
218                 return Version(1, 2, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
219         }
220 };
221
222 MODULE_INIT(ModuleCustomTitle)