1 /*
2  *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3  *
4  *  Copyright (c) 1997-2021 ircd-hybrid development team
5  *
6  *  This program is free software; you can redistribute it and/or modify
7  *  it under the terms of the GNU General Public License as published by
8  *  the Free Software Foundation; either version 2 of the License, or
9  *  (at your option) any later version.
10  *
11  *  This program is distributed in the hope that it will be useful,
12  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *  GNU General Public License for more details.
15  *
16  *  You should have received a copy of the GNU General Public License
17  *  along with this program; if not, write to the Free Software
18  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19  *  USA
20  */
21 
22 /*! \file m_topic.c
23  * \brief Includes required functions for processing the TOPIC command.
24  * \version $Id: m_topic.c 9858 2021-01-01 04:43:42Z michael $
25  */
26 
27 #include "stdinc.h"
28 #include "list.h"
29 #include "channel.h"
30 #include "channel_mode.h"
31 #include "client.h"
32 #include "conf.h"
33 #include "hash.h"
34 #include "irc_string.h"
35 #include "ircd.h"
36 #include "numeric.h"
37 #include "send.h"
38 #include "parse.h"
39 #include "modules.h"
40 #include "packet.h"
41 
42 
43 /*! \brief TOPIC command handler
44  *
45  * \param source_p Pointer to allocated Client struct from which the message
46  *                 originally comes from.  This can be a local or remote client.
47  * \param parc     Integer holding the number of supplied arguments.
48  * \param parv     Argument vector where parv[0] .. parv[parc-1] are non-NULL
49  *                 pointers.
50  * \note Valid arguments for this command are:
51  *      - parv[0] = command
52  *      - parv[1] = channel name
53  *      - parv[2] = topic text, if setting topic (can be an empty string)
54  */
55 static void
m_topic(struct Client * source_p,int parc,char * parv[])56 m_topic(struct Client *source_p, int parc, char *parv[])
57 {
58   struct Channel *channel = hash_find_channel(parv[1]);
59   if (channel == NULL)
60   {
61     sendto_one_numeric(source_p, &me, ERR_NOSUCHCHANNEL, parv[1]);
62     return;
63   }
64 
65   /* Setting topic */
66   if (parc > 2)
67   {
68     const struct ChannelMember *member = member_find_link(source_p, channel);
69     if (member == NULL)
70     {
71       sendto_one_numeric(source_p, &me, ERR_NOTONCHANNEL, channel->name);
72       return;
73     }
74 
75     if (!HasCMode(channel, MODE_TOPICLIMIT) ||
76         member_has_flags(member, CHFL_CHANOP | CHFL_HALFOP) == true)
77     {
78       char topic_info[NICKLEN + USERLEN + HOSTLEN + 3];  /* +3 for !, @, \0 */
79 
80       snprintf(topic_info, sizeof(topic_info), "%s!%s@%s", source_p->name,
81                source_p->username, source_p->host);
82       channel_set_topic(channel, parv[2], topic_info, event_base->time.sec_real, true);
83 
84       sendto_server(source_p, 0, 0, ":%s TOPIC %s :%s",
85                     source_p->id, channel->name,
86                     channel->topic);
87       sendto_channel_local(NULL, channel, 0, 0, 0, ":%s!%s@%s TOPIC %s :%s",
88                            source_p->name,
89                            source_p->username,
90                            source_p->host,
91                            channel->name, channel->topic);
92     }
93     else
94       sendto_one_numeric(source_p, &me, ERR_CHANOPRIVSNEEDED, channel->name);
95   }
96   else  /* Only asking for topic */
97   {
98     if (!SecretChannel(channel) || member_find_link(source_p, channel))
99     {
100       if (channel->topic[0] == '\0')
101         sendto_one_numeric(source_p, &me, RPL_NOTOPIC, channel->name);
102       else
103       {
104         sendto_one_numeric(source_p, &me, RPL_TOPIC,
105                            channel->name, channel->topic);
106         sendto_one_numeric(source_p, &me, RPL_TOPICWHOTIME, channel->name,
107                            channel->topic_info,
108                            channel->topic_time);
109       }
110     }
111     else
112       sendto_one_numeric(source_p, &me, ERR_NOTONCHANNEL, channel->name);
113   }
114 }
115 
116 
117 /*! \brief TOPIC command handler
118  *
119  * \param source_p Pointer to allocated Client struct from which the message
120  *                 originally comes from.  This can be a local or remote client.
121  * \param parc     Integer holding the number of supplied arguments.
122  * \param parv     Argument vector where parv[0] .. parv[parc-1] are non-NULL
123  *                 pointers.
124  * \note Valid arguments for this command are:
125  *      - parv[0] = command
126  *      - parv[1] = channel name
127  *      - parv[2] = topic text (can be an empty string)
128  */
129 static void
ms_topic(struct Client * source_p,int parc,char * parv[])130 ms_topic(struct Client *source_p, int parc, char *parv[])
131 {
132   struct Channel *channel = hash_find_channel(parv[1]);
133   if (channel == NULL)
134   {
135     sendto_one_numeric(source_p, &me, ERR_NOSUCHCHANNEL, parv[1]);
136     return;
137   }
138 
139   char topic_info[NICKLEN + USERLEN + HOSTLEN + 3];  /* +3 for !, @, \0 */
140   if (IsClient(source_p))
141     snprintf(topic_info, sizeof(topic_info), "%s!%s@%s", source_p->name,
142              source_p->username, source_p->host);
143   else if (IsHidden(source_p) || ConfigServerHide.hide_servers)
144     strlcpy(topic_info, me.name, sizeof(topic_info));
145   else
146     strlcpy(topic_info, source_p->name, sizeof(topic_info));
147 
148   channel_set_topic(channel, parv[2], topic_info, event_base->time.sec_real, false);
149 
150   sendto_server(source_p, 0, 0, ":%s TOPIC %s :%s",
151                 source_p->id, channel->name,
152                 channel->topic);
153 
154   if (IsClient(source_p))
155     sendto_channel_local(NULL, channel, 0, 0, 0, ":%s!%s@%s TOPIC %s :%s",
156                          source_p->name,
157                          source_p->username,
158                          source_p->host,
159                          channel->name, channel->topic);
160   else
161     sendto_channel_local(NULL, channel, 0, 0, 0, ":%s TOPIC %s :%s",
162                          (IsHidden(source_p) || ConfigServerHide.hide_servers) ? me.name : source_p->name,
163                          channel->name, channel->topic);
164 }
165 
166 static struct Message topic_msgtab =
167 {
168   .cmd = "TOPIC",
169   .handlers[UNREGISTERED_HANDLER] = { .handler = m_unregistered },
170   .handlers[CLIENT_HANDLER] = { .handler = m_topic, .args_min = 2, .end_grace_period = true },
171   .handlers[SERVER_HANDLER] = { .handler = ms_topic, .args_min = 3, .empty_last_arg = true },
172   .handlers[ENCAP_HANDLER] = { .handler = m_ignore },
173   .handlers[OPER_HANDLER] = { .handler = m_topic, .args_min = 2, .end_grace_period = true }
174 };
175 
176 static void
module_init(void)177 module_init(void)
178 {
179   mod_add_cmd(&topic_msgtab);
180 }
181 
182 static void
module_exit(void)183 module_exit(void)
184 {
185   mod_del_cmd(&topic_msgtab);
186 }
187 
188 struct module module_entry =
189 {
190   .version = "$Revision: 9858 $",
191   .modinit = module_init,
192   .modexit = module_exit,
193 };
194