]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_chghost.cpp
8c39114184ef3e32951089de5384eda2f0bfd438
[user/henk/code/inspircd.git] / src / modules / m_chghost.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2010 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 CHGHOST command */
17
18 /** Handle /CHGHOST
19  */
20 class CommandChghost : public Command
21 {
22  private:
23         char* hostmap;
24  public:
25         CommandChghost(Module* Creator, char* hmap) : Command(Creator,"CHGHOST", 2), hostmap(hmap)
26         {
27                 flags_needed = 'o';
28                 syntax = "<nick> <newhost>";
29                 TRANSLATE3(TR_NICK, TR_TEXT, TR_END);
30         }
31
32         CmdResult Handle(const std::vector<std::string> &parameters, User *user)
33         {
34                 const char* x = parameters[1].c_str();
35
36                 for (; *x; x++)
37                 {
38                         if (!hostmap[(unsigned char)*x])
39                         {
40                                 user->WriteServ("NOTICE "+std::string(user->nick)+" :*** CHGHOST: Invalid characters in hostname");
41                                 return CMD_FAILURE;
42                         }
43                 }
44
45                 if ((parameters[1].c_str() - x) > 63)
46                 {
47                         user->WriteServ("NOTICE %s :*** CHGHOST: Host too long", user->nick.c_str());
48                         return CMD_FAILURE;
49                 }
50                 User* dest = ServerInstance->FindNick(parameters[0]);
51
52                 if (!dest)
53                 {
54                         user->WriteNumeric(ERR_NOSUCHNICK, "%s %s :No such nick/channel", user->nick.c_str(), parameters[0].c_str());
55                         return CMD_FAILURE;
56                 }
57
58                 if (IS_LOCAL(dest))
59                 {
60                         if ((dest->ChangeDisplayedHost(parameters[1].c_str())) && (!ServerInstance->ULine(user->server)))
61                         {
62                                 // fix by brain - ulines set hosts silently
63                                 ServerInstance->SNO->WriteGlobalSno('a', std::string(user->nick)+" used CHGHOST to make the displayed host of "+dest->nick+" become "+dest->dhost);
64                         }
65                 }
66
67                 return CMD_SUCCESS;
68         }
69
70         RouteDescriptor GetRouting(User* user, const std::vector<std::string>& parameters)
71         {
72                 User* dest = ServerInstance->FindNick(parameters[0]);
73                 if (dest)
74                         return ROUTE_OPT_UCAST(dest->server);
75                 return ROUTE_LOCALONLY;
76         }
77 };
78
79
80 class ModuleChgHost : public Module
81 {
82         CommandChghost cmd;
83         char hostmap[256];
84  public:
85         ModuleChgHost() : cmd(this, hostmap)
86         {
87                 OnRehash(NULL);
88                 ServerInstance->AddCommand(&cmd);
89                 Implementation eventlist[] = { I_OnRehash };
90                 ServerInstance->Modules->Attach(eventlist, this, 1);
91         }
92
93
94         void OnRehash(User* user)
95         {
96                 ConfigReader Conf;
97                 std::string hmap = Conf.ReadValue("hostname", "charmap", 0);
98
99                 if (hmap.empty())
100                         hmap = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_/0123456789";
101
102                 memset(hostmap, 0, sizeof(hostmap));
103                 for (std::string::iterator n = hmap.begin(); n != hmap.end(); n++)
104                         hostmap[(unsigned char)*n] = 1;
105         }
106
107         ~ModuleChgHost()
108         {
109         }
110
111         Version GetVersion()
112         {
113                 return Version("Provides support for the CHGHOST command", VF_OPTCOMMON | VF_VENDOR);
114         }
115
116 };
117
118 MODULE_INIT(ModuleChgHost)