]> jfr.im git - irc/quakenet/snircd.git/blob - ircd/m_topic.c
Initial import of 2.10.12.01
[irc/quakenet/snircd.git] / ircd / m_topic.c
1 /*
2 * IRC - Internet Relay Chat, ircd/m_topic.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
5 *
6 * See file AUTHORS in IRC package for additional names of
7 * the programmers.
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 1, or (at your option)
12 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
22 *
23 * $Id: m_topic.c,v 1.21 2005/08/17 02:35:36 entrope Exp $
24 */
25
26 /*
27 * m_functions execute protocol messages on this server:
28 *
29 * cptr is always NON-NULL, pointing to a *LOCAL* client
30 * structure (with an open socket connected!). This
31 * identifies the physical socket where the message
32 * originated (or which caused the m_function to be
33 * executed--some m_functions may call others...).
34 *
35 * sptr is the source of the message, defined by the
36 * prefix part of the message if present. If not
37 * or prefix not found, then sptr==cptr.
38 *
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
41 *
42 * (IsServer(cptr))
43 * (sptr == cptr) => the message didn't
44 * have the prefix.
45 *
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
48 *
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
51 * user (not local).
52 *
53 * combining
54 *
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
58 *
59 * *Always* true (if 'parse' and others are working correct):
60 *
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
62 *
63 * 2) MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64 * *cannot* be a local connection, unless it's
65 * actually cptr!). [MyConnect(x) should probably
66 * be defined as (x == x->from) --msa ]
67 *
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
70 *
71 * parv a NULL terminated list of parameter pointers,
72 *
73 * parv[0], sender (prefix string), if not present
74 * this points to an empty string.
75 * parv[1]...parv[parc-1]
76 * pointers to additional parameters
77 * parv[parc] == NULL, *always*
78 *
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
80 * non-NULL pointers.
81 */
82 #include "config.h"
83
84 #include "channel.h"
85 #include "client.h"
86 #include "hash.h"
87 #include "ircd.h"
88 #include "ircd_features.h"
89 #include "ircd_log.h"
90 #include "ircd_reply.h"
91 #include "ircd_string.h"
92 #include "msg.h"
93 #include "numeric.h"
94 #include "numnicks.h"
95 #include "send.h"
96
97 /* #include <assert.h> -- Now using assert in ircd_log.h */
98 #include <stdlib.h> /* for atoi() */
99
100 static void do_settopic(struct Client *sptr, struct Client *cptr,
101 struct Channel *chptr, char *topic, time_t ts)
102 {
103 struct Client *from;
104 int newtopic;
105
106 if (feature_bool(FEAT_HIS_BANWHO) && IsServer(sptr))
107 from = &his;
108 else
109 from = sptr;
110 if (IsChannelService(sptr))
111 {
112 /* allow off-channel services to set the topic of any channel */
113 }
114 else if ((chptr->mode.mode & MODE_TOPICLIMIT) && !is_chan_op(sptr, chptr))
115 {
116 /* if +t and not @'d, return an error and ignore the topic */
117 send_reply(sptr, ERR_CHANOPRIVSNEEDED, chptr->chname);
118 return;
119 }
120 else if (!client_can_send_to_channel(sptr, chptr, 1))
121 {
122 send_reply(sptr, ERR_CANNOTSENDTOCHAN, chptr->chname);
123 return;
124 }
125 /* Note if this is just a refresh of an old topic, and don't
126 * send it to all the clients to save bandwidth. We still send
127 * it to other servers as they may have split and lost the topic.
128 */
129 newtopic=ircd_strncmp(chptr->topic,topic,TOPICLEN)!=0;
130 /* setting a topic */
131 ircd_strncpy(chptr->topic, topic, TOPICLEN);
132 ircd_strncpy(chptr->topic_nick, cli_name(from), NICKLEN);
133 chptr->topic_time = ts ? ts : TStime();
134 /* Fixed in 2.10.11: Don't propagate local topics */
135 if (!IsLocalChannel(chptr->chname))
136 sendcmdto_serv_butone(sptr, CMD_TOPIC, cptr, "%H %Tu %Tu :%s", chptr,
137 chptr->creationtime, chptr->topic_time, chptr->topic);
138 if (newtopic)
139 sendcmdto_channel_butserv_butone(from, CMD_TOPIC, chptr, NULL, 0,
140 "%H :%s", chptr, chptr->topic);
141 /* if this is the same topic as before we send it to the person that
142 * set it (so they knew it went through ok), but don't bother sending
143 * it to everyone else on the channel to save bandwidth
144 */
145 else if (MyUser(sptr))
146 sendcmdto_one(sptr, CMD_TOPIC, sptr, "%H :%s", chptr, chptr->topic);
147 }
148
149 /*
150 * m_topic - generic message handler
151 *
152 * parv[0] = sender prefix
153 * parv[1] = channel
154 * parv[parc - 1] = topic (if parc > 2)
155 */
156 int m_topic(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
157 {
158 struct Channel *chptr;
159 char *topic = 0, *name, *p = 0;
160
161 if (parc < 2)
162 return need_more_params(sptr, "TOPIC");
163
164 if (parc > 2)
165 topic = parv[parc - 1];
166
167 for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
168 {
169 chptr = 0;
170 /* Does the channel exist */
171 if (!IsChannelName(name) || !(chptr = FindChannel(name)))
172 {
173 send_reply(sptr,ERR_NOSUCHCHANNEL,name);
174 continue;
175 }
176 /* Trying to check a topic outside a secret channel */
177 if ((topic || SecretChannel(chptr)) && !find_channel_member(sptr, chptr))
178 {
179 send_reply(sptr, ERR_NOTONCHANNEL, chptr->chname);
180 continue;
181 }
182
183 /* only asking for topic */
184 if (!topic)
185 {
186 if (chptr->topic[0] == '\0')
187 send_reply(sptr, RPL_NOTOPIC, chptr->chname);
188 else
189 {
190 send_reply(sptr, RPL_TOPIC, chptr->chname, chptr->topic);
191 send_reply(sptr, RPL_TOPICWHOTIME, chptr->chname, chptr->topic_nick,
192 chptr->topic_time);
193 }
194 }
195 else
196 do_settopic(sptr,cptr,chptr,topic,0);
197 }
198 return 0;
199 }
200
201 /*
202 * ms_topic - server message handler
203 *
204 * parv[0] = sender prefix
205 * parv[1] = channel
206 * parv[2] = channel timestamp (optional)
207 * parv[3] = topic timestamp (optional)
208 * parv[parc - 1] = topic
209 */
210 int ms_topic(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
211 {
212 struct Channel *chptr;
213 char *topic = 0, *name, *p = 0;
214 time_t ts = 0;
215
216 if (parc < 3)
217 return need_more_params(sptr, "TOPIC");
218
219 topic = parv[parc - 1];
220
221 for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
222 {
223 chptr = 0;
224 /* Does the channel exist */
225 if (!IsChannelName(name) || !(chptr = FindChannel(name)))
226 {
227 send_reply(sptr,ERR_NOSUCHCHANNEL,name);
228 continue;
229 }
230
231 /* Ignore requests for topics from remote servers */
232 if (IsLocalChannel(name) && !MyUser(sptr))
233 {
234 protocol_violation(sptr,"Topic request");
235 continue;
236 }
237
238 /* If existing channel is older or has newer topic, ignore */
239 if (parc > 3 && (ts = atoi(parv[2])) && chptr->creationtime < ts)
240 continue;
241
242 if (parc > 4 && (ts = atoi(parv[3])) && chptr->topic_time > ts)
243 continue;
244
245 do_settopic(sptr,cptr,chptr,topic, ts);
246 }
247 return 0;
248 }