]> git.netwichtig.de Git - user/henk/code/inspircd.git/blobdiff - src/modules/m_commonchans.cpp
Add support for blocking tag messages with the deaf mode.
[user/henk/code/inspircd.git] / src / modules / m_commonchans.cpp
index cc00b53f508cafca8f52cf1dcbaa07596e349cb2..23a1472c2ac2f387046a4f7d9717e04f13df5d6a 100644 (file)
@@ -1,94 +1,73 @@
-/*       +------------------------------------+
- *       | Inspire Internet Relay Chat Daemon |
- *       +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
  *
- *  InspIRCd: (C) 2002-2008 InspIRCd Development Team
- * See: http://www.inspircd.org/wiki/index.php/Credits
+ *   Copyright (C) 2013, 2017, 2019-2020 Sadie Powell <sadie@witchery.services>
+ *   Copyright (C) 2012, 2019 Robby <robby@chatbelgie.be>
+ *   Copyright (C) 2012 Attila Molnar <attilamolnar@hush.com>
+ *   Copyright (C) 2009-2010 Daniel De Graaf <danieldg@inspircd.org>
+ *   Copyright (C) 2007 Craig Edwards <brain@inspircd.org>
  *
- * This program is free but copyrighted software; see
- *            the file COPYING for details.
+ * 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"
 
-/* $ModDesc: Adds user mode +c, which if set, users must be on a common channel with you to private message you */
+#include "inspircd.h"
+#include "modules/ctctags.h"
 
-/** Handles user mode +c
- */
-class PrivacyMode : public ModeHandler
+class ModuleCommonChans
+       : public CTCTags::EventListener
+       , public Module
 {
- public:
-       PrivacyMode(InspIRCd* Instance) : ModeHandler(Instance, 'c', 0, 0, false, MODETYPE_USER, false) { }
+ private:
+       SimpleUserModeHandler mode;
 
-       ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
+       ModResult HandleMessage(User* user, const MessageTarget& target)
        {
-               if (adding)
-               {
-                       if (!dest->IsModeSet('c'))
-                       {
-                               dest->SetMode('c',true);
-                               return MODEACTION_ALLOW;
-                       }
-               }
-               else
-               {
-                       if (dest->IsModeSet('c'))
-                       {
-                               dest->SetMode('c',false);
-                               return MODEACTION_ALLOW;
-                       }
-               }
+               if (target.type != MessageTarget::TYPE_USER)
+                       return MOD_RES_PASSTHRU;
 
-               return MODEACTION_DENY;
-       }
-};
+               User* targuser = target.Get<User>();
+               if (!targuser->IsModeSet(mode) || user->SharesChannelWith(targuser))
+                       return MOD_RES_PASSTHRU;
 
-class ModulePrivacyMode : public Module
-{
-       PrivacyMode* pm;
- public:
-       ModulePrivacyMode(InspIRCd* Me) : Module(Me)
-       {
-               pm = new PrivacyMode(ServerInstance);
-               if (!ServerInstance->Modes->AddMode(pm))
-                       throw ModuleException("Could not add new modes!");
-               Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice };
-               ServerInstance->Modules->Attach(eventlist, this, 2);
-       }
+               if (user->HasPrivPermission("users/ignore-commonchans") || user->server->IsULine())
+                       return MOD_RES_PASSTHRU;
 
+               user->WriteNumeric(Numerics::CannotSendTo(targuser, "messages", &mode));
+               return MOD_RES_DENY;
+       }
 
-       virtual ~ModulePrivacyMode()
+ public:
+       ModuleCommonChans()
+               : CTCTags::EventListener(this)
+               , mode(this, "deaf_commonchan", 'c')
        {
-               ServerInstance->Modes->DelMode(pm);
-               delete pm;
        }
 
-       virtual Version GetVersion()
+       Version GetVersion() CXX11_OVERRIDE
        {
-               return Version("$Id$", VF_COMMON|VF_VENDOR, API_VERSION);
+               return Version("Adds user mode c (deaf_commonchan) which requires users to have a common channel before they can privately message each other.", VF_VENDOR);
        }
 
-       virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
+       ModResult OnUserPreMessage(User* user, const MessageTarget& target, MessageDetails& details) CXX11_OVERRIDE
        {
-               if (target_type == TYPE_USER)
-               {
-                       User* t = (User*)dest;
-                       if (!IS_OPER(user) && (t->IsModeSet('c')) && (!ServerInstance->ULine(user->server)) && !user->SharesChannelWith(t))
-                       {
-                               user->WriteNumeric(ERR_CANTSENDTOUSER, "%s %s :You are not permitted to send private messages to this user (+c set)", user->nick.c_str(), t->nick.c_str());
-                               return 1;
-                       }
-               }
-               return 0;
+               return HandleMessage(user, target);
        }
 
-       virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
+       ModResult OnUserPreTagMessage(User* user, const MessageTarget& target, CTCTags::TagMessageDetails& details) CXX11_OVERRIDE
        {
-               return OnUserPreMessage(user, dest, target_type, text, status, exempt_list);
+               return HandleMessage(user, target);
        }
 };
 
-
-MODULE_INIT(ModulePrivacyMode)
+MODULE_INIT(ModuleCommonChans)