]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_deaf.cpp
Fix for bug #791, if an error is set treat this as 'could not get certificate'
[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) : ModeHandler(Instance, 'd', 0, 0, false, MODETYPE_USER, false) { }
24
25         ModeAction OnModeChange(User* source, User* dest, Channel* channel, std::string &parameter, bool adding, bool)
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)
58         {
59                 m1 = new User_d(ServerInstance);
60                 if (!ServerInstance->Modes->AddMode(m1))
61                         throw ModuleException("Could not add new modes!");
62
63                 OnRehash(NULL, "");
64                 Implementation eventlist[] = { I_OnUserPreMessage, I_OnUserPreNotice, I_OnRehash, I_OnBuildExemptList };
65                 ServerInstance->Modules->Attach(eventlist, this, 4);
66         }
67
68
69         virtual void OnRehash(User* user, const std::string&)
70         {
71                 ConfigReader* conf = new ConfigReader(ServerInstance);
72                 deaf_bypasschars = conf->ReadValue("deaf", "bypasschars", 0);
73                 deaf_bypasschars_uline = conf->ReadValue("deaf", "bypasscharsuline", 0);
74
75                 delete conf;
76         }
77
78         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
79         {
80                 if (target_type == TYPE_CHANNEL)
81                 {
82                         Channel* chan = (Channel*)dest;
83                         if (chan)
84                                 this->BuildDeafList(MSG_NOTICE, chan, user, status, text, exempt_list);
85                 }
86
87                 return 0;
88         }
89
90         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
91         {
92                 if (target_type == TYPE_CHANNEL)
93                 {
94                         Channel* chan = (Channel*)dest;
95                         if (chan)
96                                 this->BuildDeafList(MSG_PRIVMSG, chan, user, status, text, exempt_list);
97                 }
98
99                 return 0;
100         }
101
102         virtual void OnBuildExemptList(MessageType message_type, Channel* chan, User* sender, char status, CUList &exempt_list, const std::string &text)
103         {
104                 BuildDeafList(message_type, chan, sender, status, text, exempt_list);
105         }
106
107         virtual void BuildDeafList(MessageType message_type, Channel* chan, User* sender, char status, const std::string &text, CUList &exempt_list)
108         {
109                 CUList *ulist = chan->GetUsers();
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) != std::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) != std::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                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
136                 {
137                         /* not +d ? */
138                         if (!i->first->IsModeSet('d'))
139                                 continue; /* deliver message */
140                         /* matched both U-line only and regular bypasses */
141                         if (is_bypasschar && is_bypasschar_uline)
142                                 continue; /* deliver message */
143
144                         is_a_uline = ServerInstance->ULine(i->first->server);
145                         /* matched a U-line only bypass */
146                         if (is_bypasschar_uline && is_a_uline)
147                                 continue; /* deliver message */
148                         /* matched a regular bypass */
149                         if (is_bypasschar && !is_a_uline)
150                                 continue; /* deliver message */
151
152                         if (status && !strchr(chan->GetAllPrefixChars(i->first), status))
153                                 continue;
154
155                         /* don't deliver message! */
156                         exempt_list[i->first] = i->first->nick;
157                 }
158         }
159
160         virtual ~ModuleDeaf()
161         {
162                 ServerInstance->Modes->DelMode(m1);
163                 delete m1;
164         }
165
166         virtual Version GetVersion()
167         {
168                 return Version("$Id$", VF_COMMON|VF_VENDOR,API_VERSION);
169         }
170
171 };
172
173 MODULE_INIT(ModuleDeaf)