]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_shun.cpp
Add a factory to produce shuns, don't autoapply to userlist, etc.
[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                 // Application is done by the module.
45         }
46
47
48         void DisplayExpiry()
49         {
50                 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed shun %s (set by %s %ld seconds ago)", this->matchtext.c_str(), this->source, this->duration);
51         }
52
53         const char* Displayable()
54         {
55                 return matchtext.c_str();
56         }
57 };
58
59 /** An XLineFactory specialized to generate shun pointers
60  */
61 class ShunFactory : public XLineFactory
62 {
63  public:
64         ShunFactory(InspIRCd* Instance) : XLineFactory(Instance, "SHUN") { }
65
66         /** Generate a shun
67         */
68         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
69         {
70                 return new Shun(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
71         }
72
73         virtual bool AutoApplyToUserList(XLine*)
74         {
75                 // No, we don't want to be applied to users automagically.
76                 return false;
77         }
78 };
79
80 //typedef std::vector<Shun> shunlist;
81
82 class cmd_shun : public Command
83 {
84  private:
85         InspIRCd *Srv;
86
87  public:
88         cmd_shun(InspIRCd* Me) : Command(Me, "SHUN", "o", 1), Srv(Me)
89         {
90                 this->source = "m_shun.so";
91         }
92
93         CmdResult Handle(const char* const*parameters, int pcnt, User *user)
94         {
95                 /* syntax: SHUN nick!user@host time :reason goes here */
96                 /* 'time' is a human-readable timestring, like 2d3h2s. */
97
98                 if(pcnt == 1)
99                 {
100                         if (ServerInstance->XLines->DelLine(parameters[0], "S", user))
101                         {
102                                 ServerInstance->SNO->WriteToSnoMask('x',"%s Removed shun on %s.",user->nick,parameters[0]);
103                         }
104                         else
105                         {
106                                 // XXX todo implement stats
107                                 user->WriteServ("NOTICE %s :*** Shun %s not found in list, try /stats s.",user->nick,parameters[0]);
108                         }
109
110                         return CMD_SUCCESS;
111                 }
112                 else if (pcnt >= 2)
113                 {
114                         // Adding - XXX todo make this respect <insane> tag perhaps..
115                         long duration = ServerInstance->Duration(parameters[1]);
116                         Shun *r = NULL;
117
118                         try
119                         {
120                                 r = new Shun(ServerInstance, ServerInstance->Time(), duration, user->nick, parameters[2], parameters[0]);
121                         }
122                         catch (...)
123                         {
124                                 ; // Do nothing. If we get here, the regex was fucked up, and they already got told it fucked up.
125                         }
126
127                         if (r)
128                         {
129                                 if (ServerInstance->XLines->AddLine(r, user))
130                                 {
131                                         if (!duration)
132                                         {
133                                                 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent shun for %s.", user->nick, parameters[0]);
134                                         }
135                                         else
136                                         {
137                                                 time_t c_requires_crap = duration + ServerInstance->Time();
138                                                 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed shun for %s, expires on %s", user->nick, parameters[0],
139                                                 ServerInstance->TimeString(c_requires_crap).c_str());
140                                         }
141
142                                         ServerInstance->XLines->ApplyLines();
143                                 }
144                                 else
145                                 {
146                                         delete r;
147                                         user->WriteServ("NOTICE %s :*** Shun for %s already exists", user->nick, parameters[0]);
148                                 }
149                         }
150                 }
151
152                 return CMD_FAILURE;
153         }
154 };
155
156 class ModuleShun : public Module
157 {
158         cmd_shun* mycommand;
159         ShunFactory *f;
160
161  public:
162         ModuleShun(InspIRCd* Me) : Module(Me)
163         {
164                 f = new ShunFactory(ServerInstance);
165                 ServerInstance->XLines->RegisterFactory(f);
166
167                 mycommand = new cmd_shun(ServerInstance);
168                 ServerInstance->AddCommand(mycommand);
169         }
170
171         virtual ~ModuleShun()
172         {
173                 ServerInstance->XLines->UnregisterFactory(f);
174         }
175
176         void Implements(char* List)
177         {
178                 List[I_OnPreCommand] = List[I_OnStats] = 1;
179         }
180         
181         virtual int OnStats(char symbol, User* user, string_list& out)
182         {
183                 // XXX write me
184 //format << Srv->Config->ServerName << " 223 " << user->nick << " :" << iter->banmask << " " << iter->set_on << " " << iter->length << " " << 
185 //iter->set_by << " " << iter->reason; 
186                 
187                 return 0;
188         }
189
190         virtual int OnPreCommand(const std::string &command, const char* const*parameters, int pcnt, User* user, bool validated, const std::string &original_line)
191         {
192                 if (user->registered != REG_ALL)
193                         return 0;
194
195                 if((command != "PONG") && (command != "PING"))
196                 {
197                         // Don't let them issue cmd if they are shunned..
198                         XLine *rl = ServerInstance->XLines->MatchesLine("S", user);
199
200                         if (rl)
201                         {
202                                 return 1;
203                         }
204                 }
205
206                 return 0;
207         }
208
209         virtual Version GetVersion()
210         {
211                 return Version(1,0,0,0,0,API_VERSION);
212         }
213 };
214
215 MODULE_INIT(ModuleShun)
216