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