]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_customtitle.cpp
Whitespace and empty destructor removal, minor coding style changes
[user/henk/code/inspircd.git] / src / modules / m_customtitle.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
5  *   Copyright (C) 2008 Pippijn van Steenhoven <pip88nl@gmail.com>
6  *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "inspircd.h"
23
24 /* $ModDesc: Provides the TITLE command which allows setting of CUSTOM WHOIS TITLE line */
25
26 /** Handle /TITLE
27  */
28 class CommandTitle : public Command
29 {
30  public:
31         StringExtItem ctitle;
32         CommandTitle(Module* Creator) : Command(Creator,"TITLE", 2),
33                 ctitle("ctitle", Creator)
34         {
35                 syntax = "<user> <password>";
36         }
37
38         bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
39         {
40                 std::stringstream hl(hostlist);
41                 std::string xhost;
42                 while (hl >> xhost)
43                 {
44                         if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
45                         {
46                                 return true;
47                         }
48                 }
49                 return false;
50         }
51
52         CmdResult Handle(const std::vector<std::string> &parameters, User* user)
53         {
54                 char TheHost[MAXBUF];
55                 char TheIP[MAXBUF];
56
57                 snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(), user->host.c_str());
58                 snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(), user->GetIPString().c_str());
59
60                 ConfigTagList tags = ServerInstance->Config->ConfTags("title");
61                 for (ConfigIter i = tags.first; i != tags.second; ++i)
62                 {
63                         std::string Name = i->second->getString("name");
64                         std::string pass = i->second->getString("password");
65                         std::string hash = i->second->getString("hash");
66                         std::string host = i->second->getString("host", "*@*");
67                         std::string title = i->second->getString("title");
68                         std::string vhost = i->second->getString("vhost");
69
70                         if (Name == parameters[0] && !ServerInstance->PassCompare(user, pass, parameters[1], hash) && OneOfMatches(TheHost,TheIP,host.c_str()) && !title.empty())
71                         {
72                                 ctitle.set(user, title);
73
74                                 ServerInstance->PI->SendMetaData(user, "ctitle", title);
75
76                                 if (!vhost.empty())
77                                         user->ChangeDisplayedHost(vhost.c_str());
78
79                                 user->WriteServ("NOTICE %s :Custom title set to '%s'",user->nick.c_str(), title.c_str());
80
81                                 return CMD_SUCCESS;
82                         }
83                 }
84
85                 user->WriteServ("NOTICE %s :Invalid title credentials",user->nick.c_str());
86                 return CMD_SUCCESS;
87         }
88
89 };
90
91 class ModuleCustomTitle : public Module
92 {
93         CommandTitle cmd;
94
95  public:
96         ModuleCustomTitle() : cmd(this)
97         {
98         }
99
100         void init()
101         {
102                 ServerInstance->Modules->AddService(cmd);
103                 ServerInstance->Modules->AddService(cmd.ctitle);
104                 ServerInstance->Modules->Attach(I_OnWhoisLine, this);
105         }
106
107         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
108         ModResult OnWhoisLine(User* user, User* dest, int &numeric, std::string &text)
109         {
110                 /* We use this and not OnWhois because this triggers for remote, too */
111                 if (numeric == 312)
112                 {
113                         /* Insert our numeric before 312 */
114                         const std::string* ctitle = cmd.ctitle.get(dest);
115                         if (ctitle)
116                         {
117                                 ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :%s",user->nick.c_str(), dest->nick.c_str(), ctitle->c_str());
118                         }
119                 }
120                 /* Don't block anything */
121                 return MOD_RES_PASSTHRU;
122         }
123
124         Version GetVersion()
125         {
126                 return Version("Custom Title for users", VF_OPTCOMMON | VF_VENDOR);
127         }
128 };
129
130 MODULE_INIT(ModuleCustomTitle)