]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_customtitle.cpp
Replace copyright headers with headers granting specific authors copyright
[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                 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
37         }
38
39         bool OneOfMatches(const char* host, const char* ip, const char* hostlist)
40         {
41                 std::stringstream hl(hostlist);
42                 std::string xhost;
43                 while (hl >> xhost)
44                 {
45                         if (InspIRCd::Match(host, xhost, ascii_case_insensitive_map) || InspIRCd::MatchCIDR(ip, xhost, ascii_case_insensitive_map))
46                         {
47                                 return true;
48                         }
49                 }
50                 return false;
51         }
52
53         CmdResult Handle(const std::vector<std::string> &parameters, User* user)
54         {
55                 char TheHost[MAXBUF];
56                 char TheIP[MAXBUF];
57
58                 snprintf(TheHost,MAXBUF,"%s@%s",user->ident.c_str(), user->host.c_str());
59                 snprintf(TheIP, MAXBUF,"%s@%s",user->ident.c_str(), user->GetIPString());
60
61                 ConfigReader Conf;
62                 for (int i=0; i<Conf.Enumerate("title"); i++)
63                 {
64                         std::string Name = Conf.ReadValue("title", "name", "", i);
65                         std::string pass = Conf.ReadValue("title", "password", "", i);
66                         std::string hash = Conf.ReadValue("title", "hash", "", i);
67                         std::string host = Conf.ReadValue("title", "host", "*@*", i);
68                         std::string title = Conf.ReadValue("title", "title", "", i);
69                         std::string vhost = Conf.ReadValue("title", "vhost", "", i);
70
71                         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())
72                         {
73                                 ctitle.set(user, title);
74
75                                 ServerInstance->PI->SendMetaData(user, "ctitle", title);
76
77                                 if (!vhost.empty())
78                                         user->ChangeDisplayedHost(vhost.c_str());
79
80                                 user->WriteServ("NOTICE %s :Custom title set to '%s'",user->nick.c_str(), title.c_str());
81
82                                 return CMD_SUCCESS;
83                         }
84                 }
85
86                 user->WriteServ("NOTICE %s :Invalid title credentials",user->nick.c_str());
87                 return CMD_SUCCESS;
88         }
89
90 };
91
92 class ModuleCustomTitle : public Module
93 {
94         CommandTitle cmd;
95
96  public:
97         ModuleCustomTitle() : cmd(this)
98         {
99                 ServerInstance->AddCommand(&cmd);
100                 ServerInstance->Extensions.Register(&cmd.ctitle);
101                 ServerInstance->Modules->Attach(I_OnWhoisLine, this);
102         }
103
104         // :kenny.chatspike.net 320 Brain Azhrarn :is getting paid to play games.
105         ModResult OnWhoisLine(User* user, User* dest, int &numeric, std::string &text)
106         {
107                 /* We use this and not OnWhois because this triggers for remote, too */
108                 if (numeric == 312)
109                 {
110                         /* Insert our numeric before 312 */
111                         const std::string* ctitle = cmd.ctitle.get(dest);
112                         if (ctitle)
113                         {
114                                 ServerInstance->SendWhoisLine(user, dest, 320, "%s %s :%s",user->nick.c_str(), dest->nick.c_str(), ctitle->c_str());
115                         }
116                 }
117                 /* Don't block anything */
118                 return MOD_RES_PASSTHRU;
119         }
120
121         ~ModuleCustomTitle()
122         {
123         }
124
125         Version GetVersion()
126         {
127                 return Version("Custom Title for users", VF_OPTCOMMON | VF_VENDOR);
128         }
129 };
130
131 MODULE_INIT(ModuleCustomTitle)