]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_deaf.cpp
Merge pull request #495 from SaberUK/master+fix-libcpp
[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 /* $ModDesc: Provides usermode +d to block channel messages and channel notices */
25
26 /** User mode +d - filter out channel messages and channel notices
27  */
28 class User_d : public ModeHandler
29 {
30  public:
31         User_d(Module* Creator) : ModeHandler(Creator, "deaf", 'd', PARAM_NONE, MODETYPE_USER) { }
32
33         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding)
34         {
35                 if (adding)
36                 {
37                         if (!dest->IsModeSet('d'))
38                         {
39                                 dest->WriteServ("NOTICE %s :*** 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 %s -d.", dest->nick.c_str(), dest->nick.c_str());
40                                 dest->SetMode('d',true);
41                                 return MODEACTION_ALLOW;
42                         }
43                 }
44                 else
45                 {
46                         if (dest->IsModeSet('d'))
47                         {
48                                 dest->SetMode('d',false);
49                                 return MODEACTION_ALLOW;
50                         }
51                 }
52                 return MODEACTION_DENY;
53         }
54 };
55
56 class ModuleDeaf : public Module
57 {
58         User_d m1;
59         std::string deaf_bypasschars;
60         std::string deaf_bypasschars_uline;
61
62  public:
63         ModuleDeaf()
64                 : m1(this)
65         {
66         }
67
68         void init()
69         {
70                 ServerInstance->Modules->AddService(m1);
71
72                 OnRehash(NULL);
73                 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice, I_OnRehash };
74                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
75         }
76
77         virtual void OnRehash(User* user)
78         {
79                 ConfigTag* tag = ServerInstance->Config->ConfValue("deaf");
80                 deaf_bypasschars = tag->getString("bypasschars");
81                 deaf_bypasschars_uline = tag->getString("bypasscharsuline");
82         }
83
84         virtual ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
85         {
86                 if (target_type == TYPE_CHANNEL)
87                 {
88                         Channel* chan = (Channel*)dest;
89                         if (chan)
90                                 this->BuildDeafList(MSG_NOTICE, chan, user, status, text, exempt_list);
91                 }
92
93                 return MOD_RES_PASSTHRU;
94         }
95
96         virtual ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
97         {
98                 if (target_type == TYPE_CHANNEL)
99                 {
100                         Channel* chan = (Channel*)dest;
101                         if (chan)
102                                 this->BuildDeafList(MSG_PRIVMSG, chan, user, status, text, exempt_list);
103                 }
104
105                 return MOD_RES_PASSTHRU;
106         }
107
108         virtual void BuildDeafList(MessageType message_type, Channel* chan, User* sender, char status, const std::string &text, CUList &exempt_list)
109         {
110                 const UserMembList *ulist = chan->GetUsers();
111                 bool is_a_uline;
112                 bool is_bypasschar, is_bypasschar_avail;
113                 bool is_bypasschar_uline, is_bypasschar_uline_avail;
114
115                 is_bypasschar = is_bypasschar_avail = is_bypasschar_uline = is_bypasschar_uline_avail = 0;
116                 if (!deaf_bypasschars.empty())
117                 {
118                         is_bypasschar_avail = 1;
119                         if (deaf_bypasschars.find(text[0], 0) != std::string::npos)
120                                 is_bypasschar = 1;
121                 }
122                 if (!deaf_bypasschars_uline.empty())
123                 {
124                         is_bypasschar_uline_avail = 1;
125                         if (deaf_bypasschars_uline.find(text[0], 0) != std::string::npos)
126                                 is_bypasschar_uline = 1;
127                 }
128
129                 /*
130                  * If we have no bypasschars_uline in config, and this is a bypasschar (regular)
131                  * Than it is obviously going to get through +d, no build required
132                  */
133                 if (!is_bypasschar_uline_avail && is_bypasschar)
134                         return;
135
136                 for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
137                 {
138                         /* not +d ? */
139                         if (!i->first->IsModeSet('d'))
140                                 continue; /* deliver message */
141                         /* matched both U-line only and regular bypasses */
142                         if (is_bypasschar && is_bypasschar_uline)
143                                 continue; /* deliver message */
144
145                         is_a_uline = ServerInstance->ULine(i->first->server);
146                         /* matched a U-line only bypass */
147                         if (is_bypasschar_uline && is_a_uline)
148                                 continue; /* deliver message */
149                         /* matched a regular bypass */
150                         if (is_bypasschar && !is_a_uline)
151                                 continue; /* deliver message */
152
153                         if (status && !strchr(chan->GetAllPrefixChars(i->first), status))
154                                 continue;
155
156                         /* don't deliver message! */
157                         exempt_list.insert(i->first);
158                 }
159         }
160
161         virtual Version GetVersion()
162         {
163                 return Version("Provides usermode +d to block channel messages and channel notices", VF_VENDOR);
164         }
165 };
166
167 MODULE_INIT(ModuleDeaf)