Anope IRC Services  Version 1.8
actions.c
Go to the documentation of this file.
1 /* Various routines to perform simple actions.
2  *
3  * (C) 2003-2014 Anope Team
4  * Contact us at team@anope.org
5  *
6  * Please read COPYING and README for further details.
7  *
8  * Based on the original code of Epona by Lara.
9  * Based on the original code of Services by Andy Church.
10  *
11  *
12  */
13 
14 #include "services.h"
15 
16 /*************************************************************************/
17 
24 void bad_password(User * u)
25 {
26  time_t now = time(NULL);
27 
28  if (!u || !BadPassLimit) {
29  return;
30  }
31 
32  if (BadPassTimeout > 0 && u->invalid_pw_time > 0
33  && u->invalid_pw_time < now - BadPassTimeout)
34  u->invalid_pw_count = 0;
35  u->invalid_pw_count++;
36  u->invalid_pw_time = now;
37  if (u->invalid_pw_count >= BadPassLimit) {
38  kill_user(NULL, u->nick, "Too many invalid passwords");
39  }
40 }
41 
42 /*************************************************************************/
43 
51 void kill_user(char *source, char *user, char *reason)
52 {
53  char buf[BUFSIZE];
54 
55  if (!user || !*user) {
56  return;
57  }
58  if (!source || !*source) {
59  source = ServerName;
60  }
61  if (!reason) {
62  reason = "";
63  }
64 
65  snprintf(buf, sizeof(buf), "%s (%s)", source, reason);
66 
67  anope_cmd_svskill(source, user, buf);
68 
69  if (!ircd->quitonkill && finduser(user)) {
70  do_kill(user, buf);
71  }
72 }
73 
74 /*************************************************************************/
75 
82 void sqline(char *mask, char *reason)
83 {
84  int i;
85  Channel *c, *next;
86  char *av[3];
87  struct c_userlist *cu, *cunext;
88 
89  if (ircd->chansqline) {
90  if (*mask == '#') {
91  anope_cmd_sqline(mask, reason);
92 
93  for (i = 0; i < 1024; i++) {
94  for (c = chanlist[i]; c; c = next) {
95  next = c->next;
96 
97  if (!match_wild_nocase(mask, c->name)) {
98  continue;
99  }
100  for (cu = c->users; cu; cu = cunext) {
101  cunext = cu->next;
102  if (is_oper(cu->user)) {
103  continue;
104  }
105  av[0] = c->name;
106  av[1] = cu->user->nick;
107  av[2] = reason;
108  anope_cmd_kick(s_OperServ, av[0], av[1],
109  "Q-Lined: %s", av[2]);
110  do_kick(s_ChanServ, 3, av);
111  }
112  }
113  }
114  } else {
115  anope_cmd_sqline(mask, reason);
116  }
117  } else {
118  anope_cmd_sqline(mask, reason);
119  }
120 }
121 
122 /*************************************************************************/
123 
131 static void _common_unban(ChannelInfo * ci, char *nick, boolean full)
132 {
133  char *av[4];
134  char *host = NULL;
135  char buf[BUFSIZE];
136  int ac;
137  uint32 ip = 0;
138  User *u;
139  Entry *ban, *next;
140 
141  if (!ci || !ci->c || !nick) {
142  return;
143  }
144 
145  if (!(u = finduser(nick))) {
146  return;
147  }
148 
149  if (!ci->c->bans || (ci->c->bans->count == 0))
150  return;
151 
152  if (u->hostip == NULL) {
153  host = host_resolve(u->host);
154  /* we store the just resolved hostname so we don't
155  * need to do this again */
156  if (host) {
157  u->hostip = sstrdup(host);
158  }
159  } else {
160  host = sstrdup(u->hostip);
161  }
162  /* Convert the host to an IP.. */
163  if (host)
164  ip = str_is_ip(host);
165 
166  if (ircdcap->tsmode) {
167  snprintf(buf, BUFSIZE - 1, "%ld", (long int) time(NULL));
168  av[0] = ci->name;
169  av[1] = buf;
170  av[2] = sstrdup("-b");
171  ac = 4;
172  } else {
173  av[0] = ci->name;
174  av[1] = sstrdup("-b");
175  ac = 3;
176  }
177 
178  for (ban = ci->c->bans->entries; ban; ban = next) {
179  next = ban->next;
180  if ((full && entry_match(ban, u->nick, u->username, u->host, ip)) ||
181  entry_match(ban, u->nick, u->vident, u->vhost, 0) ||
182  entry_match(ban, u->nick, u->username, u->chost, 0)) {
183  anope_cmd_mode(whosends(ci), ci->name, "-b %s", ban->mask);
184  if (ircdcap->tsmode)
185  av[3] = sstrdup(ban->mask);
186  else
187  av[2] = sstrdup(ban->mask);
188 
189  do_cmode(whosends(ci), ac, av);
190 
191  if (ircdcap->tsmode)
192  free(av[3]);
193  else
194  free(av[2]);
195  }
196  }
197 
198  if (ircdcap->tsmode)
199  free(av[2]);
200  else
201  free(av[1]);
202 
203  /* host_resolve() sstrdup us this info so we gotta free it */
204  if (host) {
205  free(host);
206  }
207 }
208 
209 void common_unban(ChannelInfo * ci, char *nick)
210 {
211  _common_unban(ci, nick, false);
212 }
213 
214 void common_unban_full(ChannelInfo * ci, char *nick, boolean full)
215 {
216  _common_unban(ci, nick, full);
217 }
218 
219 /*************************************************************************/
220 
228 void common_svsmode(User * u, char *modes, char *arg)
229 {
230  int ac = 1;
231  char *av[2];
232 
233  av[0] = modes;
234  if (arg) {
235  av[1] = arg;
236  ac++;
237  }
238 
239  anope_cmd_svsmode(u, ac, av);
240  anope_set_umode(u, ac, av);
241 }
242 
243 /*************************************************************************/
244 
252 {
253  if (!u)
254  return NULL;
255 
256  if (u->vhost)
257  return u->vhost;
258  else if (ircd->vhostmode && (u->mode & ircd->vhostmode) && u->chost)
259  return u->chost;
260  else
261  return u->host;
262 }
263 
264 /*************************************************************************/
265 
273 {
274  if (!u)
275  return NULL;
276 
277  if (ircd->vhostmode && (u->mode & ircd->vhostmode))
278  return u->vident;
279  else if (ircd->vident && u->vident)
280  return u->vident;
281  else
282  return u->username;
283 }
E int is_oper(User *user)
Definition: users.c:937
void sqline(char *mask, char *reason)
Definition: actions.c:82
E IRCDCAPAB * ircdcap
Definition: extern.h:40
uint32 vhostmode
Definition: services.h:348
E int match_wild_nocase(const char *pattern, const char *str)
Definition: misc.c:268
char nick[NICKMAX]
Definition: services.h:875
E int snprintf(char *buf, size_t size, const char *fmt,...)
Definition: compat.c:37
E IRCDVar * ircd
Definition: extern.h:39
E void anope_cmd_svsmode(User *u, int ac, char **av)
Definition: ircd.c:175
void kill_user(char *source, char *user, char *reason)
Definition: actions.c:51
E void do_kick(const char *source, int ac, char **av)
Definition: channels.c:638
E int BadPassLimit
Definition: extern.h:342
Entry * next
Definition: services.h:1038
char * vhost
Definition: services.h:880
void bad_password(User *u)
Definition: actions.c:24
char * chost
Definition: services.h:881
int32 count
Definition: services.h:1034
Channel * next
Definition: services.h:999
void common_svsmode(User *u, char *modes, char *arg)
Definition: actions.c:228
char * host
Definition: services.h:878
int vident
Definition: services.h:337
char * common_get_vhost(User *u)
Definition: actions.c:251
char name[CHANMAX]
Definition: services.h:654
E void anope_set_umode(User *user, int ac, char **av)
Definition: ircd.c:130
E char * sstrdup(const char *s)
Definition: memory.c:105
E void anope_cmd_kick(char *source, char *chan, char *user, const char *fmt,...)
Definition: ircd.c:230
short invalid_pw_count
Definition: services.h:909
char * hostip
Definition: services.h:879
E char * s_OperServ
Definition: extern.h:289
E int BadPassTimeout
Definition: extern.h:343
int quitonkill
Definition: services.h:326
Definition: services.h:1037
E User * finduser(const char *nick)
Definition: users.c:323
E void anope_cmd_svskill(char *source, char *user, const char *fmt,...)
Definition: ircd.c:162
E int entry_match(Entry *e, char *nick, char *user, char *host, uint32 ip)
Definition: channels.c:2272
struct channel_ * c
Definition: services.h:692
int chansqline
Definition: services.h:325
Command * c
Definition: ns_recover.c:17
u_int32_t uint32
Definition: db-merger.c:123
E void anope_cmd_sqline(char *mask, char *reason)
Definition: ircd.c:524
void common_unban(ChannelInfo *ci, char *nick)
Definition: actions.c:209
E char * host_resolve(char *host)
Definition: misc.c:1134
#define whosends(ci)
Definition: extern.h:163
char * username
Definition: services.h:877
static void _common_unban(ChannelInfo *ci, char *nick, boolean full)
Definition: actions.c:131
char * common_get_vident(User *u)
Definition: actions.c:272
char * vident
Definition: services.h:882
EList * bans
Definition: services.h:1012
E void anope_cmd_mode(char *source, char *dest, const char *fmt,...)
Definition: ircd.c:211
E void do_kill(char *source, char *reason)
Definition: users.c:889
E void do_cmode(const char *source, int ac, char **av)
Definition: channels.c:1183
time_t invalid_pw_time
Definition: services.h:910
void common_unban_full(ChannelInfo *ci, char *nick, boolean full)
Definition: actions.c:214
uint32 tsmode
Definition: services.h:381
Entry * entries
Definition: services.h:1033
char name[CHANMAX]
Definition: services.h:1000
uint32 mode
Definition: services.h:889
E char * ServerName
Definition: extern.h:274
E char * s_ChanServ
Definition: extern.h:285
char * mask
Definition: services.h:1042
E uint32 str_is_ip(char *str)
Definition: misc.c:1565
#define BUFSIZE
Definition: config.h:47
struct channel_::c_userlist * users
E Channel * chanlist[1024]
Definition: extern.h:88