summaryrefslogtreecommitdiff
path: root/src/modules/m_chanfilter.cpp
blob: 7c888c05716d03909776b44ed6642a814d2eb670 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  InspIRCd: (C) 2002-2007 InspIRCd Development Team
 * See: http://www.inspircd.org/wiki/index.php/Credits
 *
 * This program is free but copyrighted software; see
 *            the file COPYING for details.
 *
 * ---------------------------------------------------
 */

#include <stdio.h>
#include <string>
#include <vector>
#include "users.h"
#include "channels.h"
#include "modules.h"
#include "hashcomp.h"
#include "u_listmode.h"
#include "inspircd.h"

/* $ModDesc: Provides channel-specific censor lists (like mode +G but varies from channel to channel) */
/* $ModDep: ../../include/u_listmode.h */

/** Handles channel mode +g
 */
class ChanFilter : public ListModeBase
{
 public:
	ChanFilter(InspIRCd* Instance) : ListModeBase(Instance, 'g', "End of channel spamfilter list", "941", "940", false, "chanfilter") { }
	
	virtual bool ValidateParam(userrec* user, chanrec* chan, std::string &word)
	{
		if ((word.length() > 35) || (word.empty()))
		{
			user->WriteServ("935 %s %s %s :word is too %s for censor list",user->nick, chan->name,word.c_str(), (word.empty() ? "short" : "long"));
			return false;
		}
		
		return true;
	}
	
	virtual bool TellListTooLong(userrec* user, chanrec* chan, std::string &word)
	{
		user->WriteServ("939 %s %s %s :Channel spamfilter list is full",user->nick, chan->name, word.c_str());
		return true;
	}
	
	virtual void TellAlreadyOnList(userrec* user, chanrec* chan, std::string &word)
	{
		user->WriteServ("937 %s %s :The word %s is already on the spamfilter list",user->nick, chan->name,word.c_str());
	}
	
	virtual void TellNotSet(userrec* user, chanrec* chan, std::string &word)
	{
		user->WriteServ("938 %s %s :No such spamfilter word is set",user->nick, chan->name);
	}
};

class ModuleChanFilter : public Module
{
	
	ChanFilter* cf;
	
 public:
 
	ModuleChanFilter(InspIRCd* Me)
		: Module::Module(Me)
	{
		cf = new ChanFilter(ServerInstance);
		if (!ServerInstance->AddMode(cf, 'g'))
			throw ModuleException("Could not add new modes!");
	}

	void Implements(char* List) 
	{ 
		cf->DoImplements(List);
		List[I_OnCleanup] = List[I_OnChannelDelete] = List[I_OnRehash] = List[I_OnUserPreMessage] = List[I_OnUserPreNotice] = List[I_OnSyncChannel] = 1;
	}

	virtual void OnChannelDelete(chanrec* chan)
	{
		cf->DoChannelDelete(chan);
	}

	virtual void OnRehash(userrec* user, const std::string &parameter)
	{
		cf->DoRehash();
	}

	virtual int ProcessMessages(userrec* user,chanrec* chan,std::string &text)
	{
		// Create a copy of the string in irc::string
		irc::string line = text.c_str();

		modelist* list;
		chan->GetExt(cf->GetInfoKey(), list);

		if (list)
		{
			for (modelist::iterator i = list->begin(); i != list->end(); i++)
			{
				if (line.find(i->mask.c_str()) != std::string::npos)
				{
					user->WriteServ("936 %s %s %s :Your message contained a censored word, and was blocked",user->nick, chan->name, i->mask.c_str());
					return 1;
				}
			}
		}
		return 0;
	}

	virtual int OnUserPreMessage(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
	{
		if (target_type == TYPE_CHANNEL)
		{
			return ProcessMessages(user,(chanrec*)dest,text);
		}
		else return 0;
	}

	virtual void OnCleanup(int target_type, void* item)
	{
		cf->DoCleanup(target_type, item);
	}
	
	virtual int OnUserPreNotice(userrec* user,void* dest,int target_type, std::string &text, char status, CUList &exempt_list)
	{
		return OnUserPreMessage(user,dest,target_type,text,status,exempt_list);
	}
	
	virtual void OnSyncChannel(chanrec* chan, Module* proto, void* opaque)
	{
		cf->DoSyncChannel(chan, proto, opaque);
	}

	virtual Version GetVersion()
	{
		return Version(1, 1, 0, 0, VF_COMMON | VF_VENDOR, API_VERSION);
	}
	
	virtual ~ModuleChanFilter()
	{
		ServerInstance->Modes->DelMode(cf);
		DELETE(cf);
	}
};


class ModuleChanFilterFactory : public ModuleFactory
{
 public:
	ModuleChanFilterFactory()
	{
	}
	
	~ModuleChanFilterFactory()
	{
	}
	
	virtual Module * CreateModule(InspIRCd* Me)
	{
		return new ModuleChanFilter(Me);
	}
	
};


extern "C" void * init_module( void )
{
	return new ModuleChanFilterFactory;
}