]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_deaf.cpp
Convert all to new Attach() system. The Implements() method needs removing from all...
[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(User* source, User* dest, Channel* 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))
64                         throw ModuleException("Could not add new modes!");
65
66                 OnRehash(NULL, "");
67                 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice, I_OnRehash, I_OnBuildExemptList };
68                 ServerInstance->Modules->Attach(eventlist, this, 4);
69         }
70
71         void Implements(char* List)
72         {
73                 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnRehash] = List[I_OnBuildExemptList] = 1;
74         }
75
76         virtual void OnRehash(User* user, const std::string&)
77         {
78                 ConfigReader* conf = new ConfigReader(ServerInstance);
79                 deaf_bypasschars = conf->ReadValue("deaf", "bypasschars", 0);
80                 deaf_bypasschars_uline = conf->ReadValue("deaf", "bypasscharsuline", 0);
81
82                 DELETE(conf);
83         }
84
85         virtual int 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 0;
95         }
96
97         virtual int 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 0;
107         }
108
109         virtual void OnBuildExemptList(MessageType message_type, Channel* chan, User* sender, char status, CUList &exempt_list, const std::string &text)
110         {
111                 BuildDeafList(message_type, chan, sender, status, text, exempt_list);
112         }
113
114         virtual void BuildDeafList(MessageType message_type, Channel* chan, User* sender, char status, const std::string &text, CUList &exempt_list)
115         {
116                 CUList *ulist;
117                 bool is_a_uline;
118                 bool is_bypasschar, is_bypasschar_avail;
119                 bool is_bypasschar_uline, is_bypasschar_uline_avail;
120
121                 is_bypasschar = is_bypasschar_avail = is_bypasschar_uline = is_bypasschar_uline_avail = 0;
122                 if (!deaf_bypasschars.empty())
123                 {
124                         is_bypasschar_avail = 1;
125                         if (deaf_bypasschars.find(text[0], 0) != string::npos)
126                                 is_bypasschar = 1;
127                 }
128                 if (!deaf_bypasschars_uline.empty())
129                 {
130                         is_bypasschar_uline_avail = 1;
131                         if (deaf_bypasschars_uline.find(text[0], 0) != string::npos)
132                                 is_bypasschar_uline = 1;
133                 }
134
135                 /*
136                  * If we have no bypasschars_uline in config, and this is a bypasschar (regular)
137                  * Than it is obviously going to get through +d, no build required
138                  */
139                 if (!is_bypasschar_uline_avail && is_bypasschar)
140                         return;
141
142                 switch (status)
143                 {
144                         case '@':
145                                 ulist = chan->GetOppedUsers();
146                                 break;
147                         case '%':
148                                 ulist = chan->GetHalfoppedUsers();
149                                 break;
150                         case '+':
151                                 ulist = chan->GetVoicedUsers();
152                                 break;
153                         default:
154                                 ulist = chan->GetUsers();
155                                 break;
156                 }
157
158                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
159                 {
160                         /* not +d ? */
161                         if (!i->first->IsModeSet('d'))
162                                 continue; /* deliver message */
163                         /* matched both U-line only and regular bypasses */
164                         if (is_bypasschar && is_bypasschar_uline)
165                                 continue; /* deliver message */
166
167                         is_a_uline = ServerInstance->ULine(i->first->server);
168                         /* matched a U-line only bypass */
169                         if (is_bypasschar_uline && is_a_uline)
170                                 continue; /* deliver message */
171                         /* matched a regular bypass */
172                         if (is_bypasschar && !is_a_uline)
173                                 continue; /* deliver message */
174
175                         /* don't deliver message! */
176                         exempt_list[i->first] = i->first->nick;
177                 }
178         }
179
180         virtual ~ModuleDeaf()
181         {
182                 ServerInstance->Modes->DelMode(m1);
183                 DELETE(m1);
184         }
185
186         virtual Version GetVersion()
187         {
188                 return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
189         }
190
191 };
192
193 MODULE_INIT(ModuleDeaf)