]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_hostchange.cpp
Fixes by misspell-fixer
[user/henk/code/inspircd.git] / src / modules / m_hostchange.cpp
index f7ff58fa17aba57b79e9723f2b867c3a11ebb961..8d7abede47bbaca538b9db5465900bd5095ce5fd 100644 (file)
@@ -1 +1,244 @@
-/*       +------------------------------------+\r *       | Inspire Internet Relay Chat Daemon |\r *       +------------------------------------+\r *\r *  InspIRCd: (C) 2002-2007 InspIRCd Development Team\r * See: http://www.inspircd.org/wiki/index.php/Credits\r *\r * This program is free but copyrighted software; see\r *            the file COPYING for details.\r *\r * ---------------------------------------------------\r */\r\r#include "inspircd.h"\r#include "users.h"\r#include "channels.h"\r#include "modules.h"\r\r/* $ModDesc: Provides masking of user hostnames in a different way to m_cloaking */\r\r/** Holds information on a host set by m_hostchange\r */\rclass Host : public classbase\r{\r public:\r        std::string action;\r    std::string newhost;\r};\r\rtypedef std::map<std::string,Host*> hostchanges_t;\r\rclass ModuleHostChange : public Module\r{\r private:\r        hostchanges_t hostchanges;\r     std::string MySuffix;\r  std::string MyPrefix;\r  std::string MySeparator;\r        \r public:\r     ModuleHostChange(InspIRCd* Me)\r         : Module(Me)\r   {\r              OnRehash(NULL,"");\r     }\r      \r       virtual ~ModuleHostChange()\r    {\r              for (hostchanges_t::iterator i = hostchanges.begin(); i != hostchanges.end(); i++)\r             {\r                      DELETE(i->second);\r             }\r              hostchanges.clear();\r   }\r\r     Priority Prioritize()\r  {\r              return (Priority)ServerInstance->PriorityAfter("m_cloaking.so");\r       }\r\r     void Implements(char* List)\r    {\r              List[I_OnRehash] = List[I_OnUserConnect] = 1;\r  }\r\r     virtual void OnRehash(userrec* user, const std::string &parameter)\r     {\r              ConfigReader Conf(ServerInstance);\r             MySuffix = Conf.ReadValue("host","suffix",0);\r          MyPrefix = Conf.ReadValue("host","prefix","",0);\r               MySeparator = Conf.ReadValue("host","separator",".",0);\r                for (hostchanges_t::iterator i = hostchanges.begin(); i != hostchanges.end(); i++)\r             {\r                      DELETE(i->second);\r             }\r              hostchanges.clear();\r           for (int index = 0; index < Conf.Enumerate("hostchange"); index++)\r             {\r                      std::string mask = Conf.ReadValue("hostchange","mask",index);\r                  std::string action = Conf.ReadValue("hostchange","action",index);\r                      std::string newhost = Conf.ReadValue("hostchange","value",index);\r                      Host* x = new Host;\r                    x->action = action;\r                    x->newhost = newhost;\r                  hostchanges[mask] = x;\r         }\r      }\r      \r       virtual Version GetVersion()\r   {\r              // returns the version number of the module to be\r              // listed in /MODULES\r          return Version(1,1,0,1,VF_VENDOR,API_VERSION);\r }\r      \r       virtual void OnUserConnect(userrec* user)\r      {\r              for (hostchanges_t::iterator i = hostchanges.begin(); i != hostchanges.end(); i++)\r             {\r                      if (ServerInstance->MatchText(std::string(user->ident)+"@"+std::string(user->host),i->first))\r                  {\r                              Host* h = (Host*)i->second;\r                            // host of new user matches a hostchange tag's mask\r                            std::string newhost;\r                           if (h->action == "set")\r                                {\r                                      newhost = h->newhost;\r                          }\r                              else if (h->action == "suffix")\r                                {\r                                      newhost = MySuffix;\r                            }\r                              else if (h->action == "addnick")\r                               {\r                                      // first take their nick and strip out non-dns, leaving just [A-Z0-9\-]\r                                        std::string complete;\r                                  std::string old = user->nick;\r                                  for (unsigned int j = 0; j < old.length(); j++)\r                                        {\r                                              if  (((old[j] >= 'A') && (old[j] <= 'Z')) ||\r                                               ((old[j] >= 'a') && (old[j] <= 'z')) ||\r                                                ((old[j] >= '0') && (old[j] <= '9')) ||\r                                                (old[j] == '-'))\r                                           {\r                                                      complete = complete + old[j];\r                                          }\r                                      }\r                                      if (complete.empty())\r                                          complete = "i-have-a-lame-nick";\r                                               \r                                       if (!MyPrefix.empty())\r                                         newhost = MyPrefix + MySeparator + complete;\r                                   else\r                                           newhost = complete + MySeparator + MySuffix;\r                           }\r                              if (!newhost.empty())\r                          {\r                                      user->WriteServ("NOTICE "+std::string(user->nick)+" :Setting your virtual host: " + newhost);\r                                  if (!user->ChangeDisplayedHost(newhost.c_str()))\r                                               user->WriteServ("NOTICE "+std::string(user->nick)+" :Could not set your virtual host: " + newhost);\r                                    return;\r                                }\r                      }\r              }\r      }\r};\r\rMODULE_INIT(ModuleHostChange)\r
\ No newline at end of file
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
+ *
+ *   Copyright (C) 2013, 2018-2020 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2012-2014 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2012 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2009 Robin Burchell <robin+git@viroteck.net>
+ *   Copyright (C) 2009 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2007 Dennis Friis <peavey@inspircd.org>
+ *   Copyright (C) 2006-2007, 2010 Craig Edwards <brain@inspircd.org>
+ *
+ * This file is part of InspIRCd.  InspIRCd is free software: you can
+ * redistribute it and/or modify it under the terms of the GNU General Public
+ * License as published by the Free Software Foundation, version 2.
+ *
+ * This program is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
+ * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
+ * details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+
+#include "inspircd.h"
+#include "modules/account.h"
+
+// Holds information about a <hostchange> rule.
+class HostRule
+{
+ public:
+       enum HostChangeAction
+       {
+               // Add the user's account name to their hostname.
+               HCA_ADDACCOUNT,
+
+               // Add the user's nickname to their hostname.
+               HCA_ADDNICK,
+
+               // Set the user's hostname to the specific value.
+               HCA_SET
+       };
+
+ private:
+       HostChangeAction action;
+       std::string host;
+       std::string mask;
+       insp::flat_set<int> ports;
+       std::string prefix;
+       std::string suffix;
+
+ public:
+       HostRule(const std::string& Mask, const std::string& Host, const insp::flat_set<int>& Ports)
+               : action(HCA_SET)
+               , host(Host)
+               , mask(Mask)
+               , ports(Ports)
+       {
+       }
+
+       HostRule(HostChangeAction Action, const std::string& Mask, const insp::flat_set<int>& Ports, const std::string& Prefix, const std::string& Suffix)
+               : action(Action)
+               , mask(Mask)
+               , ports(Ports)
+               , prefix(Prefix)
+               , suffix(Suffix)
+       {
+       }
+
+       HostChangeAction GetAction() const
+       {
+               return action;
+       }
+
+       const std::string& GetHost() const
+       {
+               return host;
+       }
+
+       bool Matches(LocalUser* user) const
+       {
+               if (!ports.empty() && !ports.count(user->server_sa.port()))
+                       return false;
+
+               if (InspIRCd::MatchCIDR(user->MakeHost(), mask))
+                       return true;
+
+               return InspIRCd::MatchCIDR(user->MakeHostIP(), mask);
+       }
+
+       void Wrap(const std::string& value, std::string& out) const
+       {
+               if (!prefix.empty())
+                       out.append(prefix);
+
+               out.append(value);
+
+               if (!suffix.empty())
+                       out.append(suffix);
+       }
+};
+
+typedef std::vector<HostRule> HostRules;
+
+class ModuleHostChange : public Module
+{
+private:
+       std::bitset<UCHAR_MAX> hostmap;
+       HostRules hostrules;
+
+       std::string CleanName(const std::string& name)
+       {
+               std::string buffer;
+               buffer.reserve(name.length());
+               for (std::string::const_iterator iter = name.begin(); iter != name.end(); ++iter)
+               {
+                       if (hostmap.test(static_cast<unsigned char>(*iter)))
+                       {
+                               buffer.push_back(*iter);
+                       }
+               }
+               return buffer;
+       }
+
+ public:
+       void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
+       {
+               HostRules rules;
+
+               ConfigTagList tags = ServerInstance->Config->ConfTags("hostchange");
+               for (ConfigIter i = tags.first; i != tags.second; ++i)
+               {
+                       ConfigTag* tag = i->second;
+
+                       // Ensure that we have the <hostchange:mask> parameter.
+                       const std::string mask = tag->getString("mask");
+                       if (mask.empty())
+                               throw ModuleException("<hostchange:mask> is a mandatory field, at " + tag->getTagLocation());
+
+                       insp::flat_set<int> ports;
+                       const std::string portlist = tag->getString("ports");
+                       if (!portlist.empty())
+                       {
+                               irc::portparser portrange(portlist, false);
+                               while (int port = portrange.GetToken())
+                                       ports.insert(port);
+                       }
+
+                       // Determine what type of host rule this is.
+                       const std::string action = tag->getString("action");
+                       if (stdalgo::string::equalsci(action, "addaccount"))
+                       {
+                               // The hostname is in the format [prefix]<account>[suffix].
+                               rules.push_back(HostRule(HostRule::HCA_ADDACCOUNT, mask, ports, tag->getString("prefix"), tag->getString("suffix")));
+                       }
+                       else if (stdalgo::string::equalsci(action, "addnick"))
+                       {
+                               // The hostname is in the format [prefix]<nick>[suffix].
+                               rules.push_back(HostRule(HostRule::HCA_ADDNICK, mask, ports, tag->getString("prefix"), tag->getString("suffix")));
+                       }
+                       else if (stdalgo::string::equalsci(action, "set"))
+                       {
+                               // Ensure that we have the <hostchange:value> parameter.
+                               const std::string value = tag->getString("value");
+                               if (value.empty())
+                                       throw ModuleException("<hostchange:value> is a mandatory field when using the 'set' action, at " + tag->getTagLocation());
+
+                               // The hostname is in the format <value>.
+                               rules.push_back(HostRule(mask, value, ports));
+                               continue;
+                       }
+                       else
+                       {
+                               throw ModuleException(action + " is an invalid <hostchange:action> type, at " + tag->getTagLocation());
+                       }
+               }
+
+               ConfigTag* tag = ServerInstance->Config->ConfValue("hostname");
+               const std::string hmap = tag->getString("charmap", "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.-_/0123456789", 1);
+
+               hostmap.reset();
+               for (std::string::const_iterator iter = hmap.begin(); iter != hmap.end(); ++iter)
+                       hostmap.set(static_cast<unsigned char>(*iter));
+               hostrules.swap(rules);
+       }
+
+       Version GetVersion() CXX11_OVERRIDE
+       {
+               return Version("Allows the server administrator to define custom rules for applying hostnames to users.", VF_VENDOR);
+       }
+
+       void OnUserConnect(LocalUser* user) CXX11_OVERRIDE
+       {
+               for (HostRules::const_iterator iter = hostrules.begin(); iter != hostrules.end(); ++iter)
+               {
+                       const HostRule& rule = *iter;
+                       if (!rule.Matches(user))
+                               continue;
+
+                       std::string newhost;
+                       if (rule.GetAction() == HostRule::HCA_ADDACCOUNT)
+                       {
+                               // Retrieve the account name.
+                               const AccountExtItem* accountext = GetAccountExtItem();
+                               const std::string* accountptr = accountext ? accountext->get(user) : NULL;
+                               if (!accountptr)
+                                       continue;
+
+                               // Remove invalid hostname characters.
+                               std::string accountname = CleanName(*accountptr);
+                               if (accountname.empty())
+                                       continue;
+
+                               // Create the hostname.
+                               rule.Wrap(accountname, newhost);
+                       }
+                       else if (rule.GetAction() == HostRule::HCA_ADDNICK)
+                       {
+                               // Remove invalid hostname characters.
+                               const std::string nickname = CleanName(user->nick);
+                               if (nickname.empty())
+                                       continue;
+
+                               // Create the hostname.
+                               rule.Wrap(nickname, newhost);
+                       }
+                       else if (rule.GetAction() == HostRule::HCA_SET)
+                       {
+                               newhost.assign(rule.GetHost());
+                       }
+
+                       if (!newhost.empty())
+                       {
+                               user->WriteNotice("Setting your virtual host: " + newhost);
+                               if (!user->ChangeDisplayedHost(newhost))
+                                       user->WriteNotice("Could not set your virtual host: " + newhost);
+                               return;
+                       }
+               }
+       }
+};
+
+MODULE_INIT(ModuleHostChange)