]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cban.cpp
Updated copyrights in headers etc using perl inplace edit
[user/henk/code/inspircd.git] / src / modules / m_cban.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd is copyright (C) 2002-2006 ChatSpike-Dev.
6  *                       E-mail:
7  *                <brain@chatspike.net>
8  *                <Craig@chatspike.net>
9  *     
10  * Written by Craig Edwards, Craig McLure, and others.
11  * This program is free but copyrighted software; see
12  *            the file COPYING for details.
13  *
14  * ---------------------------------------------------
15  */
16
17 using namespace std;
18
19 #include <stdio.h>
20 #include "users.h"
21 #include "channels.h"
22 #include "modules.h"
23 #include "helperfuncs.h"
24
25 /* $ModDesc: Gives /cban, aka C:lines. Think Q:lines, for channels. */
26
27 extern time_t TIME;
28
29 class CBan
30 {
31  private:
32         unsigned long expiry;
33         std::string chname;
34         std::string reason;
35
36  public:
37         CBan(std::string cn, std::string rs, unsigned long ex)
38         {
39                 chname = cn;
40                 reason = rs;
41                 expiry = ex;
42         }
43
44         std::string GetName()
45         {
46                 return this->chname;
47         }
48
49         std::string GetReason()
50         {
51                 return this->reason;
52         }
53
54         unsigned long GetExpiry()
55         {
56                 return this->expiry;
57         }
58 };
59
60 /* cbans is declared here, as our type is right above. Don't try move it. */
61 vector<CBan> cbans;
62
63 class cmd_cban : public command_t
64 {
65  private:
66         Server *Srv;
67
68  public:
69         cmd_cban (Server* Me) : command_t("CBAN", 'o', 1)
70         {
71                 this->source = "m_cban.so";
72                 this->Srv = Me;
73         }
74
75         void Handle(char **parameters, int pcnt, userrec *user)
76         {
77                 /* syntax: CBAN #channel time :reason goes here */
78                 /* 'time' is a human-readable timestring, like 2d3h2s. */
79
80                 std::string chname;
81                 std::string reason;
82                 unsigned long expiry;
83
84                 if (pcnt == 1)
85                 {
86                         /* form: CBAN #channel removes a CBAN */
87                         for (vector<CBan>::iterator myiter; myiter < cbans.end(); myiter++)
88                         {
89                                 if (parameters[0] == (*myiter).GetName())
90                                 {
91                                         cbans.erase(myiter);
92                                         break;
93                                 }
94                         }
95                 }
96                 else if (pcnt >= 2)
97                 {
98                         /* full form to add a CBAN */
99                         /* XXX - checking on chnames */
100                         chname = parameters[0];
101                         expiry = TIME + Srv->CalcDuration(parameters[1]);
102                         reason = parameters[2];
103
104                         CBan meow(chname, reason, expiry);
105                         cbans.push_back(meow);
106                 }
107         }
108 };
109
110 class ModuleCBan : public Module
111 {
112         cmd_cban* mycommand;
113         Server* Srv;
114
115  public:
116         ModuleCBan(Server* Me) : Module::Module(Me)
117         {
118                 Srv = Me;
119                 mycommand = new cmd_cban(Srv);
120                 Srv->AddCommand(mycommand);
121         }
122
123         void Implements(char* List)
124         {
125                 List[I_OnUserPreJoin] = 1;
126         }
127
128         virtual int OnUserPreJoin(userrec *user, chanrec *chan, const char *cname)
129         {
130                 /* check cbans in here, and apply as necessary. */
131                 log(DEBUG,"In OnUserPreJoin cbans.size() == %d",cbans.size());
132
133                 std::string chname = cname;
134
135                 for (unsigned int a = 0; a < cbans.size(); a++)
136                 {
137                         log(DEBUG,"m_cban: DEBUG: checking %s against %s in OnPreUserJoin()", chname.c_str(), cbans[a].GetName().c_str());
138                         if (chname == cbans[a].GetName())
139                         {
140                                 /* matches CBAN */
141                                 WriteOpers("DENY join");
142                                 return 1;
143                         }
144                 }
145
146                 log(DEBUG,"DONE checking, allowed");
147
148                 /* Allow the change. */
149                 return 0;
150         }
151
152         virtual ~ModuleCBan()
153         {
154         }
155         
156         virtual Version GetVersion()
157         {
158                 return Version(1,0,0,0,VF_VENDOR);
159         }
160 };
161
162
163 class ModuleCBanFactory : public ModuleFactory
164 {
165  public:
166         ModuleCBanFactory()
167         {
168         }
169         
170         ~ModuleCBanFactory()
171         {
172         }
173         
174         virtual Module * CreateModule(Server* Me)
175         {
176                 return new ModuleCBan(Me);
177         }
178         
179 };
180
181
182 extern "C" void * init_module( void )
183 {
184         return new ModuleCBanFactory;
185 }
186