]> jfr.im git - solanum.git/blame - src/tgchange.c
Merge pull request #53 from ShadowNinja/clarify_U+R
[solanum.git] / src / tgchange.c
CommitLineData
4f2685f3
JT
1/*
2 * charybdis: an advanced Internet Relay Chat Daemon(ircd).
3 * tgchange.c - code for restricting private messages
4 *
5 * Copyright (C) 2004-2005 Lee Hardy <lee@leeh.co.uk>
6 * Copyright (C) 2005-2010 Jilles Tjoelker <jilles@stack.nl>
7 * Copyright (C) 2004-2005 ircd-ratbox development team
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
22 * USA
23 */
24
25#include "stdinc.h"
26#include "tgchange.h"
27#include "channel.h"
28#include "client.h"
29#include "s_stats.h"
30#include "hash.h"
31#include "s_newconf.h"
bc4dea69 32#include "s_serv.h"
77d3d2db 33#include "send.h"
4f2685f3 34
717238d2
JT
35static int add_hashed_target(struct Client *source_p, uint32_t hashv);
36
4f2685f3
JT
37struct Channel *
38find_allowing_channel(struct Client *source_p, struct Client *target_p)
39{
40 rb_dlink_node *ptr;
41 struct membership *msptr;
42
43 RB_DLINK_FOREACH(ptr, source_p->user->channel.head)
44 {
45 msptr = ptr->data;
46 if (is_chanop_voiced(msptr) && IsMember(target_p, msptr->chptr))
47 return msptr->chptr;
48 }
49 return NULL;
50}
51
52int
53add_target(struct Client *source_p, struct Client *target_p)
54{
4f2685f3 55 uint32_t hashv;
4f2685f3
JT
56
57 /* can msg themselves or services without using any target slots */
58 if(source_p == target_p || IsService(target_p))
59 return 1;
60
61 /* special condition for those who have had PRIVMSG crippled to allow them
62 * to talk to IRCops still.
63 *
64 * XXX: is this controversial?
65 */
66 if(source_p->localClient->target_last > rb_current_time() && IsOper(target_p))
67 return 1;
68
69 hashv = fnv_hash_upper((const unsigned char *)use_id(target_p), 32);
717238d2
JT
70 return add_hashed_target(source_p, hashv);
71}
72
73int
74add_channel_target(struct Client *source_p, struct Channel *chptr)
75{
76 uint32_t hashv;
77
78 hashv = fnv_hash_upper((const unsigned char *)chptr->chname, 32);
79 return add_hashed_target(source_p, hashv);
80}
81
82static int
83add_hashed_target(struct Client *source_p, uint32_t hashv)
84{
85 int i, j;
86 uint32_t *targets;
87
4f2685f3
JT
88 targets = source_p->localClient->targets;
89
90 /* check for existing target, and move it to the head */
91 for(i = 0; i < TGCHANGE_NUM + TGCHANGE_REPLY; i++)
92 {
93 if(targets[i] == hashv)
94 {
95 for(j = i; j > 0; j--)
96 targets[j] = targets[j - 1];
97 targets[0] = hashv;
98 return 1;
99 }
100 }
101
102 if(source_p->localClient->targets_free < TGCHANGE_NUM)
103 {
104 /* first message after connect, we may only start clearing
105 * slots after this message --anfl
106 */
107 if(!IsTGChange(source_p))
108 {
109 SetTGChange(source_p);
110 source_p->localClient->target_last = rb_current_time();
111 }
112 /* clear as many targets as we can */
113 else if((i = (rb_current_time() - source_p->localClient->target_last) / 60))
114 {
115 if(i + source_p->localClient->targets_free > TGCHANGE_NUM)
116 source_p->localClient->targets_free = TGCHANGE_NUM;
117 else
118 source_p->localClient->targets_free += i;
119
120 source_p->localClient->target_last = rb_current_time();
121 }
122 /* cant clear any, full target list */
123 else if(source_p->localClient->targets_free == 0)
124 {
125 ServerStats.is_tgch++;
126 add_tgchange(source_p->sockhost);
ab894d74
KB
127
128 if (!IsTGExcessive(source_p))
129 {
130 SetTGExcessive(source_p);
bc4dea69
KB
131 /* This is sent to L_ALL because it's regenerated on all servers
132 * that have the TGINFO module loaded.
133 */
134 sendto_realops_snomask(SNO_BOTS, L_ALL,
ab894d74
KB
135 "Excessive target change from %s (%s@%s)",
136 source_p->name, source_p->username,
137 source_p->orighost);
138 }
139
bc4dea69
KB
140 sendto_match_servs(source_p, "*", CAP_ENCAP, NOCAPS,
141 "ENCAP * TGINFO 0");
142
4f2685f3
JT
143 return 0;
144 }
145 }
146 /* no targets in use, reset their target_last so that they cant
147 * abuse a long idle to get targets back more quickly
148 */
149 else
150 {
151 source_p->localClient->target_last = rb_current_time();
152 SetTGChange(source_p);
153 }
154
155 for(i = TGCHANGE_NUM + TGCHANGE_REPLY - 1; i > 0; i--)
156 targets[i] = targets[i - 1];
157 targets[0] = hashv;
158 source_p->localClient->targets_free--;
159 return 1;
160}
161
162void
163add_reply_target(struct Client *source_p, struct Client *target_p)
164{
165 int i, j;
166 uint32_t hashv;
167 uint32_t *targets;
168
169 /* can msg themselves or services without using any target slots */
170 if(source_p == target_p || IsService(target_p))
171 return;
172
173 hashv = fnv_hash_upper((const unsigned char *)use_id(target_p), 32);
174 targets = source_p->localClient->targets;
175
176 /* check for existing target, and move it to the first reply slot
177 * if it is in a reply slot
178 */
179 for(i = 0; i < TGCHANGE_NUM + TGCHANGE_REPLY; i++)
180 {
181 if(targets[i] == hashv)
182 {
183 if(i > TGCHANGE_NUM)
184 {
185 for(j = i; j > TGCHANGE_NUM; j--)
186 targets[j] = targets[j - 1];
187 targets[TGCHANGE_NUM] = hashv;
188 }
189 return;
190 }
191 }
192 for(i = TGCHANGE_NUM + TGCHANGE_REPLY - 1; i > TGCHANGE_NUM; i--)
193 targets[i] = targets[i - 1];
194 targets[TGCHANGE_NUM] = hashv;
195}