2 * ircd-ratbox: A slightly useful ircd.
3 * m_join.c: Joins a channel.
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
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.
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.
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307
24 * $Id: m_join.c 3494 2007-05-27 13:07:27Z jilles $
38 #include "s_newconf.h"
45 static int m_join(struct Client
*, struct Client
*, int, const char **);
46 static int ms_join(struct Client
*, struct Client
*, int, const char **);
47 static int ms_sjoin(struct Client
*, struct Client
*, int, const char **);
49 struct Message join_msgtab
= {
50 "JOIN", 0, 0, 0, MFLG_SLOW
,
51 {mg_unreg
, {m_join
, 2}, {ms_join
, 2}, mg_ignore
, mg_ignore
, {m_join
, 2}}
54 struct Message sjoin_msgtab
= {
55 "SJOIN", 0, 0, 0, MFLG_SLOW
,
56 {mg_unreg
, mg_ignore
, mg_ignore
, {ms_sjoin
, 4}, mg_ignore
, mg_ignore
}
59 mapi_clist_av1 join_clist
[] = { &join_msgtab
, &sjoin_msgtab
, NULL
};
61 DECLARE_MODULE_AV1(join
, NULL
, NULL
, join_clist
, NULL
, NULL
, "$Revision: 3494 $");
63 static void set_final_mode(struct Mode
*mode
, struct Mode
*oldmode
);
64 static void remove_our_modes(struct Channel
*chptr
, struct Client
*source_p
);
66 static void remove_ban_list(struct Channel
*chptr
, struct Client
*source_p
,
67 rb_dlink_list
* list
, char c
, int mems
);
69 static char modebuf
[MODEBUFLEN
];
70 static char parabuf
[MODEBUFLEN
];
71 static const char *para
[MAXMODEPARAMS
];
78 * parv[2] = channel password (key)
81 m_join(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
83 user_join(client_p
, source_p
, parv
[1], parc
> 2 ? parv
[2] : NULL
); /* channel.c */
90 * parv[1] = channel TS
92 * parv[3] = "+", formerly channel modes but now unused
93 * alternatively, a single "0" parameter parts all channels
96 ms_join(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
98 struct Channel
*chptr
;
99 static struct Mode mode
;
103 int keep_our_modes
= YES
;
104 int keep_new_modes
= YES
;
105 rb_dlink_node
*ptr
, *next_ptr
;
107 /* special case for join 0 */
108 if((parv
[1][0] == '0') && (parv
[1][1] == '\0') && parc
== 2)
110 do_join_0(client_p
, source_p
);
117 if(!IsChannelName(parv
[2]) || !check_channel_name(parv
[2]))
120 /* joins for local channels cant happen. */
121 if(parv
[2][0] == '&')
125 mode
.key
[0] = mode
.forward
[0] = '\0';
126 mode
.mode
= mode
.limit
= mode
.join_num
= mode
.join_time
= 0;
128 if((chptr
= get_or_create_channel(source_p
, parv
[2], &isnew
)) == NULL
)
131 newts
= atol(parv
[1]);
132 oldts
= chptr
->channelts
;
134 #ifdef IGNORE_BOGUS_TS
135 if(newts
< 800000000)
137 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
138 "*** Bogus TS %ld on %s ignored from %s",
139 (long) newts
, chptr
->chname
, client_p
->name
);
140 newts
= (oldts
== 0) ? oldts
: 800000000;
143 /* making a channel TS0 */
144 if(!isnew
&& !newts
&& oldts
)
146 sendto_channel_local(ALL_MEMBERS
, chptr
,
147 ":%s NOTICE %s :*** Notice -- TS for %s changed from %ld to 0",
148 me
.name
, chptr
->chname
, chptr
->chname
, (long) oldts
);
149 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
150 "Server %s changing TS on %s from %ld to 0",
151 source_p
->name
, chptr
->chname
, (long) oldts
);
156 chptr
->channelts
= newts
;
157 else if(newts
== 0 || oldts
== 0)
158 chptr
->channelts
= 0;
159 else if(newts
== oldts
)
161 else if(newts
< oldts
)
164 chptr
->channelts
= newts
;
169 /* Lost the TS, other side wins, so remove modes on this side */
172 set_final_mode(&mode
, &chptr
->mode
);
174 remove_our_modes(chptr
, source_p
);
175 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->invites
.head
)
177 del_invite(chptr
, ptr
->data
);
179 /* If setting -j, clear join throttle state -- jilles */
180 chptr
->join_count
= chptr
->join_delta
= 0;
181 sendto_channel_local(ALL_MEMBERS
, chptr
,
182 ":%s NOTICE %s :*** Notice -- TS for %s changed from %ld to %ld",
183 me
.name
, chptr
->chname
, chptr
->chname
,
184 (long) oldts
, (long) newts
);
185 /* Update capitalization in channel name, this makes the
186 * capitalization timestamped like modes are -- jilles */
187 strcpy(chptr
->chname
, parv
[2]);
189 sendto_channel_local(ALL_MEMBERS
, chptr
,
191 source_p
->servptr
->name
,
192 chptr
->chname
, modebuf
, parabuf
);
193 *modebuf
= *parabuf
= '\0';
196 if(!IsMember(source_p
, chptr
))
198 add_user_to_channel(chptr
, source_p
, CHFL_PEON
);
199 if (chptr
->mode
.join_num
&&
200 rb_current_time() - chptr
->join_delta
>= chptr
->mode
.join_time
)
202 chptr
->join_count
= 0;
203 chptr
->join_delta
= rb_current_time();
206 sendto_channel_local(ALL_MEMBERS
, chptr
, ":%s!%s@%s JOIN :%s",
207 source_p
->name
, source_p
->username
,
208 source_p
->host
, chptr
->chname
);
211 sendto_server(client_p
, chptr
, CAP_TS6
, NOCAPS
,
213 source_p
->id
, (long) chptr
->channelts
, chptr
->chname
);
218 ms_sjoin(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
220 static char buf_uid
[BUFSIZE
];
221 static const char empty_modes
[] = "0";
222 struct Channel
*chptr
;
223 struct Client
*target_p
, *fakesource_p
;
226 static struct Mode mode
, *oldmode
;
229 int keep_our_modes
= 1;
230 int keep_new_modes
= 1;
241 int i
, joinc
= 0, timeslice
= 0;
242 static char empty
[] = "";
243 rb_dlink_node
*ptr
, *next_ptr
;
245 if(!IsChannelName(parv
[2]) || !check_channel_name(parv
[2]))
248 /* SJOIN's for local channels can't happen. */
252 modebuf
[0] = parabuf
[0] = mode
.key
[0] = mode
.forward
[0] = '\0';
253 pargs
= mode
.mode
= mode
.limit
= mode
.join_num
= mode
.join_time
= 0;
255 /* Hide connecting server on netburst -- jilles */
256 if (ConfigServerHide
.flatten_links
&& !HasSentEob(source_p
))
259 fakesource_p
= source_p
;
262 newts
= atol(parv
[1]);
270 rb_strlcpy(mode
.forward
, parv
[4 + args
], sizeof(mode
.forward
));
276 sscanf(parv
[4 + args
], "%d:%d", &joinc
, ×lice
);
278 mode
.join_num
= joinc
;
279 mode
.join_time
= timeslice
;
284 rb_strlcpy(mode
.key
, parv
[4 + args
], sizeof(mode
.key
));
290 mode
.limit
= atoi(parv
[4 + args
]);
296 if(chmode_flags
[(int) *s
] != 0)
298 mode
.mode
|= chmode_flags
[(int) *s
];
307 /* remove any leading spaces */
314 if((chptr
= get_or_create_channel(source_p
, parv
[2], &isnew
)) == NULL
)
315 return 0; /* channel name too long? */
318 oldts
= chptr
->channelts
;
319 oldmode
= &chptr
->mode
;
321 #ifdef IGNORE_BOGUS_TS
322 if(newts
< 800000000)
324 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
325 "*** Bogus TS %ld on %s ignored from %s",
326 (long) newts
, chptr
->chname
, client_p
->name
);
328 newts
= (oldts
== 0) ? oldts
: 800000000;
331 if(!isnew
&& !newts
&& oldts
)
333 sendto_channel_local(ALL_MEMBERS
, chptr
,
334 ":%s NOTICE %s :*** Notice -- TS for %s "
335 "changed from %ld to 0",
336 me
.name
, chptr
->chname
, chptr
->chname
, (long) oldts
);
337 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
338 "Server %s changing TS on %s from %ld to 0",
339 source_p
->name
, chptr
->chname
, (long) oldts
);
344 chptr
->channelts
= newts
;
346 else if(newts
== 0 || oldts
== 0)
347 chptr
->channelts
= 0;
348 else if(newts
== oldts
)
350 else if(newts
< oldts
)
352 /* If configured, kick people trying to join +i/+k
353 * channels by recreating them on split servers.
354 * Don't kick if the source has sent EOB (services
355 * deopping everyone by TS-1 SJOIN).
357 if (ConfigChannel
.kick_on_split_riding
&&
358 !HasSentEob(source_p
) &&
359 ((mode
.mode
& MODE_INVITEONLY
) ||
360 (mode
.key
[0] != 0 && irccmp(mode
.key
, oldmode
->key
) != 0)))
362 struct membership
*msptr
;
364 int l
= rb_dlink_list_length(&chptr
->members
);
366 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->locmembers
.head
)
369 who
= msptr
->client_p
;
370 sendto_one(who
, ":%s KICK %s %s :Net Rider",
371 me
.name
, chptr
->chname
, who
->name
);
373 sendto_server(NULL
, chptr
, CAP_TS6
, NOCAPS
,
374 ":%s KICK %s %s :Net Rider",
375 me
.id
, chptr
->chname
,
377 remove_user_from_channel(msptr
);
383 /* Channel was emptied, create a new one */
384 if((chptr
= get_or_create_channel(source_p
, parv
[2], &isnew
)) == NULL
)
385 return 0; /* oops! */
387 oldmode
= &chptr
->mode
;
391 chptr
->channelts
= newts
;
398 else if(keep_our_modes
)
400 mode
.mode
|= oldmode
->mode
;
401 if(oldmode
->limit
> mode
.limit
)
402 mode
.limit
= oldmode
->limit
;
403 if(strcmp(mode
.key
, oldmode
->key
) < 0)
404 strcpy(mode
.key
, oldmode
->key
);
405 if(oldmode
->join_num
> mode
.join_num
||
406 (oldmode
->join_num
== mode
.join_num
&&
407 oldmode
->join_time
> mode
.join_time
))
409 mode
.join_num
= oldmode
->join_num
;
410 mode
.join_time
= oldmode
->join_time
;
412 if(irccmp(mode
.forward
, oldmode
->forward
) < 0)
413 strcpy(mode
.forward
, oldmode
->forward
);
417 /* If setting -j, clear join throttle state -- jilles */
419 chptr
->join_count
= chptr
->join_delta
= 0;
422 set_final_mode(&mode
, oldmode
);
425 /* Lost the TS, other side wins, so remove modes on this side */
428 remove_our_modes(chptr
, fakesource_p
);
429 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->invites
.head
)
431 del_invite(chptr
, ptr
->data
);
434 if(rb_dlink_list_length(&chptr
->banlist
) > 0)
435 remove_ban_list(chptr
, fakesource_p
, &chptr
->banlist
, 'b', ALL_MEMBERS
);
436 if(rb_dlink_list_length(&chptr
->exceptlist
) > 0)
437 remove_ban_list(chptr
, fakesource_p
, &chptr
->exceptlist
,
439 if(rb_dlink_list_length(&chptr
->invexlist
) > 0)
440 remove_ban_list(chptr
, fakesource_p
, &chptr
->invexlist
,
442 if(rb_dlink_list_length(&chptr
->quietlist
) > 0)
443 remove_ban_list(chptr
, fakesource_p
, &chptr
->quietlist
,
447 sendto_channel_local(ALL_MEMBERS
, chptr
,
448 ":%s NOTICE %s :*** Notice -- TS for %s changed from %ld to %ld",
449 me
.name
, chptr
->chname
, chptr
->chname
,
450 (long) oldts
, (long) newts
);
451 /* Update capitalization in channel name, this makes the
452 * capitalization timestamped like modes are -- jilles */
453 strcpy(chptr
->chname
, parv
[2]);
457 sendto_channel_local(ALL_MEMBERS
, chptr
, ":%s MODE %s %s %s",
458 fakesource_p
->name
, chptr
->chname
, modebuf
, parabuf
);
460 *modebuf
= *parabuf
= '\0';
462 if(parv
[3][0] != '0' && keep_new_modes
)
463 modes
= channel_modes(chptr
, source_p
);
467 mlen_uid
= rb_sprintf(buf_uid
, ":%s SJOIN %ld %s %s :",
468 use_id(source_p
), (long) chptr
->channelts
, parv
[2], modes
);
469 ptr_uid
= buf_uid
+ mlen_uid
;
472 para
[0] = para
[1] = para
[2] = para
[3] = empty
;
474 len_nick
= len_uid
= 0;
476 /* if theres a space, theres going to be more than one nick, change the
477 * first space to \0, so s is just the first nick, and point p to the
480 if((p
= strchr(s
, ' ')) != NULL
)
491 for (i
= 0; i
< 2; i
++)
505 /* if the client doesnt exist or is fake direction, skip. */
506 if(!(target_p
= find_client(s
)) ||
507 (target_p
->from
!= client_p
) || !IsPerson(target_p
))
510 /* we assume for these we can fit at least one nick/uid in.. */
512 /* check we can fit another status+nick+space into a buffer */
513 if((mlen_uid
+ len_uid
+ IDLEN
+ 3) > (BUFSIZE
- 3))
515 *(ptr_uid
- 1) = '\0';
516 sendto_server(client_p
->from
, NULL
, CAP_TS6
, NOCAPS
, "%s", buf_uid
);
517 ptr_uid
= buf_uid
+ mlen_uid
;
537 /* copy the nick to the two buffers */
538 len
= rb_sprintf(ptr_uid
, "%s ", use_id(target_p
));
545 if(!IsMember(target_p
, chptr
))
547 add_user_to_channel(chptr
, target_p
, fl
);
548 sendto_channel_local(ALL_MEMBERS
, chptr
, ":%s!%s@%s JOIN :%s",
550 target_p
->username
, target_p
->host
, parv
[2]);
557 para
[pargs
++] = target_p
->name
;
559 /* a +ov user.. bleh */
562 /* its possible the +o has filled up MAXMODEPARAMS, if so, start
565 if(pargs
>= MAXMODEPARAMS
)
568 sendto_channel_local(ALL_MEMBERS
, chptr
,
569 ":%s MODE %s %s %s %s %s %s",
570 fakesource_p
->name
, chptr
->chname
,
572 para
[0], para
[1], para
[2], para
[3]);
575 para
[0] = para
[1] = para
[2] = para
[3] = NULL
;
580 para
[pargs
++] = target_p
->name
;
583 else if(fl
& CHFL_VOICE
)
586 para
[pargs
++] = target_p
->name
;
589 if(pargs
>= MAXMODEPARAMS
)
592 sendto_channel_local(ALL_MEMBERS
, chptr
,
593 ":%s MODE %s %s %s %s %s %s",
596 modebuf
, para
[0], para
[1], para
[2], para
[3]);
599 para
[0] = para
[1] = para
[2] = para
[3] = NULL
;
604 /* p points to the next nick */
607 /* if there was a trailing space and p was pointing to it, then we
608 * need to exit.. this has the side effect of breaking double spaces
609 * in an sjoin.. but that shouldnt happen anyway
611 if(s
&& (*s
== '\0'))
614 /* if p was NULL due to no spaces, s wont exist due to the above, so
615 * we cant check it for spaces.. if there are no spaces, then when
616 * we next get here, s will be NULL
618 if(s
&& ((p
= strchr(s
, ' ')) != NULL
))
627 sendto_channel_local(ALL_MEMBERS
, chptr
,
628 ":%s MODE %s %s %s %s %s %s",
629 fakesource_p
->name
, chptr
->chname
, modebuf
,
630 para
[0], CheckEmpty(para
[1]),
631 CheckEmpty(para
[2]), CheckEmpty(para
[3]));
634 if(!joins
&& !(chptr
->mode
.mode
& MODE_PERMANENT
) && isnew
)
636 destroy_channel(chptr
);
641 /* Keep the colon if we're sending an SJOIN without nicks -- jilles */
644 *(ptr_uid
- 1) = '\0';
647 sendto_server(client_p
->from
, NULL
, CAP_TS6
, NOCAPS
, "%s", buf_uid
);
654 set_final_mode(struct Mode
*mode
, struct Mode
*oldmode
)
656 int dir
= MODE_QUERY
;
657 char *pbuf
= parabuf
;
661 /* ok, first get a list of modes we need to add */
662 for (i
= 0; i
< 256; i
++)
664 if((mode
->mode
& chmode_flags
[i
]) && !(oldmode
->mode
& chmode_flags
[i
]))
675 /* now the ones we need to remove. */
676 for (i
= 0; i
< 256; i
++)
678 if((oldmode
->mode
& chmode_flags
[i
]) && !(mode
->mode
& chmode_flags
[i
]))
689 if(oldmode
->limit
&& !mode
->limit
)
698 if(oldmode
->key
[0] && !mode
->key
[0])
706 len
= rb_sprintf(pbuf
, "%s ", oldmode
->key
);
709 if(oldmode
->join_num
&& !mode
->join_num
)
718 if(oldmode
->forward
[0] && !mode
->forward
[0])
727 if(mode
->limit
&& oldmode
->limit
!= mode
->limit
)
735 len
= rb_sprintf(pbuf
, "%d ", mode
->limit
);
738 if(mode
->key
[0] && strcmp(oldmode
->key
, mode
->key
))
746 len
= rb_sprintf(pbuf
, "%s ", mode
->key
);
749 if(mode
->join_num
&& (oldmode
->join_num
!= mode
->join_num
|| oldmode
->join_time
!= mode
->join_time
))
757 len
= rb_sprintf(pbuf
, "%d:%d ", mode
->join_num
, mode
->join_time
);
760 if(mode
->forward
[0] && strcmp(oldmode
->forward
, mode
->forward
) && ConfigChannel
.use_forward
)
768 len
= rb_sprintf(pbuf
, "%s ", mode
->forward
);
782 remove_our_modes(struct Channel
*chptr
, struct Client
*source_p
)
784 struct membership
*msptr
;
786 char lmodebuf
[MODEBUFLEN
];
787 char *lpara
[MAXMODEPARAMS
];
794 for(i
= 0; i
< MAXMODEPARAMS
; i
++)
797 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
803 msptr
->flags
&= ~CHFL_CHANOP
;
804 lpara
[count
++] = msptr
->client_p
->name
;
807 /* +ov, might not fit so check. */
810 if(count
>= MAXMODEPARAMS
)
813 sendto_channel_local(ALL_MEMBERS
, chptr
,
814 ":%s MODE %s %s %s %s %s %s",
815 source_p
->name
, chptr
->chname
,
816 lmodebuf
, lpara
[0], lpara
[1],
819 /* preserve the initial '-' */
824 for(i
= 0; i
< MAXMODEPARAMS
; i
++)
828 msptr
->flags
&= ~CHFL_VOICE
;
829 lpara
[count
++] = msptr
->client_p
->name
;
833 else if(is_voiced(msptr
))
835 msptr
->flags
&= ~CHFL_VOICE
;
836 lpara
[count
++] = msptr
->client_p
->name
;
842 if(count
>= MAXMODEPARAMS
)
845 sendto_channel_local(ALL_MEMBERS
, chptr
,
846 ":%s MODE %s %s %s %s %s %s",
847 source_p
->name
, chptr
->chname
, lmodebuf
,
848 lpara
[0], lpara
[1], lpara
[2], lpara
[3]);
853 for(i
= 0; i
< MAXMODEPARAMS
; i
++)
861 sendto_channel_local(ALL_MEMBERS
, chptr
,
862 ":%s MODE %s %s %s %s %s %s",
863 source_p
->name
, chptr
->chname
, lmodebuf
,
864 EmptyString(lpara
[0]) ? "" : lpara
[0],
865 EmptyString(lpara
[1]) ? "" : lpara
[1],
866 EmptyString(lpara
[2]) ? "" : lpara
[2],
867 EmptyString(lpara
[3]) ? "" : lpara
[3]);
874 * inputs - channel, source, list to remove, char of mode, caps needed
876 * side effects - given list is removed, with modes issued to local clients
879 remove_ban_list(struct Channel
*chptr
, struct Client
*source_p
,
880 rb_dlink_list
* list
, char c
, int mems
)
882 static char lmodebuf
[BUFSIZE
];
883 static char lparabuf
[BUFSIZE
];
886 rb_dlink_node
*next_ptr
;
889 int cur_len
, mlen
, plen
;
893 cur_len
= mlen
= rb_sprintf(lmodebuf
, ":%s MODE %s -", source_p
->name
, chptr
->chname
);
894 mbuf
= lmodebuf
+ mlen
;
896 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, list
->head
)
900 /* trailing space, and the mode letter itself */
901 plen
= strlen(banptr
->banstr
) + 2;
903 if(count
>= MAXMODEPARAMS
|| (cur_len
+ plen
) > BUFSIZE
- 4)
905 /* remove trailing space */
909 sendto_channel_local(mems
, chptr
, "%s %s", lmodebuf
, lparabuf
);
912 mbuf
= lmodebuf
+ mlen
;
919 pbuf
+= rb_sprintf(pbuf
, "%s ", banptr
->banstr
);
927 sendto_channel_local(mems
, chptr
, "%s %s", lmodebuf
, lparabuf
);
929 list
->head
= list
->tail
= NULL
;