summaryrefslogtreecommitdiff
path: root/src/actions.c
blob: eea1906ca953a522ec0b642c46fa5d748d886785 (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
/* Various routines to perform simple actions.
 *
 * (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.
 *
 * $Id$
 *
 */

#include "services.h"

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

/**
 * Note a bad password attempt for the given user.  If they've used up
 * their limit, toss them off.
 * @param u the User to check
 * @return true if the user was killed, otherwise false
 */
bool bad_password(User *u)
{
	time_t now = time(NULL);

	if (!u || !Config.BadPassLimit)
		return false;

	if (Config.BadPassTimeout > 0 && u->invalid_pw_time > 0 && u->invalid_pw_time < now - Config.BadPassTimeout)
		u->invalid_pw_count = 0;
	++u->invalid_pw_count;
	u->invalid_pw_time = now;
	if (u->invalid_pw_count >= Config.BadPassLimit)
	{
		kill_user("", u->nick, "Too many invalid passwords");
		return true;
	}

	return false;
}

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

/**
 * Remove a user from the IRC network.
 * @param source is the nick which should generate the kill, or NULL for a server-generated kill.
 * @param user to remove
 * @param reason for the kill
 * @return void
 */
void kill_user(const std::string &source, const std::string &user, const std::string &reason)
{
	char buf[BUFSIZE];

	if (user.empty())
		return;

	std::string real_source = source.empty() ? Config.ServerName : source;

	snprintf(buf, sizeof(buf), "%s (%s)", source.c_str(), reason.c_str());

	ircdproto->SendSVSKill(findbot(source), finduser(user), buf);

	if (!ircd->quitonkill && finduser(user))
		do_kill(user, buf);
}

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

/**
 * Check and enforce SQlines
 * @param mask of the sqline
 * @param reason for the sqline
 * @return void
 */
void sqline(const std::string &mask, const std::string &reason)
{
	int i;
	Channel *c, *next;

	if (ircd->chansqline)
	{
		if (mask[0] == '#')
		{
			ircdproto->SendSQLine(mask, reason);

			for (i = 0; i < 1024; ++i)
			{
				for (c = chanlist[i]; c; c = next)
				{
					next = c->next;

					if (!Anope::Match(c->name, mask, false))
						continue;
					for (CUserList::iterator it = c->users.begin(); it != c->users.end();)
					{
						UserContainer *uc = *it;
						++it;

						if (is_oper(uc->user))
							continue;
						c->Kick(NULL, uc->user, "%s", reason.c_str());
					}
				}
			}
		}
		else
			ircdproto->SendSQLine(mask, reason);
	}
	else
		ircdproto->SendSQLine(mask, reason);
}

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

/**
 * Unban the nick from a channel
 * @param ci channel info for the channel
 * @param nick to remove the ban for
 * @return void
 */
void common_unban(ChannelInfo *ci, const std::string &nick)
{
	char *host = NULL;
	uint32 ip = 0;
	User *u;
	Entry *ban, *next;

	if (!ci || !ci->c || nick.empty())
		return;

	if (!(u = finduser(nick)))
		return;

	if (!ci->c->bans || !ci->c->bans->count)
		return;

	if (u->hostip == NULL)
	{
		host = host_resolve(u->host);
		/* we store the just resolved hostname so we don't
		 * need to do this again */
		if (host)
			u->hostip = sstrdup(host);
	}
	else
		host = sstrdup(u->hostip);
	/* Convert the host to an IP.. */
	if (host)
		ip = str_is_ip(host);

	if (ircd->svsmode_unban)
		ircdproto->SendBanDel(ci->c, nick);
	else
	{
		for (ban = ci->c->bans->entries; ban; ban = next)
		{
			next = ban->next;
			if (entry_match(ban, u->nick.c_str(), u->GetIdent().c_str(), u->host, ip) || entry_match(ban, u->nick.c_str(), u->GetIdent().c_str(), u->GetDisplayedHost().c_str(), ip))
				ci->c->RemoveMode(NULL, CMODE_BAN, ban->mask);
		}
	}
	/* host_resolve() sstrdup us this info so we gotta free it */
	if (host)
		delete [] host;
}

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