2 * IRC - Internet Relay Chat, ircd/m_topic.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
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)
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.
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.
32 #include "ircd_features.h"
34 #include "ircd_reply.h"
35 #include "ircd_string.h"
41 /* #include <assert.h> -- Now using assert in ircd_log.h */
42 #include <stdlib.h> /* for atoi() */
44 /** Set a channel topic or report an error.
45 * @param[in] sptr Original topic setter.
46 * @param[in] cptr Neighbor that sent the topic message.
47 * @param[in] chptr Channel to set topic on.
48 * @param[in] topic New topic.
49 * @param[in] ts Timestamp that topic was set (0 for current time).
51 static void do_settopic(struct Client *sptr, struct Client *cptr,
52 struct Channel *chptr, char *topic, time_t ts)
57 if (feature_bool(FEAT_HIS_BANWHO) && IsServer(sptr))
61 /* Note if this is just a refresh of an old topic, and don't
62 * send it to all the clients to save bandwidth. We still send
63 * it to other servers as they may have split and lost the topic.
65 newtopic=ircd_strncmp(chptr->topic,topic,TOPICLEN)!=0;
67 ircd_strncpy(chptr->topic, topic, TOPICLEN);
68 ircd_strncpy(chptr->topic_nick, cli_name(from), NICKLEN);
69 chptr->topic_time = ts ? ts : TStime();
70 /* Fixed in 2.10.11: Don't propagate local topics */
71 if (!IsLocalChannel(chptr->chname))
72 sendcmdto_serv_butone(sptr, CMD_TOPIC, cptr, "%H %Tu %Tu :%s", chptr,
73 chptr->creationtime, chptr->topic_time, chptr->topic);
75 sendcmdto_channel_butserv_butone(from, CMD_TOPIC, chptr, NULL, 0,
76 "%H :%s", chptr, chptr->topic);
77 /* if this is the same topic as before we send it to the person that
78 * set it (so they knew it went through ok), but don't bother sending
79 * it to everyone else on the channel to save bandwidth
81 else if (MyUser(sptr))
82 sendcmdto_one(sptr, CMD_TOPIC, sptr, "%H :%s", chptr, chptr->topic);
85 /** Handle a local user's attempt to get or set a channel topic.
87 * \a parv has the following elements:
88 * \li \a parv[1] is the channel name
89 * \li \a parv[\a parc - 1] is the topic (if \a parc > 2)
91 * See @ref m_functions for discussion of the arguments.
92 * @param[in] cptr Client that sent us the message.
93 * @param[in] sptr Original source of message.
94 * @param[in] parc Number of arguments.
95 * @param[in] parv Argument vector.
97 int m_topic(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
99 struct Channel *chptr;
100 char *topic = 0, *name, *p = 0;
103 return need_more_params(sptr, "TOPIC");
106 topic = parv[parc - 1];
108 for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
111 /* Does the channel exist */
112 if (!IsChannelName(name) || !(chptr = FindChannel(name)))
114 send_reply(sptr,ERR_NOSUCHCHANNEL,name);
117 /* Trying to check a topic outside a secret channel */
118 if ((topic || SecretChannel(chptr)) && !find_channel_member(sptr, chptr))
120 send_reply(sptr, ERR_NOTONCHANNEL, chptr->chname);
124 /* only asking for topic */
127 if (chptr->topic[0] == '\0')
128 send_reply(sptr, RPL_NOTOPIC, chptr->chname);
131 send_reply(sptr, RPL_TOPIC, chptr->chname, chptr->topic);
132 send_reply(sptr, RPL_TOPICWHOTIME, chptr->chname, chptr->topic_nick,
136 else if ((chptr->mode.mode & MODE_TOPICLIMIT) && !is_chan_op(sptr, chptr))
137 send_reply(sptr, ERR_CHANOPRIVSNEEDED, chptr->chname);
138 else if (!client_can_send_to_channel(sptr, chptr, 1))
139 send_reply(sptr, ERR_CANNOTSENDTOCHAN, chptr->chname);
141 do_settopic(sptr,cptr,chptr,topic,0);
146 /** Handle a remote user's attempt to set a channel topic.
147 * \a parv has the following elements:
148 * \li \a parv[1] is the channel name
149 * \li \a parv[2] is the channel creation timestamp (optional)
150 * \li \a parv[2] is the topic's timestamp (optional)
151 * \li \a parv[\a parc - 1] is the topic
153 * See @ref m_functions for discussion of the arguments.
154 * @param[in] cptr Client that sent us the message.
155 * @param[in] sptr Original source of message.
156 * @param[in] parc Number of arguments.
157 * @param[in] parv Argument vector.
159 int ms_topic(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
161 struct Channel *chptr;
162 char *topic = 0, *name, *p = 0;
166 return need_more_params(sptr, "TOPIC");
168 topic = parv[parc - 1];
170 for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
173 /* Does the channel exist */
174 if (!IsChannelName(name) || !(chptr = FindChannel(name)))
176 send_reply(sptr,ERR_NOSUCHCHANNEL,name);
180 /* Ignore requests for topics from remote servers */
181 if (IsLocalChannel(name) && !MyUser(sptr))
183 protocol_violation(sptr,"Topic request");
187 /* If existing channel is older or has newer topic, ignore */
188 if (parc > 3 && (ts = atoi(parv[2])) && chptr->creationtime < ts)
191 if (parc > 4 && (ts = atoi(parv[3])) && chptr->topic_time > ts)
194 do_settopic(sptr,cptr,chptr,topic, ts);