]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_shun.cpp
Add m_shun from third party to 1.2, update it to compile (currently untested..) and...
[user/henk/code/inspircd.git] / src / modules / m_shun.cpp
1 #include <algorithm>
2 #include <vector>
3 #include <string>
4 #include <sstream>
5 #include "inspircd.h"
6 #include "modules.h"
7 #include "hashcomp.h"
8 #include "configreader.h"
9
10 /* $ModDesc: Gives the /shun command, which stops a user executing all commands except PING and PONG. */
11 /* $ModAuthor: Om */
12 /* $ModAuthorMail: om@inspircd.org */
13
14 class Shun
15 {
16 public:
17         std::string banmask;
18         std::string set_by;
19         time_t set_on;
20         long length;
21         std::string reason;
22
23         Shun() { }
24
25         Shun(std::string bm, std::string sb, time_t so, long ln, std::string rs) : banmask(bm), set_by(sb), set_on(so), length(ln), reason(rs) { }
26         
27         bool operator<(const Shun &ban2) const
28         {
29                 return ((this->set_on + this->length) < (ban2.set_on + ban2.length));
30         }
31 };
32
33 typedef std::vector<Shun> shunlist;
34
35 class ModuleShunBase
36 {
37  public:
38         /* shuns is declared here, as our type is right above. Don't try move it. */
39         shunlist shuns;
40  
41         InspIRCd* Srv;
42          
43         std::string EncodeShun(const Shun &shun)
44         {
45                 std::ostringstream stream;      
46                 stream << shun.banmask << " " << shun.set_by << " " << shun.set_on << " " << shun.length << " " << shun.reason;
47                 return stream.str();    
48         }
49
50         Shun DecodeShun(const std::string &data)
51         {
52                 Shun res;
53                 std::istringstream stream(data);
54                 stream >> res.banmask;
55                 stream >> res.set_by;
56                 stream >> res.set_on;
57                 stream >> res.length;
58                 res.reason = stream.str();
59         
60                 return res;
61         }
62
63         void ExpireBans()
64         {
65                 while(shuns.size() && shuns.begin()->length && ((shuns.begin()->set_on + shuns.begin()->length) <= Srv->Time()))
66                 {
67                         shunlist::iterator iter = shuns.begin();
68                 
69                         Srv->SNO->WriteToSnoMask('X', "*** %ld second shun on '%s' (%s) set by %s %ld seconds ago expired", iter->length, iter->banmask.c_str(), iter->reason.c_str(), iter->set_by.c_str(), Srv->Time() - iter->set_on);
70                         shuns.erase(iter);
71                 }
72         }
73 };
74
75 class cmd_shun : public Command
76 {
77  private:
78         InspIRCd *Srv;
79         ModuleShunBase* base;
80
81  public:
82         cmd_shun(InspIRCd* Me, ModuleShunBase* b)
83         : Command(Me, "SHUN", "o", 1), Srv(Me), base(b)
84         {
85                 this->source = "m_shun.so";
86         }
87
88         CmdResult Handle(const char* const*parameters, int pcnt, User *user)
89         {
90                 /* syntax: SHUN nick!user@host time :reason goes here */
91                 /* 'time' is a human-readable timestring, like 2d3h2s. */
92                 
93                 base->ExpireBans();
94
95                 if(pcnt == 1)
96                 {
97                         /* form: SHUN mask removes a SHUN */
98                         for(shunlist::iterator iter = base->shuns.begin(); iter != base->shuns.end(); iter++)
99                         {
100                                 if(parameters[0] == iter->banmask)
101                                 {
102                                         Srv->SNO->WriteToSnoMask('X', "*** %s removed shun '%s', set %ld seconds ago with reason '%s'", user->nick, iter->banmask.c_str(), Srv->Time() - iter->set_on, iter->reason.c_str());
103                                         base->shuns.erase(iter);
104                                         return CMD_SUCCESS;
105                                 }
106                         }
107
108                         user->WriteServ("NOTICE %s :*** The mask %s is not currently shunned, try /stats s", user->nick, parameters[0]);
109                         return CMD_FAILURE;
110                 }
111                 else if (pcnt >= 2)
112                 {
113                         /* full form to add a shun */
114                         if(Srv->IsValidMask(parameters[0]))
115                         {
116                                 // parameters[0] = Foamy!*@*
117                                 // parameters[1] = 1h3m2s
118                                 // parameters[2] = Tortoise abuser
119                                 for(shunlist::iterator iter = base->shuns.begin(); iter != base->shuns.end(); iter++)
120                                 {
121                                         if (parameters[0] == iter->banmask)
122                                         {
123                                                 user->WriteServ("NOTICE %s :*** Shun on %s already exists", user->nick, parameters[0]);
124                                                 return CMD_FAILURE;
125                                         }
126                                 }
127                                 
128                                 long length = Srv->Duration(parameters[1]);
129                                 
130                                 std::string reason = (pcnt > 2) ? parameters[2] : "No reason supplied";
131                                 
132                                 base->shuns.push_back(Shun(parameters[0], user->nick, Srv->Time(), length, reason));
133                                         
134                                 std::sort(base->shuns.begin(), base->shuns.end());
135                                 
136                                 if(length > 0)
137                                         Srv->SNO->WriteToSnoMask('X', "*** %s added %ld second shun on '%s' (%s)", user->nick, length, parameters[0], reason.c_str());
138                                 else
139                                         Srv->SNO->WriteToSnoMask('X', "*** %s added permanent shun on '%s' (%s)", user->nick, parameters[0], reason.c_str());
140                                 
141                                 return CMD_SUCCESS;
142                         }
143                         else
144                         {
145                                 user->WriteServ("NOTICE %s :*** Invalid ban mask (%s)", user->nick, parameters[0]);
146                         }
147                 }
148
149                 return CMD_FAILURE;
150         }
151 };
152
153 class ModuleShun : public Module, public ModuleShunBase
154 {
155         cmd_shun* mycommand;
156
157  public:
158         ModuleShun(InspIRCd* Me)
159         : Module::Module(Me)
160         {
161                 this->Srv = Me;
162                 mycommand = new cmd_shun(Srv, this);
163                 Srv->AddCommand(mycommand);
164         }
165
166         void Implements(char* List)
167         {
168                 List[I_OnPreCommand] = List[I_OnSyncOtherMetaData] = List[I_OnDecodeMetaData] = List[I_OnStats] = 1;
169         }
170         
171         virtual int OnStats(char symbol, User* user, string_list& out)
172         {
173                 ExpireBans();
174         
175                 if(symbol == 's')
176                 {
177                         for(shunlist::iterator iter = shuns.begin(); iter != shuns.end(); iter++)
178                         {
179                                 std::ostringstream format;
180                                 format << Srv->Config->ServerName << " 223 " << user->nick << " :" << iter->banmask << " " << iter->set_on << " " << iter->length << " " << iter->set_by << " " << iter->reason;
181                                 out.push_back(format.str());
182                         }
183                 }
184                 
185                 return 0;
186         }
187
188         virtual int OnPreCommand(const std::string &command, const char* const*parameters, int pcnt, User* user, bool validated, const std::string &original_line)
189         {
190                 if((command != "PONG") && (command != "PING"))
191                 {
192                         ExpireBans();
193                 
194                         for(shunlist::iterator iter = shuns.begin(); iter != shuns.end(); iter++)
195                                 if(Srv->MatchText(user->GetFullHost(), iter->banmask) || Srv->MatchText(user->GetFullRealHost(), iter->banmask))
196                                         return 1;
197                 }
198                 
199                 return 0;
200         }
201
202         virtual void OnSyncOtherMetaData(Module* proto, void* opaque, bool displayable)
203         {
204                 for(shunlist::iterator iter = shuns.begin(); iter != shuns.end(); iter++)
205                 {
206                         proto->ProtoSendMetaData(opaque, TYPE_OTHER, NULL, "shun", EncodeShun(*iter));
207                 }
208         }
209
210         virtual void OnDecodeMetaData(int target_type, void* target, const std::string &extname, const std::string &extdata)
211         {
212                 if((target_type == TYPE_OTHER) && (extname == "shun"))
213                 {
214                         shuns.push_back(DecodeShun(extdata));
215                         std::sort(shuns.begin(), shuns.end());
216                 }
217         }
218
219         virtual ~ModuleShun()
220         {
221         }
222
223         virtual Version GetVersion()
224         {
225                 return Version(1,0,0,0,0,API_VERSION);
226         }
227 };
228
229 MODULE_INIT(ModuleShun)
230