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)) &&
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;
412 unsigned int flags = 0;
420 * Doesn't make sense having a server join a channel, and besides
426 if (parc < 2 || *parv[1] == '\0')
427 return need_more_params(sptr, "JOIN");
429 keysOrTS = parv[2]; /* Remember where our keys are or the TS is;
430 parv[2] needs to be NULL for the call to
431 m_names below -Kev */
434 *jbuf = *mbuf = '\0'; /* clear both join and mode buffers -Kev */
436 * Rebuild list of channels joined to be the actual result of the
437 * JOIN. Note that "JOIN 0" is the destructive problem.
439 for (name = ircd_strtok(&p, parv[1], ","); name; name = ircd_strtok(&p, NULL, ","))
442 if (IsLocalChannel(name))
445 if (!IsChannelName(name))
447 sendto_one(sptr, err_str(ERR_NOSUCHCHANNEL), me.name, parv[0], name);
451 chptr = get_channel(sptr, name, CGT_CREATE);
452 if (chptr && (member = find_member_link(chptr, sptr)))
454 if (!IsZombie(member))
457 /* If they are a zombie, make them really part
461 flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
462 remove_user_from_channel(sptr, chptr);
463 chptr = get_channel(sptr, name, CGT_CREATE);
467 name = chptr->chname;
469 if (!chptr->creationtime) /* A remote JOIN created this channel ? */
470 chptr->creationtime = MAGIC_REMOTE_JOIN_TS;
474 if (chptr->creationtime == MAGIC_REMOTE_JOIN_TS)
475 chptr->creationtime = atoi(keysOrTS);
477 parc = 2; /* Don't pass it on */
482 if (sptr->flags & FLAGS_TS8)
483 flags |= CHFL_SERVOPOK;
487 * Complete user entry to the new channel (if any)
489 add_user_to_channel(chptr, sptr, flags);
492 * Notify all other users on the new channel
494 sendto_channel_butserv(chptr, sptr, ":%s JOIN :%s", parv[0], name);
496 /* Select proper buffer; mbuf for creation, jbuf otherwise */
499 continue; /* Head off local channels at the pass */
501 bufptr = (sendcreate == 0) ? jbuf : mbuf;
502 buflen = (sendcreate == 0) ? &jlen : &mlen;
505 if (*buflen < BUFSIZE - len - 2)
509 strcat(bufptr, ","); /* Add to join buf */
512 strncat(bufptr, name, BUFSIZE - *buflen - 1);
515 sendcreate = 0; /* Reset sendcreate */
518 if (*jbuf) /* Propgate joins to P10 servers */
519 sendto_serv_butone(cptr,
520 parc > 2 ? "%s%s " TOK_JOIN " %s %s" : "%s%s " TOK_JOIN " %s", NumNick(sptr), jbuf, keysOrTS);
521 if (*mbuf) /* and now creation events */
522 sendto_serv_butone(cptr, "%s%s " TOK_CREATE " %s " TIME_T_FMT,
523 NumNick(sptr), mbuf, TStime());
533 * parv[0] = sender prefix
535 * parv[2] = channel keys (client), or channel TS (server)
537 int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
539 static char jbuf[BUFSIZE];
540 static char mbuf[BUFSIZE];
541 struct Membership* member;
542 struct Channel* chptr;
544 char* keysOrTS = NULL;
548 unsigned int flags = 0;
556 * Doesn't make sense having a server join a channel, and besides
562 if (parc < 2 || *parv[1] == '\0')
563 return need_more_params(sptr, "JOIN");
565 for (p = parv[1]; *p; p++) /* find the last "JOIN 0" in the line -Kev */
567 && (*(p + 1) == ',' || *(p + 1) == '\0' || !IsChannelChar(*(p + 1))))
569 /* If it's a single "0", remember the place; we will start parsing
570 the channels after the last 0 in the line -Kev */
577 { /* Step through to the next comma or until the
578 end of the line, in an attempt to save CPU
580 while (*p != ',' && *p != '\0')
586 keysOrTS = parv[2]; /* Remember where our keys are or the TS is;
587 parv[2] needs to be NULL for the call to
588 m_names below -Kev */
591 *jbuf = *mbuf = '\0'; /* clear both join and mode buffers -Kev */
593 * Rebuild list of channels joined to be the actual result of the
594 * JOIN. Note that "JOIN 0" is the destructive problem.
596 for (name = ircd_strtok(&p, parv[1], ","); name; name = ircd_strtok(&p, NULL, ","))
600 clean_channelname(name);
601 else if (IsLocalChannel(name))
603 if (*name == '0' && *(name + 1) == '\0')
605 /* Remove the user from all his channels -Kev */
606 while ((member = sptr->user->channel))
608 chptr = member->channel;
609 if (!IsZombie(member))
610 sendto_channel_butserv(chptr, sptr, PartFmt2,
611 parv[0], chptr->chname, "Left all channels");
612 remove_user_from_channel(sptr, chptr);
615 *mbuf = *jbuf = '\0';
619 { /* not a /join 0, so treat it as
620 a /join #channel -Kev */
621 if (!IsChannelName(name))
624 sendto_one(sptr, err_str(ERR_NOSUCHCHANNEL), me.name, parv[0], name);
633 if ((gline = gline_find(name, GLINE_BADCHAN)) &&
634 GlineIsActive(gline) && !IsAnOper(sptr))
636 sendto_one(sptr, err_str(ERR_BADCHANNAME), me.name, parv[0], name);
641 * Local client is first to enter previously nonexistant
642 * channel so make them (rightfully) the Channel Operator.
643 * This looks kind of ugly because we try to avoid calling the strlen()
645 if (ChannelExists(name))
647 flags = CHFL_DEOPPED;
650 else if (strlen(name) > CHANNELLEN)
652 *(name + CHANNELLEN) = '\0';
653 if (ChannelExists(name))
655 flags = CHFL_DEOPPED;
660 flags = IsModelessChannel(name) ? CHFL_DEOPPED : CHFL_CHANOP;
666 flags = IsModelessChannel(name) ? CHFL_DEOPPED : CHFL_CHANOP;
670 #ifdef OPER_NO_CHAN_LIMIT
672 * Opers are allowed to join any number of channels
674 if (sptr->user->joined >= MAXCHANNELSPERUSER && !IsAnOper(sptr))
676 if (sptr->user->joined >= MAXCHANNELSPERUSER)
679 chptr = get_channel(sptr, name, CGT_NO_CREATE);
680 sendto_one(sptr, err_str(ERR_TOOMANYCHANNELS),
681 me.name, parv[0], chptr ? chptr->chname : name);
683 * Can't return, else he won't get on ANY channels!
684 * Break out of the for loop instead. -Kev
689 chptr = get_channel(sptr, name, CGT_CREATE);
690 if (chptr && (member = find_member_link(chptr, sptr)))
692 if (IsZombie(member))
695 flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
696 remove_user_from_channel(sptr, chptr);
697 chptr = get_channel(sptr, name, CGT_CREATE);
702 name = chptr->chname;
703 if (!chptr->creationtime) /* A remote JOIN created this channel ? */
704 chptr->creationtime = MAGIC_REMOTE_JOIN_TS;
707 if (chptr->creationtime == MAGIC_REMOTE_JOIN_TS)
708 chptr->creationtime = atoi(keysOrTS);
710 parc = 2; /* Don't pass it on */
714 if (!MyConnect(sptr))
715 flags = CHFL_DEOPPED;
716 if (sptr->flags & FLAGS_TS8)
717 flags |= CHFL_SERVOPOK;
721 int created = chptr->users == 0;
722 if (check_target_limit(sptr, chptr, chptr->chname, created))
724 if (created) /* Did we create the channel? */
725 sub1_from_channel(chptr); /* Remove it again! */
728 if ((i = can_join(sptr, chptr, keysOrTS)))
730 sendto_one(sptr, err_str(i), me.name, parv[0], chptr->chname);
731 #ifdef OPER_WALK_THROUGH_LMODES
732 if (i > MAGIC_OPER_OVERRIDE)
734 switch(i - MAGIC_OPER_OVERRIDE)
736 case ERR_CHANNELISFULL: i = 'l'; break;
737 case ERR_INVITEONLYCHAN: i = 'i'; break;
738 case ERR_BANNEDFROMCHAN: i = 'b'; break;
739 case ERR_BADCHANNELKEY: i = 'k'; break;
741 sendto_op_mask(SNO_HACK4,"OPER JOIN: %s JOIN %s (overriding +%c)",sptr->name,chptr->chname,i);
751 * Complete user entry to the new channel (if any)
753 add_user_to_channel(chptr, sptr, flags);
756 * Notify all other users on the new channel
758 sendto_channel_butserv(chptr, sptr, ":%s JOIN :%s", parv[0], name);
762 del_invite(sptr, chptr);
763 if (chptr->topic[0] != '\0')
765 sendto_one(sptr, rpl_str(RPL_TOPIC), me.name,
766 parv[0], name, chptr->topic);
767 sendto_one(sptr, rpl_str(RPL_TOPICWHOTIME), me.name, parv[0], name,
768 chptr->topic_nick, chptr->topic_time);
771 m_names(cptr, sptr, 2, parv);
775 /* Select proper buffer; mbuf for creation, jbuf otherwise */
778 continue; /* Head off local channels at the pass */
780 bufptr = (sendcreate == 0) ? jbuf : mbuf;
781 buflen = (sendcreate == 0) ? &jlen : &mlen;
783 if (*buflen < BUFSIZE - len - 2)
787 strcat(bufptr, ","); /* Add to join buf */
790 strncat(bufptr, name, BUFSIZE - *buflen - 1);
793 sendcreate = 0; /* Reset sendcreate */
796 if (*jbuf) /* Propgate joins to P10 servers */
797 sendto_serv_butone(cptr,
798 parc > 2 ? "%s%s " TOK_JOIN " %s %s" : "%s%s " TOK_JOIN " %s", NumNick(sptr), jbuf, keysOrTS);
799 if (*mbuf) /* and now creation events */
800 sendto_serv_butone(cptr, "%s%s " TOK_CREATE " %s " TIME_T_FMT,
801 NumNick(sptr), mbuf, TStime());
804 { /* shouldn't ever set TS for remote JOIN's */
806 { /* check for channels that need TS's */
808 for (name = ircd_strtok(&p, jbuf, ","); name; name = ircd_strtok(&p, NULL, ","))
810 chptr = get_channel(sptr, name, CGT_NO_CREATE);
811 if (chptr && chptr->mode.mode & MODE_SENDTS)
813 sendto_serv_butone(cptr, "%s " TOK_MODE " %s + " TIME_T_FMT, NumServ(&me),
814 chptr->chname, chptr->creationtime); /* ok, send TS */
815 chptr->mode.mode &= ~MODE_SENDTS; /* reset flag */