]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_sethost.cpp
ffbc66ddcf4bb2a2db53cff629f227ccf7f7a912
[user/henk/code/inspircd.git] / src / modules / m_sethost.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2009 InspIRCd Development Team
6  * See: http://wiki.inspircd.org/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 support for the SETHOST command */
17
18 /** Handle /SETHOST
19  */
20 class CommandSethost : public Command
21 {
22  private:
23         char* hostmap;
24  public:
25         CommandSethost(Module* Creator, char* hmap) : Command(Creator,"SETHOST", 1), hostmap(hmap)
26         {
27                 flags_needed = 'o'; syntax = "<new-hostname>";
28                 TRANSLATE2(TR_TEXT, TR_END);
29         }
30
31         CmdResult Handle (const std::vector<std::string>& parameters, User *user)
32         {
33                 size_t len = 0;
34                 for (std::string::const_iterator x = parameters[0].begin(); x != parameters[0].end(); x++, len++)
35                 {
36                         if (!hostmap[(const unsigned char)*x])
37                         {
38                                 user->WriteServ("NOTICE "+std::string(user->nick)+" :*** SETHOST: Invalid characters in hostname");
39                                 return CMD_FAILURE;
40                         }
41                 }
42                 if (len == 0)
43                 {
44                         user->WriteServ("NOTICE %s :*** SETHOST: Host must be specified", user->nick.c_str());
45                         return CMD_FAILURE;
46                 }
47                 if (len > 64)
48                 {
49                         user->WriteServ("NOTICE %s :*** SETHOST: Host too long",user->nick.c_str());
50                         return CMD_FAILURE;
51                 }
52
53                 if (user->ChangeDisplayedHost(parameters[0].c_str()))
54                 {
55                         ServerInstance->SNO->WriteGlobalSno('a', std::string(user->nick)+" used SETHOST to change their displayed host to "+user->dhost);
56                         return CMD_SUCCESS;
57                 }
58
59                 return CMD_FAILURE;
60         }
61 };
62
63
64 class ModuleSetHost : public Module
65 {
66         CommandSethost cmd;
67         char hostmap[256];
68  public:
69         ModuleSetHost(InspIRCd* Me)
70                 : Module(Me), cmd(this, hostmap)
71         {
72                 OnRehash(NULL);
73                 ServerInstance->AddCommand(&cmd);
74                 Implementation eventlist[] = { I_OnRehash };
75                 ServerInstance->Modules->Attach(eventlist, this, 1);
76         }
77
78
79         void OnRehash(User* user)
80         {
81                 ConfigReader Conf(ServerInstance);
82                 std::string hmap = Conf.ReadValue("hostname", "charmap", 0);
83
84                 if (hmap.empty())
85                         hmap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_/0123456789";
86
87                 memset(hostmap, 0, sizeof(hostmap));
88                 for (std::string::iterator n = hmap.begin(); n != hmap.end(); n++)
89                         hostmap[(unsigned char)*n] = 1;
90         }
91
92         virtual ~ModuleSetHost()
93         {
94         }
95
96         virtual Version GetVersion()
97         {
98                 return Version("Provides support for the SETHOST command", VF_VENDOR, API_VERSION);
99         }
100
101 };
102
103 MODULE_INIT(ModuleSetHost)