]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_deaf.cpp
Header rearrangement, move inspircd.h to top, remove stdio, stdlib, stdblahblah that...
[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  public:
55         ModuleDeaf(InspIRCd* Me)
56                 : Module(Me)
57         {
58                 m1 = new User_d(ServerInstance);
59                 if (!ServerInstance->AddMode(m1, 'd'))
60                         throw ModuleException("Could not add new modes!");
61         }
62
63         void Implements(char* List)
64         {
65                 List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnBuildExemptList] = 1;
66         }
67
68         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
69         {
70                 return PreText(user, dest, target_type, text, status, exempt_list);
71         }
72
73         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
74         {
75                 return PreText(user, dest, target_type, text, status, exempt_list);
76         }
77
78         virtual void OnBuildExemptList(MessageType message_type, chanrec* chan, userrec* sender, char status, CUList &exempt_list)
79         {
80                 CUList *ulist;
81                 switch (status)
82                 {
83                         case '@':
84                                 ulist = chan->GetOppedUsers();
85                                 break;
86                         case '%':
87                                 ulist = chan->GetHalfoppedUsers();
88                                 break;
89                         case '+':
90                                 ulist = chan->GetVoicedUsers();
91                                 break;
92                         default:
93                                 ulist = chan->GetUsers();
94                                 break;
95                 }
96
97                 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
98                 {
99                         if (IS_LOCAL(i->first))
100                         {
101                                 if (i->first->IsModeSet('d'))
102                                 {
103                                         exempt_list[i->first] = i->first->nick;
104                                 }
105                         }
106                 }
107         }
108
109         virtual int PreText(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
110         {
111                 if ((target_type == TYPE_CHANNEL) & (IS_LOCAL(user)))
112                 {
113                         chanrec* chan = (chanrec*)dest;
114                         if (chan)
115                         {
116                                 this->OnBuildExemptList(MSG_PRIVMSG, chan, user, status, exempt_list);
117                         }
118                 }
119                 return 0;
120         }
121
122         virtual ~ModuleDeaf()
123         {
124                 ServerInstance->Modes->DelMode(m1);
125                 DELETE(m1);
126         }
127
128         virtual Version GetVersion()
129         {
130                 return Version(1,1,0,0,VF_COMMON|VF_VENDOR,API_VERSION);
131         }
132
133 };
134
135
136 class ModuleDeafFactory : public ModuleFactory
137 {
138  public:
139         ModuleDeafFactory()
140         {
141         }
142         
143         ~ModuleDeafFactory()
144         {
145         }
146         
147         virtual Module * CreateModule(InspIRCd* Me)
148         {
149                 return new ModuleDeaf(Me);
150         }
151         
152 };
153
154
155 extern "C" DllExport void * init_module( void )
156 {
157         return new ModuleDeafFactory;
158 }
159