]> jfr.im git - irc/rqf/shadowircd.git/blob - src/tgchange.c
Add me_svsjoin function to allow services to "force"join clients
[irc/rqf/shadowircd.git] / src / tgchange.c
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
33 struct Channel *
34 find_allowing_channel(struct Client *source_p, struct Client *target_p)
35 {
36 rb_dlink_node *ptr;
37 struct membership *msptr;
38
39 RB_DLINK_FOREACH(ptr, source_p->user->channel.head)
40 {
41 msptr = ptr->data;
42 if (is_chanop_voiced(msptr) && IsMember(target_p, msptr->chptr))
43 return msptr->chptr;
44 }
45 return NULL;
46 }
47
48 int
49 add_target(struct Client *source_p, struct Client *target_p)
50 {
51 int i, j;
52 uint32_t hashv;
53 uint32_t *targets;
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);
68 targets = source_p->localClient->targets;
69
70 /* check for existing target, and move it to the head */
71 for(i = 0; i < TGCHANGE_NUM + TGCHANGE_REPLY; i++)
72 {
73 if(targets[i] == hashv)
74 {
75 for(j = i; j > 0; j--)
76 targets[j] = targets[j - 1];
77 targets[0] = hashv;
78 return 1;
79 }
80 }
81
82 if(source_p->localClient->targets_free < TGCHANGE_NUM)
83 {
84 /* first message after connect, we may only start clearing
85 * slots after this message --anfl
86 */
87 if(!IsTGChange(source_p))
88 {
89 SetTGChange(source_p);
90 source_p->localClient->target_last = rb_current_time();
91 }
92 /* clear as many targets as we can */
93 else if((i = (rb_current_time() - source_p->localClient->target_last) / 60))
94 {
95 if(i + source_p->localClient->targets_free > TGCHANGE_NUM)
96 source_p->localClient->targets_free = TGCHANGE_NUM;
97 else
98 source_p->localClient->targets_free += i;
99
100 source_p->localClient->target_last = rb_current_time();
101 }
102 /* cant clear any, full target list */
103 else if(source_p->localClient->targets_free == 0)
104 {
105 ServerStats.is_tgch++;
106 add_tgchange(source_p->sockhost);
107 return 0;
108 }
109 }
110 /* no targets in use, reset their target_last so that they cant
111 * abuse a long idle to get targets back more quickly
112 */
113 else
114 {
115 source_p->localClient->target_last = rb_current_time();
116 SetTGChange(source_p);
117 }
118
119 for(i = TGCHANGE_NUM + TGCHANGE_REPLY - 1; i > 0; i--)
120 targets[i] = targets[i - 1];
121 targets[0] = hashv;
122 source_p->localClient->targets_free--;
123 return 1;
124 }
125
126 void
127 add_reply_target(struct Client *source_p, struct Client *target_p)
128 {
129 int i, j;
130 uint32_t hashv;
131 uint32_t *targets;
132
133 /* can msg themselves or services without using any target slots */
134 if(source_p == target_p || IsService(target_p))
135 return;
136
137 hashv = fnv_hash_upper((const unsigned char *)use_id(target_p), 32);
138 targets = source_p->localClient->targets;
139
140 /* check for existing target, and move it to the first reply slot
141 * if it is in a reply slot
142 */
143 for(i = 0; i < TGCHANGE_NUM + TGCHANGE_REPLY; i++)
144 {
145 if(targets[i] == hashv)
146 {
147 if(i > TGCHANGE_NUM)
148 {
149 for(j = i; j > TGCHANGE_NUM; j--)
150 targets[j] = targets[j - 1];
151 targets[TGCHANGE_NUM] = hashv;
152 }
153 return;
154 }
155 }
156 for(i = TGCHANGE_NUM + TGCHANGE_REPLY - 1; i > TGCHANGE_NUM; i--)
157 targets[i] = targets[i - 1];
158 targets[TGCHANGE_NUM] = hashv;
159 }