+ if (jbuf->jb_type == JOINBUF_TYPE_PART ||
+ jbuf->jb_type == JOINBUF_TYPE_PARTALL) {
+ struct Membership *member = find_member_link(chan, jbuf->jb_source);
+ if (IsUserParting(member))
+ return;
+ SetUserParting(member);
+
+ /* Send notification to channel */
+ if (!(flags & (CHFL_ZOMBIE | CHFL_DELAYED)))
+ sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_PART, chan, NULL, 0,
+ (flags & CHFL_BANNED || !jbuf->jb_comment) ?
+ ":%H" : "%H :%s", chan, jbuf->jb_comment);
+ else if (MyUser(jbuf->jb_source))
+ sendcmdto_one(jbuf->jb_source, CMD_PART, jbuf->jb_source,
+ (flags & CHFL_BANNED || !jbuf->jb_comment) ?
+ ":%H" : "%H :%s", chan, jbuf->jb_comment);
+ /* XXX: Shouldn't we send a PART here anyway? */
+ /* to users on the channel? Why? From their POV, the user isn't on
+ * the channel anymore anyway. We don't send to servers until below,
+ * when we gang all the channel parts together. Note that this is
+ * exactly the same logic, albeit somewhat more concise, as was in
+ * the original m_part.c */
+
+ if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
+ is_local) /* got to remove user here */
+ remove_user_from_channel(jbuf->jb_source, chan);
+ } else {
+ int oplevel = !chan->mode.apass[0] ? MAXOPLEVEL
+ : (flags & CHFL_CHANNEL_MANAGER) ? 0
+ : 1;
+ /* Add user to channel */
+ if ((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))
+ add_user_to_channel(chan, jbuf->jb_source, flags | CHFL_DELAYED, oplevel);
+ else
+ add_user_to_channel(chan, jbuf->jb_source, flags, oplevel);
+
+ /* send JOIN notification to all servers (CREATE is sent later). */
+ if (jbuf->jb_type != JOINBUF_TYPE_CREATE && !is_local)
+ sendcmdto_serv_butone(jbuf->jb_source, CMD_JOIN, jbuf->jb_connect,
+ "%H %Tu", chan, chan->creationtime);
+
+ if (!((chan->mode.mode & MODE_DELJOINS) && !(flags & CHFL_VOICED_OR_OPPED))) {
+ /* Send the notification to the channel */
+ sendcmdto_channel_butserv_butone(jbuf->jb_source, CMD_JOIN, chan, NULL, 0, "%H", chan);
+
+ /* send an op, too, if needed */
+ if (flags & CHFL_CHANOP && (oplevel < MAXOPLEVEL || !MyUser(jbuf->jb_source)))
+ sendcmdto_channel_butserv_butone((chan->mode.apass[0] ? &his : jbuf->jb_source),
+ CMD_MODE, chan, NULL, 0, "%H +o %C",
+ chan, jbuf->jb_source);
+ } else if (MyUser(jbuf->jb_source))
+ sendcmdto_one(jbuf->jb_source, CMD_JOIN, jbuf->jb_source, ":%H", chan);
+ }
+
+ if (jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
+ jbuf->jb_type == JOINBUF_TYPE_JOIN || is_local)
+ return; /* don't send to remote */
+
+ /* figure out if channel name will cause buffer to be overflowed */
+ len = chan ? strlen(chan->chname) + 1 : 2;
+ if (jbuf->jb_strlen + len > BUFSIZE)
+ joinbuf_flush(jbuf);
+
+ /* add channel to list of channels to send and update counts */
+ jbuf->jb_channels[jbuf->jb_count++] = chan;
+ jbuf->jb_strlen += len;
+
+ /* if we've used up all slots, flush */
+ if (jbuf->jb_count >= MAXJOINARGS)
+ joinbuf_flush(jbuf);
+}
+
+/*
+ * Flush the channel list to remote servers
+ */
+int
+joinbuf_flush(struct JoinBuf *jbuf)
+{
+ char chanlist[BUFSIZE];
+ int chanlist_i = 0;
+ int i;
+
+ if (!jbuf->jb_count || jbuf->jb_type == JOINBUF_TYPE_PARTALL ||
+ jbuf->jb_type == JOINBUF_TYPE_JOIN)
+ return 0; /* no joins to process */
+
+ for (i = 0; i < jbuf->jb_count; i++) { /* build channel list */
+ build_string(chanlist, &chanlist_i,
+ jbuf->jb_channels[i] ? jbuf->jb_channels[i]->chname : "0", 0,
+ i == 0 ? '\0' : ',');
+ if (JOINBUF_TYPE_PART == jbuf->jb_type)
+ /* Remove user from channel */
+ remove_user_from_channel(jbuf->jb_source, jbuf->jb_channels[i]);
+
+ jbuf->jb_channels[i] = 0; /* mark slot empty */
+ }
+
+ jbuf->jb_count = 0; /* reset base counters */
+ jbuf->jb_strlen = ((jbuf->jb_type == JOINBUF_TYPE_PART ?
+ STARTJOINLEN : STARTCREATELEN) +
+ (jbuf->jb_comment ? strlen(jbuf->jb_comment) + 2 : 0));
+
+ /* and send the appropriate command */
+ switch (jbuf->jb_type) {
+ case JOINBUF_TYPE_CREATE:
+ sendcmdto_serv_butone(jbuf->jb_source, CMD_CREATE, jbuf->jb_connect,
+ "%s %Tu", chanlist, jbuf->jb_create);
+ break;
+
+ case JOINBUF_TYPE_PART:
+ sendcmdto_serv_butone(jbuf->jb_source, CMD_PART, jbuf->jb_connect,
+ jbuf->jb_comment ? "%s :%s" : "%s", chanlist,
+ jbuf->jb_comment);
+ break;
+ }
+
+ return 0;
+}
+
+/* Returns TRUE (1) if client is invited, FALSE (0) if not */
+int IsInvited(struct Client* cptr, const void* chptr)
+{
+ struct SLink *lp;
+
+ for (lp = (cli_user(cptr))->invited; lp; lp = lp->next)
+ if (lp->value.chptr == chptr)
+ return 1;
+ return 0;
+}
+
+/* RevealDelayedJoin: sends a join for a hidden user */
+
+void RevealDelayedJoin(struct Membership *member)
+{
+ ClearDelayedJoin(member);
+ sendcmdto_channel_butserv_butone(member->user, CMD_JOIN, member->channel, member->user, 0, ":%H",
+ member->channel);
+ CheckDelayedJoins(member->channel);
+}
+
+/* CheckDelayedJoins: checks and clear +d if necessary */
+
+void CheckDelayedJoins(struct Channel *chan)
+{
+ if ((chan->mode.mode & MODE_WASDELJOINS) && !find_delayed_joins(chan)) {
+ chan->mode.mode &= ~MODE_WASDELJOINS;
+ sendcmdto_channel_butserv_butone(&his, CMD_MODE, chan, NULL, 0,
+ "%H -d", chan);