]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_silence.cpp
f809d7d5762e0ed545b989f4b668e61d20df9e75
[user/henk/code/inspircd.git] / src / modules / m_silence.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 "wildcard.h"
16
17 /* $ModDesc: Provides support for the /SILENCE command */
18
19 // This typedef holds a silence list. Each user may or may not have a
20 // silencelist, if a silence list is empty for a user, he/she does not
21 // have one of these structures associated with their user record.
22 typedef std::map<irc::string, time_t> silencelist;
23
24 class cmd_silence : public command_t
25 {
26         unsigned int& maxsilence;
27  public:
28         cmd_silence (InspIRCd* Instance, unsigned int &max) : command_t(Instance,"SILENCE", 0, 0), maxsilence(max)
29         {
30                 this->source = "m_silence.so";
31                 syntax = "{[+|-]<mask>}";
32                 TRANSLATE2(TR_TEXT, TR_END);
33         }
34
35         CmdResult Handle (const char** parameters, int pcnt, userrec *user)
36         {
37                 if (!pcnt)
38                 {
39                         // no parameters, show the current silence list.
40                         // Use Extensible::GetExt to fetch the silence list
41                         silencelist* sl;
42                         user->GetExt("silence_list", sl);
43                         // if the user has a silence list associated with their user record, show it
44                         if (sl)
45                         {
46                                 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
47                                 {
48                                         user->WriteServ("271 %s %s %s :%lu",user->nick, user->nick, c->first.c_str(), (unsigned long)c->second);
49                                 }
50                         }
51                         user->WriteServ("272 %s :End of Silence List",user->nick);
52
53                         return CMD_SUCCESS;
54                 }
55                 else if (pcnt > 0)
56                 {
57                         // one or more parameters, add or delete entry from the list (only the first parameter is used)
58                         std::string mask = parameters[0] + 1;
59                         char action = *parameters[0];
60                         
61                         if (!mask.length())
62                         {
63                                 // 'SILENCE +' or 'SILENCE -', assume *!*@*
64                                 mask = "*!*@*";
65                         }
66                         
67                         ModeParser::CleanMask(mask);
68
69                         if (action == '-')
70                         {
71                                 // fetch their silence list
72                                 silencelist* sl;
73                                 user->GetExt("silence_list", sl);
74                                 // does it contain any entries and does it exist?
75                                 if (sl)
76                                 {
77                                         silencelist::iterator i = sl->find(mask.c_str());
78                                         if (i != sl->end())
79                                         {
80                                                 sl->erase(i);
81                                                 user->WriteServ("950 %s %s :Removed %s from silence list",user->nick, user->nick, mask.c_str());
82                                                 if (!sl->size())
83                                                 {
84                                                         // tidy up -- if a user's list is empty, theres no use having it
85                                                         // hanging around in the user record.
86                                                         DELETE(sl);
87                                                         user->Shrink("silence_list");
88                                                 }
89                                         }
90                                         else
91                                                 user->WriteServ("952 %s %s :%s does not exist on your silence list",user->nick, user->nick, mask.c_str());
92                                 }
93                         }
94                         else if (action == '+')
95                         {
96                                 // fetch the user's current silence list
97                                 silencelist* sl;
98                                 user->GetExt("silence_list", sl);
99                                 // what, they dont have one??? WE'RE ALL GONNA DIE! ...no, we just create an empty one.
100                                 if (!sl)
101                                 {
102                                         sl = new silencelist;
103                                         user->Extend("silence_list", sl);
104                                 }
105                                 silencelist::iterator n = sl->find(mask.c_str());
106                                 if (n != sl->end())
107                                 {
108                                         user->WriteServ("952 %s %s :%s is already on your silence list",user->nick, user->nick, mask.c_str());
109                                         return CMD_FAILURE;
110                                 }
111                                 if (sl->size() >= maxsilence)
112                                 {
113                                         user->WriteServ("952 %s %s :Your silence list is full",user->nick, user->nick, mask.c_str());
114                                         return CMD_FAILURE;
115                                 }
116                                 sl->insert(std::make_pair<irc::string, time_t>(mask.c_str(), ServerInstance->Time()));
117                                 user->WriteServ("951 %s %s :Added %s to silence list",user->nick, user->nick, mask.c_str());
118                                 return CMD_SUCCESS;
119                         }
120                 }
121                 return CMD_SUCCESS;
122         }
123 };
124
125 class ModuleSilence : public Module
126 {
127         
128         cmd_silence* mycommand;
129         unsigned int maxsilence;
130  public:
131  
132         ModuleSilence(InspIRCd* Me)
133                 : Module(Me), maxsilence(32)
134         {
135                 OnRehash(NULL, "");
136                 mycommand = new cmd_silence(ServerInstance, maxsilence);
137                 ServerInstance->AddCommand(mycommand);
138         }
139
140         void Implements(char* List)
141         {
142                 List[I_OnRehash] = List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnUserPreNotice] = List[I_OnUserPreMessage] = 1;
143         }
144
145         virtual void OnRehash(userrec* user, const std::string &parameter)
146         {
147                 ConfigReader Conf(ServerInstance);
148                 maxsilence = Conf.ReadInteger("silence", "maxentries", 0, true);
149                 if (!maxsilence)
150                         maxsilence = 32;
151         }
152
153         virtual void OnUserQuit(userrec* user, const std::string &reason, const std::string &oper_message)
154         {
155                 // when the user quits tidy up any silence list they might have just to keep things tidy
156                 // and to prevent a HONKING BIG MEMORY LEAK!
157                 silencelist* sl;
158                 user->GetExt("silence_list", sl);
159                 if (sl)
160                 {
161                         DELETE(sl);
162                         user->Shrink("silence_list");
163                 }
164         }
165
166         virtual void On005Numeric(std::string &output)
167         {
168                 // we don't really have a limit...
169                 output = output + " SILENCE=" + ConvToStr(maxsilence);
170         }
171         
172         virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
173         {
174                 // im not sure how unreal's silence operates but ours is sensible. It blocks notices and
175                 // privmsgs from people on the silence list, directed privately at the user.
176                 // channel messages are unaffected (ever tried to follow the flow of conversation in
177                 // a channel when you've set an ignore on the two most talkative people?)
178                 if ((target_type == TYPE_USER) && (IS_LOCAL(user)))
179                 {
180                         userrec* u = (userrec*)dest;
181                         silencelist* sl;
182                         u->GetExt("silence_list", sl);
183                         if (sl)
184                         {
185                                 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
186                                 {
187                                         if (match(user->GetFullHost(), c->first.c_str()))
188                                         {
189                                                 return 1;
190                                         }
191                                 }
192                         }
193                 }
194                 return 0;
195         }
196
197         virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
198         {
199                 return OnUserPreNotice(user,dest,target_type,text,status,exempt_list);
200         }
201
202         virtual ~ModuleSilence()
203         {
204         }
205         
206         virtual Version GetVersion()
207         {
208                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
209         }
210 };
211
212 MODULE_INIT(ModuleSilence)