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