]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_shun.cpp
Add and document <shun:enabledcommands>, allowing customisation of specifically which...
[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), Srv(Me)
77         {
78                 this->source = "m_shun.so";
79         }
80
81         CmdResult Handle(const std::vector<std::string>& parameters, User *user)
82         {
83                 /* syntax: SHUN nick!user@host time :reason goes here */
84                 /* 'time' is a human-readable timestring, like 2d3h2s. */
85
86                 if (parameters.size() == 1)
87                 {
88                         if (ServerInstance->XLines->DelLine(parameters[0].c_str(), "SHUN", user))
89                         {
90                                 ServerInstance->SNO->WriteToSnoMask('x',"%s Removed shun on %s.",user->nick.c_str(),parameters[0].c_str());
91                         }
92                         else
93                         {
94                                 // XXX todo implement stats
95                                 user->WriteServ("NOTICE %s :*** Shun %s not found in list, try /stats s.",user->nick.c_str(),parameters[0].c_str());
96                         }
97
98                         return CMD_SUCCESS;
99                 }
100                 else if (parameters.size() >= 2)
101                 {
102                         // Adding - XXX todo make this respect <insane> tag perhaps..
103                         long duration = ServerInstance->Duration(parameters[1]);
104                         Shun *r = NULL;
105
106                         try
107                         {
108                                 r = new Shun(ServerInstance, ServerInstance->Time(), duration, user->nick.c_str(), parameters[2].c_str(), parameters[0].c_str());
109                         }
110                         catch (...)
111                         {
112                                 ; // Do nothing. If we get here, the regex was fucked up, and they already got told it fucked up.
113                         }
114
115                         if (r)
116                         {
117                                 if (ServerInstance->XLines->AddLine(r, user))
118                                 {
119                                         if (!duration)
120                                         {
121                                                 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent shun for %s.", user->nick.c_str(), parameters[0].c_str());
122                                         }
123                                         else
124                                         {
125                                                 time_t c_requires_crap = duration + ServerInstance->Time();
126                                                 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed shun for %s, expires on %s", user->nick.c_str(), parameters[0].c_str(),
127                                                 ServerInstance->TimeString(c_requires_crap).c_str());
128                                         }
129
130                                         ServerInstance->XLines->ApplyLines();
131                                 }
132                                 else
133                                 {
134                                         delete r;
135                                         user->WriteServ("NOTICE %s :*** Shun for %s already exists", user->nick.c_str(), parameters[0].c_str());
136                                 }
137                         }
138                 }
139
140                 return CMD_FAILURE;
141         }
142 };
143
144 class ModuleShun : public Module
145 {
146         cmd_shun* mycommand;
147         ShunFactory *f;
148         std::map<std::string, bool> ShunEnabledCommands;
149
150  public:
151         ModuleShun(InspIRCd* Me) : Module(Me)
152         {
153                 f = new ShunFactory(ServerInstance);
154                 ServerInstance->XLines->RegisterFactory(f);
155
156                 mycommand = new cmd_shun(ServerInstance);
157                 ServerInstance->AddCommand(mycommand);
158
159                 Implementation eventlist[] = { I_OnStats, I_OnPreCommand, I_OnUserConnect, I_OnRehash };
160                 ServerInstance->Modules->Attach(eventlist, this, 4);
161                 OnRehash(NULL, "");
162         }
163
164         virtual ~ModuleShun()
165         {
166                 ServerInstance->XLines->DelAll("SHUN");
167                 ServerInstance->XLines->UnregisterFactory(f);
168         }
169
170         virtual int OnStats(char symbol, User* user, string_list& out)
171         {
172                 if (symbol != 'S')
173                         return 0;
174
175                 ServerInstance->XLines->InvokeStats("SHUN", 223, user, out);
176                 return 1;
177         }
178
179         virtual void OnRehash(User* user, const std::string &parameter)
180         {
181                 ConfigReader MyConf(ServerInstance);
182                 std::string cmds = MyConf.ReadValue("shun", "enabledcommands", 0);
183
184                 if (cmds.empty())
185                         cmds = "PING PONG QUIT";
186
187                 ShunEnabledCommands.clear();
188
189                 std::stringstream dcmds(cmds);
190                 std::string thiscmd;
191
192                 while (dcmds >> thiscmd)
193                 {
194                         ShunEnabledCommands[thiscmd] = true;
195                 }
196         }
197
198         virtual void OnUserConnect(User* user)
199         {
200                 if (!IS_LOCAL(user))
201                         return;
202
203                 // Apply lines on user connect
204                 XLine *rl = ServerInstance->XLines->MatchesLine("SHUN", user);
205
206                 if (rl)
207                 {
208                         // Bang. :P
209                         rl->Apply(user);
210                 }
211         }
212
213         virtual int OnPreCommand(std::string &command, std::vector<std::string>& parameters, User* user, bool validated, const std::string &original_line)
214         {
215                 if (validated || !user->GetExt("shunned"))
216                         return 0;
217
218                 if (!ServerInstance->XLines->MatchesLine("SHUN", user))
219                 {
220                         /* The shun previously set on this user has expired or been removed */
221                         user->Shrink("shunned");
222                         return 0;
223                 }
224
225                 std::map<std::string, bool>::iterator i = ShunEnabledCommands.find(command);
226
227                 if (i == ShunEnabledCommands.end())
228                         return 1;
229
230                 if (command == "QUIT")
231                 {
232                         /* Allow QUIT but dont show any quit message */
233                         parameters.clear();
234                 }
235                 else if (command == "PART")
236                 {
237                         /* same for PART */
238                         parameters.clear();
239                 }
240
241                 return 1;
242         }
243
244         virtual Version GetVersion()
245         {
246                 return Version("$Id$",VF_VENDOR|VF_COMMON,API_VERSION);
247         }
248 };
249
250 MODULE_INIT(ModuleShun)
251