]> 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 b4e274f6cdbf1d0fda3af81ade633043188c83c2..23a1472c2ac2f387046a4f7d9717e04f13df5d6a 100644 (file)
-/*       +------------------------------------+
- *       | Inspire Internet Relay Chat Daemon |
- *       +------------------------------------+
+/*
+ * InspIRCd -- Internet Relay Chat Daemon
  *
- *  InspIRCd: (C) 2002-2007 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"
-#include <stdio.h>
-#include <string>
-#include "users.h"
-#include "channels.h"
-#include "modules.h"
-#include "configreader.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(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
+       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;
-                       }
-               }
-               
-               return MODEACTION_DENY;
+               if (target.type != MessageTarget::TYPE_USER)
+                       return MOD_RES_PASSTHRU;
+
+               User* targuser = target.Get<User>();
+               if (!targuser->IsModeSet(mode) || user->SharesChannelWith(targuser))
+                       return MOD_RES_PASSTHRU;
+
+               if (user->HasPrivPermission("users/ignore-commonchans") || user->server->IsULine())
+                       return MOD_RES_PASSTHRU;
+
+               user->WriteNumeric(Numerics::CannotSendTo(targuser, "messages", &mode));
+               return MOD_RES_DENY;
        }
-};
 
-class ModulePrivacyMode : public Module
-{
-       PrivacyMode* pm;
  public:
-       ModulePrivacyMode(InspIRCd* Me) : Module(Me)
+       ModuleCommonChans()
+               : CTCTags::EventListener(this)
+               , mode(this, "deaf_commonchan", 'c')
        {
-               pm = new PrivacyMode(ServerInstance);
-               if (!ServerInstance->AddMode(pm, 'c'))
-                       throw ModuleException("Could not add new modes!");
        }
 
-       void Implements(char* List)
-       {
-               List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = 1;
-       }
-       
-       virtual ~ModulePrivacyMode()
+       Version GetVersion() CXX11_OVERRIDE
        {
-               ServerInstance->Modes->DelMode(pm);
-               DELETE(pm);
-       }
-       
-       virtual Version GetVersion()
-       {
-               return Version(1,1,0,0, 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(userrec* 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)
-               {
-                       userrec* t = (userrec*)dest;
-                       if (!IS_OPER(user) && (t->IsModeSet('c')) && (!ServerInstance->ULine(user->server)) && !user->SharesChannelWith(t))
-                       {
-                               user->WriteServ("531 %s %s :You are not permitted to send private messages to this user (+c set)", user->nick, t->nick);
-                               return 1;
-                       }
-               }
-               return 0;
+               return HandleMessage(user, target);
        }
 
-       virtual int OnUserPreNotice(userrec* 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)