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