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);
203 if (bad_channel(name) && !IsAnOper(sptr))
205 sendto_one(sptr, err_str(ERR_BADCHANNAME), me.name, parv[0], name);
210 * Local client is first to enter previously nonexistant
211 * channel so make them (rightfully) the Channel Operator.
212 * This looks kind of ugly because we try to avoid calling the strlen()
214 if (ChannelExists(name))
216 flags = CHFL_DEOPPED;
219 else if (strlen(name) > CHANNELLEN)
221 *(name + CHANNELLEN) = '\0';
222 if (ChannelExists(name))
224 flags = CHFL_DEOPPED;
229 flags = IsModelessChannel(name) ? CHFL_DEOPPED : CHFL_CHANOP;
235 flags = IsModelessChannel(name) ? CHFL_DEOPPED : CHFL_CHANOP;
239 #ifdef OPER_NO_CHAN_LIMIT
241 * Opers are allowed to join any number of channels
243 if (sptr->user->joined >= MAXCHANNELSPERUSER && !IsAnOper(sptr))
245 if (sptr->user->joined >= MAXCHANNELSPERUSER)
248 chptr = get_channel(sptr, name, CGT_NO_CREATE);
249 sendto_one(sptr, err_str(ERR_TOOMANYCHANNELS),
250 me.name, parv[0], chptr ? chptr->chname : name);
252 * Can't return, else he won't get on ANY channels!
253 * Break out of the for loop instead. -Kev
258 chptr = get_channel(sptr, name, CGT_CREATE);
259 if (chptr && (member = find_member_link(chptr, sptr)))
261 if (IsZombie(member))
264 flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
265 remove_user_from_channel(sptr, chptr);
266 chptr = get_channel(sptr, name, CGT_CREATE);
271 name = chptr->chname;
272 if (!chptr->creationtime) /* A remote JOIN created this channel ? */
273 chptr->creationtime = MAGIC_REMOTE_JOIN_TS;
276 if (chptr->creationtime == MAGIC_REMOTE_JOIN_TS)
277 chptr->creationtime = atoi(keysOrTS);
279 parc = 2; /* Don't pass it on */
283 if (!MyConnect(sptr))
284 flags = CHFL_DEOPPED;
285 if (sptr->flags & FLAGS_TS8)
286 flags |= CHFL_SERVOPOK;
290 int created = chptr->users == 0;
291 if (check_target_limit(sptr, chptr, chptr->chname, created))
293 if (created) /* Did we create the channel? */
294 sub1_from_channel(chptr); /* Remove it again! */
297 if ((i = can_join(sptr, chptr, keysOrTS)))
299 #ifdef OPER_WALK_THROUGH_LMODES
300 if (i > MAGIC_OPER_OVERRIDE)
302 switch(i - MAGIC_OPER_OVERRIDE)
304 case ERR_CHANNELISFULL: i = 'l'; break;
305 case ERR_INVITEONLYCHAN: i = 'i'; break;
306 case ERR_BANNEDFROMCHAN: i = 'b'; break;
307 case ERR_BADCHANNELKEY: i = 'k'; break;
309 sendto_op_mask(SNO_HACK4,"OPER JOIN: %s JOIN %s (overriding +%c)",sptr->name,chptr->chname,i);
313 sendto_one(sptr, err_str(i), me.name, parv[0], chptr->chname);
317 sendto_one(sptr, err_str(i), me.name, parv[0], chptr->chname);
323 * Complete user entry to the new channel (if any)
325 add_user_to_channel(chptr, sptr, flags);
328 * Notify all other users on the new channel
330 sendto_channel_butserv(chptr, sptr, ":%s JOIN :%s", parv[0], name);
334 del_invite(sptr, chptr);
335 if (chptr->topic[0] != '\0')
337 sendto_one(sptr, rpl_str(RPL_TOPIC), me.name,
338 parv[0], name, chptr->topic);
339 sendto_one(sptr, rpl_str(RPL_TOPICWHOTIME), me.name, parv[0], name,
340 chptr->topic_nick, chptr->topic_time);
343 m_names(cptr, sptr, 2, parv);
347 /* Select proper buffer; mbuf for creation, jbuf otherwise */
350 continue; /* Head off local channels at the pass */
352 bufptr = (sendcreate == 0) ? jbuf : mbuf;
353 buflen = (sendcreate == 0) ? &jlen : &mlen;
355 if (*buflen < BUFSIZE - len - 2)
359 strcat(bufptr, ","); /* Add to join buf */
362 strncat(bufptr, name, BUFSIZE - *buflen - 1);
365 sendcreate = 0; /* Reset sendcreate */
368 if (*jbuf) /* Propgate joins to P10 servers */
369 sendto_serv_butone(cptr,
370 parc > 2 ? "%s%s " TOK_JOIN " %s %s" : "%s%s " TOK_JOIN " %s", NumNick(sptr), jbuf, keysOrTS);
371 if (*mbuf) /* and now creation events */
372 sendto_serv_butone(cptr, "%s%s " TOK_CREATE " %s " TIME_T_FMT,
373 NumNick(sptr), mbuf, TStime());
376 { /* shouldn't ever set TS for remote JOIN's */
378 { /* check for channels that need TS's */
380 for (name = ircd_strtok(&p, jbuf, ","); name; name = ircd_strtok(&p, NULL, ","))
382 chptr = get_channel(sptr, name, CGT_NO_CREATE);
383 if (chptr && chptr->mode.mode & MODE_SENDTS)
385 sendto_serv_butone(cptr, "%s " TOK_MODE " %s + " TIME_T_FMT, NumServ(&me),
386 chptr->chname, chptr->creationtime); /* ok, send TS */
387 chptr->mode.mode &= ~MODE_SENDTS; /* reset flag */
396 * ms_join - server message handler
398 int ms_join(struct Client* cptr, struct Client* sptr, int parc, char* parv[])
400 static char jbuf[BUFSIZE];
401 static char mbuf[BUFSIZE];
402 struct Membership* member;
403 struct Channel* chptr;
405 char* keysOrTS = NULL;
409 unsigned int flags = 0;
417 * Doesn't make sense having a server join a channel, and besides
423 if (parc < 2 || *parv[1] == '\0')
424 return need_more_params(sptr, "JOIN");
426 keysOrTS = parv[2]; /* Remember where our keys are or the TS is;
427 parv[2] needs to be NULL for the call to
428 m_names below -Kev */
431 *jbuf = *mbuf = '\0'; /* clear both join and mode buffers -Kev */
433 * Rebuild list of channels joined to be the actual result of the
434 * JOIN. Note that "JOIN 0" is the destructive problem.
436 for (name = ircd_strtok(&p, parv[1], ","); name; name = ircd_strtok(&p, NULL, ","))
439 if (IsLocalChannel(name))
442 if (!IsChannelName(name))
444 sendto_one(sptr, err_str(ERR_NOSUCHCHANNEL), me.name, parv[0], name);
448 chptr = get_channel(sptr, name, CGT_CREATE);
449 if (chptr && (member = find_member_link(chptr, sptr)))
451 if (!IsZombie(member))
454 /* If they are a zombie, make them really part
458 flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
459 remove_user_from_channel(sptr, chptr);
460 chptr = get_channel(sptr, name, CGT_CREATE);
464 name = chptr->chname;
466 if (!chptr->creationtime) /* A remote JOIN created this channel ? */
467 chptr->creationtime = MAGIC_REMOTE_JOIN_TS;
471 if (chptr->creationtime == MAGIC_REMOTE_JOIN_TS)
472 chptr->creationtime = atoi(keysOrTS);
474 parc = 2; /* Don't pass it on */
479 if (sptr->flags & FLAGS_TS8)
480 flags |= CHFL_SERVOPOK;
484 * Complete user entry to the new channel (if any)
486 add_user_to_channel(chptr, sptr, flags);
489 * Notify all other users on the new channel
491 sendto_channel_butserv(chptr, sptr, ":%s JOIN :%s", parv[0], name);
493 /* Select proper buffer; mbuf for creation, jbuf otherwise */
496 continue; /* Head off local channels at the pass */
498 bufptr = (sendcreate == 0) ? jbuf : mbuf;
499 buflen = (sendcreate == 0) ? &jlen : &mlen;
502 if (*buflen < BUFSIZE - len - 2)
506 strcat(bufptr, ","); /* Add to join buf */
509 strncat(bufptr, name, BUFSIZE - *buflen - 1);
512 sendcreate = 0; /* Reset sendcreate */
515 if (*jbuf) /* Propgate joins to P10 servers */
516 sendto_serv_butone(cptr,
517 parc > 2 ? "%s%s " TOK_JOIN " %s %s" : "%s%s " TOK_JOIN " %s", NumNick(sptr), jbuf, keysOrTS);
518 if (*mbuf) /* and now creation events */
519 sendto_serv_butone(cptr, "%s%s " TOK_CREATE " %s " TIME_T_FMT,
520 NumNick(sptr), mbuf, TStime());
530 * parv[0] = sender prefix
532 * parv[2] = channel keys (client), or channel TS (server)
534 int m_join(struct Client *cptr, struct Client *sptr, int parc, char *parv[])
536 static char jbuf[BUFSIZE];
537 static char mbuf[BUFSIZE];
538 struct Membership* member;
539 struct Channel* chptr;
541 char* keysOrTS = NULL;
545 unsigned int flags = 0;
553 * Doesn't make sense having a server join a channel, and besides
559 if (parc < 2 || *parv[1] == '\0')
560 return need_more_params(sptr, "JOIN");
562 for (p = parv[1]; *p; p++) /* find the last "JOIN 0" in the line -Kev */
564 && (*(p + 1) == ',' || *(p + 1) == '\0' || !IsChannelChar(*(p + 1))))
566 /* If it's a single "0", remember the place; we will start parsing
567 the channels after the last 0 in the line -Kev */
574 { /* Step through to the next comma or until the
575 end of the line, in an attempt to save CPU
577 while (*p != ',' && *p != '\0')
583 keysOrTS = parv[2]; /* Remember where our keys are or the TS is;
584 parv[2] needs to be NULL for the call to
585 m_names below -Kev */
588 *jbuf = *mbuf = '\0'; /* clear both join and mode buffers -Kev */
590 * Rebuild list of channels joined to be the actual result of the
591 * JOIN. Note that "JOIN 0" is the destructive problem.
593 for (name = ircd_strtok(&p, parv[1], ","); name; name = ircd_strtok(&p, NULL, ","))
597 clean_channelname(name);
598 else if (IsLocalChannel(name))
600 if (*name == '0' && *(name + 1) == '\0')
602 /* Remove the user from all his channels -Kev */
603 while ((member = sptr->user->channel))
605 chptr = member->channel;
606 if (!IsZombie(member))
607 sendto_channel_butserv(chptr, sptr, PartFmt2,
608 parv[0], chptr->chname, "Left all channels");
609 remove_user_from_channel(sptr, chptr);
612 *mbuf = *jbuf = '\0';
616 { /* not a /join 0, so treat it as
617 a /join #channel -Kev */
618 if (!IsChannelName(name))
621 sendto_one(sptr, err_str(ERR_NOSUCHCHANNEL), me.name, parv[0], name);
628 if (bad_channel(name) && !IsAnOper(sptr))
630 sendto_one(sptr, err_str(ERR_BADCHANNAME), me.name, parv[0], name);
635 * Local client is first to enter previously nonexistant
636 * channel so make them (rightfully) the Channel Operator.
637 * This looks kind of ugly because we try to avoid calling the strlen()
639 if (ChannelExists(name))
641 flags = CHFL_DEOPPED;
644 else if (strlen(name) > CHANNELLEN)
646 *(name + CHANNELLEN) = '\0';
647 if (ChannelExists(name))
649 flags = CHFL_DEOPPED;
654 flags = IsModelessChannel(name) ? CHFL_DEOPPED : CHFL_CHANOP;
660 flags = IsModelessChannel(name) ? CHFL_DEOPPED : CHFL_CHANOP;
664 #ifdef OPER_NO_CHAN_LIMIT
666 * Opers are allowed to join any number of channels
668 if (sptr->user->joined >= MAXCHANNELSPERUSER && !IsAnOper(sptr))
670 if (sptr->user->joined >= MAXCHANNELSPERUSER)
673 chptr = get_channel(sptr, name, CGT_NO_CREATE);
674 sendto_one(sptr, err_str(ERR_TOOMANYCHANNELS),
675 me.name, parv[0], chptr ? chptr->chname : name);
677 * Can't return, else he won't get on ANY channels!
678 * Break out of the for loop instead. -Kev
683 chptr = get_channel(sptr, name, CGT_CREATE);
684 if (chptr && (member = find_member_link(chptr, sptr)))
686 if (IsZombie(member))
689 flags = member->status & (CHFL_DEOPPED | CHFL_SERVOPOK);
690 remove_user_from_channel(sptr, chptr);
691 chptr = get_channel(sptr, name, CGT_CREATE);
696 name = chptr->chname;
697 if (!chptr->creationtime) /* A remote JOIN created this channel ? */
698 chptr->creationtime = MAGIC_REMOTE_JOIN_TS;
701 if (chptr->creationtime == MAGIC_REMOTE_JOIN_TS)
702 chptr->creationtime = atoi(keysOrTS);
704 parc = 2; /* Don't pass it on */
708 if (!MyConnect(sptr))
709 flags = CHFL_DEOPPED;
710 if (sptr->flags & FLAGS_TS8)
711 flags |= CHFL_SERVOPOK;
715 int created = chptr->users == 0;
716 if (check_target_limit(sptr, chptr, chptr->chname, created))
718 if (created) /* Did we create the channel? */
719 sub1_from_channel(chptr); /* Remove it again! */
722 if ((i = can_join(sptr, chptr, keysOrTS)))
724 sendto_one(sptr, err_str(i), me.name, parv[0], chptr->chname);
725 #ifdef OPER_WALK_THROUGH_LMODES
726 if (i > MAGIC_OPER_OVERRIDE)
728 switch(i - MAGIC_OPER_OVERRIDE)
730 case ERR_CHANNELISFULL: i = 'l'; break;
731 case ERR_INVITEONLYCHAN: i = 'i'; break;
732 case ERR_BANNEDFROMCHAN: i = 'b'; break;
733 case ERR_BADCHANNELKEY: i = 'k'; break;
735 sendto_op_mask(SNO_HACK4,"OPER JOIN: %s JOIN %s (overriding +%c)",sptr->name,chptr->chname,i);
745 * Complete user entry to the new channel (if any)
747 add_user_to_channel(chptr, sptr, flags);
750 * Notify all other users on the new channel
752 sendto_channel_butserv(chptr, sptr, ":%s JOIN :%s", parv[0], name);
756 del_invite(sptr, chptr);
757 if (chptr->topic[0] != '\0')
759 sendto_one(sptr, rpl_str(RPL_TOPIC), me.name,
760 parv[0], name, chptr->topic);
761 sendto_one(sptr, rpl_str(RPL_TOPICWHOTIME), me.name, parv[0], name,
762 chptr->topic_nick, chptr->topic_time);
765 m_names(cptr, sptr, 2, parv);
769 /* Select proper buffer; mbuf for creation, jbuf otherwise */
772 continue; /* Head off local channels at the pass */
774 bufptr = (sendcreate == 0) ? jbuf : mbuf;
775 buflen = (sendcreate == 0) ? &jlen : &mlen;
777 if (*buflen < BUFSIZE - len - 2)
781 strcat(bufptr, ","); /* Add to join buf */
784 strncat(bufptr, name, BUFSIZE - *buflen - 1);
787 sendcreate = 0; /* Reset sendcreate */
790 if (*jbuf) /* Propgate joins to P10 servers */
791 sendto_serv_butone(cptr,
792 parc > 2 ? "%s%s " TOK_JOIN " %s %s" : "%s%s " TOK_JOIN " %s", NumNick(sptr), jbuf, keysOrTS);
793 if (*mbuf) /* and now creation events */
794 sendto_serv_butone(cptr, "%s%s " TOK_CREATE " %s " TIME_T_FMT,
795 NumNick(sptr), mbuf, TStime());
798 { /* shouldn't ever set TS for remote JOIN's */
800 { /* check for channels that need TS's */
802 for (name = ircd_strtok(&p, jbuf, ","); name; name = ircd_strtok(&p, NULL, ","))
804 chptr = get_channel(sptr, name, CGT_NO_CREATE);
805 if (chptr && chptr->mode.mode & MODE_SENDTS)
807 sendto_serv_butone(cptr, "%s " TOK_MODE " %s + " TIME_T_FMT, NumServ(&me),
808 chptr->chname, chptr->creationtime); /* ok, send TS */
809 chptr->mode.mode &= ~MODE_SENDTS; /* reset flag */