]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_shun.cpp
0cf62dd35126571127753248aa1e2c2ba7db57d0
[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 #include "xline.h"
10
11 /* $ModDesc: Provides the /shun command, which stops a user executing all commands except PING and PONG. */
12
13 class Shun : public XLine
14 {
15 public:
16         std::string matchtext;
17
18         Shun(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char *shunmask) : XLine(Instance, s_time, d, src, re, "SHUN")
19         {
20                 this->matchtext = shunmask;
21         }
22
23         ~Shun()
24         {
25         }
26
27         bool Matches(User *u)
28         {
29                 if (ServerInstance->MatchText(u->GetFullHost(), matchtext) || ServerInstance->MatchText(u->GetFullRealHost(), matchtext))
30                         return true;
31
32                 return false;
33         }
34
35         bool Matches(const std::string &s)
36         {
37                 if (matchtext == s)
38                         return true;
39                 return false;
40         }
41
42         void Apply(User *u)
43         {
44                 if (!u->GetExt("shunned"))
45                         u->Extend("shunned");
46         }
47
48
49         void DisplayExpiry()
50         {
51                 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed shun %s (set by %s %ld seconds ago)", this->matchtext.c_str(), this->source, this->duration);
52         }
53
54         const char* Displayable()
55         {
56                 return matchtext.c_str();
57         }
58 };
59
60 /** An XLineFactory specialized to generate shun pointers
61  */
62 class ShunFactory : public XLineFactory
63 {
64  public:
65         ShunFactory(InspIRCd* Instance) : XLineFactory(Instance, "SHUN") { }
66
67         /** Generate a shun
68         */
69         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
70         {
71                 return new Shun(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
72         }
73 };
74
75 //typedef std::vector<Shun> shunlist;
76
77 class cmd_shun : public Command
78 {
79  private:
80         InspIRCd *Srv;
81
82  public:
83         cmd_shun(InspIRCd* Me) : Command(Me, "SHUN", "o", 1), Srv(Me)
84         {
85                 this->source = "m_shun.so";
86         }
87
88         CmdResult Handle(const std::vector<std::string>& parameters, User *user)
89         {
90                 /* syntax: SHUN nick!user@host time :reason goes here */
91                 /* 'time' is a human-readable timestring, like 2d3h2s. */
92
93                 if (parameters.size() == 1)
94                 {
95                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "SHUN", user))
96                         {
97                                 ServerInstance->SNO->WriteToSnoMask('x',"%s Removed shun on %s.",user->nick.c_str(),parameters[0].c_str());
98                         }
99                         else
100                         {
101                                 // XXX todo implement stats
102                                 user->WriteServ("NOTICE %s :*** Shun %s not found in list, try /stats s.",user->nick.c_str(),parameters[0].c_str());
103                         }
104
105                         return CMD_SUCCESS;
106                 }
107                 else if (parameters.size() >= 2)
108                 {
109                         // Adding - XXX todo make this respect <insane> tag perhaps..
110                         long duration = ServerInstance->Duration(parameters[1]);
111                         Shun *r = NULL;
112
113                         try
114                         {
115                                 r = new Shun(ServerInstance, ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
116                         }
117                         catch (...)
118                         {
119                                 ; // Do nothing. If we get here, the regex was fucked up, and they already got told it fucked up.
120                         }
121
122                         if (r)
123                         {
124                                 if (ServerInstance->XLines->AddLine(r, user))
125                                 {
126                                         if (!duration)
127                                         {
128                                                 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent shun for %s.", user->nick.c_str(), parameters[0].c_str());
129                                         }
130                                         else
131                                         {
132                                                 time_t c_requires_crap = duration + ServerInstance->Time();
133                                                 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed shun for %s, expires on %s", user->nick.c_str(), parameters[0].c_str(),
134                                                 ServerInstance->TimeString(c_requires_crap).c_str());
135                                         }
136
137                                         ServerInstance->XLines->ApplyLines();
138                                 }
139                                 else
140                                 {
141                                         delete r;
142                                         user->WriteServ("NOTICE %s :*** Shun for %s already exists", user->nick.c_str(), parameters[0].c_str());
143                                 }
144                         }
145                 }
146
147                 return CMD_FAILURE;
148         }
149 };
150
151 class ModuleShun : public Module
152 {
153         cmd_shun* mycommand;
154         ShunFactory *f;
155
156  public:
157         ModuleShun(InspIRCd* Me) : Module(Me)
158         {
159                 f = new ShunFactory(ServerInstance);
160                 ServerInstance->XLines->RegisterFactory(f);
161
162                 mycommand = new cmd_shun(ServerInstance);
163                 ServerInstance->AddCommand(mycommand);
164
165                 Implementation eventlist[] = { I_OnStats, I_OnPreCommand, I_OnUserConnect };
166                 ServerInstance->Modules->Attach(eventlist, this, 3);
167         }
168
169         virtual ~ModuleShun()
170         {
171                 ServerInstance->XLines->UnregisterFactory(f);
172         }
173
174         virtual int OnStats(char symbol, User* user, string_list& out)
175         {
176                 if (symbol != 'S')
177                         return 0;
178
179                 ServerInstance->XLines->InvokeStats("SHUN", 223, user, out);
180                 return 1;
181         }
182
183         virtual void OnUserConnect(User* user)
184         {
185                 if (!IS_LOCAL(user))
186                         return;
187
188                 // Apply lines on user connect
189                 XLine *rl = ServerInstance->XLines->MatchesLine("SHUN", user);
190
191                 if (rl)
192                 {
193                         // Bang. :P
194                         rl->Apply(user);
195                 }
196         }
197
198         virtual int OnPreCommand(std::string &command, std::vector<std::string>& parameters, User* user, bool validated, const std::string &original_line)
199         {
200                 if (validated || !user->GetExt("shunned"))
201                         return 0;
202
203                 if (!ServerInstance->XLines->MatchesLine("SHUN", user))
204                 {
205                         /* The shun previously set on this user has expired or been removed */
206                         user->Shrink("shunned");
207                         return 0;
208                 }
209
210                 if (command == "QUIT")
211                 {
212                         /* Allow QUIT but dont show any quit message */
213                         parameters.clear();
214                         return 0;
215                 }
216
217                 /* Always allow PONG and PING */
218                 if (command == "PONG" || command == "PING")
219                         return 0;
220
221                 return 1;
222         }
223
224         virtual Version GetVersion()
225         {
226                 return Version(1,2,0,0,VF_VENDOR|VF_COMMON,API_VERSION);
227         }
228 };
229
230 MODULE_INIT(ModuleShun)
231