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 $
33 #include "irc_string.h"
39 #include "s_newconf.h"
43 #include "sprintf_irc.h"
46 static int m_join(struct Client
*, struct Client
*, int, const char **);
47 static int ms_join(struct Client
*, struct Client
*, int, const char **);
49 static int h_can_create_channel
;
50 static int h_channel_join
;
52 struct Message join_msgtab
= {
53 "JOIN", 0, 0, 0, MFLG_SLOW
,
54 {mg_unreg
, {m_join
, 2}, {ms_join
, 2}, mg_ignore
, mg_ignore
, {m_join
, 2}}
57 mapi_clist_av1 join_clist
[] = { &join_msgtab
, NULL
};
59 mapi_hlist_av1 join_hlist
[] = {
60 { "can_create_channel", &h_can_create_channel
},
61 { "channel_join", &h_channel_join
},
65 DECLARE_MODULE_AV1(join
, NULL
, NULL
, join_clist
, join_hlist
, NULL
, "$Revision: 3494 $");
67 static void do_join_0(struct Client
*client_p
, struct Client
*source_p
);
68 static int check_channel_name_loc(struct Client
*source_p
, const char *name
);
70 static void set_final_mode(struct Mode
*mode
, struct Mode
*oldmode
);
71 static void remove_our_modes(struct Channel
*chptr
, struct Client
*source_p
);
73 static char modebuf
[MODEBUFLEN
];
74 static char parabuf
[MODEBUFLEN
];
77 /* Check what we will forward to, without sending any notices to the user
80 static struct Channel
*
81 check_forward(struct Client
*source_p
, struct Channel
*chptr
,
87 if (IsNoForward(source_p
))
92 chptr
= find_channel(chptr
->mode
.forward
);
93 /* Can only forward to existing channels */
96 /* Already on there, show original error message */
97 if (IsMember(source_p
, chptr
))
99 /* Juped. Sending a warning notice would be unfair */
100 if (hash_find_resv(chptr
->chname
))
102 /* Don't forward to +Q channel */
103 if (chptr
->mode
.mode
& MODE_DISFORWARD
)
105 i
= can_join(source_p
, chptr
, key
);
108 if (i
!= ERR_INVITEONLYCHAN
&& i
!= ERR_NEEDREGGEDNICK
&& i
!= ERR_THROTTLE
&& i
!= ERR_CHANNELISFULL
)
118 * parv[0] = sender prefix
120 * parv[2] = channel password (key)
123 m_join(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
125 static char jbuf
[BUFSIZE
];
126 struct Channel
*chptr
= NULL
;
127 struct ConfItem
*aconf
;
131 char *p
= NULL
, *p2
= NULL
;
134 int successful_join_count
= 0; /* Number of channels successfully joined */
138 /* rebuild the list of channels theyre supposed to be joining.
139 * this code has a side effect of losing keys, but..
141 chanlist
= LOCAL_COPY(parv
[1]);
142 for(name
= strtoken(&p
, chanlist
, ","); name
; name
= strtoken(&p
, NULL
, ","))
144 /* check the length and name of channel is ok */
145 if(!check_channel_name_loc(source_p
, name
) || (strlen(name
) > LOC_CHANNELLEN
))
147 sendto_one_numeric(source_p
, ERR_BADCHANNAME
,
148 form_str(ERR_BADCHANNAME
), (unsigned char *) name
);
152 /* join 0 parts all channels */
153 if(*name
== '0' && (name
[1] == ',' || name
[1] == '\0') && name
== chanlist
)
155 (void) strcpy(jbuf
, "0");
159 /* check it begins with # or &, and local chans are disabled */
160 else if(!IsChannelName(name
))
162 sendto_one_numeric(source_p
, ERR_NOSUCHCHANNEL
,
163 form_str(ERR_NOSUCHCHANNEL
), name
);
167 /* see if its resv'd */
168 if(!IsExemptResv(source_p
) && (aconf
= hash_find_resv(name
)))
170 sendto_one_numeric(source_p
, ERR_BADCHANNAME
,
171 form_str(ERR_BADCHANNAME
), name
);
173 /* dont warn for opers */
174 if(!IsExemptJupe(source_p
) && !IsOper(source_p
))
175 sendto_realops_snomask(SNO_SPY
, L_NETWIDE
,
176 "User %s (%s@%s) is attempting to join locally juped channel %s (%s)",
177 source_p
->name
, source_p
->username
,
178 source_p
->orighost
, name
, aconf
->passwd
);
179 /* dont update tracking for jupe exempt users, these
180 * are likely to be spamtrap leaves
182 else if(IsExemptJupe(source_p
))
188 if(splitmode
&& !IsOper(source_p
) && (*name
!= '&') &&
189 ConfigChannel
.no_join_on_split
)
191 sendto_one(source_p
, form_str(ERR_UNAVAILRESOURCE
),
192 me
.name
, source_p
->name
, name
);
197 (void) strcat(jbuf
, ",");
198 (void) strlcat(jbuf
, name
, sizeof(jbuf
));
203 mykey
= LOCAL_COPY(parv
[2]);
204 key
= strtoken(&p2
, mykey
, ",");
207 for(name
= strtoken(&p
, jbuf
, ","); name
;
208 key
= (key
) ? strtoken(&p2
, NULL
, ",") : NULL
, name
= strtoken(&p
, NULL
, ","))
210 hook_data_channel_activity hook_info
;
212 /* JOIN 0 simply parts all channels the user is in */
213 if(*name
== '0' && !atoi(name
))
215 if(source_p
->user
->channel
.head
== NULL
)
218 do_join_0(&me
, source_p
);
222 /* look for the channel */
223 if((chptr
= find_channel(name
)) != NULL
)
225 if(IsMember(source_p
, chptr
))
232 hook_data_client_approval moduledata
;
234 moduledata
.client
= source_p
;
235 moduledata
.approved
= 0;
237 call_hook(h_can_create_channel
, &moduledata
);
239 if(moduledata
.approved
!= 0)
241 sendto_one(source_p
, form_str(moduledata
.approved
),
242 me
.name
, source_p
->name
, name
);
246 if(splitmode
&& !IsOper(source_p
) && (*name
!= '&') &&
247 ConfigChannel
.no_create_on_split
)
249 sendto_one(source_p
, form_str(ERR_UNAVAILRESOURCE
),
250 me
.name
, source_p
->name
, name
);
257 if((dlink_list_length(&source_p
->user
->channel
) >=
258 (unsigned long) ConfigChannel
.max_chans_per_user
) &&
259 (!IsOper(source_p
) ||
260 (dlink_list_length(&source_p
->user
->channel
) >=
261 (unsigned long) ConfigChannel
.max_chans_per_user
* 3)))
263 sendto_one(source_p
, form_str(ERR_TOOMANYCHANNELS
),
264 me
.name
, source_p
->name
, name
);
265 if(successful_join_count
)
266 source_p
->localClient
->last_join_time
= CurrentTime
;
270 if(flags
== 0) /* if channel doesn't exist, don't penalize */
271 successful_join_count
++;
273 if(chptr
== NULL
) /* If I already have a chptr, no point doing this */
275 chptr
= get_or_create_channel(source_p
, name
, NULL
);
279 sendto_one(source_p
, form_str(ERR_UNAVAILRESOURCE
),
280 me
.name
, source_p
->name
, name
);
281 if(successful_join_count
> 0)
282 successful_join_count
--;
287 if(!IsOper(source_p
) && !IsExemptSpambot(source_p
))
288 check_spambot_warning(source_p
, name
);
290 /* can_join checks for +i key, bans etc */
291 if((i
= can_join(source_p
, chptr
, key
)))
293 if ((i
!= ERR_NEEDREGGEDNICK
&& i
!= ERR_THROTTLE
&& i
!= ERR_INVITEONLYCHAN
&& i
!= ERR_CHANNELISFULL
) ||
294 (!ConfigChannel
.use_forward
|| (chptr
= check_forward(source_p
, chptr
, key
)) == NULL
))
296 sendto_one(source_p
, form_str(i
), me
.name
, source_p
->name
, name
);
297 if(successful_join_count
> 0)
298 successful_join_count
--;
301 sendto_one_numeric(source_p
, ERR_LINKCHANNEL
, form_str(ERR_LINKCHANNEL
), name
, chptr
->chname
);
304 /* add the user to the channel */
305 add_user_to_channel(chptr
, source_p
, flags
);
306 if (chptr
->mode
.join_num
&&
307 CurrentTime
- chptr
->join_delta
>= chptr
->mode
.join_time
)
309 chptr
->join_count
= 0;
310 chptr
->join_delta
= CurrentTime
;
314 /* we send the user their join here, because we could have to
315 * send a mode out next.
317 sendto_channel_local(ALL_MEMBERS
, chptr
, ":%s!%s@%s JOIN :%s",
319 source_p
->username
, source_p
->host
, chptr
->chname
);
321 /* its a new channel, set +nt and burst. */
322 if(flags
& CHFL_CHANOP
)
324 chptr
->channelts
= CurrentTime
;
325 chptr
->mode
.mode
|= MODE_TOPICLIMIT
;
326 chptr
->mode
.mode
|= MODE_NOPRIVMSGS
;
328 sendto_channel_local(ONLY_CHANOPS
, chptr
, ":%s MODE %s +nt",
329 me
.name
, chptr
->chname
);
331 if(*chptr
->chname
== '#')
333 sendto_server(client_p
, chptr
, CAP_TS6
, NOCAPS
,
334 ":%s SJOIN %ld %s +nt :@%s",
335 me
.id
, (long) chptr
->channelts
,
336 chptr
->chname
, source_p
->id
);
337 sendto_server(client_p
, chptr
, NOCAPS
, CAP_TS6
,
338 ":%s SJOIN %ld %s +nt :@%s",
339 me
.name
, (long) chptr
->channelts
,
340 chptr
->chname
, source_p
->name
);
345 sendto_server(client_p
, chptr
, CAP_TS6
, NOCAPS
,
347 use_id(source_p
), (long) chptr
->channelts
,
350 sendto_server(client_p
, chptr
, NOCAPS
, CAP_TS6
,
351 ":%s SJOIN %ld %s + :%s",
352 me
.name
, (long) chptr
->channelts
,
353 chptr
->chname
, source_p
->name
);
356 del_invite(chptr
, source_p
);
358 if(chptr
->topic
!= NULL
)
360 sendto_one(source_p
, form_str(RPL_TOPIC
), me
.name
,
361 source_p
->name
, chptr
->chname
, chptr
->topic
);
363 sendto_one(source_p
, form_str(RPL_TOPICWHOTIME
),
364 me
.name
, source_p
->name
, chptr
->chname
,
365 chptr
->topic_info
, chptr
->topic_time
);
368 channel_member_names(chptr
, source_p
, 1);
370 if(successful_join_count
)
371 source_p
->localClient
->last_join_time
= CurrentTime
;
373 hook_info
.client
= source_p
;
374 hook_info
.chptr
= chptr
;
376 call_hook(h_channel_join
, &hook_info
);
387 * side effects - handles remote JOIN's sent by servers. In TSora
388 * remote clients are joined using SJOIN, hence a
389 * JOIN sent by a server on behalf of a client is an error.
390 * here, the initial code is in to take an extra parameter
391 * and use it for the TimeStamp on a new channel.
394 ms_join(struct Client
*client_p
, struct Client
*source_p
, int parc
, const char *parv
[])
396 struct Channel
*chptr
;
397 static struct Mode mode
;
401 int keep_our_modes
= YES
;
402 int keep_new_modes
= YES
;
403 dlink_node
*ptr
, *next_ptr
;
405 /* special case for join 0 */
406 if((parv
[1][0] == '0') && (parv
[1][1] == '\0') && parc
== 2)
408 do_join_0(client_p
, source_p
);
415 if(!IsChannelName(parv
[2]) || !check_channel_name(parv
[2]))
418 /* joins for local channels cant happen. */
419 if(parv
[2][0] == '&')
423 mode
.key
[0] = mode
.forward
[0] = '\0';
424 mode
.mode
= mode
.limit
= mode
.join_num
= mode
.join_time
= 0;
426 if((chptr
= get_or_create_channel(source_p
, parv
[2], &isnew
)) == NULL
)
429 newts
= atol(parv
[1]);
430 oldts
= chptr
->channelts
;
432 #ifdef IGNORE_BOGUS_TS
433 if(newts
< 800000000)
435 sendto_realops_snomask(SNO_DEBUG
, L_ALL
,
436 "*** Bogus TS %ld on %s ignored from %s",
437 (long) newts
, chptr
->chname
, client_p
->name
);
438 newts
= (oldts
== 0) ? oldts
: 800000000;
441 /* making a channel TS0 */
442 if(!isnew
&& !newts
&& oldts
)
444 sendto_channel_local(ALL_MEMBERS
, chptr
,
445 ":%s NOTICE %s :*** Notice -- TS for %s changed from %ld to 0",
446 me
.name
, chptr
->chname
, chptr
->chname
, (long) oldts
);
447 sendto_realops_snomask(SNO_GENERAL
, L_ALL
,
448 "Server %s changing TS on %s from %ld to 0",
449 source_p
->name
, chptr
->chname
, (long) oldts
);
454 chptr
->channelts
= newts
;
455 else if(newts
== 0 || oldts
== 0)
456 chptr
->channelts
= 0;
457 else if(newts
== oldts
)
459 else if(newts
< oldts
)
462 chptr
->channelts
= newts
;
467 /* Lost the TS, other side wins, so remove modes on this side */
470 set_final_mode(&mode
, &chptr
->mode
);
472 remove_our_modes(chptr
, source_p
);
473 DLINK_FOREACH_SAFE(ptr
, next_ptr
, chptr
->invites
.head
)
475 del_invite(chptr
, ptr
->data
);
477 /* If setting -j, clear join throttle state -- jilles */
478 chptr
->join_count
= chptr
->join_delta
= 0;
479 sendto_channel_local(ALL_MEMBERS
, chptr
,
480 ":%s NOTICE %s :*** Notice -- TS for %s changed from %ld to %ld",
481 me
.name
, chptr
->chname
, chptr
->chname
,
482 (long) oldts
, (long) newts
);
483 /* Update capitalization in channel name, this makes the
484 * capitalization timestamped like modes are -- jilles */
485 strcpy(chptr
->chname
, parv
[2]);
487 sendto_channel_local(ALL_MEMBERS
, chptr
,
489 source_p
->servptr
->name
,
490 chptr
->chname
, modebuf
, parabuf
);
491 *modebuf
= *parabuf
= '\0';
494 if(!IsMember(source_p
, chptr
))
496 add_user_to_channel(chptr
, source_p
, CHFL_PEON
);
497 if (chptr
->mode
.join_num
&&
498 CurrentTime
- chptr
->join_delta
>= chptr
->mode
.join_time
)
500 chptr
->join_count
= 0;
501 chptr
->join_delta
= CurrentTime
;
504 sendto_channel_local(ALL_MEMBERS
, chptr
, ":%s!%s@%s JOIN :%s",
505 source_p
->name
, source_p
->username
,
506 source_p
->host
, chptr
->chname
);
509 sendto_server(client_p
, chptr
, CAP_TS6
, NOCAPS
,
511 source_p
->id
, (long) chptr
->channelts
, chptr
->chname
);
512 sendto_server(client_p
, chptr
, NOCAPS
, CAP_TS6
,
513 ":%s SJOIN %ld %s %s :%s",
514 source_p
->servptr
->name
, (long) chptr
->channelts
,
515 chptr
->chname
, keep_new_modes
? "+" : "0",
523 * inputs - pointer to client doing join 0
525 * side effects - Use has decided to join 0. This is legacy
526 * from the days when channels were numbers not names. *sigh*
527 * There is a bunch of evilness necessary here due to
531 do_join_0(struct Client
*client_p
, struct Client
*source_p
)
533 struct membership
*msptr
;
534 struct Channel
*chptr
= NULL
;
537 /* Finish the flood grace period... */
538 if(MyClient(source_p
) && !IsFloodDone(source_p
))
539 flood_endgrace(source_p
);
542 sendto_server(client_p
, NULL
, CAP_TS6
, NOCAPS
, ":%s JOIN 0", use_id(source_p
));
543 sendto_server(client_p
, NULL
, NOCAPS
, CAP_TS6
, ":%s JOIN 0", source_p
->name
);
545 if(source_p
->user
->channel
.head
&& MyConnect(source_p
) &&
546 !IsOper(source_p
) && !IsExemptSpambot(source_p
))
547 check_spambot_warning(source_p
, NULL
);
549 while((ptr
= source_p
->user
->channel
.head
))
552 chptr
= msptr
->chptr
;
553 sendto_channel_local(ALL_MEMBERS
, chptr
, ":%s!%s@%s PART %s",
555 source_p
->username
, source_p
->host
, chptr
->chname
);
556 remove_user_from_channel(msptr
);
561 check_channel_name_loc(struct Client
*source_p
, const char *name
)
563 s_assert(name
!= NULL
);
564 if(EmptyString(name
))
567 if(ConfigFileEntry
.disable_fake_channels
&& !IsOper(source_p
))
571 if(!IsChanChar(*name
) || IsFakeChanChar(*name
))
579 if(!IsChanChar(*name
))
593 static struct mode_letter flags
[] = {
594 {MODE_NOPRIVMSGS
, 'n'},
595 {MODE_TOPICLIMIT
, 't'},
597 {MODE_MODERATED
, 'm'},
598 {MODE_INVITEONLY
, 'i'},
602 {MODE_PERMANENT
, 'P'},
604 {MODE_FREEINVITE
, 'g'},
605 {MODE_OPMODERATE
, 'z'},
606 {MODE_FREETARGET
, 'F'},
607 {MODE_DISFORWARD
, 'Q'},
612 set_final_mode(struct Mode
*mode
, struct Mode
*oldmode
)
614 int dir
= MODE_QUERY
;
615 char *pbuf
= parabuf
;
619 /* ok, first get a list of modes we need to add */
620 for(i
= 0; flags
[i
].letter
; i
++)
622 if((mode
->mode
& flags
[i
].mode
) && !(oldmode
->mode
& flags
[i
].mode
))
629 *mbuf
++ = flags
[i
].letter
;
633 /* now the ones we need to remove. */
634 for(i
= 0; flags
[i
].letter
; i
++)
636 if((oldmode
->mode
& flags
[i
].mode
) && !(mode
->mode
& flags
[i
].mode
))
643 *mbuf
++ = flags
[i
].letter
;
647 if(oldmode
->limit
&& !mode
->limit
)
656 if(oldmode
->key
[0] && !mode
->key
[0])
664 len
= ircsprintf(pbuf
, "%s ", oldmode
->key
);
667 if(oldmode
->join_num
&& !mode
->join_num
)
676 if(oldmode
->forward
[0] && !mode
->forward
[0])
685 if(mode
->limit
&& oldmode
->limit
!= mode
->limit
)
693 len
= ircsprintf(pbuf
, "%d ", mode
->limit
);
696 if(mode
->key
[0] && strcmp(oldmode
->key
, mode
->key
))
704 len
= ircsprintf(pbuf
, "%s ", mode
->key
);
707 if(mode
->join_num
&& (oldmode
->join_num
!= mode
->join_num
|| oldmode
->join_time
!= mode
->join_time
))
715 len
= ircsprintf(pbuf
, "%d:%d ", mode
->join_num
, mode
->join_time
);
718 if(mode
->forward
[0] && strcmp(oldmode
->forward
, mode
->forward
) && ConfigChannel
.use_forward
)
726 len
= ircsprintf(pbuf
, "%s ", mode
->forward
);
740 remove_our_modes(struct Channel
*chptr
, struct Client
*source_p
)
742 struct membership
*msptr
;
744 char lmodebuf
[MODEBUFLEN
];
745 char *lpara
[MAXMODEPARAMS
];
752 for(i
= 0; i
< MAXMODEPARAMS
; i
++)
755 DLINK_FOREACH(ptr
, chptr
->members
.head
)
761 msptr
->flags
&= ~CHFL_CHANOP
;
762 lpara
[count
++] = msptr
->client_p
->name
;
765 /* +ov, might not fit so check. */
768 if(count
>= MAXMODEPARAMS
)
771 sendto_channel_local(ALL_MEMBERS
, chptr
,
772 ":%s MODE %s %s %s %s %s %s",
773 me
.name
, chptr
->chname
,
774 lmodebuf
, lpara
[0], lpara
[1],
777 /* preserve the initial '-' */
782 for(i
= 0; i
< MAXMODEPARAMS
; i
++)
786 msptr
->flags
&= ~CHFL_VOICE
;
787 lpara
[count
++] = msptr
->client_p
->name
;
791 else if(is_voiced(msptr
))
793 msptr
->flags
&= ~CHFL_VOICE
;
794 lpara
[count
++] = msptr
->client_p
->name
;
800 if(count
>= MAXMODEPARAMS
)
803 sendto_channel_local(ALL_MEMBERS
, chptr
,
804 ":%s MODE %s %s %s %s %s %s",
805 me
.name
, chptr
->chname
, lmodebuf
,
806 lpara
[0], lpara
[1], lpara
[2], lpara
[3]);
811 for(i
= 0; i
< MAXMODEPARAMS
; i
++)
819 sendto_channel_local(ALL_MEMBERS
, chptr
,
820 ":%s MODE %s %s %s %s %s %s",
821 me
.name
, chptr
->chname
, lmodebuf
,
822 EmptyString(lpara
[0]) ? "" : lpara
[0],
823 EmptyString(lpara
[1]) ? "" : lpara
[1],
824 EmptyString(lpara
[2]) ? "" : lpara
[2],
825 EmptyString(lpara
[3]) ? "" : lpara
[3]);