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