]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_cban.cpp
Optimized out strcpys that copy empty strings (craq++)
[user/henk/code/inspircd.git] / src / modules / m_cban.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  Inspire is copyright (C) 2002-2005 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 vector<CBan> cbans;
61 /* don't ask why this is here. */
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         virtual int OnUserPreJoin(userrec *user, chanrec *chan, const char *cname)
124         {
125                 /* check cbans in here, and apply as necessary. */
126                 log(DEBUG,"In OnUserPreJoin cbans.size() == %d",cbans.size());
127
128                 std::string chname = cname;
129
130                 for (unsigned int a = 0; a < cbans.size(); a++)
131                 {
132                         log(DEBUG,"m_cban: DEBUG: checking %s against %s in OnPreUserJoin()", chname.c_str(), cbans[a].GetName().c_str());
133                         if (chname == cbans[a].GetName())
134                         {
135                                 /* matches CBAN */
136                                 WriteOpers("DENY join");
137                                 return 1;
138                         }
139                 }
140
141                 log(DEBUG,"DONE checking, allowed");
142
143                 /* Allow the change. */
144                 return 0;
145         }
146
147         virtual ~ModuleCBan()
148         {
149         }
150         
151         virtual Version GetVersion()
152         {
153                 return Version(1,0,0,0,VF_VENDOR);
154         }
155 };
156
157
158 class ModuleCBanFactory : public ModuleFactory
159 {
160  public:
161         ModuleCBanFactory()
162         {
163         }
164         
165         ~ModuleCBanFactory()
166         {
167         }
168         
169         virtual Module * CreateModule(Server* Me)
170         {
171                 return new ModuleCBan(Me);
172         }
173         
174 };
175
176
177 extern "C" void * init_module( void )
178 {
179         return new ModuleCBanFactory;
180 }
181