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