]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_deaf.cpp
Introduce Server class
[user/henk/code/inspircd.git] / src / modules / m_deaf.cpp
1 /*
2  * InspIRCd -- Internet Relay Chat Daemon
3  *
4  *   Copyright (C) 2006, 2008 Craig Edwards <craigedwards@brainbox.cc>
5  *   Copyright (C) 2007 Robin Burchell <robin+git@viroteck.net>
6  *   Copyright (C) 2006-2007 Dennis Friis <peavey@inspircd.org>
7  *
8  * This file is part of InspIRCd.  InspIRCd is free software: you can
9  * redistribute it and/or modify it under the terms of the GNU General Public
10  * License as published by the Free Software Foundation, version 2.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
14  * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
15  * details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21
22 #include "inspircd.h"
23
24 /** User mode +d - filter out channel messages and channel notices
25  */
26 class User_d : public ModeHandler
27 {
28  public:
29         User_d(Module* Creator) : ModeHandler(Creator, "deaf", 'd', PARAM_NONE, MODETYPE_USER) { }
30
31         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
32         {
33                 if (adding == dest->IsModeSet(this))
34                         return MODEACTION_DENY;
35
36                 if (adding)
37                         dest->WriteNotice("*** You have enabled usermode +d, deaf mode. This mode means you WILL NOT receive any messages from any channels you are in. If you did NOT mean to do this, use /mode " + dest->nick + " -d.");
38
39                 dest->SetMode(this, adding);
40                 return MODEACTION_ALLOW;
41         }
42 };
43
44 class ModuleDeaf : public Module
45 {
46         User_d m1;
47         std::string deaf_bypasschars;
48         std::string deaf_bypasschars_uline;
49
50  public:
51         ModuleDeaf()
52                 : m1(this)
53         {
54         }
55
56         void ReadConfig(ConfigStatus& status) CXX11_OVERRIDE
57         {
58                 ConfigTag* tag = ServerInstance->Config->ConfValue("deaf");
59                 deaf_bypasschars = tag->getString("bypasschars");
60                 deaf_bypasschars_uline = tag->getString("bypasscharsuline");
61         }
62
63         ModResult OnUserPreMessage(User* user, void* dest, int target_type, std::string& text, char status, CUList& exempt_list, MessageType msgtype) CXX11_OVERRIDE
64         {
65                 if (target_type == TYPE_CHANNEL)
66                 {
67                         Channel* chan = (Channel*)dest;
68                         if (chan)
69                                 this->BuildDeafList(msgtype, chan, user, status, text, exempt_list);
70                 }
71
72                 return MOD_RES_PASSTHRU;
73         }
74
75         void BuildDeafList(MessageType message_type, Channel* chan, User* sender, char status, const std::string &text, CUList &exempt_list)
76         {
77                 const UserMembList *ulist = chan->GetUsers();
78                 bool is_a_uline;
79                 bool is_bypasschar, is_bypasschar_avail;
80                 bool is_bypasschar_uline, is_bypasschar_uline_avail;
81
82                 is_bypasschar = is_bypasschar_avail = is_bypasschar_uline = is_bypasschar_uline_avail = 0;
83                 if (!deaf_bypasschars.empty())
84                 {
85                         is_bypasschar_avail = 1;
86                         if (deaf_bypasschars.find(text[0], 0) != std::string::npos)
87                                 is_bypasschar = 1;
88                 }
89                 if (!deaf_bypasschars_uline.empty())
90                 {
91                         is_bypasschar_uline_avail = 1;
92                         if (deaf_bypasschars_uline.find(text[0], 0) != std::string::npos)
93                                 is_bypasschar_uline = 1;
94                 }
95
96                 /*
97                  * If we have no bypasschars_uline in config, and this is a bypasschar (regular)
98                  * Than it is obviously going to get through +d, no build required
99                  */
100                 if (!is_bypasschar_uline_avail && is_bypasschar)
101                         return;
102
103                 for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
104                 {
105                         /* not +d ? */
106                         if (!i->first->IsModeSet(m1))
107                                 continue; /* deliver message */
108                         /* matched both U-line only and regular bypasses */
109                         if (is_bypasschar && is_bypasschar_uline)
110                                 continue; /* deliver message */
111
112                         is_a_uline = i->first->server->IsULine();
113                         /* matched a U-line only bypass */
114                         if (is_bypasschar_uline && is_a_uline)
115                                 continue; /* deliver message */
116                         /* matched a regular bypass */
117                         if (is_bypasschar && !is_a_uline)
118                                 continue; /* deliver message */
119
120                         if (status && !strchr(chan->GetAllPrefixChars(i->first), status))
121                                 continue;
122
123                         /* don't deliver message! */
124                         exempt_list.insert(i->first);
125                 }
126         }
127
128         Version GetVersion() CXX11_OVERRIDE
129         {
130                 return Version("Provides usermode +d to block channel messages and channel notices", VF_VENDOR);
131         }
132 };
133
134 MODULE_INIT(ModuleDeaf)