]> jfr.im git - solanum.git/blame - src/tgchange.c
Ensure consistent indexing into user_modes independent on signedness of char.
[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"
4f2685f3 33
717238d2
JT
34static int add_hashed_target(struct Client *source_p, uint32_t hashv);
35
4f2685f3
JT
36struct Channel *
37find_allowing_channel(struct Client *source_p, struct Client *target_p)
38{
39 rb_dlink_node *ptr;
40 struct membership *msptr;
41
42 RB_DLINK_FOREACH(ptr, source_p->user->channel.head)
43 {
44 msptr = ptr->data;
45 if (is_chanop_voiced(msptr) && IsMember(target_p, msptr->chptr))
46 return msptr->chptr;
47 }
48 return NULL;
49}
50
51int
52add_target(struct Client *source_p, struct Client *target_p)
53{
4f2685f3 54 uint32_t hashv;
4f2685f3
JT
55
56 /* can msg themselves or services without using any target slots */
57 if(source_p == target_p || IsService(target_p))
58 return 1;
59
60 /* special condition for those who have had PRIVMSG crippled to allow them
61 * to talk to IRCops still.
62 *
63 * XXX: is this controversial?
64 */
65 if(source_p->localClient->target_last > rb_current_time() && IsOper(target_p))
66 return 1;
67
68 hashv = fnv_hash_upper((const unsigned char *)use_id(target_p), 32);
717238d2
JT
69 return add_hashed_target(source_p, hashv);
70}
71
72int
73add_channel_target(struct Client *source_p, struct Channel *chptr)
74{
75 uint32_t hashv;
76
77 hashv = fnv_hash_upper((const unsigned char *)chptr->chname, 32);
78 return add_hashed_target(source_p, hashv);
79}
80
81static int
82add_hashed_target(struct Client *source_p, uint32_t hashv)
83{
84 int i, j;
85 uint32_t *targets;
86
4f2685f3
JT
87 targets = source_p->localClient->targets;
88
89 /* check for existing target, and move it to the head */
90 for(i = 0; i < TGCHANGE_NUM + TGCHANGE_REPLY; i++)
91 {
92 if(targets[i] == hashv)
93 {
94 for(j = i; j > 0; j--)
95 targets[j] = targets[j - 1];
96 targets[0] = hashv;
97 return 1;
98 }
99 }
100
101 if(source_p->localClient->targets_free < TGCHANGE_NUM)
102 {
103 /* first message after connect, we may only start clearing
104 * slots after this message --anfl
105 */
106 if(!IsTGChange(source_p))
107 {
108 SetTGChange(source_p);
109 source_p->localClient->target_last = rb_current_time();
110 }
111 /* clear as many targets as we can */
112 else if((i = (rb_current_time() - source_p->localClient->target_last) / 60))
113 {
114 if(i + source_p->localClient->targets_free > TGCHANGE_NUM)
115 source_p->localClient->targets_free = TGCHANGE_NUM;
116 else
117 source_p->localClient->targets_free += i;
118
119 source_p->localClient->target_last = rb_current_time();
120 }
121 /* cant clear any, full target list */
122 else if(source_p->localClient->targets_free == 0)
123 {
124 ServerStats.is_tgch++;
125 add_tgchange(source_p->sockhost);
ab894d74
KB
126
127 if (!IsTGExcessive(source_p))
128 {
129 SetTGExcessive(source_p);
bc4dea69
KB
130 /* This is sent to L_ALL because it's regenerated on all servers
131 * that have the TGINFO module loaded.
132 */
133 sendto_realops_snomask(SNO_BOTS, L_ALL,
ab894d74
KB
134 "Excessive target change from %s (%s@%s)",
135 source_p->name, source_p->username,
136 source_p->orighost);
137 }
138
bc4dea69
KB
139 sendto_match_servs(source_p, "*", CAP_ENCAP, NOCAPS,
140 "ENCAP * TGINFO 0");
141
4f2685f3
JT
142 return 0;
143 }
144 }
145 /* no targets in use, reset their target_last so that they cant
146 * abuse a long idle to get targets back more quickly
147 */
148 else
149 {
150 source_p->localClient->target_last = rb_current_time();
151 SetTGChange(source_p);
152 }
153
154 for(i = TGCHANGE_NUM + TGCHANGE_REPLY - 1; i > 0; i--)
155 targets[i] = targets[i - 1];
156 targets[0] = hashv;
157 source_p->localClient->targets_free--;
158 return 1;
159}
160
161void
162add_reply_target(struct Client *source_p, struct Client *target_p)
163{
164 int i, j;
165 uint32_t hashv;
166 uint32_t *targets;
167
168 /* can msg themselves or services without using any target slots */
169 if(source_p == target_p || IsService(target_p))
170 return;
171
172 hashv = fnv_hash_upper((const unsigned char *)use_id(target_p), 32);
173 targets = source_p->localClient->targets;
174
175 /* check for existing target, and move it to the first reply slot
176 * if it is in a reply slot
177 */
178 for(i = 0; i < TGCHANGE_NUM + TGCHANGE_REPLY; i++)
179 {
180 if(targets[i] == hashv)
181 {
182 if(i > TGCHANGE_NUM)
183 {
184 for(j = i; j > TGCHANGE_NUM; j--)
185 targets[j] = targets[j - 1];
186 targets[TGCHANGE_NUM] = hashv;
187 }
188 return;
189 }
190 }
191 for(i = TGCHANGE_NUM + TGCHANGE_REPLY - 1; i > TGCHANGE_NUM; i--)
192 targets[i] = targets[i - 1];
193 targets[TGCHANGE_NUM] = hashv;
194}