summaryrefslogtreecommitdiff
path: root/src/process.cpp
blob: 4a7811c6979150701fcf3bd0ada04aa29a732841 (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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* Main processing code for Services.
 *
 * (C) 2003-2010 Anope Team
 * Contact us at team@anope.org
 *
 * Please read COPYING and README for further details.
 *
 * Based on the original code of Epona by Lara.
 * Based on the original code of Services by Andy Church.
 */

#include "services.h"
#include "modules.h"

/*************************************************************************/

/* Use ignore code? */
int allow_ignore = 1;

/* Masks to ignore. */
std::list<IgnoreData *> ignore;

/*************************************************************************/

/**
 * Add a mask/nick to the ignorelist for delta seconds.
 * @param nick Nick or (nick!)user@host to add to the ignorelist.
 * @param delta Seconds untill new entry is set to expire. 0 for permanent.
 */
void add_ignore(const Anope::string &nick, time_t delta)
{
	if (nick.empty())
		return;
	/* If it s an existing user, we ignore the hostmask. */
	Anope::string mask;
	User *u = finduser(nick);
	size_t user, host;
	if (u)
		mask = "*!*@" + u->host;
	/* Determine whether we get a nick or a mask. */
	else if ((host = nick.find('@')) != Anope::string::npos)
	{
		/* Check whether we have a nick too.. */
		if ((user = nick.find('!')) != Anope::string::npos)
		{
			/* this should never happen */
			if (user > host)
				return;
			mask = nick;
		}
		else
			/* We have user@host. Add nick wildcard. */
			mask = "*!" + nick;
	}
	/* We only got a nick.. */
	else
		mask = nick + "!*@*";
	/* Check if we already got an identical entry. */
	std::list<IgnoreData *>::iterator ign = ignore.begin(), ign_end = ignore.end();
	for (; ign != ign_end; ++ign)
		if (mask.equals_ci((*ign)->mask))
			break;
	/* Found one.. */
	if (ign != ign_end)
	{
		if (!delta)
			(*ign)->time = 0;
		else if ((*ign)->time < Anope::CurTime + delta)
			(*ign)->time = Anope::CurTime + delta;
	}
	/* Create new entry.. */
	else
	{
		IgnoreData *newign = new IgnoreData();
		newign->mask = mask;
		newign->time = delta ? Anope::CurTime + delta : 0;
		ignore.push_front(newign);
		Log(LOG_DEBUG) << "Added new ignore entry for " << mask;
	}
}

/*************************************************************************/

/**
 * Retrieve an ignorance record for a nick or mask.
 * If the nick isn't being ignored, we return NULL and if necesary
 * flush the record from the ignore list (i.e. ignore timed out).
 * @param nick Nick or (nick!)user@host to look for on the ignorelist.
 * @return Pointer to the ignore record, NULL if none was found.
 */
IgnoreData *get_ignore(const Anope::string &nick)
{
	if (nick.empty())
		return NULL;
	/* User has disabled the IGNORE system */
	if (!allow_ignore)
		return NULL;
	User *u = finduser(nick);
	std::list<IgnoreData *>::iterator ign = ignore.begin(), ign_end = ignore.end();
	/* If we find a real user, match his mask against the ignorelist. */
	if (u)
	{
		/* Opers are not ignored, even if a matching entry may be present. */
		if (u->HasMode(UMODE_OPER))
			return NULL;
		for (; ign != ign_end; ++ign)
			if (match_usermask((*ign)->mask, u))
				break;
	}
	else
	{
		Anope::string tmp;
		size_t user, host;
		/* We didn't get a user.. generate a valid mask. */
		if ((host = nick.find('@')) != Anope::string::npos)
		{
			if ((user = nick.find('!')) != Anope::string::npos)
			{
				/* this should never happen */
				if (user > host)
					return NULL;
				tmp = nick;
			}
			else
				/* We have user@host. Add nick wildcard. */
			tmp = "*!" + nick;
		}
		/* We only got a nick.. */
		else
			tmp = nick + "!*@*";
		for (; ign != ign_end; ++ign)
			if (Anope::Match(tmp, (*ign)->mask))
				break;
	}
	/* Check whether the entry has timed out */
	if (ign != ign_end && (*ign)->time && (*ign)->time <= Anope::CurTime)
	{
		Log(LOG_DEBUG) << "Expiring ignore entry " << (*ign)->mask;
		delete *ign;
		ignore.erase(ign);
		ign = ign_end = ignore.end();
	}
	if (ign != ign_end)
		Log(LOG_DEBUG) << "Found ignore entry (" << (*ign)->mask << ") for " << nick;
	return ign == ign_end ? NULL : *ign;
}

/*************************************************************************/

/**
 * Deletes a given nick/mask from the ignorelist.
 * @param nick Nick or (nick!)user@host to delete from the ignorelist.
 * @return Returns 1 on success, 0 if no entry is found.
 */
int delete_ignore(const Anope::string &nick)
{
	if (nick.empty())
		return 0;
	/* If it s an existing user, we ignore the hostmask. */
	Anope::string tmp;
	size_t user, host;
	User *u = finduser(nick);
	if (u)
		tmp = "*!*@" + u->host;
	/* Determine whether we get a nick or a mask. */
	else if ((host = nick.find('@')) != Anope::string::npos)
	{
		/* Check whether we have a nick too.. */
		if ((user = nick.find('!')) != Anope::string::npos)
		{
			/* this should never happen */
			if (user > host)
				return 0;
			tmp = nick;
		}
		else
			/* We have user@host. Add nick wildcard. */
			tmp = "*!" + nick;
	}
	/* We only got a nick.. */
	else
		tmp = nick + "!*@*";
	std::list<IgnoreData *>::iterator ign = ignore.begin(), ign_end = ignore.end();
	for (; ign != ign_end; ++ign)
		if (tmp.equals_ci((*ign)->mask))
			break;
	/* No matching ignore found. */
	if (ign == ign_end)
		return 0;
	Log(LOG_DEBUG) << "Deleting ignore entry " << (*ign)->mask;
	/* Delete the entry and all references to it. */
	delete *ign;
	ignore.erase(ign);
	return 1;
}

/*************************************************************************/

/**
 * Clear the ignorelist.
 * @return The number of entries deleted.
 */
int clear_ignores()
{
	if (ignore.empty())
		return 0;
	for (std::list<IgnoreData *>::iterator ign = ignore.begin(), ign_end = ignore.end(); ign != ign_end; ++ign)
	{
		Log(LOG_DEBUG) << "Deleting ignore entry " << (*ign)->mask;
		delete *ign;
	}
	int deleted = ignore.size();
	ignore.clear();
	return deleted;
}

/** Main process routine
 * @param buffer A raw line from the uplink to do things with
 */
void process(const Anope::string &buffer)
{
	/* If debugging, log the buffer */
	Log(LOG_RAWIO) << "Received: " << buffer;

	/* Strip all extra spaces */
	Anope::string buf = buffer;
	buf = buf.replace_all_cs("  ", " ");

	if (buf.empty())
		return;

	Anope::string source;
	if (buf[0] == ':')
	{
		size_t space = buf.find_first_of(" ");
		if (space == Anope::string::npos)
			return;
		source = buf.substr(1, space - 1);
		buf = buf.substr(space + 1);
		if (source.empty() || buf.empty())
			return;
	}

	spacesepstream buf_sep(buf);
	Anope::string buf_token;

	Anope::string command = buf;
	if (buf_sep.GetToken(buf_token))
		command = buf_token;
	
	std::vector<Anope::string> params;
	while (buf_sep.GetToken(buf_token))
	{
		if (buf_token[0] == ':')
		{
			if (!buf_sep.StreamEnd())
				params.push_back(buf_token.substr(1) + " " + buf_sep.GetRemaining());
			else
				params.push_back(buf_token.substr(1));
			break;
		}
		else
			params.push_back(buf_token);
	}

	if (protocoldebug)
	{
		Log() << "Source : " << (source.empty() ? "No source" : source);
		Log() << "Command: " << command;

		if (params.empty())
			Log() << "No params";
		else
			for (unsigned i = 0; i < params.size(); ++i)
				Log() << "params " << i << ": " << params[i];
	}

	std::vector<Message *> messages = Anope::FindMessage(command);

	if (!messages.empty())
	{
		bool retVal = true;

		for (std::vector<Message *>::iterator it = messages.begin(), it_end = messages.end(); retVal == true && it != it_end; ++it)
		{
			Message *m = *it;

			if (m->func)
				retVal = m->func(source, params);
		}
	}
	else
		Log(LOG_DEBUG) << "unknown message from server (" << buffer << ")";
}