summaryrefslogtreecommitdiff
path: root/src/actions.c
blob: 738711502e6ac51e4d6dc2eb432fc431ad7cd66f (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
/* Various routines to perform simple actions.
 *
 * (C) 2003-2012 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"

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

/**
 * 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 void
 */
void bad_password(User * u)
{
    time_t now = time(NULL);

    if (!u || !BadPassLimit) {
        return;
    }

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

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

/**
 * 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(char *source, char *user, char *reason)
{
    char buf[BUFSIZE];

    if (!user || !*user) {
        return;
    }
    if (!source || !*source) {
        source = ServerName;
    }
    if (!reason) {
        reason = "";
    }

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

    anope_cmd_svskill(source, 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(char *mask, char *reason)
{
    int i;
    Channel *c, *next;
    char *av[3];
    struct c_userlist *cu, *cunext;

    if (ircd->chansqline) {
        if (*mask == '#') {
            anope_cmd_sqline(mask, reason);

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

                    if (!match_wild_nocase(mask, c->name)) {
                        continue;
                    }
                    for (cu = c->users; cu; cu = cunext) {
                        cunext = cu->next;
                        if (is_oper(cu->user)) {
                            continue;
                        }
                        av[0] = c->name;
                        av[1] = cu->user->nick;
                        av[2] = reason;
                        anope_cmd_kick(s_OperServ, av[0], av[1],
                                       "Q-Lined: %s", av[2]);
                        do_kick(s_ChanServ, 3, av);
                    }
                }
            }
        } else {
            anope_cmd_sqline(mask, reason);
        }
    } else {
        anope_cmd_sqline(mask, reason);
    }
}

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

/**
 * Unban the nick from a channel
 * @param ci channel info for the channel
 * @param nick to remove the ban for
 * @param full True to match against realhost
 * @return void
 */
static void _common_unban(ChannelInfo * ci, char *nick, boolean full)
{
    char *av[4];
    char *host = NULL;
    char buf[BUFSIZE];
    int ac;
    uint32 ip = 0;
    User *u;
    Entry *ban, *next;

    if (!ci || !ci->c || !nick) {
        return;
    }

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

    if (!ci->c->bans || (ci->c->bans->count == 0))
        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 (ircdcap->tsmode) {
        snprintf(buf, BUFSIZE - 1, "%ld", (long int) time(NULL));
        av[0] = ci->name;
        av[1] = buf;
        av[2] = sstrdup("-b");
        ac = 4;
    } else {
        av[0] = ci->name;
        av[1] = sstrdup("-b");
        ac = 3;
    }

    for (ban = ci->c->bans->entries; ban; ban = next) {
        next = ban->next;
        if ((full && entry_match(ban, u->nick, u->username, u->host, ip)) ||
            entry_match(ban, u->nick, u->vident, u->vhost, 0) ||
            entry_match(ban, u->nick, u->username, u->chost, 0)) {
            anope_cmd_mode(whosends(ci), ci->name, "-b %s", ban->mask);
            if (ircdcap->tsmode)
                av[3] = sstrdup(ban->mask);
            else
                av[2] = sstrdup(ban->mask);

            do_cmode(whosends(ci), ac, av);

            if (ircdcap->tsmode)
                free(av[3]);
            else
                free(av[2]);
        }
    }

    if (ircdcap->tsmode)
        free(av[2]);
    else
        free(av[1]);

    /* host_resolve() sstrdup us this info so we gotta free it */
    if (host) {
        free(host);
    }
}

void common_unban(ChannelInfo * ci, char *nick)
{
	_common_unban(ci, nick, false);
}

void common_unban_full(ChannelInfo * ci, char *nick, boolean full)
{
	_common_unban(ci, nick, full);
}

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

/**
 * Prepare to set SVSMODE and update internal user modes
 * @param u user to apply modes to
 * @param modes the modes to set on the user
 * @param arg the arguments for the user modes
 * @return void
 */
void common_svsmode(User * u, char *modes, char *arg)
{
    int ac = 1;
    char *av[2];

    av[0] = modes;
    if (arg) {
        av[1] = arg;
        ac++;
    }

    anope_cmd_svsmode(u, ac, av);
    anope_set_umode(u, ac, av);
}

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

/**
 * Get the vhost for the user, if set else return the host, on ircds without
 * vhost this returns the host
 * @param u user to get the vhost for
 * @return vhost
 */
char *common_get_vhost(User * u)
{
    if (!u)
        return NULL;

    if (u->vhost)
        return u->vhost;
    else if (ircd->vhostmode && (u->mode & ircd->vhostmode) && u->chost)
        return u->chost;
    else
        return u->host;
}

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

/**
 * Get the vident for the user, if set else return the ident, on ircds without
 * vident this returns the ident
 * @param u user to get info the vident for
 * @return vident
 */
char *common_get_vident(User * u)
{
    if (!u)
        return NULL;

    if (ircd->vhostmode && (u->mode & ircd->vhostmode))
        return u->vident;
    else if (ircd->vident && u->vident)
        return u->vident;
    else
        return u->username;
}