Author: Isomer <perry@coders.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 static void do_settopic(struct Client *sptr, struct Client *cptr, 
104                         struct Channel *chptr,char *topic)
105 {
106    int newtopic;
107    /* if +n and not @'d, return an error and ignore the topic */
108    if ((chptr->mode.mode & MODE_TOPICLIMIT) != 0 && !is_chan_op(sptr, chptr)) 
109    {
110       send_reply(sptr, ERR_CHANOPRIVSNEEDED, chptr->chname);
111       return;
112    }
113    /* Note if this is just a refresh of an old topic, and don't
114     * send it to all the clients to save bandwidth.  We still send
115     * it to other servers as they may have split and lost the topic.
116     */
117    newtopic=ircd_strncmp(chptr->topic,topic,TOPICLEN)!=0;
118    /* setting a topic */
119    ircd_strncpy(chptr->topic, topic, TOPICLEN);
120    ircd_strncpy(chptr->topic_nick, sptr->name, NICKLEN);
121    chptr->topic_time = CurrentTime;
122    /* Fixed in 2.10.11: Don't propergate local topics */
123    if (!IsLocalChannel(chptr->chname))
124      sendcmdto_serv_butone(sptr, CMD_TOPIC, cptr, "%H :%s", chptr,
125                            chptr->topic);
126    if (newtopic)
127       sendcmdto_channel_butserv(sptr, CMD_TOPIC, chptr, "%H :%s", chptr,
128                                   chptr->topic);
129       /* if this is the same topic as before we send it to the person that
130        * set it (so they knew it went through ok), but don't bother sending
131        * it to everyone else on the channel to save bandwidth
132        */
133     else if (MyUser(sptr))
134       sendcmdto_one(sptr, CMD_TOPIC, sptr, "%H :%s", chptr, chptr->topic);      
135 }
136
137 /*
138  * m_topic - generic message handler
139  *
140  * parv[0]        = sender prefix
141  * parv[1]        = channel
142  * parv[parc - 1] = topic (if parc > 2)
143  */
144 int m_topic(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
145 {
146   struct Channel *chptr;
147   char *topic = 0, *name, *p = 0;
148
149   if (parc < 2)
150     return need_more_params(sptr, "TOPIC");
151
152   if (parc > 2)
153     topic = parv[parc - 1];
154
155   for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
156   {
157     chptr = 0;
158     /* Does the channel exist */
159     if (!IsChannelName(name) || !(chptr = FindChannel(name)))
160     {
161         send_reply(sptr,ERR_NOSUCHCHANNEL,name);
162         continue;
163     }
164     /* Trying to check a topic outside a secret channel */
165     if ((topic || SecretChannel(chptr)) && !find_channel_member(sptr, chptr))
166     {
167       send_reply(sptr, ERR_NOTONCHANNEL, chptr->chname);
168       continue;
169     }
170     /* Modeless Channels don't have topics */
171     if (IsModelessChannel(name))
172     {
173       send_reply(sptr, ERR_CHANOPRIVSNEEDED, chptr->chname);
174       continue;
175     }
176
177     if (!topic)                 /* only asking for topic */
178     {
179       if (chptr->topic[0] == '\0')
180         send_reply(sptr, RPL_NOTOPIC, chptr->chname);
181       else
182       {
183         send_reply(sptr, RPL_TOPIC, chptr->chname, chptr->topic);
184         send_reply(sptr, RPL_TOPICWHOTIME, chptr->chname, chptr->topic_nick,
185                    chptr->topic_time);
186       }
187     }
188     else 
189      do_settopic(sptr,cptr,chptr,topic);
190   }
191   return 0;
192 }
193
194 /*
195  * ms_topic - generic message handler
196  *
197  * parv[0]        = sender prefix
198  * parv[1]        = channel
199  * parv[parc - 1] = topic
200  */
201 int ms_topic(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
202 {
203   struct Channel *chptr;
204   char *topic = 0, *name, *p = 0;
205
206   if (parc < 3)
207     return need_more_params(sptr, "TOPIC");
208
209   topic = parv[parc - 1];
210
211   for (; (name = ircd_strtok(&p, parv[1], ",")); parv[1] = 0)
212   {
213     chptr = 0;
214     /* Does the channel exist */
215     if (!IsChannelName(name) || !(chptr = FindChannel(name)))
216     {
217         send_reply(sptr,ERR_NOSUCHCHANNEL,name);
218         continue;
219     }
220     /* Modeless Channels don't have topics */
221     if (IsModelessChannel(name))
222     {
223       protocol_violation(sptr,"Attempted to topic modeless channel");
224       send_reply(sptr, ERR_CHANOPRIVSNEEDED, chptr->chname);
225       continue;
226     }
227     /* Ignore requests for topics from remote servers */
228     if (IsLocalChannel(name) && !MyUser(sptr))
229       protocol_violation(sptr,"Topic request");
230       continue;
231
232     do_settopic(sptr,cptr,chptr,topic);
233   }
234   return 0;
235 }