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