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 $
32 #include "irc_string.h"
38 #include "s_newconf.h"
42 #include "sprintf_irc.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 **);
48 static int h_can_create_channel
;
49 static int h_channel_join
;
51 struct Message join_msgtab
= {
52 "JOIN", 0, 0, 0, MFLG_SLOW
,
53 {mg_unreg
, {m_join
, 2}, {ms_join
, 2}, mg_ignore
, mg_ignore
, {m_join
, 2}}
56 mapi_clist_av1 join_clist
[] = { &join_msgtab
, NULL
};
58 mapi_hlist_av1 join_hlist
[] = {
59 { "can_create_channel", &h_can_create_channel
},
60 { "channel_join", &h_channel_join
},
64 DECLARE_MODULE_AV1(join
, NULL
, NULL
, join_clist
, join_hlist
, NULL
, "$Revision: 3494 $");
66 static void do_join_0(struct Client
*client_p
, struct Client
*source_p
);
67 static int check_channel_name_loc(struct Client
*source_p
, const char *name
);
69 static void set_final_mode(struct Mode
*mode
, struct Mode
*oldmode
);
70 static void remove_our_modes(struct Channel
*chptr
, struct Client
*source_p
);
72 static char modebuf
[MODEBUFLEN
];
73 static char parabuf
[MODEBUFLEN
];
76 /* Check what we will forward to, without sending any notices to the user
79 static struct Channel
*
80 check_forward(struct Client
*source_p
, struct Channel
*chptr
,
86 if (IsNoForward(source_p
))
91 chptr
= find_channel(chptr
->mode
.forward
);
92 /* Can only forward to existing channels */
95 /* Already on there, show original error message */
96 if (IsMember(source_p
, chptr
))
98 /* Juped. Sending a warning notice would be unfair */
99 if (hash_find_resv(chptr
->chname
))
101 /* Don't forward to +Q channel */
102 if (chptr
->mode
.mode
& MODE_DISFORWARD
)
104 i
= can_join(source_p
, chptr
, key
);
107 if (i
!= ERR_INVITEONLYCHAN
&& i
!= ERR_NEEDREGGEDNICK
&& i
!= ERR_THROTTLE
&& i
!= ERR_CHANNELISFULL
)
117 * parv[0] = sender prefix
119 * parv[2] = channel password (key)
122 m_join(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
124 static char jbuf
[BUFSIZE
];
125 struct Channel
*chptr
= NULL
;
126 struct ConfItem
*aconf
;
130 char *p
= NULL
, *p2
= NULL
;
133 int successful_join_count
= 0; /* Number of channels successfully joined */
137 /* rebuild the list of channels theyre supposed to be joining.
138 * this code has a side effect of losing keys, but..
140 chanlist
= LOCAL_COPY(parv
[1]);
141 for(name
= strtoken(&p
, chanlist
, ","); name
; name
= strtoken(&p
, NULL
, ","))
143 /* check the length and name of channel is ok */
144 if(!check_channel_name_loc(source_p
, name
) || (strlen(name
) > LOC_CHANNELLEN
))
146 sendto_one_numeric(source_p
, ERR_BADCHANNAME
,
147 form_str(ERR_BADCHANNAME
), (unsigned char *) name
);
151 /* join 0 parts all channels */
152 if(*name
== '0' && (name
[1] == ',' || name
[1] == '\0') && name
== chanlist
)
154 (void) strcpy(jbuf
, "0");
158 /* check it begins with # or &, and local chans are disabled */
159 else if(!IsChannelName(name
))
161 sendto_one_numeric(source_p
, ERR_NOSUCHCHANNEL
,
162 form_str(ERR_NOSUCHCHANNEL
), name
);
166 /* see if its resv'd */
167 if(!IsExemptResv(source_p
) && (aconf
= hash_find_resv(name
)))
169 sendto_one_numeric(source_p
, ERR_BADCHANNAME
,
170 form_str(ERR_BADCHANNAME
), name
);
172 /* dont warn for opers */
173 if(!IsExemptJupe(source_p
) && !IsOper(source_p
))
174 sendto_realops_snomask(SNO_SPY
, L_NETWIDE
,
175 "User %s (%s@%s) is attempting to join locally juped channel %s (%s)",
176 source_p
->name
, source_p
->username
,
177 source_p
->orighost
, name
, aconf
->passwd
);
178 /* dont update tracking for jupe exempt users, these
179 * are likely to be spamtrap leaves
181 else if(IsExemptJupe(source_p
))
187 if(splitmode
&& !IsOper(source_p
) && (*name
!= '&') &&
188 ConfigChannel
.no_join_on_split
)
190 sendto_one(source_p
, form_str(ERR_UNAVAILRESOURCE
),
191 me
.name
, source_p
->name
, name
);
196 (void) strcat(jbuf
, ",");
197 (void) strlcat(jbuf
, name
, sizeof(jbuf
));
202 mykey
= LOCAL_COPY(parv
[2]);
203 key
= strtoken(&p2
, mykey
, ",");
206 for(name
= strtoken(&p
, jbuf
, ","); name
;
207 key
= (key
) ? strtoken(&p2
, NULL
, ",") : NULL
, name
= strtoken(&p
, NULL
, ","))
209 hook_data_channel_activity hook_info
;
211 /* JOIN 0 simply parts all channels the user is in */
212 if(*name
== '0' && !atoi(name
))
214 if(source_p
->user
->channel
.head
== NULL
)
217 do_join_0(&me
, source_p
);
221 /* look for the channel */
222 if((chptr
= find_channel(name
)) != NULL
)
224 if(IsMember(source_p
, chptr
))
231 hook_data_client_approval moduledata
;
233 moduledata
.client
= source_p
;
234 moduledata
.approved
= 0;
236 call_hook(h_can_create_channel
, &moduledata
);
238 if(moduledata
.approved
!= 0)
240 sendto_one(source_p
, form_str(moduledata
.approved
),
241 me
.name
, source_p
->name
, name
);
245 if(splitmode
&& !IsOper(source_p
) && (*name
!= '&') &&
246 ConfigChannel
.no_create_on_split
)
248 sendto_one(source_p
, form_str(ERR_UNAVAILRESOURCE
),
249 me
.name
, source_p
->name
, name
);
256 if((rb_dlink_list_length(&source_p
->user
->channel
) >=
257 (unsigned long) ConfigChannel
.max_chans_per_user
) &&
258 (!IsOper(source_p
) ||
259 (rb_dlink_list_length(&source_p
->user
->channel
) >=
260 (unsigned long) ConfigChannel
.max_chans_per_user
* 3)))
262 sendto_one(source_p
, form_str(ERR_TOOMANYCHANNELS
),
263 me
.name
, source_p
->name
, name
);
264 if(successful_join_count
)
265 source_p
->localClient
->last_join_time
= rb_current_time();
269 if(flags
== 0) /* if channel doesn't exist, don't penalize */
270 successful_join_count
++;
272 if(chptr
== NULL
) /* If I already have a chptr, no point doing this */
274 chptr
= get_or_create_channel(source_p
, name
, NULL
);
278 sendto_one(source_p
, form_str(ERR_UNAVAILRESOURCE
),
279 me
.name
, source_p
->name
, name
);
280 if(successful_join_count
> 0)
281 successful_join_count
--;
286 if(!IsOper(source_p
) && !IsExemptSpambot(source_p
))
287 check_spambot_warning(source_p
, name
);
289 /* can_join checks for +i key, bans etc */
290 if((i
= can_join(source_p
, chptr
, key
)))
292 if ((i
!= ERR_NEEDREGGEDNICK
&& i
!= ERR_THROTTLE
&& i
!= ERR_INVITEONLYCHAN
&& i
!= ERR_CHANNELISFULL
) ||
293 (!ConfigChannel
.use_forward
|| (chptr
= check_forward(source_p
, chptr
, key
)) == NULL
))
295 sendto_one(source_p
, form_str(i
), me
.name
, source_p
->name
, name
);
296 if(successful_join_count
> 0)
297 successful_join_count
--;
300 sendto_one_numeric(source_p
, ERR_LINKCHANNEL
, form_str(ERR_LINKCHANNEL
), name
, chptr
->chname
);
303 /* add the user to the channel */
304 add_user_to_channel(chptr
, source_p
, flags
);
305 if (chptr
->mode
.join_num
&&
306 rb_current_time() - chptr
->join_delta
>= chptr
->mode
.join_time
)
308 chptr
->join_count
= 0;
309 chptr
->join_delta
= rb_current_time();
313 /* we send the user their join here, because we could have to
314 * send a mode out next.
316 sendto_channel_local(ALL_MEMBERS
, chptr
, ":%s!%s@%s JOIN :%s",
318 source_p
->username
, source_p
->host
, chptr
->chname
);
320 /* its a new channel, set +nt and burst. */
321 if(flags
& CHFL_CHANOP
)
323 chptr
->channelts
= rb_current_time();
324 chptr
->mode
.mode
|= MODE_TOPICLIMIT
;
325 chptr
->mode
.mode
|= MODE_NOPRIVMSGS
;
327 sendto_channel_local(ONLY_CHANOPS
, chptr
, ":%s MODE %s +nt",
328 me
.name
, chptr
->chname
);
330 if(*chptr
->chname
== '#')
332 sendto_server(client_p
, chptr
, CAP_TS6
, NOCAPS
,
333 ":%s SJOIN %ld %s +nt :@%s",
334 me
.id
, (long) chptr
->channelts
,
335 chptr
->chname
, source_p
->id
);
336 sendto_server(client_p
, chptr
, NOCAPS
, CAP_TS6
,
337 ":%s SJOIN %ld %s +nt :@%s",
338 me
.name
, (long) chptr
->channelts
,
339 chptr
->chname
, source_p
->name
);
344 sendto_server(client_p
, chptr
, CAP_TS6
, NOCAPS
,
346 use_id(source_p
), (long) chptr
->channelts
,
349 sendto_server(client_p
, chptr
, NOCAPS
, CAP_TS6
,
350 ":%s SJOIN %ld %s + :%s",
351 me
.name
, (long) chptr
->channelts
,
352 chptr
->chname
, source_p
->name
);
355 del_invite(chptr
, source_p
);
357 if(chptr
->topic
!= NULL
)
359 sendto_one(source_p
, form_str(RPL_TOPIC
), me
.name
,
360 source_p
->name
, chptr
->chname
, chptr
->topic
);
362 sendto_one(source_p
, form_str(RPL_TOPICWHOTIME
),
363 me
.name
, source_p
->name
, chptr
->chname
,
364 chptr
->topic_info
, chptr
->topic_time
);
367 channel_member_names(chptr
, source_p
, 1);
369 if(successful_join_count
)
370 source_p
->localClient
->last_join_time
= rb_current_time();
372 hook_info
.client
= source_p
;
373 hook_info
.chptr
= chptr
;
375 call_hook(h_channel_join
, &hook_info
);
386 * side effects - handles remote JOIN's sent by servers. In TSora
387 * remote clients are joined using SJOIN, hence a
388 * JOIN sent by a server on behalf of a client is an error.
389 * here, the initial code is in to take an extra parameter
390 * and use it for the TimeStamp on a new channel.
393 ms_join(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
395 struct Channel
*chptr
;
396 static struct Mode mode
;
400 int keep_our_modes
= YES
;
401 int keep_new_modes
= YES
;
402 rb_dlink_node
*ptr
, *next_ptr
;
404 /* special case for join 0 */
405 if((parv
[1][0] == '0') && (parv
[1][1] == '\0') && parc
== 2)
407 do_join_0(client_p
, source_p
);
414 if(!IsChannelName(parv
[2]) || !check_channel_name(parv
[2]))
417 /* joins for local channels cant happen. */
418 if(parv
[2][0] == '&')
422 mode
.key
[0] = mode
.forward
[0] = '\0';
423 mode
.mode
= mode
.limit
= mode
.join_num
= mode
.join_time
= 0;
425 if((chptr
= get_or_create_channel(source_p
, parv
[2], &isnew
)) == NULL
)
428 newts
= atol(parv
[1]);
429 oldts
= chptr
->channelts
;
431 #ifdef IGNORE_BOGUS_TS
432 if(newts
< 800000000)
434 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
435 "*** Bogus TS %ld on %s ignored from %s",
436 (long) newts
, chptr
->chname
, client_p
->name
);
437 newts
= (oldts
== 0) ? oldts
: 800000000;
440 /* making a channel TS0 */
441 if(!isnew
&& !newts
&& oldts
)
443 sendto_channel_local(ALL_MEMBERS
, chptr
,
444 ":%s NOTICE %s :*** Notice -- TS for %s changed from %ld to 0",
445 me
.name
, chptr
->chname
, chptr
->chname
, (long) oldts
);
446 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
447 "Server %s changing TS on %s from %ld to 0",
448 source_p
->name
, chptr
->chname
, (long) oldts
);
453 chptr
->channelts
= newts
;
454 else if(newts
== 0 || oldts
== 0)
455 chptr
->channelts
= 0;
456 else if(newts
== oldts
)
458 else if(newts
< oldts
)
461 chptr
->channelts
= newts
;
466 /* Lost the TS, other side wins, so remove modes on this side */
469 set_final_mode(&mode
, &chptr
->mode
);
471 remove_our_modes(chptr
, source_p
);
472 RB_DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->invites
.head
)
474 del_invite(chptr
, ptr
->data
);
476 /* If setting -j, clear join throttle state -- jilles */
477 chptr
->join_count
= chptr
->join_delta
= 0;
478 sendto_channel_local(ALL_MEMBERS
, chptr
,
479 ":%s NOTICE %s :*** Notice -- TS for %s changed from %ld to %ld",
480 me
.name
, chptr
->chname
, chptr
->chname
,
481 (long) oldts
, (long) newts
);
482 /* Update capitalization in channel name, this makes the
483 * capitalization timestamped like modes are -- jilles */
484 strcpy(chptr
->chname
, parv
[2]);
486 sendto_channel_local(ALL_MEMBERS
, chptr
,
488 source_p
->servptr
->name
,
489 chptr
->chname
, modebuf
, parabuf
);
490 *modebuf
= *parabuf
= '\0';
493 if(!IsMember(source_p
, chptr
))
495 add_user_to_channel(chptr
, source_p
, CHFL_PEON
);
496 if (chptr
->mode
.join_num
&&
497 rb_current_time() - chptr
->join_delta
>= chptr
->mode
.join_time
)
499 chptr
->join_count
= 0;
500 chptr
->join_delta
= rb_current_time();
503 sendto_channel_local(ALL_MEMBERS
, chptr
, ":%s!%s@%s JOIN :%s",
504 source_p
->name
, source_p
->username
,
505 source_p
->host
, chptr
->chname
);
508 sendto_server(client_p
, chptr
, CAP_TS6
, NOCAPS
,
510 source_p
->id
, (long) chptr
->channelts
, chptr
->chname
);
511 sendto_server(client_p
, chptr
, NOCAPS
, CAP_TS6
,
512 ":%s SJOIN %ld %s %s :%s",
513 source_p
->servptr
->name
, (long) chptr
->channelts
,
514 chptr
->chname
, keep_new_modes
? "+" : "0",
522 * inputs - pointer to client doing join 0
524 * side effects - Use has decided to join 0. This is legacy
525 * from the days when channels were numbers not names. *sigh*
526 * There is a bunch of evilness necessary here due to
530 do_join_0(struct Client
*client_p
, struct Client
*source_p
)
532 struct membership
*msptr
;
533 struct Channel
*chptr
= NULL
;
536 /* Finish the flood grace period... */
537 if(MyClient(source_p
) && !IsFloodDone(source_p
))
538 flood_endgrace(source_p
);
541 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
, ":%s JOIN 0", use_id(source_p
));
542 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
, ":%s JOIN 0", source_p
->name
);
544 if(source_p
->user
->channel
.head
&& MyConnect(source_p
) &&
545 !IsOper(source_p
) && !IsExemptSpambot(source_p
))
546 check_spambot_warning(source_p
, NULL
);
548 while((ptr
= source_p
->user
->channel
.head
))
551 chptr
= msptr
->chptr
;
552 sendto_channel_local(ALL_MEMBERS
, chptr
, ":%s!%s@%s PART %s",
554 source_p
->username
, source_p
->host
, chptr
->chname
);
555 remove_user_from_channel(msptr
);
560 check_channel_name_loc(struct Client
*source_p
, const char *name
)
562 s_assert(name
!= NULL
);
563 if(EmptyString(name
))
566 if(ConfigFileEntry
.disable_fake_channels
&& !IsOper(source_p
))
570 if(!IsChanChar(*name
) || IsFakeChanChar(*name
))
578 if(!IsChanChar(*name
))
587 set_final_mode(struct Mode
*mode
, struct Mode
*oldmode
)
589 int dir
= MODE_QUERY
;
590 char *pbuf
= parabuf
;
594 /* ok, first get a list of modes we need to add */
595 for(i
= 0; chmode_flags
[i
].letter
; i
++)
597 if((mode
->mode
& chmode_flags
[i
].mode
) && !(oldmode
->mode
& chmode_flags
[i
].mode
))
604 *mbuf
++ = chmode_flags
[i
].letter
;
608 /* now the ones we need to remove. */
609 for(i
= 0; chmode_flags
[i
].letter
; i
++)
611 if((oldmode
->mode
& chmode_flags
[i
].mode
) && !(mode
->mode
& chmode_flags
[i
].mode
))
618 *mbuf
++ = chmode_flags
[i
].letter
;
622 if(oldmode
->limit
&& !mode
->limit
)
631 if(oldmode
->key
[0] && !mode
->key
[0])
639 len
= rb_sprintf(pbuf
, "%s ", oldmode
->key
);
642 if(oldmode
->join_num
&& !mode
->join_num
)
651 if(oldmode
->forward
[0] && !mode
->forward
[0])
660 if(mode
->limit
&& oldmode
->limit
!= mode
->limit
)
668 len
= rb_sprintf(pbuf
, "%d ", mode
->limit
);
671 if(mode
->key
[0] && strcmp(oldmode
->key
, mode
->key
))
679 len
= rb_sprintf(pbuf
, "%s ", mode
->key
);
682 if(mode
->join_num
&& (oldmode
->join_num
!= mode
->join_num
|| oldmode
->join_time
!= mode
->join_time
))
690 len
= rb_sprintf(pbuf
, "%d:%d ", mode
->join_num
, mode
->join_time
);
693 if(mode
->forward
[0] && strcmp(oldmode
->forward
, mode
->forward
) && ConfigChannel
.use_forward
)
701 len
= rb_sprintf(pbuf
, "%s ", mode
->forward
);
715 remove_our_modes(struct Channel
*chptr
, struct Client
*source_p
)
717 struct membership
*msptr
;
719 char lmodebuf
[MODEBUFLEN
];
720 char *lpara
[MAXMODEPARAMS
];
727 for(i
= 0; i
< MAXMODEPARAMS
; i
++)
730 RB_DLINK_FOREACH(ptr
, chptr
->members
.head
)
736 msptr
->flags
&= ~CHFL_CHANOP
;
737 lpara
[count
++] = msptr
->client_p
->name
;
740 /* +ov, might not fit so check. */
743 if(count
>= MAXMODEPARAMS
)
746 sendto_channel_local(ALL_MEMBERS
, chptr
,
747 ":%s MODE %s %s %s %s %s %s",
748 me
.name
, chptr
->chname
,
749 lmodebuf
, lpara
[0], lpara
[1],
752 /* preserve the initial '-' */
757 for(i
= 0; i
< MAXMODEPARAMS
; i
++)
761 msptr
->flags
&= ~CHFL_VOICE
;
762 lpara
[count
++] = msptr
->client_p
->name
;
766 else if(is_voiced(msptr
))
768 msptr
->flags
&= ~CHFL_VOICE
;
769 lpara
[count
++] = msptr
->client_p
->name
;
775 if(count
>= MAXMODEPARAMS
)
778 sendto_channel_local(ALL_MEMBERS
, chptr
,
779 ":%s MODE %s %s %s %s %s %s",
780 me
.name
, chptr
->chname
, lmodebuf
,
781 lpara
[0], lpara
[1], lpara
[2], lpara
[3]);
786 for(i
= 0; i
< MAXMODEPARAMS
; i
++)
794 sendto_channel_local(ALL_MEMBERS
, chptr
,
795 ":%s MODE %s %s %s %s %s %s",
796 me
.name
, chptr
->chname
, lmodebuf
,
797 EmptyString(lpara
[0]) ? "" : lpara
[0],
798 EmptyString(lpara
[1]) ? "" : lpara
[1],
799 EmptyString(lpara
[2]) ? "" : lpara
[2],
800 EmptyString(lpara
[3]) ? "" : lpara
[3]);