]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_deaf.cpp
b5328f785293243e5fc653e05a4596523432244f
[user/henk/code/inspircd.git] / src / modules / m_deaf.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *            the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include "users.h"
16 #include "channels.h"
17 #include "modules.h"
18
19 /* $ModDesc: Provides support for ircu style usermode +d (deaf to channel messages and channel notices) */
20
21 /** User mode +d - filter out channel messages and channel notices
22  */
23 class User_d : public ModeHandler
24 {
25  public:
26         User_d(InspIRCd* Instance) : ModeHandler(Instance, 'd', 0, 0, false, MODETYPE_USER, false) { }
27
28         ModeAction OnModeChange(userrec* source, userrec* dest, chanrec* channel, std::string &parameter, bool adding)
29         {
30                 if (adding)
31                 {
32                         if (!dest->IsModeSet('d'))
33                         {
34                                 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, dest->nick);
35                                 dest->SetMode('d',true);
36                                 return MODEACTION_ALLOW;
37                         }
38                 }
39                 else
40                 {
41                         if (dest->IsModeSet('d'))
42                         {
43                                 dest->SetMode('d',false);
44                                 return MODEACTION_ALLOW;
45                         }
46                 }
47                 return MODEACTION_DENY;
48         }
49 };
50
51 class ModuleDeaf : public Module
52 {
53         User_d* m1;
54
55         std::string deaf_bypasschars;
56         std::string deaf_bypasschars_uline;
57
58  public:
59         ModuleDeaf(InspIRCd* Me)
60                 : Module(Me)
61         {
62                 m1 = new User_d(ServerInstance);
63                 if (!ServerInstance->AddMode(m1, 'd'))
64                         throw ModuleException("Could not add new modes!");
65
66                 OnRehash(NULL, "");
67         }
68
69         void Implements(char* List)
70         {
71                 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = 1;
72         }
73
74         virtual void OnRehash(userrec* user, const std::string&)
75         {
76                 ConfigReader* conf = new ConfigReader(ServerInstance);
77                 deaf_bypasschars = conf->ReadValue("deaf", "bypasschars", 0);
78                 deaf_bypasschars_uline = conf->ReadValue("deaf", "bypasscharsuline", 0);
79
80                 DELETE(conf);
81         }
82
83         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
84         {
85                 if (target_type == TYPE_CHANNEL)
86                 {
87                         chanrec* chan = (chanrec*)dest;
88                         if (chan)
89                                 this->BuildDeafList(MSG_NOTICE, chan, user, status, text, exempt_list);
90                 }
91
92                 return 0;
93         }
94
95         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
96         {
97                 if (target_type == TYPE_CHANNEL)
98                 {
99                         chanrec* chan = (chanrec*)dest;
100                         if (chan)
101                                 this->BuildDeafList(MSG_PRIVMSG, chan, user, status, text, exempt_list);
102                 }
103
104                 return 0;
105         }
106
107         virtual void BuildDeafList(MessageType message_type, chanrec* chan, userrec* sender, char status, std::string &text, CUList &exempt_list)
108         {
109                 CUList *ulist;
110                 bool is_a_uline;
111                 bool is_bypasschar, is_bypasschar_avail;
112                 bool is_bypasschar_uline, is_bypasschar_uline_avail;
113
114                 is_bypasschar = is_bypasschar_avail = is_bypasschar_uline = is_bypasschar_uline_avail = 0;
115                 if (!deaf_bypasschars.empty())
116                 {
117                         is_bypasschar_avail = 1;
118                         if (deaf_bypasschars.find(text[0], 0) != string::npos)
119                                 is_bypasschar = 1;
120                 }
121                 if (!deaf_bypasschars_uline.empty())
122                 {
123                         is_bypasschar_uline_avail = 1;
124                         if (deaf_bypasschars_uline.find(text[0], 0) != string::npos)
125                                 is_bypasschar_uline = 1;
126                 }
127
128                 /*
129                  * If we have no bypasschars_uline in config, and this is a bypasschar (regular)
130                  * Than it is obviously going to get through +d, no build required
131                  */
132                 if (!is_bypasschar_uline_avail && is_bypasschar)
133                         return;
134
135                 switch (status)
136                 {
137                         case '@':
138                                 ulist = chan->GetOppedUsers();
139                                 break;
140                         case '%':
141                                 ulist = chan->GetHalfoppedUsers();
142                                 break;
143                         case '+':
144                                 ulist = chan->GetVoicedUsers();
145                                 break;
146                         default:
147                                 ulist = chan->GetUsers();
148                                 break;
149                 }
150
151                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
152                 {
153                         /* not +d ? */
154                         if (!i->first->IsModeSet('d'))
155                                 continue; /* deliver message */
156                         /* matched both U-line only and regular bypasses */
157                         if (is_bypasschar && is_bypasschar_uline)
158                                 continue; /* deliver message */
159
160                         is_a_uline = ServerInstance->ULine(i->first->server);
161                         /* matched a U-line only bypass */
162                         if (is_bypasschar_uline && is_a_uline)
163                                 continue; /* deliver message */
164                         /* matched a regular bypass */
165                         if (is_bypasschar && !is_a_uline)
166                                 continue; /* deliver message */
167
168                         /* don't deliver message! */
169                         exempt_list[i->first] = i->first->nick;
170                 }
171         }
172
173         virtual ~ModuleDeaf()
174         {
175                 ServerInstance->Modes->DelMode(m1);
176                 DELETE(m1);
177         }
178
179         virtual Version GetVersion()
180         {
181                 return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
182         }
183
184 };
185
186 MODULE_INIT(ModuleDeaf)