]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_silence.cpp
Header update: 2007 -> 2008
[user/henk/code/inspircd.git] / src / modules / m_silence.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2008 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 CommandSilence : public Command
25 {
26         unsigned int& maxsilence;
27  public:
28         CommandSilence (InspIRCd* Instance, unsigned int &max) : Command(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, User *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         CommandSilence* mycommand;
129         unsigned int maxsilence;
130  public:
131  
132         ModuleSilence(InspIRCd* Me)
133                 : Module(Me), maxsilence(32)
134         {
135                 OnRehash(NULL, "");
136                 mycommand = new CommandSilence(ServerInstance, maxsilence);
137                 ServerInstance->AddCommand(mycommand);
138                 Implementation eventlist[] = { I_OnRehash, I_OnUserQuit, I_On005Numeric, I_OnUserPreNotice, I_OnUserPreMessage };
139                 ServerInstance->Modules->Attach(eventlist, this, 5);
140         }
141
142
143         virtual void OnRehash(User* user, const std::string &parameter)
144         {
145                 ConfigReader Conf(ServerInstance);
146                 maxsilence = Conf.ReadInteger("silence", "maxentries", 0, true);
147                 if (!maxsilence)
148                         maxsilence = 32;
149         }
150
151         virtual void OnUserQuit(User* user, const std::string &reason, const std::string &oper_message)
152         {
153                 // when the user quits tidy up any silence list they might have just to keep things tidy
154                 // and to prevent a HONKING BIG MEMORY LEAK!
155                 silencelist* sl;
156                 user->GetExt("silence_list", sl);
157                 if (sl)
158                 {
159                         delete sl;
160                         user->Shrink("silence_list");
161                 }
162         }
163
164         virtual void On005Numeric(std::string &output)
165         {
166                 // we don't really have a limit...
167                 output = output + " SILENCE=" + ConvToStr(maxsilence);
168         }
169         
170         virtual int OnUserPreNotice(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
171         {
172                 // im not sure how unreal's silence operates but ours is sensible. It blocks notices and
173                 // privmsgs from people on the silence list, directed privately at the user.
174                 // channel messages are unaffected (ever tried to follow the flow of conversation in
175                 // a channel when you've set an ignore on the two most talkative people?)
176                 if ((target_type == TYPE_USER) && (IS_LOCAL(user)))
177                 {
178                         User* u = (User*)dest;
179                         silencelist* sl;
180                         u->GetExt("silence_list", sl);
181                         if (sl)
182                         {
183                                 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
184                                 {
185                                         if (match(user->GetFullHost(), c->first.c_str()))
186                                         {
187                                                 return 1;
188                                         }
189                                 }
190                         }
191                 }
192                 return 0;
193         }
194
195         virtual int OnUserPreMessage(User* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
196         {
197                 return OnUserPreNotice(user,dest,target_type,text,status,exempt_list);
198         }
199
200         virtual ~ModuleSilence()
201         {
202         }
203         
204         virtual Version GetVersion()
205         {
206                 return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
207         }
208 };
209
210 MODULE_INIT(ModuleSilence)