fixed ssl.c bug when ssl backend returns IO_BLOCKED but IO engine doesn't get informe...
[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: m_topic.c 1903 2009-01-13 03:54:45Z entrope $
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    {
76      struct Membership *member;
77
78      /* If the member is delayed-join, show them. */
79      member = find_channel_member(sptr, chptr);
80      if (member && IsDelayedJoin(member))
81        RevealDelayedJoin(member);
82
83      sendcmdto_channel_butserv_butone(from, CMD_TOPIC, chptr, NULL, 0,
84                                        "%H :%s", chptr, chptr->topic);
85    }
86       /* if this is the same topic as before we send it to the person that
87        * set it (so they knew it went through ok), but don't bother sending
88        * it to everyone else on the channel to save bandwidth
89        */
90     else if (MyUser(sptr))
91       sendcmdto_one(sptr, CMD_TOPIC, sptr, "%H :%s", chptr, chptr->topic);
92 }
93
94 /** Handle a local user's attempt to get or set a channel topic.
95  *
96  * \a parv has the following elements:
97  * \li \a parv[1] is the channel name
98  * \li \a parv[\a parc - 1] is the topic (if \a parc > 2)
99  *
100  * See @ref m_functions for discussion of the arguments.
101  * @param[in] cptr Client that sent us the message.
102  * @param[in] sptr Original source of message.
103  * @param[in] parc Number of arguments.
104  * @param[in] parv Argument vector.
105  */
106 int m_topic(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
107 {
108   struct Channel *chptr;
109   char *topic = 0, *name, *p = 0;
110
111   if (parc < 2)
112     return need_more_params(sptr, "TOPIC");
113
114   if (parc > 2)
115     topic = parv[parc - 1];
116
117   for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
118   {
119     chptr = 0;
120     /* Does the channel exist */
121     if (!IsChannelName(name) || !(chptr = FindChannel(name)))
122     {
123         send_reply(sptr,ERR_NOSUCHCHANNEL,name);
124         continue;
125     }
126     /* Trying to check a topic outside a secret channel */
127     if ((topic || SecretChannel(chptr)) && !find_channel_member(sptr, chptr))
128     {
129       send_reply(sptr, ERR_NOTONCHANNEL, chptr->chname);
130       continue;
131     }
132
133     /* only asking for topic */
134     if (!topic)
135     {
136       if (chptr->topic[0] == '\0')
137         send_reply(sptr, RPL_NOTOPIC, chptr->chname);
138       else
139       {
140         send_reply(sptr, RPL_TOPIC, chptr->chname, chptr->topic);
141         send_reply(sptr, RPL_TOPICWHOTIME, chptr->chname, chptr->topic_nick,
142                    chptr->topic_time);
143       }
144     }
145     else if ((chptr->mode.mode & MODE_TOPICLIMIT) && (!is_chan_op(sptr, chptr) && !is_halfop(sptr, chptr) && !(IsNetServ(sptr) && IsSecurityServ(sptr) && IsChannelService(sptr))))
146       send_reply(sptr, ERR_CHANOPRIVSNEEDED, chptr->chname);
147     else if (!client_can_send_to_channel(sptr, chptr, 1))
148       send_reply(sptr, ERR_CANNOTSENDTOCHAN, chptr->chname);
149     else
150       do_settopic(sptr,cptr,chptr,topic,0);
151   }
152   return 0;
153 }
154
155 /** Handle a remote user's attempt to set a channel topic.
156  * \a parv has the following elements:
157  * \li \a parv[1] is the channel name
158  * \li \a parv[2] is the channel creation timestamp (optional)
159  * \li \a parv[2] is the topic's timestamp (optional)
160  * \li \a parv[\a parc - 1] is the topic
161  *
162  * See @ref m_functions for discussion of the arguments.
163  * @param[in] cptr Client that sent us the message.
164  * @param[in] sptr Original source of message.
165  * @param[in] parc Number of arguments.
166  * @param[in] parv Argument vector.
167  */
168 int ms_topic(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
169 {
170   struct Channel *chptr;
171   char *topic = 0, *name, *p = 0;
172   time_t ts = 0;
173
174   if (parc < 3)
175     return need_more_params(sptr, "TOPIC");
176
177   topic = parv[parc - 1];
178
179   for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
180   {
181     chptr = 0;
182     /* Does the channel exist */
183     if (!IsChannelName(name) || !(chptr = FindChannel(name)))
184     {
185         send_reply(sptr,ERR_NOSUCHCHANNEL,name);
186         continue;
187     }
188
189     /* Ignore requests for topics from remote servers */
190     if (IsLocalChannel(name) && !MyUser(sptr))
191     {
192       protocol_violation(sptr,"Topic request");
193       continue;
194     }
195
196     /* If existing channel is older or has newer topic, ignore */
197     if (parc > 3 && (ts = atoi(parv[2])) && chptr->creationtime < ts)
198       continue;
199
200     if (parc > 4 && (ts = atoi(parv[3])) && chptr->topic_time > ts)
201       continue;
202
203     do_settopic(sptr,cptr,chptr,topic, ts);
204   }
205   return 0;
206 }