ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid-8/modules/core/m_join.c
Revision: 873
Committed: Tue Oct 23 11:02:09 2007 UTC (16 years, 5 months ago) by michael
Content type: text/x-csrc
Original Path: ircd-hybrid-7.2/modules/core/m_join.c
File size: 18854 byte(s)
Log Message:
- fixed longstanding topic desync bug during netbursts, which has been
  reported by several people. We now basically do what ircu does, i.e.
  unset topics set by netriders

File Contents

# User Rev Content
1 adx 30 /*
2     * ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
3     * m_join.c: Joins a channel.
4     *
5     * Copyright (C) 2002 by the past and present ircd coders, and others.
6     *
7     * This program is free software; you can redistribute it and/or modify
8     * it under the terms of the GNU General Public License as published by
9     * the Free Software Foundation; either version 2 of the License, or
10     * (at your option) any later version.
11     *
12     * This program is distributed in the hope that it will be useful,
13     * but WITHOUT ANY WARRANTY; without even the implied warranty of
14     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15     * GNU General Public License for more details.
16     *
17     * You should have received a copy of the GNU General Public License
18     * along with this program; if not, write to the Free Software
19     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20     * USA
21     *
22 knight 31 * $Id$
23 adx 30 */
24    
25     #include "stdinc.h"
26     #include "tools.h"
27     #include "handlers.h"
28     #include "channel.h"
29     #include "channel_mode.h"
30     #include "client.h"
31     #include "common.h" /* bleah */
32     #include "hash.h"
33     #include "irc_string.h"
34     #include "sprintf_irc.h"
35     #include "ircd.h"
36     #include "list.h"
37     #include "numeric.h"
38     #include "send.h"
39     #include "s_serv.h"
40     #include "s_conf.h"
41     #include "msg.h"
42     #include "parse.h"
43     #include "modules.h"
44    
45    
46     static void m_join(struct Client *, struct Client *, int, char **);
47     static void ms_join(struct Client *, struct Client *, int, char **);
48     static void do_join_0(struct Client *client_p, struct Client *source_p);
49    
50     static void set_final_mode(struct Mode *, struct Mode *);
51     static void remove_our_modes(struct Channel *, struct Client *);
52     static void remove_a_mode(struct Channel *, struct Client *, int, char);
53    
54     static char modebuf[MODEBUFLEN];
55     static char parabuf[MODEBUFLEN];
56     static char sendbuf[MODEBUFLEN];
57     static char *mbuf;
58    
59     struct Message join_msgtab = {
60     "JOIN", 0, 0, 2, 0, MFLG_SLOW, 0,
61     {m_unregistered, m_join, ms_join, m_ignore, m_join, m_ignore}
62     };
63    
64     #ifndef STATIC_MODULES
65     void
66     _modinit(void)
67     {
68     mod_add_cmd(&join_msgtab);
69     }
70    
71     void
72     _moddeinit(void)
73     {
74     mod_del_cmd(&join_msgtab);
75     }
76    
77 knight 31 const char *_version = "$Revision$";
78 adx 30 #endif
79    
80 michael 488 /* last0() stolen from ircu */
81     static char *
82     last0(struct Client *client_p, struct Client *source_p, char *chanlist)
83     {
84     char *p;
85     int join0 = 0;
86    
87     for (p = chanlist; *p; ++p) /* find last "JOIN 0" */
88     {
89     if (*p == '0' && (*(p + 1) == ',' || *(p + 1) == '\0'))
90     {
91     if ((*p + 1) == ',')
92     ++p;
93    
94     chanlist = p + 1;
95     join0 = 1;
96     }
97     else
98     {
99     while (*p != ',' && *p != '\0') /* skip past channel name */
100     ++p;
101    
102     if (*p == '\0') /* hit the end */
103     break;
104     }
105     }
106    
107     if (join0)
108     do_join_0(client_p, source_p);
109    
110     return chanlist;
111     }
112    
113 adx 30 /* m_join()
114     * parv[0] = sender prefix
115     * parv[1] = channel
116     * parv[2] = channel password (key)
117     */
118     static void
119     m_join(struct Client *client_p, struct Client *source_p,
120     int parc, char *parv[])
121     {
122 michael 488 char *p = NULL;
123     char *key_list = NULL;
124     char *chan_list = NULL;
125     char *chan = NULL;
126 adx 30 struct Channel *chptr = NULL;
127 michael 488 int i = 0;
128 adx 30 unsigned int flags = 0;
129    
130 michael 632 if (EmptyString(parv[1]))
131 adx 30 {
132     sendto_one(source_p, form_str(ERR_NEEDMOREPARAMS),
133     me.name, source_p->name, "JOIN");
134     return;
135     }
136    
137 michael 487 assert(client_p == source_p);
138 adx 30
139 michael 488 key_list = parv[2];
140     chan_list = last0(client_p, source_p, parv[1]);
141 michael 487
142 michael 488 for (chan = strtoken(&p, chan_list, ","); chan;
143     chan = strtoken(&p, NULL, ","))
144 adx 30 {
145 michael 488 char *key = NULL;
146 adx 30
147 michael 488 /* If we have any more keys, take the first for this channel. */
148     if (!EmptyString(key_list) && (key_list = strchr(key = key_list, ',')))
149     *key_list++ = '\0';
150    
151     /* Empty keys are the same as no keys. */
152     if (key && *key == '\0')
153     key = NULL;
154    
155 michael 632 if (!check_channel_name(chan, 1))
156 michael 488 {
157     sendto_one(source_p, form_str(ERR_BADCHANNAME),
158     me.name, source_p->name, chan);
159 adx 30 continue;
160 michael 488 }
161 adx 30
162 michael 488 if (ConfigChannel.disable_local_channels && (*chan == '&'))
163     {
164     sendto_one(source_p, form_str(ERR_NOSUCHCHANNEL),
165     me.name, source_p->name, chan);
166     continue;
167     }
168    
169     if (!IsExemptResv(source_p) &&
170     !(IsOper(source_p) && ConfigFileEntry.oper_pass_resv) &&
171     (!hash_find_resv(chan) == ConfigChannel.restrict_channels))
172     {
173     sendto_one(source_p, form_str(ERR_BADCHANNAME),
174     me.name, source_p->name, chan);
175     sendto_realops_flags(UMODE_SPY, L_ALL,
176     "User %s (%s@%s) is attempting to join locally juped channel %s",
177     source_p->name, source_p->username, source_p->host, chan);
178     continue;
179     }
180    
181     if ((dlink_list_length(&source_p->channel) >= ConfigChannel.max_chans_per_user) &&
182     (!IsOper(source_p) || (dlink_list_length(&source_p->channel) >=
183     ConfigChannel.max_chans_per_user * 3)))
184     {
185 michael 494 sendto_one(source_p, form_str(ERR_TOOMANYCHANNELS),
186     me.name, source_p->name, chan);
187     break;
188 michael 488 }
189    
190     if ((chptr = hash_find_channel(chan)) != NULL)
191     {
192     if (IsMember(source_p, chptr))
193     continue;
194    
195     if (splitmode && !IsOper(source_p) && (*chan != '&') &&
196     ConfigChannel.no_join_on_split)
197     {
198     sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
199     me.name, source_p->name, chan);
200     continue;
201     }
202    
203     /*
204     * This should never be the case unless there is some sort of
205     * persistant channels.
206     */
207     if (dlink_list_length(&chptr->members) == 0)
208     flags = CHFL_CHANOP;
209     else
210     flags = 0;
211     }
212     else
213     {
214     if (splitmode && !IsOper(source_p) && (*chan != '&') &&
215     (ConfigChannel.no_create_on_split || ConfigChannel.no_join_on_split))
216     {
217     sendto_one(source_p, form_str(ERR_UNAVAILRESOURCE),
218     me.name, source_p->name, chan);
219     continue;
220     }
221    
222     flags = CHFL_CHANOP;
223 michael 632 chptr = make_channel(chan);
224 michael 488 }
225    
226 adx 30 if (!IsOper(source_p))
227     check_spambot_warning(source_p, chptr->chname);
228    
229     /*
230     * can_join checks for +i key, bans.
231     */
232     if ((i = can_join(source_p, chptr, key)))
233     {
234 michael 488 sendto_one(source_p, form_str(i), me.name,
235     source_p->name, chptr->chname);
236 adx 30 continue;
237     }
238    
239     add_user_to_channel(chptr, source_p, flags, YES);
240    
241     /*
242 michael 488 * Set timestamp if appropriate, and propagate
243     */
244 adx 30 if (flags & CHFL_CHANOP)
245     {
246     chptr->channelts = CurrentTime;
247     chptr->mode.mode |= MODE_TOPICLIMIT;
248     chptr->mode.mode |= MODE_NOPRIVMSGS;
249    
250     sendto_server(client_p, source_p, chptr, CAP_TS6, NOCAPS, LL_ICLIENT,
251     ":%s SJOIN %lu %s +nt :@%s",
252     me.id, (unsigned long)chptr->channelts,
253     chptr->chname, source_p->id);
254     sendto_server(client_p, source_p, chptr, NOCAPS, CAP_TS6, LL_ICLIENT,
255     ":%s SJOIN %lu %s +nt :@%s",
256     me.name, (unsigned long)chptr->channelts,
257 michael 632 chptr->chname, source_p->name);
258 adx 30 /*
259     * notify all other users on the new channel
260     */
261     sendto_channel_local(ALL_MEMBERS, NO, chptr, ":%s!%s@%s JOIN :%s",
262     source_p->name, source_p->username,
263     source_p->host, chptr->chname);
264 michael 488 sendto_channel_local(ALL_MEMBERS, NO, chptr, ":%s MODE %s +nt",
265 adx 30 me.name, chptr->chname);
266     }
267     else
268     {
269     sendto_server(client_p, source_p, chptr, CAP_TS6, NOCAPS, LL_ICLIENT,
270     ":%s JOIN %lu %s +",
271     source_p->id, (unsigned long)chptr->channelts,
272     chptr->chname);
273     sendto_server(client_p, source_p, chptr, NOCAPS, CAP_TS6, LL_ICLIENT,
274     ":%s SJOIN %lu %s + :%s",
275     me.name, (unsigned long)chptr->channelts,
276     chptr->chname, source_p->name);
277    
278     sendto_channel_local(ALL_MEMBERS, NO, chptr, ":%s!%s@%s JOIN :%s",
279     source_p->name, source_p->username,
280     source_p->host, chptr->chname);
281     }
282    
283     del_invite(chptr, source_p);
284    
285     if (chptr->topic != NULL)
286     {
287     sendto_one(source_p, form_str(RPL_TOPIC), me.name,
288     source_p->name, chptr->chname, chptr->topic);
289    
290     sendto_one(source_p, form_str(RPL_TOPICWHOTIME),
291     me.name, source_p->name, chptr->chname,
292     chptr->topic_info, chptr->topic_time);
293     }
294    
295     channel_member_names(source_p, chptr, 1);
296    
297 michael 487 source_p->localClient->last_join_time = CurrentTime;
298 adx 30 }
299     }
300    
301     /* ms_join()
302     *
303     * inputs - parv[0] = uid
304     * parv[1] = ts
305     * parv[2] = channel name
306 db 853 * parv[3] = modes (Deprecated)
307 adx 30 * output - none
308     * side effects - handles remote JOIN's sent by servers. In TSora
309     * remote clients are joined using SJOIN, hence a
310     * JOIN sent by a server on behalf of a client is an error.
311     * here, the initial code is in to take an extra parameter
312     * and use it for the TimeStamp on a new channel.
313     */
314     static void
315     ms_join(struct Client *client_p, struct Client *source_p,
316     int parc, char *parv[])
317     {
318 michael 632 time_t newts = 0;
319     time_t oldts = 0;
320     int keep_our_modes = 1;
321     int keep_new_modes = 1;
322     int isnew = 0;
323     const char *servername = NULL;
324     struct Channel *chptr = NULL;
325     struct Mode mode, *oldmode;
326 adx 30
327 michael 632 if (parc == 2 && !irccmp(parv[1], "0"))
328 adx 30 {
329     do_join_0(client_p, source_p);
330     return;
331     }
332    
333 michael 632 if (parc < 4 || *parv[2] == '&')
334 adx 30 return;
335    
336 michael 632 if (!check_channel_name(parv[2], 0))
337     {
338     sendto_realops_flags(UMODE_DEBUG, L_ALL,
339     "*** Too long or invalid channel name from %s: %s",
340     client_p->name, parv[2]);
341 adx 30 return;
342 michael 632 }
343 adx 30
344     mbuf = modebuf;
345     mode.mode = mode.limit = 0;
346     mode.key[0] = '\0';
347    
348 michael 632 if ((chptr = hash_find_channel(parv[2])) == NULL)
349     {
350     isnew = 1;
351     chptr = make_channel(parv[2]);
352     }
353    
354     newts = atol(parv[1]);
355 adx 30 oldts = chptr->channelts;
356     oldmode = &chptr->mode;
357    
358     if (ConfigFileEntry.ignore_bogus_ts)
359     {
360     if (newts < 800000000)
361     {
362     sendto_realops_flags(UMODE_DEBUG, L_ALL,
363     "*** Bogus TS %lu on %s ignored from %s",
364     (unsigned long)newts, chptr->chname,
365     client_p->name);
366    
367     newts = (oldts == 0) ? 0 : 800000000;
368     }
369     }
370     else
371     {
372     if (!newts && !isnew && oldts)
373     {
374     sendto_channel_local(ALL_MEMBERS, NO, chptr,
375 michael 873 ":%s NOTICE %s :*** Notice -- TS for %s changed from %lu to 0",
376     me.name, chptr->chname, chptr->chname, (unsigned long)oldts);
377 adx 30 sendto_realops_flags(UMODE_ALL, L_ALL,
378     "Server %s changing TS on %s from %lu to 0",
379     source_p->name, chptr->chname, (unsigned long)oldts);
380     }
381     }
382    
383     if (isnew)
384     chptr->channelts = newts;
385     else if (newts == 0 || oldts == 0)
386     chptr->channelts = 0;
387     else if (newts == oldts)
388     ;
389     else if (newts < oldts)
390     {
391     keep_our_modes = NO;
392     chptr->channelts = newts;
393     }
394     else
395     keep_new_modes = NO;
396    
397     if (!keep_new_modes)
398     mode = *oldmode;
399     else if (keep_our_modes)
400     {
401     mode.mode |= oldmode->mode;
402     if (oldmode->limit > mode.limit)
403     mode.limit = oldmode->limit;
404     if (strcmp(mode.key, oldmode->key) < 0)
405     strcpy(mode.key, oldmode->key);
406     }
407    
408     set_final_mode(&mode, oldmode);
409     chptr->mode = mode;
410    
411     /* Lost the TS, other side wins, so remove modes on this side */
412     if (!keep_our_modes)
413     {
414     remove_our_modes(chptr, source_p);
415 michael 873
416     if (chptr->topic)
417     {
418     set_channel_topic(chptr, NULL, NULL, 0);
419     chptr->topic_time = 0;
420     sendto_channel_local(ALL_MEMBERS, NO, chptr, ":%s TOPIC %s :",
421     (IsHidden(source_p) ||
422     ConfigServerHide.hide_servers) ?
423     me.name : source_p->name, chptr->chname);
424     }
425    
426 adx 30 sendto_channel_local(ALL_MEMBERS, NO, chptr,
427     ":%s NOTICE %s :*** Notice -- TS for %s changed from %lu to %lu",
428     me.name, chptr->chname, chptr->chname,
429     (unsigned long)oldts, (unsigned long)newts);
430     }
431    
432     if (*modebuf != '\0')
433     {
434     servername = (ConfigServerHide.hide_servers || IsHidden(source_p)) ?
435     me.name : source_p->name;
436    
437     /* This _SHOULD_ be to ALL_MEMBERS
438     * It contains only +imnpstlk, etc */
439     sendto_channel_local(ALL_MEMBERS, NO, chptr, ":%s MODE %s %s %s",
440     servername, chptr->chname, modebuf, parabuf);
441     }
442    
443     if (!IsMember(source_p, chptr))
444     {
445     add_user_to_channel(chptr, source_p, 0, YES);
446     sendto_channel_local(ALL_MEMBERS, NO, chptr, ":%s!%s@%s JOIN :%s",
447     source_p->name, source_p->username,
448     source_p->host, chptr->chname);
449     }
450    
451     sendto_server(client_p, NULL, chptr, CAP_TS6, NOCAPS, NOFLAGS,
452     ":%s JOIN %lu %s +",
453     ID(source_p), (unsigned long)chptr->channelts, chptr->chname);
454     sendto_server(client_p, NULL, chptr, NOCAPS, CAP_TS6, NOFLAGS,
455     ":%s SJOIN %lu %s + :%s",
456     source_p->servptr->name, (unsigned long)chptr->channelts,
457     chptr->chname, source_p->name);
458     }
459    
460     /* do_join_0()
461     *
462     * inputs - pointer to client doing join 0
463     * output - NONE
464     * side effects - Use has decided to join 0. This is legacy
465     * from the days when channels were numbers not names. *sigh*
466     * There is a bunch of evilness necessary here due to
467     * anti spambot code.
468     */
469     static void
470     do_join_0(struct Client *client_p, struct Client *source_p)
471     {
472     struct Channel *chptr = NULL;
473 michael 488 dlink_node *ptr = NULL, *ptr_next = NULL;
474 adx 30
475 michael 488 if (source_p->channel.head && MyConnect(source_p) && !IsOper(source_p))
476 adx 30 check_spambot_warning(source_p, NULL);
477    
478     DLINK_FOREACH_SAFE(ptr, ptr_next, source_p->channel.head)
479     {
480     chptr = ((struct Membership *)ptr->data)->chptr;
481    
482 michael 488 sendto_server(client_p, NULL, chptr, CAP_TS6, NOCAPS, NOFLAGS,
483     ":%s PART %s", ID(source_p), chptr->chname);
484     sendto_server(client_p, NULL, chptr, NOCAPS, CAP_TS6, NOFLAGS,
485     ":%s PART %s", source_p->name, chptr->chname);
486     sendto_channel_local(ALL_MEMBERS, NO, chptr, ":%s!%s@%s PART %s",
487     source_p->name, source_p->username,
488     source_p->host, chptr->chname);
489 adx 30
490 michael 488 remove_user_from_channel(ptr->data);
491 adx 30 }
492     }
493    
494     /* set_final_mode()
495     *
496     * inputs - pointer to mode to setup
497     * - pointer to old mode
498     * output - NONE
499     * side effects -
500     */
501     static const struct mode_letter
502     {
503     unsigned int mode;
504     unsigned char letter;
505     } flags[] = {
506     { MODE_NOPRIVMSGS, 'n' },
507     { MODE_TOPICLIMIT, 't' },
508     { MODE_SECRET, 's' },
509     { MODE_MODERATED, 'm' },
510     { MODE_INVITEONLY, 'i' },
511     { MODE_PRIVATE, 'p' },
512     { 0, '\0' }
513     };
514    
515     static void
516     set_final_mode(struct Mode *mode, struct Mode *oldmode)
517     {
518     char *pbuf = parabuf;
519     int what = 0;
520     int len;
521     int i;
522    
523     for (i = 0; flags[i].letter; i++)
524     {
525     if ((flags[i].mode & mode->mode) &&
526     !(flags[i].mode & oldmode->mode))
527     {
528     if (what != 1)
529     {
530     *mbuf++ = '+';
531     what = 1;
532     }
533     *mbuf++ = flags[i].letter;
534     }
535     }
536    
537     for (i = 0; flags[i].letter; i++)
538     {
539     if ((flags[i].mode & oldmode->mode) &&
540     !(flags[i].mode & mode->mode))
541     {
542     if (what != -1)
543     {
544     *mbuf++ = '-';
545     what = -1;
546     }
547     *mbuf++ = flags[i].letter;
548     }
549     }
550    
551     if (oldmode->limit != 0 && mode->limit == 0)
552     {
553     if (what != -1)
554     {
555     *mbuf++ = '-';
556     what = -1;
557     }
558     *mbuf++ = 'l';
559     }
560    
561     if (oldmode->key[0] && !mode->key[0])
562     {
563     if (what != -1)
564     {
565     *mbuf++ = '-';
566     what = -1;
567     }
568     *mbuf++ = 'k';
569     len = ircsprintf(pbuf, "%s ", oldmode->key);
570     pbuf += len;
571     }
572    
573     if (mode->limit != 0 && oldmode->limit != mode->limit)
574     {
575     if (what != 1)
576     {
577     *mbuf++ = '+';
578     what = 1;
579     }
580     *mbuf++ = 'l';
581     len = ircsprintf(pbuf, "%d ", mode->limit);
582     pbuf += len;
583     }
584    
585     if (mode->key[0] && strcmp(oldmode->key, mode->key))
586     {
587     if (what != 1)
588     {
589     *mbuf++ = '+';
590     what = 1;
591     }
592     *mbuf++ = 'k';
593     len = ircsprintf(pbuf, "%s ", mode->key);
594     pbuf += len;
595     }
596     *mbuf = '\0';
597     }
598    
599     /* remove_our_modes()
600     *
601     * inputs - pointer to channel to remove modes from
602     * - client pointer
603     * output - NONE
604     * side effects - Go through the local members, remove all their
605     * chanop modes etc., this side lost the TS.
606     */
607     static void
608     remove_our_modes(struct Channel *chptr, struct Client *source_p)
609     {
610     remove_a_mode(chptr, source_p, CHFL_CHANOP, 'o');
611     #ifdef HALFOPS
612     remove_a_mode(chptr, source_p, CHFL_HALFOP, 'h');
613     #endif
614     remove_a_mode(chptr, source_p, CHFL_VOICE, 'v');
615     }
616    
617     /* remove_a_mode()
618     *
619     * inputs -
620     * output - NONE
621     * side effects - remove ONE mode from a channel
622     */
623     static void
624     remove_a_mode(struct Channel *chptr, struct Client *source_p,
625     int mask, char flag)
626     {
627     dlink_node *ptr;
628     struct Membership *ms;
629     char lmodebuf[MODEBUFLEN];
630     const char *lpara[MAXMODEPARAMS];
631     int count = 0;
632     int lcount;
633    
634     mbuf = lmodebuf;
635     *mbuf++ = '-';
636    
637     for (lcount = 0; lcount < MAXMODEPARAMS; lcount++)
638     lpara[lcount] = "";
639     sendbuf[0] = '\0';
640    
641     DLINK_FOREACH(ptr, chptr->members.head)
642     {
643     ms = ptr->data;
644    
645     if ((ms->flags & mask) == 0)
646     continue;
647    
648     ms->flags &= ~mask;
649    
650     lpara[count++] = ms->client_p->name;
651    
652     *mbuf++ = flag;
653    
654     if (count >= MAXMODEPARAMS)
655     {
656     for (lcount = 0; lcount < MAXMODEPARAMS; lcount++)
657     {
658     if (*lpara[lcount] == '\0')
659     break;
660    
661     strlcat(sendbuf, " ", sizeof(sendbuf));
662     strlcat(sendbuf, lpara[lcount], sizeof(sendbuf));
663     lpara[lcount] = "";
664     }
665    
666     *mbuf = '\0';
667     sendto_channel_local(ALL_MEMBERS, NO, chptr,
668     ":%s MODE %s %s%s",
669     (IsHidden(source_p) ||
670     ConfigServerHide.hide_servers) ?
671     me.name : source_p->name,
672     chptr->chname, lmodebuf, sendbuf);
673     mbuf = lmodebuf;
674     *mbuf++ = '-';
675     count = 0;
676     sendbuf[0] = '\0';
677     }
678     }
679    
680     if (count != 0)
681     {
682     *mbuf = '\0';
683     for (lcount = 0; lcount < MAXMODEPARAMS; lcount++)
684     {
685     if (*lpara[lcount] == '\0')
686     break;
687    
688     strlcat(sendbuf, " ", sizeof(sendbuf));
689     strlcat(sendbuf, lpara[lcount], sizeof(sendbuf));
690     }
691     sendto_channel_local(ALL_MEMBERS, NO, chptr,
692     ":%s MODE %s %s%s",
693     (IsHidden(source_p) || ConfigServerHide.hide_servers) ?
694     me.name : source_p->name,
695     chptr->chname, lmodebuf, sendbuf);
696     }
697     }
698    

Properties

Name Value
svn:eol-style native
svn:keywords Id Revision