]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_deaf.cpp
Register all commands, modes and extensions using AddService()
[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
60         std::string deaf_bypasschars;
61         std::string deaf_bypasschars_uline;
62
63  public:
64         ModuleDeaf()
65                 : m1(this)
66         {
67         }
68
69         void init()
70         {
71                 ServerInstance->Modules->AddService(m1);
72
73                 OnRehash(NULL);
74                 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice, I_OnRehash };
75                 ServerInstance->Modules->Attach(eventlist, this, sizeof(eventlist)/sizeof(Implementation));
76         }
77
78         virtual void OnRehash(User* user)
79         {
80                 ConfigTag* tag = ServerInstance->Config->ConfValue("deaf");
81                 deaf_bypasschars = tag->getString("bypasschars");
82                 deaf_bypasschars_uline = tag->getString("bypasscharsuline");
83         }
84
85         virtual ModResult OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
86         {
87                 if (target_type == TYPE_CHANNEL)
88                 {
89                         Channel* chan = (Channel*)dest;
90                         if (chan)
91                                 this->BuildDeafList(MSG_NOTICE, chan, user, status, text, exempt_list);
92                 }
93
94                 return MOD_RES_PASSTHRU;
95         }
96
97         virtual ModResult OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
98         {
99                 if (target_type == TYPE_CHANNEL)
100                 {
101                         Channel* chan = (Channel*)dest;
102                         if (chan)
103                                 this->BuildDeafList(MSG_PRIVMSG, chan, user, status, text, exempt_list);
104                 }
105
106                 return MOD_RES_PASSTHRU;
107         }
108
109         virtual void BuildDeafList(MessageType message_type, Channel* chan, User* sender, char status, const std::string &text, CUList &exempt_list)
110         {
111                 const UserMembList *ulist = chan->GetUsers();
112                 bool is_a_uline;
113                 bool is_bypasschar, is_bypasschar_avail;
114                 bool is_bypasschar_uline, is_bypasschar_uline_avail;
115
116                 is_bypasschar = is_bypasschar_avail = is_bypasschar_uline = is_bypasschar_uline_avail = 0;
117                 if (!deaf_bypasschars.empty())
118                 {
119                         is_bypasschar_avail = 1;
120                         if (deaf_bypasschars.find(text[0], 0) != std::string::npos)
121                                 is_bypasschar = 1;
122                 }
123                 if (!deaf_bypasschars_uline.empty())
124                 {
125                         is_bypasschar_uline_avail = 1;
126                         if (deaf_bypasschars_uline.find(text[0], 0) != std::string::npos)
127                                 is_bypasschar_uline = 1;
128                 }
129
130                 /*
131                  * If we have no bypasschars_uline in config, and this is a bypasschar (regular)
132                  * Than it is obviously going to get through +d, no build required
133                  */
134                 if (!is_bypasschar_uline_avail && is_bypasschar)
135                         return;
136
137                 for (UserMembCIter i = ulist->begin(); i != ulist->end(); i++)
138                 {
139                         /* not +d ? */
140                         if (!i->first->IsModeSet('d'))
141                                 continue; /* deliver message */
142                         /* matched both U-line only and regular bypasses */
143                         if (is_bypasschar && is_bypasschar_uline)
144                                 continue; /* deliver message */
145
146                         is_a_uline = ServerInstance->ULine(i->first->server);
147                         /* matched a U-line only bypass */
148                         if (is_bypasschar_uline && is_a_uline)
149                                 continue; /* deliver message */
150                         /* matched a regular bypass */
151                         if (is_bypasschar && !is_a_uline)
152                                 continue; /* deliver message */
153
154                         if (status && !strchr(chan->GetAllPrefixChars(i->first), status))
155                                 continue;
156
157                         /* don't deliver message! */
158                         exempt_list.insert(i->first);
159                 }
160         }
161
162         virtual ~ModuleDeaf()
163         {
164         }
165
166         virtual Version GetVersion()
167         {
168                 return Version("Provides usermode +d to block channel messages and channel notices", VF_VENDOR);
169         }
170
171 };
172
173 MODULE_INIT(ModuleDeaf)