]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_customtitle.cpp
54dd9b0d065f760f09462ccfb0634205270b1df6
[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, Module* Creator) : Command(Instance, Creator,"TITLE",0,2)
24         {
25                 syntax = "<user> <password>";
26                 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
27         }
28
29         bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
30         {
31                 std::stringstream hl(hostlist);
32                 std::string xhost;
33                 while (hl >> xhost)
34                 {
35                         if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
36                         {
37                                 return true;
38                         }
39                 }
40                 return false;
41         }
42
43         CmdResult Handle(const std::vector<std::string> &parameters, User* user)
44         {
45                 char TheHost[MAXBUF];
46                 char TheIP[MAXBUF];
47
48                 snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(), user->host.c_str());
49                 snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(), user->GetIPString());
50
51                 ConfigReader Conf(ServerInstance);
52                 for (int i=0; i<Conf.Enumerate("title"); i++)
53                 {
54                         std::string name = Conf.ReadValue("title", "name", "", i);
55                         std::string pass = Conf.ReadValue("title", "password", "", i);
56                         std::string hash = Conf.ReadValue("title", "hash", "", i);
57                         std::string host = Conf.ReadValue("title", "host", "*@*", i);
58                         std::string title = Conf.ReadValue("title", "title", "", i);
59                         std::string vhost = Conf.ReadValue("title", "vhost", "", i);
60
61                         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())
62                         {
63                                 std::string* text;
64                                 if (user->GetExt("ctitle", text))
65                                 {
66                                         user->Shrink("ctitle");
67                                         delete text;
68                                 }
69
70                                 text = new std::string(title);
71                                 user->Extend("ctitle", text);
72
73                                 ServerInstance->PI->SendMetaData(user, "ctitle", *text);
74
75                                 if (!vhost.empty())
76                                         user->ChangeDisplayedHost(vhost.c_str());
77
78                                 user->WriteServ("NOTICE %s :Custom title set to '%s'",user->nick.c_str(), title.c_str());
79
80                                 return CMD_LOCALONLY;
81                         }
82                 }
83
84                 user->WriteServ("NOTICE %s :Invalid title credentials",user->nick.c_str());
85                 return CMD_LOCALONLY;
86         }
87
88 };
89
90 class ModuleCustomTitle : public Module
91 {
92         CommandTitle cmd;
93
94  public:
95         ModuleCustomTitle(InspIRCd* Me) : Module(Me), cmd(Me, this)
96         {
97                 ServerInstance->AddCommand(&cmd);
98                 Implementation eventlist[] = { I_OnDecodeMetaData, I_OnWhoisLine, I_OnSyncUser, I_OnUserQuit, I_OnCleanup };
99                 ServerInstance->Modules->Attach(eventlist, this, 5);
100         }
101
102
103         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
104         ModResult OnWhoisLine(User* user, User* 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* ctitle;
111                         if (dest->GetExt("ctitle", ctitle))
112                         {
113                                 ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :%s",user->nick.c_str(), dest->nick.c_str(), ctitle->c_str());
114                         }
115                 }
116                 /* Dont block anything */
117                 return MOD_RES_PASSTHRU;
118         }
119
120         // Whenever the linking module wants to send out data, but doesnt know what the data
121         // represents (e.g. it is metadata, added to a User or Channel by a module) then
122         // this method is called. We should use the ProtoSendMetaData function after we've
123         // corrected decided how the data should look, to send the metadata on its way if
124         // it is ours.
125         virtual void OnSyncUser(User* user, Module* proto, void* opaque)
126         {
127                 // check if this user has an ctitle field to send
128                 std::string* ctitle;
129                 if (user->GetExt("ctitle", ctitle))
130                         proto->ProtoSendMetaData(opaque,user,"ctitle",*ctitle);
131         }
132
133         // when a user quits, tidy up their metadata
134         virtual void OnUserQuit(User* user, const std::string &message, const std::string &oper_message)
135         {
136                 std::string* ctitle;
137                 if (user->GetExt("ctitle", ctitle))
138                 {
139                         user->Shrink("ctitle");
140                         delete ctitle;
141                 }
142         }
143
144         // if the module is unloaded, tidy up all our dangling metadata
145         virtual void OnCleanup(int target_type, void* item)
146         {
147                 if (target_type == TYPE_USER)
148                 {
149                         User* user = (User*)item;
150                         std::string* ctitle;
151                         if (user->GetExt("ctitle", ctitle))
152                         {
153                                 user->Shrink("ctitle");
154                                 delete ctitle;
155                         }
156                 }
157         }
158
159         // Whenever the linking module receives metadata from another server and doesnt know what
160         // to do with it (of course, hence the 'meta') it calls this method, and it is up to each
161         // module in turn to figure out if this metadata key belongs to them, and what they want
162         // to do with it.
163         // In our case we're only sending a single string around, so we just construct a std::string.
164         // Some modules will probably get much more complex and format more detailed structs and classes
165         // in a textual way for sending over the link.
166         virtual void OnDecodeMetaData(Extensible* target, const std::string &extname, const std::string &extdata)
167         {
168                 User* dest = dynamic_cast<User*>(target);
169                 // check if its our metadata key, and its associated with a user
170                 if (dest && (extname == "ctitle"))
171                 {
172                         std::string* text;
173                         if (dest->GetExt("ctitle", text))
174                         {
175                                 dest->Shrink("ctitle");
176                                 delete text;
177                         }
178                         if (!extdata.empty())
179                         {
180                                 text = new std::string(extdata);
181                                 dest->Extend("ctitle", text);
182                         }
183                 }
184         }
185
186         virtual ~ModuleCustomTitle()
187         {
188         }
189
190         virtual Version GetVersion()
191         {
192                 return Version("$Id$", VF_COMMON | VF_VENDOR, API_VERSION);
193         }
194 };
195
196 MODULE_INIT(ModuleCustomTitle)