1 /* +------------------------------------+
2 * | Inspire Internet Relay Chat Daemon |
3 * +------------------------------------+
5 * InspIRCd: (C) 2002-2007 InspIRCd Development Team
6 * See: http://www.inspircd.org/wiki/index.php/Credits
8 * This program is free but copyrighted software; see
9 * the file COPYING for details.
11 * ---------------------------------------------------
25 /* $ModDesc: Provides support for the /SILENCE command */
27 /* Improved drop-in replacement for the /SILENCE command
28 * syntax: /SILENCE [+|-]<mask> <p|c|i|n|t|a|x> as in <privatemessage|channelmessage|invites|privatenotice|channelnotice|all|exclude>
30 * example that blocks all except private messages
34 * example that blocks all invites except from channel services
36 * /SILENCE +chanserv!services@chatters.net ix
38 * example that blocks some bad dude from private, notice and inviting you
39 * /SILENCE +*!kiddie@lamerz.net pin
41 * TODO: possibly have add and remove check for existing host and only modify flags according to
42 * what's been changed instead of having to remove first, then add if you want to change
46 // pair of hostmask and flags
47 typedef std::pair<std::string, int> silenceset;
49 // deque list of pairs
50 typedef std::deque<silenceset> silencelist;
53 static int SILENCE_PRIVATE = 0x0001; /* p private messages */
54 static int SILENCE_CHANNEL = 0x0002; /* c channel messages */
55 static int SILENCE_INVITE = 0x0004; /* i invites */
56 static int SILENCE_NOTICE = 0x0008; /* n notices */
57 static int SILENCE_CNOTICE = 0x0010; /* t channel notices */
58 static int SILENCE_ALL = 0x0020; /* a all, (pcint) */
59 static int SILENCE_EXCLUDE = 0x0040; /* x exclude this pattern */
62 class cmd_silence : public command_t
65 cmd_silence (InspIRCd* Instance) : command_t(Instance,"SILENCE", 0, 0)
67 this->source = "m_silence_ext.so";
68 syntax = "{[+|-]<mask> <p|c|i|n|t|a|x>}";
71 CmdResult Handle (const char** parameters, int pcnt, userrec *user)
75 // no parameters, show the current silence list.
76 // Use Extensible::GetExt to fetch the silence list
78 user->GetExt("silence_list", sl);
79 // if the user has a silence list associated with their user record, show it
82 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
84 user->WriteServ("271 %s %s %s %s",user->nick, user->nick,c->first.c_str(), DecompPattern(c->second).c_str());
87 user->WriteServ("272 %s :End of Silence List",user->nick);
93 // one or more parameters, add or delete entry from the list (only the first parameter is used)
94 std::string mask = parameters[0] + 1;
95 char action = *parameters[0];
96 // Default is private and notice so clients do not break
97 int pattern = CompilePattern("pn");
99 // if pattern supplied, use it
101 pattern = CompilePattern(parameters[1]);
106 // 'SILENCE +' or 'SILENCE -', assume *!*@*
110 ModeParser::CleanMask(mask);
114 // fetch their silence list
116 user->GetExt("silence_list", sl);
117 // does it contain any entries and does it exist?
122 for (silencelist::iterator i = sl->begin(); i != sl->end(); i++)
124 // search through for the item
125 irc::string listitem = i->first.c_str();
126 if (listitem == mask && i->second == pattern)
129 user->WriteServ("950 %s %s :Removed %s %s from silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
136 // tidy up -- if a user's list is empty, theres no use having it
137 // hanging around in the user record.
139 user->Shrink("silence_list");
143 else if (action == '+')
145 // fetch the user's current silence list
147 user->GetExt("silence_list", sl);
148 // what, they dont have one??? WE'RE ALL GONNA DIE! ...no, we just create an empty one.
151 sl = new silencelist;
152 user->Extend("silence_list", sl);
154 for (silencelist::iterator n = sl->begin(); n != sl->end(); n++)
156 irc::string listitem = n->first.c_str();
157 if (listitem == mask && n->second == pattern)
159 user->WriteServ("952 %s %s :%s %s is already on your silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
163 if (((pattern & SILENCE_EXCLUDE) > 0))
165 sl->push_front(silenceset(mask,pattern));
169 sl->push_back(silenceset(mask,pattern));
171 user->WriteServ("951 %s %s :Added %s %s to silence list",user->nick, user->nick, mask.c_str(), DecompPattern(pattern).c_str());
178 /* turn the nice human readable pattern into a mask */
179 int CompilePattern(const char* pattern)
182 for (const char* n = pattern; *n; n++)
187 p |= SILENCE_PRIVATE;
190 p |= SILENCE_CHANNEL;
199 p |= SILENCE_CNOTICE;
205 p |= SILENCE_EXCLUDE;
214 /* turn the mask into a nice human readable format */
215 std::string DecompPattern (const int pattern)
218 if ((pattern & SILENCE_PRIVATE) > 0)
219 out += ",privatemessages";
220 if ((pattern & SILENCE_CHANNEL) > 0)
221 out += ",channelmessages";
222 if ((pattern & SILENCE_INVITE) > 0)
224 if ((pattern & SILENCE_NOTICE) > 0)
225 out += ",privatenotices";
226 if ((pattern & SILENCE_CNOTICE) > 0)
227 out += ",channelnotices";
228 if ((pattern & SILENCE_ALL) > 0)
230 if ((pattern & SILENCE_EXCLUDE) > 0)
232 return "<" + out.substr(1) + ">";
237 class ModuleSilence : public Module
240 cmd_silence* mycommand;
243 ModuleSilence(InspIRCd* Me)
247 mycommand = new cmd_silence(ServerInstance);
248 ServerInstance->AddCommand(mycommand);
251 void Implements(char* List)
253 List[I_OnBuildExemptList] = List[I_OnUserQuit] = List[I_On005Numeric] = List[I_OnUserPreNotice] = List[I_OnUserPreMessage] = List[I_OnUserPreInvite] = 1;
256 virtual void OnUserQuit(userrec* user, const std::string &reason)
258 // when the user quits tidy up any silence list they might have just to keep things tidy
260 user->GetExt("silence_list", sl);
264 user->Shrink("silence_list");
268 virtual void On005Numeric(std::string &output)
270 // we don't really have a limit...
271 output = output + " ESILENCE SILENCE=999";
274 virtual void OnBuildExemptList(MessageType message_type, chanrec* chan, userrec* sender, char status, CUList &exempt_list)
276 int public_silence = (message_type == MSG_PRIVMSG ? SILENCE_CHANNEL : SILENCE_CNOTICE);
281 ulist = chan->GetOppedUsers();
284 ulist = chan->GetHalfoppedUsers();
287 ulist = chan->GetVoicedUsers();
290 ulist = chan->GetUsers();
294 for (CUList::iterator i = ulist->begin(); i != ulist->end(); i++)
296 if (IS_LOCAL(i->second))
298 if (MatchPattern(i->second, sender, public_silence) == 1)
300 exempt_list[i->second] = i->second;
306 virtual int PreText(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list, int silence_type)
311 if (target_type == TYPE_USER)
313 return MatchPattern((userrec*)dest, user, silence_type);
315 else if (target_type == TYPE_CHANNEL)
317 chanrec* chan = (chanrec*)dest;
320 this->OnBuildExemptList((silence_type == SILENCE_PRIVATE ? MSG_PRIVMSG : MSG_NOTICE), chan, user, status, exempt_list);
326 virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
328 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_PRIVATE);
331 virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
333 return PreText(user, dest, target_type, text, status, exempt_list, SILENCE_NOTICE);
336 virtual int OnUserPreInvite(userrec* source,userrec* dest,chanrec* channel)
338 return MatchPattern(dest, source, SILENCE_INVITE);
341 int MatchPattern(userrec* dest, userrec* source, int pattern)
344 dest->GetExt("silence_list", sl);
347 for (silencelist::const_iterator c = sl->begin(); c != sl->end(); c++)
349 if ((match(source->GetFullHost(), c->first.c_str())) && ( ((c->second & pattern) > 0)) || ((c->second & SILENCE_ALL) > 0))
351 if (((c->second & SILENCE_EXCLUDE) > 0))
364 virtual ~ModuleSilence()
368 virtual Version GetVersion()
370 return Version(1,1,0,1,VF_VENDOR,API_VERSION);
375 class ModuleSilenceFactory : public ModuleFactory
378 ModuleSilenceFactory()
382 ~ModuleSilenceFactory()
386 virtual Module * CreateModule(InspIRCd* Me)
388 return new ModuleSilence(Me);
394 extern "C" void * init_module( void )
396 return new ModuleSilenceFactory;