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