Author: Bleep <tomh@inxpress.net>
[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 /*
27  * m_functions execute protocol messages on this server:
28  *
29  *    cptr    is always NON-NULL, pointing to a *LOCAL* client
30  *            structure (with an open socket connected!). This
31  *            identifies the physical socket where the message
32  *            originated (or which caused the m_function to be
33  *            executed--some m_functions may call others...).
34  *
35  *    sptr    is the source of the message, defined by the
36  *            prefix part of the message if present. If not
37  *            or prefix not found, then sptr==cptr.
38  *
39  *            (!IsServer(cptr)) => (cptr == sptr), because
40  *            prefixes are taken *only* from servers...
41  *
42  *            (IsServer(cptr))
43  *                    (sptr == cptr) => the message didn't
44  *                    have the prefix.
45  *
46  *                    (sptr != cptr && IsServer(sptr) means
47  *                    the prefix specified servername. (?)
48  *
49  *                    (sptr != cptr && !IsServer(sptr) means
50  *                    that message originated from a remote
51  *                    user (not local).
52  *
53  *            combining
54  *
55  *            (!IsServer(sptr)) means that, sptr can safely
56  *            taken as defining the target structure of the
57  *            message in this server.
58  *
59  *    *Always* true (if 'parse' and others are working correct):
60  *
61  *    1)      sptr->from == cptr  (note: cptr->from == cptr)
62  *
63  *    2)      MyConnect(sptr) <=> sptr == cptr (e.g. sptr
64  *            *cannot* be a local connection, unless it's
65  *            actually cptr!). [MyConnect(x) should probably
66  *            be defined as (x == x->from) --msa ]
67  *
68  *    parc    number of variable parameter strings (if zero,
69  *            parv is allowed to be NULL)
70  *
71  *    parv    a NULL terminated list of parameter pointers,
72  *
73  *                    parv[0], sender (prefix string), if not present
74  *                            this points to an empty string.
75  *                    parv[1]...parv[parc-1]
76  *                            pointers to additional parameters
77  *                    parv[parc] == NULL, *always*
78  *
79  *            note:   it is guaranteed that parv[0]..parv[parc-1] are all
80  *                    non-NULL pointers.
81  */
82 #if 0
83 /*
84  * No need to include handlers.h here the signatures must match
85  * and we don't need to force a rebuild of all the handlers everytime
86  * we add a new one to the list. --Bleep
87  */
88 #include "handlers.h"
89 #endif /* 0 */
90 #include "channel.h"
91 #include "client.h"
92 #include "hash.h"
93 #include "ircd.h"
94 #include "ircd_reply.h"
95 #include "ircd_string.h"
96 #include "msg.h"
97 #include "numeric.h"
98 #include "numnicks.h"
99 #include "send.h"
100
101 #include <assert.h>
102
103 /*
104  * m_topic - generic message handler
105  *
106  * parv[0]        = sender prefix
107  * parv[1]        = channel
108  * parv[parc - 1] = topic (if parc > 2)
109  */
110 int m_topic(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
111 {
112   struct Channel *chptr;
113   char *topic = 0, *name, *p = 0;
114
115   if (parc < 2)
116     return need_more_params(sptr, "TOPIC");
117
118   if (parc > 2)
119     topic = parv[parc - 1];
120
121   for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
122   {
123     chptr = 0;
124     if (!IsChannelName(name) || !(chptr = FindChannel(name)) ||
125         ((topic || SecretChannel(chptr)) && !find_channel_member(sptr, chptr)))
126     {
127       sendto_one(sptr, err_str(chptr ? ERR_NOTONCHANNEL : ERR_NOSUCHCHANNEL),
128           me.name, parv[0], chptr ? chptr->chname : name);
129       continue;
130     }
131     if (IsModelessChannel(name))
132     {
133       sendto_one(sptr, err_str(ERR_CHANOPRIVSNEEDED), me.name, parv[0],
134           chptr->chname);
135       continue;
136     }
137     if (IsLocalChannel(name) && !MyUser(sptr))
138       continue;
139
140     if (!topic)                 /* only asking  for topic  */
141     {
142       if (chptr->topic[0] == '\0')
143         sendto_one(sptr, rpl_str(RPL_NOTOPIC), me.name, parv[0], chptr->chname);
144       else
145       {
146         sendto_one(sptr, rpl_str(RPL_TOPIC),
147             me.name, parv[0], chptr->chname, chptr->topic);
148         sendto_one(sptr, rpl_str(RPL_TOPICWHOTIME),
149             me.name, parv[0], chptr->chname,
150             chptr->topic_nick, chptr->topic_time);
151       }
152     }
153     else if (((chptr->mode.mode & MODE_TOPICLIMIT) == 0 ||
154         is_chan_op(sptr, chptr)) && topic)
155     {
156       /* setting a topic */
157       ircd_strncpy(chptr->topic, topic, TOPICLEN);
158       ircd_strncpy(chptr->topic_nick, sptr->name, NICKLEN);
159       chptr->topic_time = CurrentTime;
160       sendto_serv_butone(cptr, "%s%s " TOK_TOPIC " %s :%s",
161           NumNick(sptr), chptr->chname, chptr->topic);
162       sendto_channel_butserv(chptr, sptr, ":%s TOPIC %s :%s",
163           parv[0], chptr->chname, chptr->topic);
164     }
165     else
166       sendto_one(sptr, err_str(ERR_CHANOPRIVSNEEDED),
167           me.name, parv[0], chptr->chname);
168   }
169   return 0;
170 }
171
172 /*
173  * ms_topic - server message handler
174  *
175  * parv[0]        = sender prefix
176  * parv[1]        = channel
177  * parv[parc - 1] = topic (if parc > 2)
178  */
179 int ms_topic(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
180 {
181   struct Channel *chptr;
182   char *topic = 0, *name, *p = 0;
183
184   if (parc < 2)
185     return need_more_params(sptr, "TOPIC");
186
187   if (parc > 2)
188     topic = parv[parc - 1];
189
190   for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
191   {
192     chptr = 0;
193     if (!IsChannelName(name) || !(chptr = FindChannel(name)) ||
194         ((topic || SecretChannel(chptr)) && !find_channel_member(sptr, chptr)))
195     {
196       sendto_one(sptr, err_str(chptr ? ERR_NOTONCHANNEL : ERR_NOSUCHCHANNEL),
197           me.name, parv[0], chptr ? chptr->chname : name);
198       continue;
199     }
200     if (IsModelessChannel(name))
201     {
202       sendto_one(sptr, err_str(ERR_CHANOPRIVSNEEDED), me.name, parv[0],
203           chptr->chname);
204       continue;
205     }
206     if (IsLocalChannel(name) && !MyUser(sptr))
207       continue;
208
209     if (!topic)                 /* only asking  for topic  */
210     {
211       if (chptr->topic[0] == '\0')
212         sendto_one(sptr, rpl_str(RPL_NOTOPIC), me.name, parv[0], chptr->chname);
213       else
214       {
215         sendto_one(sptr, rpl_str(RPL_TOPIC),
216             me.name, parv[0], chptr->chname, chptr->topic);
217         sendto_one(sptr, rpl_str(RPL_TOPICWHOTIME),
218             me.name, parv[0], chptr->chname,
219             chptr->topic_nick, chptr->topic_time);
220       }
221     }
222     else if (((chptr->mode.mode & MODE_TOPICLIMIT) == 0 ||
223         is_chan_op(sptr, chptr)) && topic)
224     {
225       /* setting a topic */
226       ircd_strncpy(chptr->topic, topic, TOPICLEN);
227       ircd_strncpy(chptr->topic_nick, sptr->name, NICKLEN);
228       chptr->topic_time = CurrentTime;
229       sendto_serv_butone(cptr, "%s%s " TOK_TOPIC " %s :%s",
230           NumNick(sptr), chptr->chname, chptr->topic);
231       sendto_channel_butserv(chptr, sptr, ":%s TOPIC %s :%s",
232           parv[0], chptr->chname, chptr->topic);
233     }
234     else
235       sendto_one(sptr, err_str(ERR_CHANOPRIVSNEEDED),
236           me.name, parv[0], chptr->chname);
237   }
238   return 0;
239 }
240
241
242 #if 0
243 /*
244  * m_topic
245  *
246  * parv[0]        = sender prefix
247  * parv[1]        = channel
248  * parv[parc - 1] = topic (if parc > 2)
249  */
250 int m_topic(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
251 {
252   struct Channel *chptr;
253   char *topic = 0, *name, *p = 0;
254
255   if (parc < 2)
256     return need_more_params(sptr, "TOPIC");
257
258   if (parc > 2)
259     topic = parv[parc - 1];
260
261   for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
262   {
263     chptr = 0;
264     if (!IsChannelName(name) || !(chptr = FindChannel(name)) ||
265         ((topic || SecretChannel(chptr)) && !find_channel_member(sptr, chptr)))
266     {
267       sendto_one(sptr, err_str(chptr ? ERR_NOTONCHANNEL : ERR_NOSUCHCHANNEL),
268           me.name, parv[0], chptr ? chptr->chname : name);
269       continue;
270     }
271     if (IsModelessChannel(name))
272     {
273       sendto_one(sptr, err_str(ERR_CHANOPRIVSNEEDED), me.name, parv[0],
274           chptr->chname);
275       continue;
276     }
277     if (IsLocalChannel(name) && !MyUser(sptr))
278       continue;
279
280     if (!topic)                 /* only asking  for topic  */
281     {
282       if (chptr->topic[0] == '\0')
283         sendto_one(sptr, rpl_str(RPL_NOTOPIC), me.name, parv[0], chptr->chname);
284       else
285       {
286         sendto_one(sptr, rpl_str(RPL_TOPIC),
287             me.name, parv[0], chptr->chname, chptr->topic);
288         sendto_one(sptr, rpl_str(RPL_TOPICWHOTIME),
289             me.name, parv[0], chptr->chname,
290             chptr->topic_nick, chptr->topic_time);
291       }
292     }
293     else if (((chptr->mode.mode & MODE_TOPICLIMIT) == 0 ||
294         is_chan_op(sptr, chptr)) && topic)
295     {
296       /* setting a topic */
297       ircd_strncpy(chptr->topic, topic, TOPICLEN);
298       ircd_strncpy(chptr->topic_nick, sptr->name, NICKLEN);
299       chptr->topic_time = CurrentTime;
300       sendto_serv_butone(cptr, "%s%s " TOK_TOPIC " %s :%s",
301           NumNick(sptr), chptr->chname, chptr->topic);
302       sendto_channel_butserv(chptr, sptr, ":%s TOPIC %s :%s",
303           parv[0], chptr->chname, chptr->topic);
304     }
305     else
306       sendto_one(sptr, err_str(ERR_CHANOPRIVSNEEDED),
307           me.name, parv[0], chptr->chname);
308   }
309   return 0;
310 }
311 #endif /* 0 */
312