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