]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_shun.cpp
Use SHUN, not S, try match on std::string for removal
[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 //typedef std::vector<Shun> shunlist;
60
61 class cmd_shun : public Command
62 {
63  private:
64         InspIRCd *Srv;
65
66  public:
67         cmd_shun(InspIRCd* Me) : Command(Me, "SHUN", "o", 1), Srv(Me)
68         {
69                 this->source = "m_shun.so";
70         }
71
72         CmdResult Handle(const char* const*parameters, int pcnt, User *user)
73         {
74                 /* syntax: SHUN nick!user@host time :reason goes here */
75                 /* 'time' is a human-readable timestring, like 2d3h2s. */
76
77                 if(pcnt == 1)
78                 {
79                         if (ServerInstance->XLines->DelLine(parameters[0], "S", user))
80                         {
81                                 ServerInstance->SNO->WriteToSnoMask('x',"%s Removed shun on %s.",user->nick,parameters[0]);
82                         }
83                         else
84                         {
85                                 // XXX todo implement stats
86                                 user->WriteServ("NOTICE %s :*** Shun %s not found in list, try /stats s.",user->nick,parameters[0]);
87                         }
88
89                         return CMD_SUCCESS;
90                 }
91                 else if (pcnt >= 2)
92                 {
93                         // Adding - XXX todo make this respect <insane> tag perhaps..
94                         long duration = ServerInstance->Duration(parameters[1]);
95                         Shun *r = NULL;
96
97                         try
98                         {
99                                 r = new Shun(ServerInstance, ServerInstance->Time(), duration, user->nick, parameters[2], parameters[0]);
100                         }
101                         catch (...)
102                         {
103                                 ; // Do nothing. If we get here, the regex was fucked up, and they already got told it fucked up.
104                         }
105
106                         if (r)
107                         {
108                                 if (ServerInstance->XLines->AddLine(r, user))
109                                 {
110                                         if (!duration)
111                                         {
112                                                 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent shun for %s.", user->nick, parameters[0]);
113                                         }
114                                         else
115                                         {
116                                                 time_t c_requires_crap = duration + ServerInstance->Time();
117                                                 ServerInstance->SNO->WriteToSnoMask('x', "%s added timed shun for %s, expires on %s", user->nick, parameters[0],
118                                                 ServerInstance->TimeString(c_requires_crap).c_str());
119                                         }
120
121                                         ServerInstance->XLines->ApplyLines();
122                                 }
123                                 else
124                                 {
125                                         delete r;
126                                         user->WriteServ("NOTICE %s :*** Shun for %s already exists", user->nick, parameters[0]);
127                                 }
128                         }
129                 }
130
131                 return CMD_FAILURE;
132         }
133 };
134
135 class ModuleShun : public Module
136 {
137         cmd_shun* mycommand;
138
139  public:
140         ModuleShun(InspIRCd* Me) : Module(Me)
141         {
142                 mycommand = new cmd_shun(ServerInstance);
143                 ServerInstance->AddCommand(mycommand);
144         }
145
146         void Implements(char* List)
147         {
148                 List[I_OnPreCommand] = List[I_OnStats] = 1;
149         }
150         
151         virtual int OnStats(char symbol, User* user, string_list& out)
152         {
153                 // XXX write me
154 //format << Srv->Config->ServerName << " 223 " << user->nick << " :" << iter->banmask << " " << iter->set_on << " " << iter->length << " " << 
155 //iter->set_by << " " << iter->reason; 
156                 
157                 return 0;
158         }
159
160         virtual int OnPreCommand(const std::string &command, const char* const*parameters, int pcnt, User* user, bool validated, const std::string &original_line)
161         {
162                 if (user->registered != REG_ALL)
163                         return 0;
164
165                 if((command != "PONG") && (command != "PING"))
166                 {
167                         // Don't let them issue cmd if they are shunned..
168                         XLine *rl = ServerInstance->XLines->MatchesLine("S", user);
169
170                         if (rl)
171                         {
172                                 return 1;
173                         }
174                 }
175
176                 return 0;
177         }
178
179         virtual ~ModuleShun()
180         {
181         }
182
183         virtual Version GetVersion()
184         {
185                 return Version(1,0,0,0,0,API_VERSION);
186         }
187 };
188
189 MODULE_INIT(ModuleShun)
190