2 * Copyright (C) 2008 kroimon
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
14 class CAmsgFilterModule
: public CModule
{
16 MODCONSTRUCTOR(CAmsgFilterModule
) {}
17 virtual ~CAmsgFilterModule() {}
19 virtual bool OnLoad(const CString
& sArgs
, CString
& sMessage
) {
21 CString sChan
= sArgs
.Token(i
++);
22 while (!sChan
.empty()) {
24 PutModule("Unable to add [" + sChan
+ "]");
25 sChan
= sArgs
.Token(i
++);
28 // Load our saved settings, ignore errors
29 for (MCString::iterator it
= BeginNV(); it
!= EndNV(); it
++)
35 virtual EModRet
OnUserMsg(CString
& sTarget
, CString
& sMessage
) {
37 sTarget
.Split(",", sTargets
);
39 if (IsAmsg(sTargets
)) {
41 for (SCString::iterator itTarget
= sTargets
.begin(); itTarget
!= sTargets
.end(); itTarget
++) {
42 if (!IsFiltered(*itTarget
)) {
45 sTarget
.append(*itTarget
);
47 PutUser(":" + GetModNick() + " PRIVMSG " + *itTarget
+ " :amsg blocked in this channel: " + sMessage
);
55 virtual void OnModCommand(const CString
& sLine
) {
56 CString sCommand
= sLine
.Token(0).AsLower();
58 if (sCommand
== "add") {
59 CString sChan
= sLine
.Token(1);
60 if (AlreadyAdded(sChan
)) {
61 PutModule(sChan
+ " is already added");
62 } else if (Add(sChan
)) {
63 PutModule("Added " + sChan
+ " to amsg filter list");
65 PutModule("Usage: Add [!]<#chan>");
68 } else if (sCommand
== "del") {
69 CString sChan
= sLine
.Token(1);
71 PutModule("Removed " + sChan
+ " from amsg filter list");
73 PutModule("Usage: Del [!]<#chan>");
75 } else if (sCommand
== "list") {
77 Table
.AddColumn("Channel");
79 for (unsigned int i
= 0; i
< vsChans
.size(); i
++) {
81 Table
.SetCell("Channel", vsChans
[i
]);
84 for (unsigned int i
= 0; i
< vsNegChans
.size(); i
++) {
86 Table
.SetCell("Channel", "!" + vsNegChans
[i
]);
92 while (Table
.GetLine(i
++, sTmp
))
95 PutModule("You have no amsg filters");
97 } else if (sCommand
== "help") {
99 Table
.AddColumn("Command");
100 Table
.AddColumn("Description");
103 Table
.SetCell("Command", "Add");
104 Table
.SetCell("Description", "Add an entry, use !#chan to negate and * for wildcards");
107 Table
.SetCell("Command", "Del");
108 Table
.SetCell("Description", "Remove an entry, needs to be an exact match");
111 Table
.SetCell("Command", "List");
112 Table
.SetCell("Description", "List all entries");
114 unsigned int uTableIdx
= 0;
117 while (Table
.GetLine(uTableIdx
++, sTmp
))
127 bool IsAmsg(SCString
& sTargets
) {
128 /* amsgs can't go to just one channel */
129 if (sTargets
.size() == 1)
132 /* check that sTargets includes all attached channels */
133 const vector
<CChan
*>& vChans
= m_pUser
->GetChans();
134 for (unsigned int i
= 0; i
< vChans
.size(); i
++) {
135 CChan
* pChan
= vChans
[i
];
136 if (pChan
->IsDetached())
138 SCString::iterator it
= sTargets
.find(pChan
->GetName());
139 if (it
== sTargets
.end())
146 bool IsFiltered(const CString
& sChan
) {
147 for (unsigned int i
= 0; i
< vsNegChans
.size(); i
++)
148 if (sChan
.WildCmp(vsNegChans
[i
]))
150 for (unsigned int i
= 0; i
< vsChans
.size(); i
++)
151 if (sChan
.WildCmp(vsChans
[i
]))
156 bool AlreadyAdded(const CString
& sInput
) {
157 VCString::iterator it
;
159 if (sInput
.Left(1) == "!") {
160 CString sChan
= sInput
.substr(1);
161 for (it
= vsNegChans
.begin(); it
!= vsNegChans
.end(); it
++)
165 for (it
= vsChans
.begin(); it
!= vsChans
.end(); it
++)
172 bool Add(const CString
& sChan
) {
173 if (sChan
.empty() || sChan
== "!")
176 if (sChan
.Left(1) == "!")
177 vsNegChans
.push_back(sChan
.substr(1));
179 vsChans
.push_back(sChan
);
181 // Also save it for next module load
187 bool Del(const CString
& sChan
) {
188 VCString::iterator it
, end
;
190 if (sChan
.empty() || sChan
== "!")
193 if (sChan
.Left(1) == "!") {
194 CString sTmp
= sChan
.substr(1);
195 it
= vsNegChans
.begin();
196 end
= vsNegChans
.end();
198 for (; it
!= end
; it
++)
205 vsNegChans
.erase(it
);
207 it
= vsChans
.begin();
210 for (; it
!= end
; it
++)
227 MODULEDEFS(CAmsgFilterModule
, "Excludes channels from amsgs")