]> jfr.im git - irc/rqf/shadowircd.git/blame - src/tgchange.c
Move cmode +N to cmode +d, so that extensions/m_roleplay can retain cmode +N and...
[irc/rqf/shadowircd.git] / src / tgchange.c
CommitLineData
878733fd
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"
32
9e94d9ea
JT
33static int add_hashed_target(struct Client *source_p, uint32_t hashv);
34
878733fd
JT
35struct Channel *
36find_allowing_channel(struct Client *source_p, struct Client *target_p)
37{
38 rb_dlink_node *ptr;
39 struct membership *msptr;
40
41 RB_DLINK_FOREACH(ptr, source_p->user->channel.head)
42 {
43 msptr = ptr->data;
44 if (is_chanop_voiced(msptr) && IsMember(target_p, msptr->chptr))
45 return msptr->chptr;
46 }
47 return NULL;
48}
49
50int
51add_target(struct Client *source_p, struct Client *target_p)
52{
878733fd 53 uint32_t hashv;
878733fd
JT
54
55 /* can msg themselves or services without using any target slots */
56 if(source_p == target_p || IsService(target_p))
57 return 1;
58
59 /* special condition for those who have had PRIVMSG crippled to allow them
60 * to talk to IRCops still.
61 *
62 * XXX: is this controversial?
63 */
64 if(source_p->localClient->target_last > rb_current_time() && IsOper(target_p))
65 return 1;
66
67 hashv = fnv_hash_upper((const unsigned char *)use_id(target_p), 32);
9e94d9ea
JT
68 return add_hashed_target(source_p, hashv);
69}
70
71int
72add_channel_target(struct Client *source_p, struct Channel *chptr)
73{
74 uint32_t hashv;
75
76 hashv = fnv_hash_upper((const unsigned char *)chptr->chname, 32);
77 return add_hashed_target(source_p, hashv);
78}
79
80static int
81add_hashed_target(struct Client *source_p, uint32_t hashv)
82{
83 int i, j;
84 uint32_t *targets;
85
878733fd
JT
86 targets = source_p->localClient->targets;
87
88 /* check for existing target, and move it to the head */
89 for(i = 0; i < TGCHANGE_NUM + TGCHANGE_REPLY; i++)
90 {
91 if(targets[i] == hashv)
92 {
93 for(j = i; j > 0; j--)
94 targets[j] = targets[j - 1];
95 targets[0] = hashv;
96 return 1;
97 }
98 }
99
100 if(source_p->localClient->targets_free < TGCHANGE_NUM)
101 {
102 /* first message after connect, we may only start clearing
103 * slots after this message --anfl
104 */
105 if(!IsTGChange(source_p))
106 {
107 SetTGChange(source_p);
108 source_p->localClient->target_last = rb_current_time();
109 }
110 /* clear as many targets as we can */
111 else if((i = (rb_current_time() - source_p->localClient->target_last) / 60))
112 {
113 if(i + source_p->localClient->targets_free > TGCHANGE_NUM)
114 source_p->localClient->targets_free = TGCHANGE_NUM;
115 else
116 source_p->localClient->targets_free += i;
117
118 source_p->localClient->target_last = rb_current_time();
119 }
120 /* cant clear any, full target list */
121 else if(source_p->localClient->targets_free == 0)
122 {
123 ServerStats.is_tgch++;
124 add_tgchange(source_p->sockhost);
125 return 0;
126 }
127 }
128 /* no targets in use, reset their target_last so that they cant
129 * abuse a long idle to get targets back more quickly
130 */
131 else
132 {
133 source_p->localClient->target_last = rb_current_time();
134 SetTGChange(source_p);
135 }
136
137 for(i = TGCHANGE_NUM + TGCHANGE_REPLY - 1; i > 0; i--)
138 targets[i] = targets[i - 1];
139 targets[0] = hashv;
140 source_p->localClient->targets_free--;
141 return 1;
142}
143
144void
145add_reply_target(struct Client *source_p, struct Client *target_p)
146{
147 int i, j;
148 uint32_t hashv;
149 uint32_t *targets;
150
151 /* can msg themselves or services without using any target slots */
152 if(source_p == target_p || IsService(target_p))
153 return;
154
155 hashv = fnv_hash_upper((const unsigned char *)use_id(target_p), 32);
156 targets = source_p->localClient->targets;
157
158 /* check for existing target, and move it to the first reply slot
159 * if it is in a reply slot
160 */
161 for(i = 0; i < TGCHANGE_NUM + TGCHANGE_REPLY; i++)
162 {
163 if(targets[i] == hashv)
164 {
165 if(i > TGCHANGE_NUM)
166 {
167 for(j = i; j > TGCHANGE_NUM; j--)
168 targets[j] = targets[j - 1];
169 targets[TGCHANGE_NUM] = hashv;
170 }
171 return;
172 }
173 }
174 for(i = TGCHANGE_NUM + TGCHANGE_REPLY - 1; i > TGCHANGE_NUM; i--)
175 targets[i] = targets[i - 1];
176 targets[TGCHANGE_NUM] = hashv;
177}