1 /*
2  *  ircd-ratbox: A slightly useful ircd.
3  *  m_topic.c: Sets a channel topic.
4  *
5  *  Copyright (C) 1990 Jarkko Oikarinen and University of Oulu, Co Center
6  *  Copyright (C) 1996-2002 Hybrid Development Team
7  *  Copyright (C) 2002-2005 ircd-ratbox development team
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 2 of the License, or
12  *  (at your option) 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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
22  *  USA
23  *
24  *  $Id: m_topic.c 26094 2008-09-19 15:33:46Z androsyn $
25  */
26 
27 #include "stdinc.h"
28 #include "struct.h"
29 #include "channel.h"
30 #include "client.h"
31 #include "hash.h"
32 #include "match.h"
33 #include "ircd.h"
34 #include "numeric.h"
35 #include "send.h"
36 #include "s_serv.h"
37 #include "parse.h"
38 #include "modules.h"
39 
40 static int m_topic(struct Client *, struct Client *, int, const char **);
41 
42 struct Message topic_msgtab = {
43 	"TOPIC", 0, 0, 0, MFLG_SLOW,
44 	{mg_unreg, {m_topic, 2}, {m_topic, 2}, mg_ignore, mg_ignore, {m_topic, 2}}
45 };
46 
47 mapi_clist_av1 topic_clist[] = { &topic_msgtab, NULL };
48 
49 DECLARE_MODULE_AV1(topic, NULL, NULL, topic_clist, NULL, NULL, "$Revision: 26094 $");
50 
51 /*
52  * m_topic
53  *      parv[0] = sender prefix
54  *      parv[1] = channel name
55  *	parv[2] = new topic, if setting topic
56  */
57 static int
m_topic(struct Client * client_p,struct Client * source_p,int parc,const char * parv[])58 m_topic(struct Client *client_p, struct Client *source_p, int parc, const char *parv[])
59 {
60 	struct Channel *chptr = NULL;
61 	struct membership *msptr;
62 	char *p = NULL;
63 
64 	if((p = strchr(parv[1], ',')))
65 		*p = '\0';
66 
67 	if(MyClient(source_p) && !IsFloodDone(source_p))
68 		flood_endgrace(source_p);
69 
70 	if(!IsChannelName(parv[1]))
71 	{
72 		sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
73 				   form_str(ERR_NOSUCHCHANNEL), parv[1]);
74 		return 0;
75 	}
76 
77 	chptr = find_channel(parv[1]);
78 
79 	if(chptr == NULL)
80 	{
81 		sendto_one_numeric(source_p, ERR_NOSUCHCHANNEL,
82 				   form_str(ERR_NOSUCHCHANNEL), parv[1]);
83 		return 0;
84 	}
85 
86 	/* setting topic */
87 	if(parc > 2)
88 	{
89 		msptr = find_channel_membership(chptr, source_p);
90 
91 		if(msptr == NULL)
92 		{
93 			sendto_one_numeric(source_p, ERR_NOTONCHANNEL,
94 					   form_str(ERR_NOTONCHANNEL), parv[1]);
95 			return 0;
96 		}
97 
98 		if((chptr->mode.mode & MODE_TOPICLIMIT) == 0 || is_chanop(msptr))
99 		{
100 			char topic_info[USERHOST_REPLYLEN];
101 			rb_sprintf(topic_info, "%s!%s@%s",
102 				   source_p->name, source_p->username, source_p->host);
103 			set_channel_topic(chptr, parv[2], topic_info, rb_current_time());
104 
105 			sendto_server(client_p, chptr, CAP_TS6, NOCAPS,
106 				      ":%s TOPIC %s :%s",
107 				      use_id(source_p), chptr->chname,
108 				      chptr->topic == NULL ? "" : chptr->topic->topic);
109 			sendto_server(client_p, chptr, NOCAPS, CAP_TS6,
110 				      ":%s TOPIC %s :%s",
111 				      source_p->name, chptr->chname,
112 				      chptr->topic == NULL ? "" : chptr->topic->topic);
113 			sendto_channel_local(ALL_MEMBERS,
114 					     chptr, ":%s!%s@%s TOPIC %s :%s",
115 					     source_p->name, source_p->username,
116 					     source_p->host, chptr->chname,
117 					     chptr->topic == NULL ? "" : chptr->topic->topic);
118 		}
119 		else
120 			sendto_one(source_p, form_str(ERR_CHANOPRIVSNEEDED),
121 				   me.name, source_p->name, parv[1]);
122 	}
123 	else if(MyClient(source_p))
124 	{
125 		if(!IsMember(source_p, chptr) && SecretChannel(chptr))
126 		{
127 			sendto_one_numeric(source_p, ERR_NOTONCHANNEL,
128 					   form_str(ERR_NOTONCHANNEL), parv[1]);
129 			return 0;
130 		}
131 		if(chptr->topic == NULL)
132 			sendto_one(source_p, form_str(RPL_NOTOPIC),
133 				   me.name, source_p->name, parv[1]);
134 		else
135 		{
136 			sendto_one(source_p, form_str(RPL_TOPIC),
137 				   me.name, source_p->name, chptr->chname, chptr->topic->topic);
138 
139 			sendto_one(source_p, form_str(RPL_TOPICWHOTIME),
140 				   me.name, source_p->name, chptr->chname,
141 				   chptr->topic->topic_info, chptr->topic->topic_time);
142 		}
143 	}
144 
145 	return 0;
146 }
147