9ef803ebaf3954199cee20c10ea88c2553e8fa61
[ircu2.10.12-pk.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$
24  */
25
26 #include "config.h"
27
28 #include "channel.h"
29 #include "client.h"
30 #include "hash.h"
31 #include "ircd.h"
32 #include "ircd_features.h"
33 #include "ircd_log.h"
34 #include "ircd_reply.h"
35 #include "ircd_string.h"
36 #include "msg.h"
37 #include "numeric.h"
38 #include "numnicks.h"
39 #include "send.h"
40
41 /* #include <assert.h> -- Now using assert in ircd_log.h */
42 #include <stdlib.h> /* for atoi() */
43
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).
50  */
51 static void do_settopic(struct Client *sptr, struct Client *cptr,
52                         struct Channel *chptr, char *topic, time_t ts)
53 {
54    struct Client *from;
55    int newtopic;
56
57    if (feature_bool(FEAT_HIS_BANWHO) && IsServer(sptr))
58        from = &his;
59    else
60        from = 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.
64     */
65    newtopic=ircd_strncmp(chptr->topic,topic,TOPICLEN)!=0;
66    /* setting a topic */
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);
74    if (newtopic)
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
80        */
81     else if (MyUser(sptr))
82       sendcmdto_one(sptr, CMD_TOPIC, sptr, "%H :%s", chptr, chptr->topic);
83 }
84
85 /** Handle a local user's attempt to get or set a channel topic.
86  *
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)
90  *
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.
96  */
97 int m_topic(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
98 {
99   struct Channel *chptr;
100   char *topic = 0, *name, *p = 0;
101
102   if (parc < 2)
103     return need_more_params(sptr, "TOPIC");
104
105   if (parc > 2)
106     topic = parv[parc - 1];
107
108   for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
109   {
110     chptr = 0;
111     /* Does the channel exist */
112     if (!IsChannelName(name) || !(chptr = FindChannel(name)))
113     {
114         send_reply(sptr,ERR_NOSUCHCHANNEL,name);
115         continue;
116     }
117     /* Trying to check a topic outside a secret channel */
118     if ((topic || SecretChannel(chptr)) && !find_channel_member(sptr, chptr))
119     {
120       send_reply(sptr, ERR_NOTONCHANNEL, chptr->chname);
121       continue;
122     }
123
124     /* only asking for topic */
125     if (!topic)
126     {
127       if (chptr->topic[0] == '\0')
128         send_reply(sptr, RPL_NOTOPIC, chptr->chname);
129       else
130       {
131         send_reply(sptr, RPL_TOPIC, chptr->chname, chptr->topic);
132         send_reply(sptr, RPL_TOPICWHOTIME, chptr->chname, chptr->topic_nick,
133                    chptr->topic_time);
134       }
135     }
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);
140     else
141       do_settopic(sptr,cptr,chptr,topic,0);
142   }
143   return 0;
144 }
145
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
152  *
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.
158  */
159 int ms_topic(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
160 {
161   struct Channel *chptr;
162   char *topic = 0, *name, *p = 0;
163   time_t ts = 0;
164
165   if (parc < 3)
166     return need_more_params(sptr, "TOPIC");
167
168   topic = parv[parc - 1];
169
170   for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
171   {
172     chptr = 0;
173     /* Does the channel exist */
174     if (!IsChannelName(name) || !(chptr = FindChannel(name)))
175     {
176         send_reply(sptr,ERR_NOSUCHCHANNEL,name);
177         continue;
178     }
179
180     /* Ignore requests for topics from remote servers */
181     if (IsLocalChannel(name) && !MyUser(sptr))
182     {
183       protocol_violation(sptr,"Topic request");
184       continue;
185     }
186
187     /* If existing channel is older or has newer topic, ignore */
188     if (parc > 3 && (ts = atoi(parv[2])) && chptr->creationtime < ts)
189       continue;
190
191     if (parc > 4 && (ts = atoi(parv[3])) && chptr->topic_time > ts)
192       continue;
193
194     do_settopic(sptr,cptr,chptr,topic, ts);
195   }
196   return 0;
197 }