]> git.netwichtig.de Git - user/henk/code/inspircd.git/blob - src/modules/m_rline.cpp
Flesh this out a little more. Create our derived XLine and factory. Doesn't implement...
[user/henk/code/inspircd.git] / src / modules / m_rline.cpp
1 /*       +------------------------------------+
2  *       | Inspire Internet Relay Chat Daemon |
3  *       +------------------------------------+
4  *
5  *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
6  * See: http://www.inspircd.org/wiki/index.php/Credits
7  *
8  * This program is free but copyrighted software; see
9  *          the file COPYING for details.
10  *
11  * ---------------------------------------------------
12  */
13
14 #include "inspircd.h"
15 #include <pcre.h>
16 #include "xline.h"
17
18 /* $ModDesc: RLINE: Regexp user banning. */
19 /* $CompileFlags: exec("pcre-config --cflags") */
20 /* $LinkerFlags: exec("pcre-config --libs") rpath("pcre-config --libs") -lpcre */
21
22 #ifdef WINDOWS
23 #pragma comment(lib, "pcre.lib")
24 #endif
25
26 class CoreExport RLine : public XLine
27 {
28   public:
29
30         /** Create a R-Line.
31          * @param s_time The set time
32          * @param d The duration of the xline
33          * @param src The sender of the xline
34          * @param re The reason of the xline
35          * @param regex Pattern to match with
36          * @
37          */
38         RLine(InspIRCd* Instance, time_t s_time, long d, const char* src, const char* re, const char* regexs) : XLine(Instance, s_time, d, src, re, "K")
39         {
40                 const char *error;
41                 int erroffset;
42
43                 matchtext = regexs;
44
45                 regex = pcre_compile(regexs, 0, &error, &erroffset, NULL);
46
47                 if (!regex)
48                 {
49                         ServerInstance->SNO->WriteToSnoMask('x',"Error in regular expression: %s at offset %d: %s\n", regexs, erroffset, error);
50                         throw CoreException("Bad regex pattern.");
51                 }
52         }
53
54         /** Destructor
55          */
56         ~RLine()
57         {
58                 pcre_free(regex);
59         }
60
61         virtual bool Matches(User *u)
62         {
63                 std::string compare = compare.assign(u->nick) + "!" + u->ident + "@" + u->host + " " + u->fullname;
64
65                 ServerInstance->Log(DEBUG, "Matching " + matchtext + " against string " + compare);
66
67                 if (pcre_exec(regex, NULL, compare.c_str(), compare.length(), 0, 0, NULL, 0) > -1)
68                 {
69                         // Bang. :D
70                         return true;
71                 }
72
73                 return false;
74         }
75
76         virtual bool Matches(const std::string&); // Ignored for us
77
78         virtual void Apply(User* u)
79         {
80                 DefaultApply(u, "R", true);
81         }
82
83         virtual void DisplayExpiry()
84         {
85                 ServerInstance->SNO->WriteToSnoMask('x',"Expiring timed R-Line %s (set by %s %d seconds ago)", this->matchtext.c_str(), this->source, this->duration);
86         }
87
88         virtual const char* Displayable()
89         {
90                 return matchtext.c_str();
91         }
92
93         std::string matchtext;
94
95         pcre *regex;
96 };
97
98
99 /** An XLineFactory specialized to generate RLine* pointers
100  */
101 class CoreExport RLineFactory : public XLineFactory
102 {
103  public:
104         RLineFactory(InspIRCd* Instance) : XLineFactory(Instance, "R") { }
105
106         /** Generate a RLine
107          */
108         XLine* Generate(time_t set_time, long duration, const char* source, const char* reason, const char* xline_specific_mask)
109         {
110                 return new RLine(ServerInstance, set_time, duration, source, reason, xline_specific_mask);
111         }
112 };
113
114 /*
115         if (pcnt >= 3)
116         {
117                 IdentHostPair ih;
118                 User* find = ServerInstance->FindNick(parameters[0]);
119                 if (find)
120                 {
121                         ih.first = "*";
122                         ih.second = find->GetIPString();
123                 }
124                 else
125                         ih = ServerInstance->XLines->IdentSplit(parameters[0]);
126
127                 if (ServerInstance->HostMatchesEveryone(ih.first+"@"+ih.second,user))
128                         return CMD_FAILURE;
129
130                 if (!strchr(parameters[0],'@'))
131                 {       
132                         user->WriteServ("NOTICE %s :*** G-Line must contain a username, e.g. *@%s",user->nick,parameters[0]);
133                         return CMD_FAILURE;
134                 }
135                 else if (strchr(parameters[0],'!'))
136                 {
137                         user->WriteServ("NOTICE %s :*** G-Line cannot contain a nickname!",user->nick);
138                         return CMD_FAILURE;
139                 }
140
141                 long duration = ServerInstance->Duration(parameters[1]);
142                 GLine* gl = new GLine(ServerInstance, ServerInstance->Time(), duration, user->nick, parameters[2], ih.first.c_str(), ih.second.c_str());
143                 if (ServerInstance->XLines->AddLine(gl, user))
144                 {
145                         if (!duration)
146                         {
147                                 ServerInstance->SNO->WriteToSnoMask('x',"%s added permanent G-line for %s.",user->nick,parameters[0]);
148                         }
149                         else
150                         {
151                                 time_t c_requires_crap = duration + ServerInstance->Time();
152                                 ServerInstance->SNO->WriteToSnoMask('x',"%s added timed G-line for %s, expires on %s",user->nick,parameters[0],
153                                                 ServerInstance->TimeString(c_requires_crap).c_str());
154                         }
155
156                         ServerInstance->XLines->ApplyLines();
157                 }
158                 else
159                 {
160                         delete gl;
161                         user->WriteServ("NOTICE %s :*** G-Line for %s already exists",user->nick,parameters[0]);
162                 }
163
164         }
165         else
166         {
167                 if (ServerInstance->XLines->DelLine(parameters[0],"G",user))
168                 {
169                         ServerInstance->SNO->WriteToSnoMask('x',"%s Removed G-line on %s.",user->nick,parameters[0]);
170                 }
171                 else
172                 {
173                         user->WriteServ("NOTICE %s :*** G-line %s not found in list, try /stats g.",user->nick,parameters[0]);
174                 }
175         }
176 */
177
178 class ModuleRLine : public Module
179 {
180  public:
181         ModuleRLine(InspIRCd* Me) : Module(Me)
182         {
183         }
184
185         virtual ~ModuleRLine()
186         {
187         }
188 };
189
190 MODULE_INIT(ModuleRLine)
191