2 * IRC - Internet Relay Chat, ircd/m_join.c
3 * Copyright (C) 1990 Jarkko Oikarinen and
4 * University of Oulu, Computing Center
6 * See file AUTHORS in IRC package for additional names of
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)
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., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * m_functions execute protocol messages on this server:
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...).
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.
39 * (!IsServer(cptr)) => (cptr == sptr), because
40 * prefixes are taken *only* from servers...
43 * (sptr == cptr) => the message didn't
46 * (sptr != cptr && IsServer(sptr) means
47 * the prefix specified servername. (?)
49 * (sptr != cptr && !IsServer(sptr) means
50 * that message originated from a remote
55 * (!IsServer(sptr)) means that, sptr can safely
56 * taken as defining the target structure of the
57 * message in this server.
59 * *Always* true (if 'parse' and others are working correct):
61 * 1) sptr->from == cptr (note: cptr->from == cptr)
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 ]
68 * parc number of variable parameter strings (if zero,
69 * parv is allowed to be NULL)
71 * parv a NULL terminated list of parameter pointers,
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*
79 * note: it is guaranteed that parv[0]..parv[parc-1] are all
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
95 #include "ircd_chattr.h"
96 #include "ircd_reply.h"
97 #include "ircd_string.h"
100 #include "numnicks.h"
108 #if !defined(XXX_BOGUS_TEMP_HACK)
109 #include "handlers.h" /* m_names */
113 * m_join - generic message handler
115 int m_join(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
117 static char jbuf[BUFSIZE];
118 static char mbuf[BUFSIZE];
119 struct Membership* member;
120 struct Channel* chptr;
122 char* keysOrTS = NULL;
126 unsigned int flags = 0;
134 if (parc < 2 || *parv[1] == '\0')
135 return need_more_params(sptr, "JOIN");
137 for (p = parv[1]; *p; p++) /* find the last "JOIN 0" in the line -Kev */
139 && (*(p + 1) == ',' || *(p + 1) == '\0' || !IsChannelChar(*(p + 1))))
141 /* If it's a single "0", remember the place; we will start parsing
142 the channels after the last 0 in the line -Kev */
149 { /* Step through to the next comma or until the
150 end of the line, in an attempt to save CPU
152 while (*p != ',' && *p != '\0')
158 keysOrTS = parv[2]; /* Remember where our keys are or the TS is;
159 parv[2] needs to be NULL for the call to
160 m_names below -Kev */
163 *jbuf = *mbuf = '\0'; /* clear both join and mode buffers -Kev */
165 * Rebuild list of channels joined to be the actual result of the
166 * JOIN. Note that "JOIN 0" is the destructive problem.
168 for (name = ircd_strtok(&p, parv[1], ","); name; name = ircd_strtok(&p, NULL, ","))
172 clean_channelname(name);
173 else if (IsLocalChannel(name))
175 if (*name == '0' && *(name + 1) == '\0')
177 /* Remove the user from all his channels -Kev */
178 while ((member = sptr->user->channel))
180 chptr = member->channel;
181 if (!IsZombie(member))
182 sendto_channel_butserv(chptr, sptr, PartFmt2,
183 parv[0], chptr->chname, "Left all channels");
184 remove_user_from_channel(sptr, chptr);
187 *mbuf = *jbuf = '\0';
191 { /* not a /join 0, so treat it as
192 a /join #channel -Kev */
193 if (!IsChannelName(name))
196 sendto_one(sptr, err_str(ERR_NOSUCHCHANNEL), me.name, parv[0], name);
205 if ((gline = gline_find(name, GLINE_BADCHAN | GLINE_EXACT)) &&
206 GlineIsActive(gline) && !IsAnOper(sptr))
208 sendto_one(sptr, err_str(ERR_BADCHANNAME), me.name, parv[0], name);
213 * Local client is first to enter previously nonexistant
214 * channel so make them (rightfully) the Channel Operator.
215 * This looks kind of ugly because we try to avoid calling the strlen()
217 if (ChannelExists(name))
219 flags = CHFL_DEOPPED;
222 else if (strlen(name) > CHANNELLEN)
224 *(name + CHANNELLEN) = '\0';
225 if (ChannelExists(name))
227 flags = CHFL_DEOPPED;
232 flags = IsModelessChannel(name) ? CHFL_DEOPPED : CHFL_CHANOP;
238 flags = IsModelessChannel(name) ? CHFL_DEOPPED : CHFL_CHANOP;
242 #ifdef OPER_NO_CHAN_LIMIT
244 * Opers are allowed to join any number of channels
246 if (sptr->user->joined >= MAXCHANNELSPERUSER && !IsAnOper(sptr))
248 if (sptr->user->joined >= MAXCHANNELSPERUSER)
251 chptr = get_channel(sptr, name, CGT_NO_CREATE);
252 sendto_one(sptr, err_str(ERR_TOOMANYCHANNELS),
253 me.name, parv[0], chptr ? chptr->chname : name);
255 * Can't return, else he won't get on ANY channels!
256 * Break out of the for loop instead. -Kev
261 chptr = get_channel(sptr, name, CGT_CREATE);
262 if (chptr && (member = find_member_link(chptr, sptr)))
264 if (IsZombie(member))
267 flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
268 remove_user_from_channel(sptr, chptr);
269 chptr = get_channel(sptr, name, CGT_CREATE);
274 name = chptr->chname;
275 if (!chptr->creationtime) /* A remote JOIN created this channel ? */
276 chptr->creationtime = MAGIC_REMOTE_JOIN_TS;
279 if (chptr->creationtime == MAGIC_REMOTE_JOIN_TS)
280 chptr->creationtime = atoi(keysOrTS);
282 parc = 2; /* Don't pass it on */
286 if (!MyConnect(sptr))
287 flags = CHFL_DEOPPED;
288 if (sptr->flags & FLAGS_TS8)
289 flags |= CHFL_SERVOPOK;
293 int created = chptr->users == 0;
294 if (check_target_limit(sptr, chptr, chptr->chname, created))
296 if (created) /* Did we create the channel? */
297 sub1_from_channel(chptr); /* Remove it again! */
300 if ((i = can_join(sptr, chptr, keysOrTS)))
302 #ifdef OPER_WALK_THROUGH_LMODES
303 if (i > MAGIC_OPER_OVERRIDE)
305 switch(i - MAGIC_OPER_OVERRIDE)
307 case ERR_CHANNELISFULL: i = 'l'; break;
308 case ERR_INVITEONLYCHAN: i = 'i'; break;
309 case ERR_BANNEDFROMCHAN: i = 'b'; break;
310 case ERR_BADCHANNELKEY: i = 'k'; break;
312 sendto_op_mask(SNO_HACK4,"OPER JOIN: %s JOIN %s (overriding +%c)",sptr->name,chptr->chname,i);
316 sendto_one(sptr, err_str(i), me.name, parv[0], chptr->chname);
320 sendto_one(sptr, err_str(i), me.name, parv[0], chptr->chname);
326 * Complete user entry to the new channel (if any)
328 add_user_to_channel(chptr, sptr, flags);
331 * Notify all other users on the new channel
333 sendto_channel_butserv(chptr, sptr, ":%s JOIN :%s", parv[0], name);
337 del_invite(sptr, chptr);
338 if (chptr->topic[0] != '\0')
340 sendto_one(sptr, rpl_str(RPL_TOPIC), me.name,
341 parv[0], name, chptr->topic);
342 sendto_one(sptr, rpl_str(RPL_TOPICWHOTIME), me.name, parv[0], name,
343 chptr->topic_nick, chptr->topic_time);
346 m_names(cptr, sptr, 2, parv);
350 /* Select proper buffer; mbuf for creation, jbuf otherwise */
353 continue; /* Head off local channels at the pass */
355 bufptr = (sendcreate == 0) ? jbuf : mbuf;
356 buflen = (sendcreate == 0) ? &jlen : &mlen;
358 if (*buflen < BUFSIZE - len - 2)
362 strcat(bufptr, ","); /* Add to join buf */
365 strncat(bufptr, name, BUFSIZE - *buflen - 1);
368 sendcreate = 0; /* Reset sendcreate */
371 if (*jbuf) /* Propgate joins to P10 servers */
372 sendto_serv_butone(cptr,
373 parc > 2 ? "%s%s " TOK_JOIN " %s %s" : "%s%s " TOK_JOIN " %s", NumNick(sptr), jbuf, keysOrTS);
374 if (*mbuf) /* and now creation events */
375 sendto_serv_butone(cptr, "%s%s " TOK_CREATE " %s " TIME_T_FMT,
376 NumNick(sptr), mbuf, TStime());
379 { /* shouldn't ever set TS for remote JOIN's */
381 { /* check for channels that need TS's */
383 for (name = ircd_strtok(&p, jbuf, ","); name; name = ircd_strtok(&p, NULL, ","))
385 chptr = get_channel(sptr, name, CGT_NO_CREATE);
386 if (chptr && chptr->mode.mode & MODE_SENDTS)
388 sendto_serv_butone(cptr, "%s " TOK_MODE " %s + " TIME_T_FMT, NumServ(&me),
389 chptr->chname, chptr->creationtime); /* ok, send TS */
390 chptr->mode.mode &= ~MODE_SENDTS; /* reset flag */
399 * ms_join - server message handler
401 int ms_join(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
403 static char jbuf[BUFSIZE];
404 static char mbuf[BUFSIZE];
405 struct Membership* member;
406 struct Channel* chptr;
408 char* keysOrTS = NULL;
411 unsigned int flags = 0;
419 * Doesn't make sense having a server join a channel, and besides
425 if (parc < 2 || *parv[1] == '\0')
426 return need_more_params(sptr, "JOIN");
428 keysOrTS = parv[2]; /* Remember where our keys are or the TS is;
429 parv[2] needs to be NULL for the call to
430 m_names below -Kev */
433 *jbuf = *mbuf = '\0'; /* clear both join and mode buffers -Kev */
435 * Rebuild list of channels joined to be the actual result of the
436 * JOIN. Note that "JOIN 0" is the destructive problem.
438 for (name = ircd_strtok(&p, parv[1], ","); name; name = ircd_strtok(&p, NULL, ","))
441 if (IsLocalChannel(name))
444 if (!IsChannelName(name))
446 sendto_one(sptr, err_str(ERR_NOSUCHCHANNEL), me.name, parv[0], name);
450 chptr = get_channel(sptr, name, CGT_CREATE);
451 if (chptr && (member = find_member_link(chptr, sptr)))
453 if (!IsZombie(member))
456 /* If they are a zombie, make them really part
460 flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
461 remove_user_from_channel(sptr, chptr);
462 chptr = get_channel(sptr, name, CGT_CREATE);
466 name = chptr->chname;
468 if (!chptr->creationtime) /* A remote JOIN created this channel ? */
469 chptr->creationtime = MAGIC_REMOTE_JOIN_TS;
473 if (chptr->creationtime == MAGIC_REMOTE_JOIN_TS)
474 chptr->creationtime = atoi(keysOrTS);
476 parc = 2; /* Don't pass it on */
481 if (sptr->flags & FLAGS_TS8)
482 flags |= CHFL_SERVOPOK;
486 * Complete user entry to the new channel (if any)
488 add_user_to_channel(chptr, sptr, flags);
491 * Notify all other users on the new channel
493 sendto_channel_butserv(chptr, sptr, ":%s JOIN :%s", parv[0], name);
495 /* Select proper buffer; mbuf for creation, jbuf otherwise */
498 continue; /* Head off local channels at the pass */
500 bufptr = (sendcreate == 0) ? jbuf : mbuf;
501 buflen = (sendcreate == 0) ? &jlen : &mlen;
504 if (*buflen < BUFSIZE - len - 2)
508 strcat(bufptr, ","); /* Add to join buf */
511 strncat(bufptr, name, BUFSIZE - *buflen - 1);
514 sendcreate = 0; /* Reset sendcreate */
517 if (*jbuf) /* Propgate joins to P10 servers */
518 sendto_serv_butone(cptr,
519 parc > 2 ? "%s%s " TOK_JOIN " %s %s" : "%s%s " TOK_JOIN " %s", NumNick(sptr), jbuf, keysOrTS);
520 if (*mbuf) /* and now creation events */
521 sendto_serv_butone(cptr, "%s%s " TOK_CREATE " %s " TIME_T_FMT,
522 NumNick(sptr), mbuf, TStime());
532 * parv[0] = sender prefix
534 * parv[2] = channel keys (client), or channel TS (server)
536 int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
538 static char jbuf[BUFSIZE];
539 static char mbuf[BUFSIZE];
540 struct Membership* member;
541 struct Channel* chptr;
543 char* keysOrTS = NULL;
547 unsigned int flags = 0;
555 * Doesn't make sense having a server join a channel, and besides
561 if (parc < 2 || *parv[1] == '\0')
562 return need_more_params(sptr, "JOIN");
564 for (p = parv[1]; *p; p++) /* find the last "JOIN 0" in the line -Kev */
566 && (*(p + 1) == ',' || *(p + 1) == '\0' || !IsChannelChar(*(p + 1))))
568 /* If it's a single "0", remember the place; we will start parsing
569 the channels after the last 0 in the line -Kev */
576 { /* Step through to the next comma or until the
577 end of the line, in an attempt to save CPU
579 while (*p != ',' && *p != '\0')
585 keysOrTS = parv[2]; /* Remember where our keys are or the TS is;
586 parv[2] needs to be NULL for the call to
587 m_names below -Kev */
590 *jbuf = *mbuf = '\0'; /* clear both join and mode buffers -Kev */
592 * Rebuild list of channels joined to be the actual result of the
593 * JOIN. Note that "JOIN 0" is the destructive problem.
595 for (name = ircd_strtok(&p, parv[1], ","); name; name = ircd_strtok(&p, NULL, ","))
599 clean_channelname(name);
600 else if (IsLocalChannel(name))
602 if (*name == '0' && *(name + 1) == '\0')
604 /* Remove the user from all his channels -Kev */
605 while ((member = sptr->user->channel))
607 chptr = member->channel;
608 if (!IsZombie(member))
609 sendto_channel_butserv(chptr, sptr, PartFmt2,
610 parv[0], chptr->chname, "Left all channels");
611 remove_user_from_channel(sptr, chptr);
614 *mbuf = *jbuf = '\0';
618 { /* not a /join 0, so treat it as
619 a /join #channel -Kev */
620 if (!IsChannelName(name))
623 sendto_one(sptr, err_str(ERR_NOSUCHCHANNEL), me.name, parv[0], name);
632 if ((gline = gline_find(name, GLINE_BADCHAN | GLINE_EXACT)) &&
633 GlineIsActive(gline) && !IsAnOper(sptr))
635 sendto_one(sptr, err_str(ERR_BADCHANNAME), me.name, parv[0], name);
640 * Local client is first to enter previously nonexistant
641 * channel so make them (rightfully) the Channel Operator.
642 * This looks kind of ugly because we try to avoid calling the strlen()
644 if (ChannelExists(name))
646 flags = CHFL_DEOPPED;
649 else if (strlen(name) > CHANNELLEN)
651 *(name + CHANNELLEN) = '\0';
652 if (ChannelExists(name))
654 flags = CHFL_DEOPPED;
659 flags = IsModelessChannel(name) ? CHFL_DEOPPED : CHFL_CHANOP;
665 flags = IsModelessChannel(name) ? CHFL_DEOPPED : CHFL_CHANOP;
669 #ifdef OPER_NO_CHAN_LIMIT
671 * Opers are allowed to join any number of channels
673 if (sptr->user->joined >= MAXCHANNELSPERUSER && !IsAnOper(sptr))
675 if (sptr->user->joined >= MAXCHANNELSPERUSER)
678 chptr = get_channel(sptr, name, CGT_NO_CREATE);
679 sendto_one(sptr, err_str(ERR_TOOMANYCHANNELS),
680 me.name, parv[0], chptr ? chptr->chname : name);
682 * Can't return, else he won't get on ANY channels!
683 * Break out of the for loop instead. -Kev
688 chptr = get_channel(sptr, name, CGT_CREATE);
689 if (chptr && (member = find_member_link(chptr, sptr)))
691 if (IsZombie(member))
694 flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
695 remove_user_from_channel(sptr, chptr);
696 chptr = get_channel(sptr, name, CGT_CREATE);
701 name = chptr->chname;
702 if (!chptr->creationtime) /* A remote JOIN created this channel ? */
703 chptr->creationtime = MAGIC_REMOTE_JOIN_TS;
706 if (chptr->creationtime == MAGIC_REMOTE_JOIN_TS)
707 chptr->creationtime = atoi(keysOrTS);
709 parc = 2; /* Don't pass it on */
713 if (!MyConnect(sptr))
714 flags = CHFL_DEOPPED;
715 if (sptr->flags & FLAGS_TS8)
716 flags |= CHFL_SERVOPOK;
720 int created = chptr->users == 0;
721 if (check_target_limit(sptr, chptr, chptr->chname, created))
723 if (created) /* Did we create the channel? */
724 sub1_from_channel(chptr); /* Remove it again! */
727 if ((i = can_join(sptr, chptr, keysOrTS)))
729 sendto_one(sptr, err_str(i), me.name, parv[0], chptr->chname);
730 #ifdef OPER_WALK_THROUGH_LMODES
731 if (i > MAGIC_OPER_OVERRIDE)
733 switch(i - MAGIC_OPER_OVERRIDE)
735 case ERR_CHANNELISFULL: i = 'l'; break;
736 case ERR_INVITEONLYCHAN: i = 'i'; break;
737 case ERR_BANNEDFROMCHAN: i = 'b'; break;
738 case ERR_BADCHANNELKEY: i = 'k'; break;
740 sendto_op_mask(SNO_HACK4,"OPER JOIN: %s JOIN %s (overriding +%c)",sptr->name,chptr->chname,i);
750 * Complete user entry to the new channel (if any)
752 add_user_to_channel(chptr, sptr, flags);
755 * Notify all other users on the new channel
757 sendto_channel_butserv(chptr, sptr, ":%s JOIN :%s", parv[0], name);
761 del_invite(sptr, chptr);
762 if (chptr->topic[0] != '\0')
764 sendto_one(sptr, rpl_str(RPL_TOPIC), me.name,
765 parv[0], name, chptr->topic);
766 sendto_one(sptr, rpl_str(RPL_TOPICWHOTIME), me.name, parv[0], name,
767 chptr->topic_nick, chptr->topic_time);
770 m_names(cptr, sptr, 2, parv);
774 /* Select proper buffer; mbuf for creation, jbuf otherwise */
777 continue; /* Head off local channels at the pass */
779 bufptr = (sendcreate == 0) ? jbuf : mbuf;
780 buflen = (sendcreate == 0) ? &jlen : &mlen;
782 if (*buflen < BUFSIZE - len - 2)
786 strcat(bufptr, ","); /* Add to join buf */
789 strncat(bufptr, name, BUFSIZE - *buflen - 1);
792 sendcreate = 0; /* Reset sendcreate */
795 if (*jbuf) /* Propgate joins to P10 servers */
796 sendto_serv_butone(cptr,
797 parc > 2 ? "%s%s " TOK_JOIN " %s %s" : "%s%s " TOK_JOIN " %s", NumNick(sptr), jbuf, keysOrTS);
798 if (*mbuf) /* and now creation events */
799 sendto_serv_butone(cptr, "%s%s " TOK_CREATE " %s " TIME_T_FMT,
800 NumNick(sptr), mbuf, TStime());
803 { /* shouldn't ever set TS for remote JOIN's */
805 { /* check for channels that need TS's */
807 for (name = ircd_strtok(&p, jbuf, ","); name; name = ircd_strtok(&p, NULL, ","))
809 chptr = get_channel(sptr, name, CGT_NO_CREATE);
810 if (chptr && chptr->mode.mode & MODE_SENDTS)
812 sendto_serv_butone(cptr, "%s " TOK_MODE " %s + " TIME_T_FMT, NumServ(&me),
813 chptr->chname, chptr->creationtime); /* ok, send TS */
814 chptr->mode.mode &= ~MODE_SENDTS; /* reset flag */