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