ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/ircd-hybrid/trunk/src/channel.c
(Generate patch)

Comparing ircd-hybrid/trunk/src/channel.c (file contents):
Revision 1661 by michael, Sat Nov 17 19:43:57 2012 UTC vs.
Revision 5983 by michael, Mon May 25 15:24:48 2015 UTC

# Line 1 | Line 1
1   /*
2 < *  ircd-hybrid: an advanced Internet Relay Chat Daemon(ircd).
2 > *  ircd-hybrid: an advanced, lightweight Internet Relay Chat Daemon (ircd)
3   *
4 < *  Copyright (C) 2002 by the past and present ircd coders, and others.
4 > *  Copyright (c) 1997-2015 ircd-hybrid development team
5   *
6   *  This program is free software; you can redistribute it and/or modify
7   *  it under the terms of the GNU General Public License as published by
# Line 15 | Line 15
15   *
16   *  You should have received a copy of the GNU General Public License
17   *  along with this program; if not, write to the Free Software
18 < *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
18 > *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301
19   *  USA
20   */
21  
# Line 33 | Line 33
33   #include "conf.h"
34   #include "hostmask.h"
35   #include "irc_string.h"
36 #include "sprintf_irc.h"
36   #include "ircd.h"
37   #include "numeric.h"
38 < #include "s_serv.h"             /* captab */
40 < #include "s_user.h"
38 > #include "server.h"
39   #include "send.h"
40   #include "event.h"
41   #include "memory.h"
42   #include "mempool.h"
43 + #include "misc.h"
44 + #include "resv.h"
45  
46 < struct config_channel_entry ConfigChannel;
47 < dlink_list global_channel_list = { NULL, NULL, 0 };
46 >
47 > dlink_list channel_list;
48   mp_pool_t *ban_pool;    /*! \todo ban_pool shouldn't be a global var */
49  
50 < static mp_pool_t *member_pool = NULL;
51 < static mp_pool_t *channel_pool = NULL;
50 > struct event splitmode_event =
51 > {
52 >  .name = "check_splitmode",
53 >  .handler = check_splitmode,
54 >  .when = 5
55 > };
56  
57 < static char buf[IRCD_BUFSIZE];
54 < static char modebuf[MODEBUFLEN];
55 < static char parabuf[MODEBUFLEN];
57 > static mp_pool_t *member_pool, *channel_pool;
58  
59  
60   /*! \brief Initializes the channel blockheap, adds known channel CAPAB
61   */
62   void
63 < init_channels(void)
63 > channel_init(void)
64   {
65 <  add_capability("EX", CAP_EX, 1);
66 <  add_capability("IE", CAP_IE, 1);
65 <  add_capability("CHW", CAP_CHW, 1);
65 >  add_capability("EX", CAP_EX);
66 >  add_capability("IE", CAP_IE);
67  
68    channel_pool = mp_pool_new(sizeof(struct Channel), MP_CHUNK_SIZE_CHANNEL);
69    ban_pool = mp_pool_new(sizeof(struct Ban), MP_CHUNK_SIZE_BAN);
70    member_pool = mp_pool_new(sizeof(struct Membership), MP_CHUNK_SIZE_MEMBER);
71   }
72  
73 < /*! \brief adds a user to a channel by adding another link to the
73 > /*! \brief Adds a user to a channel by adding another link to the
74   *         channels member chain.
75 < * \param chptr      pointer to channel to add client to
76 < * \param who        pointer to client (who) to add
77 < * \param flags      flags for chanops etc
78 < * \param flood_ctrl whether to count this join in flood calculations
75 > * \param chptr      Pointer to channel to add client to
76 > * \param who        Pointer to client (who) to add
77 > * \param flags      Flags for chanops etc
78 > * \param flood_ctrl Whether to count this join in flood calculations
79   */
80   void
81   add_user_to_channel(struct Channel *chptr, struct Client *who,
82                      unsigned int flags, int flood_ctrl)
83   {
84 <  struct Membership *ms = NULL;
84 >  struct Membership *member = NULL;
85 >
86 >  assert(IsClient(who));
87  
88    if (GlobalSetOptions.joinfloodtime > 0)
89    {
90      if (flood_ctrl)
91 <      chptr->number_joined++;
91 >      ++chptr->number_joined;
92  
93      chptr->number_joined -= (CurrentTime - chptr->last_join_time) *
94        (((float)GlobalSetOptions.joinfloodcount) /
# Line 106 | Line 109 | add_user_to_channel(struct Channel *chpt
109          sendto_realops_flags(UMODE_BOTS, L_ALL, SEND_NOTICE,
110                               "Possible Join Flooder %s on %s target: %s",
111                               get_client_name(who, HIDE_IP),
112 <                             who->servptr->name, chptr->chname);
112 >                             who->servptr->name, chptr->name);
113        }
114      }
115  
116      chptr->last_join_time = CurrentTime;
117    }
118  
119 <  ms = mp_pool_get(member_pool);
120 <  memset(ms, 0, sizeof(*ms));
119 >  member = mp_pool_get(member_pool);
120 >  member->client_p = who;
121 >  member->chptr = chptr;
122 >  member->flags = flags;
123 >
124 >  dlinkAdd(member, &member->channode, &chptr->members);
125  
126 <  ms->client_p = who;
127 <  ms->chptr = chptr;
121 <  ms->flags = flags;
126 >  if (MyConnect(who))
127 >    dlinkAdd(member, &member->locchannode, &chptr->locmembers);
128  
129 <  dlinkAdd(ms, &ms->channode, &chptr->members);
124 <  dlinkAdd(ms, &ms->usernode, &who->channel);
129 >  dlinkAdd(member, &member->usernode, &who->channel);
130   }
131  
132 < /*! \brief deletes an user from a channel by removing a link in the
132 > /*! \brief Deletes an user from a channel by removing a link in the
133   *         channels member chain.
134 < * \param member pointer to Membership struct
134 > * \param member Pointer to Membership struct
135   */
136   void
137   remove_user_from_channel(struct Membership *member)
138   {
139 <  struct Client *client_p = member->client_p;
140 <  struct Channel *chptr = member->chptr;
139 >  struct Client *const client_p = member->client_p;
140 >  struct Channel *const chptr = member->chptr;
141  
142    dlinkDelete(&member->channode, &chptr->members);
143 +
144 +  if (MyConnect(client_p))
145 +    dlinkDelete(&member->locchannode, &chptr->locmembers);
146 +
147    dlinkDelete(&member->usernode, &client_p->channel);
148  
149    mp_pool_release(member);
# Line 150 | Line 159 | remove_user_from_channel(struct Membersh
159   * side effects -
160   */
161   static void
162 < send_members(struct Client *client_p, struct Channel *chptr,
163 <             char *lmodebuf, char *lparabuf)
162 > send_members(struct Client *client_p, const struct Channel *chptr,
163 >             char *modebuf, char *parabuf)
164   {
165 <  struct Membership *ms;
166 <  dlink_node *ptr;
165 >  char buf[IRCD_BUFSIZE] = "";
166 >  const dlink_node *node = NULL;
167    int tlen;              /* length of text to append */
168    char *t, *start;       /* temp char pointer */
169  
170 <  start = t = buf + ircsprintf(buf, ":%s SJOIN %lu %s %s %s:",
171 <                               ID_or_name(&me, client_p),
172 <                               (unsigned long)chptr->channelts,
173 <                               chptr->chname, lmodebuf, lparabuf);
174 <
175 <  DLINK_FOREACH(ptr, chptr->members.head)
176 <  {
177 <    ms = ptr->data;
178 <
179 <    tlen = strlen(IsCapable(client_p, CAP_TS6) ?
180 <      ID(ms->client_p) : ms->client_p->name) + 1;  /* nick + space */
181 <
182 <    if (ms->flags & CHFL_CHANOP)
183 <      tlen++;
184 < #ifdef HALFOPS
185 <    else if (ms->flags & CHFL_HALFOP)
186 <      tlen++;
187 < #endif
188 <    if (ms->flags & CHFL_VOICE)
189 <      tlen++;
190 <
182 <    /* space will be converted into CR, but we also need space for LF..
183 <     * That's why we use '- 1' here
184 <     * -adx */
170 >  start = t = buf + snprintf(buf, sizeof(buf), ":%s SJOIN %lu %s %s %s:",
171 >                             me.id, (unsigned long)chptr->creationtime,
172 >                             chptr->name, modebuf, parabuf);
173 >
174 >  DLINK_FOREACH(node, chptr->members.head)
175 >  {
176 >    const struct Membership *member = node->data;
177 >
178 >    tlen = strlen(member->client_p->id) + 1;  /* +1 for space */
179 >
180 >    if (member->flags & CHFL_CHANOP)
181 >      ++tlen;
182 >    if (member->flags & CHFL_HALFOP)
183 >      ++tlen;
184 >    if (member->flags & CHFL_VOICE)
185 >      ++tlen;
186 >
187 >    /*
188 >     * Space will be converted into CR, but we also need space for LF..
189 >     * That's why we use '- 1' here -adx
190 >     */
191      if (t + tlen - buf > IRCD_BUFSIZE - 1)
192      {
193 <      *(t - 1) = '\0';  /* kill the space and terminate the string */
193 >      *(t - 1) = '\0';  /* Kill the space and terminate the string */
194        sendto_one(client_p, "%s", buf);
195        t = start;
196      }
197  
198 <    if ((ms->flags & (CHFL_CHANOP | CHFL_HALFOP)))
199 <      *t++ = (!(ms->flags & CHFL_CHANOP) && IsCapable(client_p, CAP_HOPS)) ?
200 <        '%' : '@';
201 <    if ((ms->flags & CHFL_VOICE))
198 >    if (member->flags & CHFL_CHANOP)
199 >      *t++ = '@';
200 >    if (member->flags & CHFL_HALFOP)
201 >      *t++ = '%';
202 >    if (member->flags & CHFL_VOICE)
203        *t++ = '+';
204  
205 <    if (IsCapable(client_p, CAP_TS6))
206 <      strcpy(t, ID(ms->client_p));
200 <    else
201 <      strcpy(t, ms->client_p->name);
205 >    strcpy(t, member->client_p->id);
206 >
207      t += strlen(t);
208      *t++ = ' ';
209    }
210  
211 <  /* should always be non-NULL unless we have a kind of persistent channels */
212 <  if (chptr->members.head != NULL)
213 <    t--;  /* take the space out */
211 >  /* Should always be non-NULL unless we have a kind of persistent channels */
212 >  if (chptr->members.head)
213 >    t--;  /* Take the space out */
214    *t = '\0';
215    sendto_one(client_p, "%s", buf);
216   }
217  
218 < /*! \brief sends +b/+e/+I
219 < * \param client_p client pointer to server
220 < * \param chptr    pointer to channel
221 < * \param top      pointer to top of mode link list to send
222 < * \param flag     char flag flagging type of mode. Currently this can be 'b', e' or 'I'
218 > /*! \brief Sends +b/+e/+I
219 > * \param client_p Client pointer to server
220 > * \param chptr    Pointer to channel
221 > * \param list     Pointer to list of modes to send
222 > * \param flag     Char flag flagging type of mode. Currently this can be 'b', e' or 'I'
223   */
224   static void
225 < send_mode_list(struct Client *client_p, struct Channel *chptr,
226 <               dlink_list *top, char flag)
225 > send_mode_list(struct Client *client_p, const struct Channel *chptr,
226 >               const dlink_list *list, const char flag)
227   {
228 <  int ts5 = !IsCapable(client_p, CAP_TS6);
229 <  dlink_node *lp;
230 <  struct Ban *banptr;
231 <  char pbuf[IRCD_BUFSIZE];
232 <  int tlen, mlen, cur_len, count = 0;
228 <  char *mp = NULL, *pp = pbuf;
228 >  const dlink_node *node = NULL;
229 >  char mbuf[IRCD_BUFSIZE] = "";
230 >  char pbuf[IRCD_BUFSIZE] = "";
231 >  int tlen, mlen, cur_len;
232 >  char *pp = pbuf;
233  
234 <  if (top == NULL || top->length == 0)
234 >  if (!list->length)
235      return;
236  
237 <  if (ts5)
238 <    mlen = ircsprintf(buf, ":%s MODE %s +", me.name, chptr->chname);
239 <  else
236 <    mlen = ircsprintf(buf, ":%s BMASK %lu %s %c :", me.id,
237 <                      (unsigned long)chptr->channelts, chptr->chname, flag);
238 <
239 <  /* MODE needs additional one byte for space between buf and pbuf */
240 <  cur_len = mlen + ts5;
241 <  mp = buf + mlen;
237 >  mlen = snprintf(mbuf, sizeof(mbuf), ":%s BMASK %lu %s %c :", me.id,
238 >                  (unsigned long)chptr->creationtime, chptr->name, flag);
239 >  cur_len = mlen;
240  
241 <  DLINK_FOREACH(lp, top->head)
241 >  DLINK_FOREACH(node, list->head)
242    {
243 <    banptr = lp->data;
243 >    const struct Ban *ban = node->data;
244  
245 <    /* must add another b/e/I letter if we use MODE */
248 <    tlen = banptr->len + 3 + ts5;
245 >    tlen = ban->len + 3;  /* +3 for ! + @ + space */
246  
247      /*
248 <     * send buffer and start over if we cannot fit another ban,
252 <     * or if the target is non-ts6 and we have too many modes in
253 <     * in this line.
248 >     * Send buffer and start over if we cannot fit another ban
249       */
250 <    if (cur_len + (tlen - 1) > IRCD_BUFSIZE - 2 ||
256 <        (!IsCapable(client_p, CAP_TS6) &&
257 <         (count >= MAXMODEPARAMS || pp - pbuf >= MODEBUFLEN)))
250 >    if (cur_len + (tlen - 1) > IRCD_BUFSIZE - 2)
251      {
252 <      *(pp - 1) = '\0';  /* get rid of trailing space on buffer */
253 <      sendto_one(client_p, "%s%s%s", buf, ts5 ? " " : "", pbuf);
252 >      *(pp - 1) = '\0';  /* Get rid of trailing space on buffer */
253 >      sendto_one(client_p, "%s%s", mbuf, pbuf);
254  
255 <      cur_len = mlen + ts5;
263 <      mp = buf + mlen;
255 >      cur_len = mlen;
256        pp = pbuf;
265      count = 0;
257      }
258  
259 <    count++;
269 <    if (ts5)
270 <    {
271 <      *mp++ = flag;
272 <      *mp = '\0';
273 <    }
274 <
275 <    pp += ircsprintf(pp, "%s!%s@%s ", banptr->name, banptr->username,
276 <                     banptr->host);
259 >    pp += sprintf(pp, "%s!%s@%s ", ban->name, ban->user, ban->host);
260      cur_len += tlen;
261    }
262  
263 <  *(pp - 1) = '\0';  /* get rid of trailing space on buffer */
264 <  sendto_one(client_p, "%s%s%s", buf, ts5 ? " " : "", pbuf);
263 >  *(pp - 1) = '\0';  /* Get rid of trailing space on buffer */
264 >  sendto_one(client_p, "%s%s", mbuf, pbuf);
265   }
266  
267 < /*! \brief send "client_p" a full list of the modes for channel chptr
268 < * \param client_p pointer to client client_p
269 < * \param chptr    pointer to channel pointer
267 > /*! \brief Send "client_p" a full list of the modes for channel chptr
268 > * \param client_p Pointer to client client_p
269 > * \param chptr    Pointer to channel pointer
270   */
271   void
272   send_channel_modes(struct Client *client_p, struct Channel *chptr)
273   {
274 <  *modebuf = *parabuf = '\0';
274 >  char modebuf[MODEBUFLEN] = "";
275 >  char parabuf[MODEBUFLEN] = "";
276 >
277    channel_modes(chptr, client_p, modebuf, parabuf);
278    send_members(client_p, chptr, modebuf, parabuf);
279  
# Line 297 | Line 282 | send_channel_modes(struct Client *client
282    send_mode_list(client_p, chptr, &chptr->invexlist, 'I');
283   }
284  
285 < /*! \brief check channel name for invalid characters
286 < * \param name pointer to channel name string
287 < * \param local indicates whether it's a local or remote creation
285 > /*! \brief Check channel name for invalid characters
286 > * \param name Pointer to channel name string
287 > * \param local Indicates whether it's a local or remote creation
288   * \return 0 if invalid, 1 otherwise
289   */
290   int
291 < check_channel_name(const char *name, int local)
291 > check_channel_name(const char *name, const int local)
292   {
293    const char *p = name;
294 <  const int max_length = local ? LOCAL_CHANNELLEN : CHANNELLEN;
295 <  assert(name != NULL);
294 >
295 >  assert(!EmptyString(p));
296  
297    if (!IsChanPrefix(*p))
298      return 0;
# Line 325 | Line 310 | check_channel_name(const char *name, int
310          return 0;
311    }
312  
313 <  return p - name <= max_length;
313 >  return p - name <= CHANNELLEN;
314   }
315  
316   void
317 < remove_ban(struct Ban *bptr, dlink_list *list)
317 > remove_ban(struct Ban *ban, dlink_list *list)
318   {
319 <  dlinkDelete(&bptr->node, list);
320 <
336 <  MyFree(bptr->name);
337 <  MyFree(bptr->username);
338 <  MyFree(bptr->host);
339 <  MyFree(bptr->who);
340 <
341 <  mp_pool_release(bptr);
319 >  dlinkDelete(&ban->node, list);
320 >  mp_pool_release(ban);
321   }
322  
323   /* free_channel_list()
# Line 350 | Line 329 | remove_ban(struct Ban *bptr, dlink_list
329   void
330   free_channel_list(dlink_list *list)
331   {
332 <  dlink_node *ptr = NULL, *next_ptr = NULL;
332 >  dlink_node *node = NULL, *node_next = NULL;
333  
334 <  DLINK_FOREACH_SAFE(ptr, next_ptr, list->head)
335 <    remove_ban(ptr->data, list);
357 <
358 <  assert(list->tail == NULL && list->head == NULL);
334 >  DLINK_FOREACH_SAFE(node, node_next, list->head)
335 >    remove_ban(node->data, list);
336   }
337  
338 < /*! \brief Get Channel block for chname (and allocate a new channel
338 > /*! \brief Get Channel block for name (and allocate a new channel
339   *         block, if it didn't exist before)
340 < * \param chname channel name
341 < * \return channel block
340 > * \param name Channel name
341 > * \return Channel block
342   */
343   struct Channel *
344 < make_channel(const char *chname)
344 > make_channel(const char *name)
345   {
346    struct Channel *chptr = NULL;
347  
348 <  assert(!EmptyString(chname));
348 >  assert(!EmptyString(name));
349  
350    chptr = mp_pool_get(channel_pool);
351  
352 <  memset(chptr, 0, sizeof(*chptr));
353 <
377 <  /* doesn't hurt to set it here */
378 <  chptr->channelts = CurrentTime;
352 >  /* Doesn't hurt to set it here */
353 >  chptr->creationtime = CurrentTime;
354    chptr->last_join_time = CurrentTime;
355  
356 <  strlcpy(chptr->chname, chname, sizeof(chptr->chname));
357 <  dlinkAdd(chptr, &chptr->node, &global_channel_list);
356 >  strlcpy(chptr->name, name, sizeof(chptr->name));
357 >  dlinkAdd(chptr, &chptr->node, &channel_list);
358  
359    hash_add_channel(chptr);
360  
361    return chptr;
362   }
363  
364 < /*! \brief walk through this channel, and destroy it.
365 < * \param chptr channel pointer
364 > /*! \brief Walk through this channel, and destroy it.
365 > * \param chptr Channel pointer
366   */
367   void
368   destroy_channel(struct Channel *chptr)
369   {
370 <  dlink_node *ptr = NULL, *ptr_next = NULL;
396 <
397 <  DLINK_FOREACH_SAFE(ptr, ptr_next, chptr->invites.head)
398 <    del_invite(chptr, ptr->data);
370 >  clear_invites_channel(chptr);
371  
372 <  /* free ban/exception/invex lists */
372 >  /* Free ban/exception/invex lists */
373    free_channel_list(&chptr->banlist);
374    free_channel_list(&chptr->exceptlist);
375    free_channel_list(&chptr->invexlist);
376  
377 <  dlinkDelete(&chptr->node, &global_channel_list);
377 >  dlinkDelete(&chptr->node, &channel_list);
378    hash_del_channel(chptr);
379  
380    mp_pool_release(chptr);
381   }
382  
383   /*!
384 < * \param chptr pointer to channel
385 < * \return string pointer "=" if public, "@" if secret else "*"
384 > * \param chptr Pointer to channel
385 > * \return String pointer "=" if public, "@" if secret else "*"
386   */
387   static const char *
388   channel_pub_or_secret(const struct Channel *chptr)
# Line 423 | Line 395 | channel_pub_or_secret(const struct Chann
395   }
396  
397   /*! \brief lists all names on given channel
398 < * \param source_p pointer to client struct requesting names
399 < * \param chptr    pointer to channel block
400 < * \param show_eon show ENDOFNAMES numeric or not
398 > * \param source_p Pointer to client struct requesting names
399 > * \param chptr    Pointer to channel block
400 > * \param show_eon Show RPL_ENDOFNAMES numeric or not
401   *                 (don't want it with /names with no params)
402   */
403   void
404   channel_member_names(struct Client *source_p, struct Channel *chptr,
405                       int show_eon)
406   {
407 <  struct Client *target_p = NULL;
408 <  struct Membership *ms = NULL;
437 <  dlink_node *ptr = NULL;
438 <  char lbuf[IRCD_BUFSIZE + 1];
407 >  const dlink_node *node = NULL;
408 >  char buf[IRCD_BUFSIZE + 1] = "";
409    char *t = NULL, *start = NULL;
410    int tlen = 0;
411 <  int is_member = IsMember(source_p, chptr);
412 <  int multi_prefix = HasCap(source_p, CAP_MULTI_PREFIX) != 0;
411 >  const int is_member = IsMember(source_p, chptr);
412 >  const int multi_prefix = HasCap(source_p, CAP_MULTI_PREFIX) != 0;
413 >  const int uhnames = HasCap(source_p, CAP_UHNAMES) != 0;
414 >
415 >  assert(IsClient(source_p));
416  
417    if (PubChannel(chptr) || is_member)
418    {
419 <    t = lbuf + ircsprintf(lbuf, form_str(RPL_NAMREPLY),
420 <                          me.name, source_p->name,
421 <                          channel_pub_or_secret(chptr),
449 <                          chptr->chname);
419 >    t = buf + snprintf(buf, sizeof(buf), numeric_form(RPL_NAMREPLY),
420 >                       me.name, source_p->name,
421 >                       channel_pub_or_secret(chptr), chptr->name);
422      start = t;
423  
424 <    DLINK_FOREACH(ptr, chptr->members.head)
424 >    DLINK_FOREACH(node, chptr->members.head)
425      {
426 <      ms       = ptr->data;
455 <      target_p = ms->client_p;
426 >      const struct Membership *member = node->data;
427  
428 <      if (HasUMode(target_p, UMODE_INVISIBLE) && !is_member)
428 >      if (HasUMode(member->client_p, UMODE_INVISIBLE) && !is_member)
429          continue;
430  
431 <      tlen = strlen(target_p->name) + 1;  /* nick + space */
431 >      if (!uhnames)
432 >        tlen = strlen(member->client_p->name) + 1;  /* +1 for space */
433 >      else
434 >        tlen = strlen(member->client_p->name) + strlen(member->client_p->username) +
435 >               strlen(member->client_p->host) + 3;  /* +3 for ! + @ + space */
436  
437        if (!multi_prefix)
438        {
439 <        if (ms->flags & (CHFL_CHANOP | CHFL_HALFOP | CHFL_VOICE))
439 >        if (member->flags & (CHFL_CHANOP | CHFL_HALFOP | CHFL_VOICE))
440            ++tlen;
441        }
442        else
443        {
444 <        if (ms->flags & CHFL_CHANOP)
444 >        if (member->flags & CHFL_CHANOP)
445            ++tlen;
446 <        if (ms->flags & CHFL_HALFOP)
446 >        if (member->flags & CHFL_HALFOP)
447            ++tlen;
448 <        if (ms->flags & CHFL_VOICE)
448 >        if (member->flags & CHFL_VOICE)
449            ++tlen;
450        }
451  
452 <      if (t + tlen - lbuf > IRCD_BUFSIZE - 2)
452 >      if (t + tlen - buf > IRCD_BUFSIZE - 2)
453        {
454          *(t - 1) = '\0';
455 <        sendto_one(source_p, "%s", lbuf);
455 >        sendto_one(source_p, "%s", buf);
456          t = start;
457        }
458  
459 <      t += ircsprintf(t, "%s%s ", get_member_status(ms, multi_prefix),
460 <                      target_p->name);
459 >      if (!uhnames)
460 >        t += sprintf(t, "%s%s ", get_member_status(member, multi_prefix),
461 >                     member->client_p->name);
462 >      else
463 >        t += sprintf(t, "%s%s!%s@%s ", get_member_status(member, multi_prefix),
464 >                     member->client_p->name, member->client_p->username,
465 >                     member->client_p->host);
466      }
467  
468 <    if (tlen != 0)
468 >    if (tlen)
469      {
470        *(t - 1) = '\0';
471 <      sendto_one(source_p, "%s", lbuf);
471 >      sendto_one(source_p, "%s", buf);
472      }
473    }
474  
475    if (show_eon)
476 <    sendto_one(source_p, form_str(RPL_ENDOFNAMES),
497 <               me.name, source_p->name, chptr->chname);
476 >    sendto_one_numeric(source_p, &me, RPL_ENDOFNAMES, chptr->name);
477   }
478  
479 < /*! \brief adds client to invite list
480 < * \param chptr pointer to channel block
481 < * \param who   pointer to client to add invite to
479 > /*! \brief Adds client to invite list
480 > * \param chptr Pointer to channel block
481 > * \param who   Pointer to client to add invite to
482   */
483   void
484   add_invite(struct Channel *chptr, struct Client *who)
485   {
486 +  assert(IsClient(who));
487 +
488    del_invite(chptr, who);
489  
490    /*
491 <   * delete last link in chain if the list is max length
491 >   * Delete last link in chain if the list is max length
492     */
493 <  if (dlink_list_length(&who->localClient->invited) >=
494 <      ConfigChannel.max_chans_per_user)
495 <    del_invite(who->localClient->invited.tail->data, who);
493 >  if (dlink_list_length(&who->connection->invited) >=
494 >      ConfigChannel.max_channels)
495 >    del_invite(who->connection->invited.tail->data, who);
496  
497 <  /* add client to channel invite list */
497 >  /* Add client to channel invite list */
498    dlinkAdd(who, make_dlink_node(), &chptr->invites);
499  
500 <  /* add channel to the end of the client invite list */
501 <  dlinkAdd(chptr, make_dlink_node(), &who->localClient->invited);
500 >  /* Add channel to the end of the client invite list */
501 >  dlinkAdd(chptr, make_dlink_node(), &who->connection->invited);
502   }
503  
504   /*! \brief Delete Invite block from channel invite list
505   *         and client invite list
506 < * \param chptr pointer to Channel struct
507 < * \param who   pointer to client to remove invites from
506 > * \param chptr Pointer to Channel struct
507 > * \param who   Pointer to client to remove invites from
508   */
509   void
510   del_invite(struct Channel *chptr, struct Client *who)
511   {
512 <  dlink_node *ptr = NULL;
512 >  dlink_node *node = NULL;
513 >
514 >  if ((node = dlinkFindDelete(&who->connection->invited, chptr)))
515 >    free_dlink_node(node);
516 >
517 >  if ((node = dlinkFindDelete(&chptr->invites, who)))
518 >    free_dlink_node(node);
519 > }
520  
521 <  if ((ptr = dlinkFindDelete(&who->localClient->invited, chptr)))
522 <    free_dlink_node(ptr);
521 > /*! \brief Removes all invites of a specific channel
522 > * \param chptr Pointer to Channel struct
523 > */
524 > void
525 > clear_invites_channel(struct Channel *chptr)
526 > {
527 >  dlink_node *node = NULL, *node_next = NULL;
528  
529 <  if ((ptr = dlinkFindDelete(&chptr->invites, who)))
530 <    free_dlink_node(ptr);
529 >  DLINK_FOREACH_SAFE(node, node_next, chptr->invites.head)
530 >    del_invite(chptr, node->data);
531 > }
532 >
533 > /*! \brief Removes all invites of a specific client
534 > * \param source_p Pointer to Client struct
535 > */
536 > void
537 > clear_invites_client(struct Client *source_p)
538 > {
539 >  dlink_node *node = NULL, *node_next = NULL;
540 >
541 >  DLINK_FOREACH_SAFE(node, node_next, source_p->connection->invited.head)
542 >    del_invite(node->data, source_p);
543   }
544  
545   /* get_member_status()
# Line 549 | Line 554 | del_invite(struct Channel *chptr, struct
554   * (like in get_client_name)
555   */
556   const char *
557 < get_member_status(const struct Membership *ms, int combine)
557 > get_member_status(const struct Membership *member, const int combine)
558   {
559 <  static char buffer[4];
560 <  char *p = NULL;
556 <
557 <  if (ms == NULL)
558 <    return "";
559 <  p = buffer;
559 >  static char buffer[4];  /* 4 for @%+\0 */
560 >  char *p = buffer;
561  
562 <  if (ms->flags & CHFL_CHANOP)
562 >  if (member->flags & CHFL_CHANOP)
563    {
564      if (!combine)
565        return "@";
566      *p++ = '@';
567    }
568  
569 < #ifdef HALFOPS
569 <  if (ms->flags & CHFL_HALFOP)
569 >  if (member->flags & CHFL_HALFOP)
570    {
571      if (!combine)
572        return "%";
573      *p++ = '%';
574    }
575 #endif
575  
576 <  if (ms->flags & CHFL_VOICE)
576 >  if (member->flags & CHFL_VOICE)
577      *p++ = '+';
578    *p = '\0';
579  
# Line 582 | Line 581 | get_member_status(const struct Membershi
581   }
582  
583   /*!
584 < * \param who  pointer to Client to check
585 < * \param list pointer to ban list to search
584 > * \param who  Pointer to Client to check
585 > * \param list Pointer to ban list to search
586   * \return 1 if ban found for given n!u\@h mask, 0 otherwise
588 *
587   */
588   static int
589   find_bmask(const struct Client *who, const dlink_list *const list)
590   {
591 <  const dlink_node *ptr = NULL;
591 >  const dlink_node *node = NULL;
592  
593 <  DLINK_FOREACH(ptr, list->head)
593 >  DLINK_FOREACH(node, list->head)
594    {
595 <    const struct Ban *bp = ptr->data;
595 >    const struct Ban *ban = node->data;
596  
597 <    if (!match(bp->name, who->name) && !match(bp->username, who->username))
597 >    if (!match(ban->name, who->name) && !match(ban->user, who->username))
598      {
599 <      switch (bp->type)
599 >      switch (ban->type)
600        {
601          case HM_HOST:
602 <          if (!match(bp->host, who->host) || !match(bp->host, who->sockhost))
602 >          if (!match(ban->host, who->host) || !match(ban->host, who->sockhost))
603              return 1;
604            break;
605          case HM_IPV4:
606 <          if (who->localClient->aftype == AF_INET)
607 <            if (match_ipv4(&who->localClient->ip, &bp->addr, bp->bits))
606 >          if (who->connection->aftype == AF_INET)
607 >            if (match_ipv4(&who->connection->ip, &ban->addr, ban->bits))
608                return 1;
609            break;
612 #ifdef IPV6
610          case HM_IPV6:
611 <          if (who->localClient->aftype == AF_INET6)
612 <            if (match_ipv6(&who->localClient->ip, &bp->addr, bp->bits))
611 >          if (who->connection->aftype == AF_INET6)
612 >            if (match_ipv6(&who->connection->ip, &ban->addr, ban->bits))
613                return 1;
614            break;
618 #endif
615          default:
616            assert(0);
617        }
# Line 626 | Line 622 | find_bmask(const struct Client *who, con
622   }
623  
624   /*!
625 < * \param chptr pointer to channel block
626 < * \param who   pointer to client to check access fo
625 > * \param chptr Pointer to channel block
626 > * \param who   Pointer to client to check access fo
627   * \return 0 if not banned, 1 otherwise
628   */
629   int
# Line 640 | Line 636 | is_banned(const struct Channel *chptr, c
636    return 0;
637   }
638  
639 < /*!
640 < * \param source_p pointer to client attempting to join
641 < * \param chptr    pointer to channel
642 < * \param key      key sent by client attempting to join if present
639 > /*! Tests if a client can join a certain channel
640 > * \param source_p Pointer to client attempting to join
641 > * \param chptr    Pointer to channel
642 > * \param key      Key sent by client attempting to join if present
643   * \return ERR_BANNEDFROMCHAN, ERR_INVITEONLYCHAN, ERR_CHANNELISFULL
644   *         or 0 if allowed to join.
645   */
646   int
647 < can_join(struct Client *source_p, struct Channel *chptr, const char *key)
647 > can_join(struct Client *source_p, const struct Channel *chptr, const char *key)
648   {
649 <  if (is_banned(chptr, source_p))
654 <    return ERR_BANNEDFROMCHAN;
655 <
656 < #ifdef HAVE_LIBCRYPTO
657 <  if ((chptr->mode.mode & MODE_SSLONLY) && !source_p->localClient->fd.ssl)
649 >  if ((chptr->mode.mode & MODE_SSLONLY) && !HasUMode(source_p, UMODE_SSL))
650      return ERR_SSLONLYCHAN;
659 #endif
651  
652    if ((chptr->mode.mode & MODE_REGONLY) && !HasUMode(source_p, UMODE_REGISTERED))
653      return ERR_NEEDREGGEDNICK;
# Line 665 | Line 656 | can_join(struct Client *source_p, struct
656      return ERR_OPERONLYCHAN;
657  
658    if (chptr->mode.mode & MODE_INVITEONLY)
659 <    if (!dlinkFind(&source_p->localClient->invited, chptr))
659 >    if (!dlinkFind(&source_p->connection->invited, chptr))
660        if (!find_bmask(source_p, &chptr->invexlist))
661          return ERR_INVITEONLYCHAN;
662  
# Line 676 | Line 667 | can_join(struct Client *source_p, struct
667        chptr->mode.limit)
668      return ERR_CHANNELISFULL;
669  
670 +  if (is_banned(chptr, source_p))
671 +    return ERR_BANNEDFROMCHAN;
672 +
673    return 0;
674   }
675  
676   int
677 < has_member_flags(struct Membership *ms, unsigned int flags)
677 > has_member_flags(const struct Membership *member, const unsigned int flags)
678   {
679 <  if (ms != NULL)
686 <    return ms->flags & flags;
687 <  return 0;
679 >  return member && (member->flags & flags);
680   }
681  
682   struct Membership *
683   find_channel_link(struct Client *client_p, struct Channel *chptr)
684   {
685 <  dlink_node *ptr = NULL;
685 >  dlink_node *node = NULL;
686  
687    if (!IsClient(client_p))
688      return NULL;
689  
690 <  DLINK_FOREACH(ptr, client_p->channel.head)
691 <    if (((struct Membership *)ptr->data)->chptr == chptr)
692 <      return ptr->data;
690 >  if (dlink_list_length(&chptr->members) < dlink_list_length(&client_p->channel))
691 >  {
692 >    DLINK_FOREACH(node, chptr->members.head)
693 >      if (((struct Membership *)node->data)->client_p == client_p)
694 >        return node->data;
695 >  }
696 >  else
697 >  {
698 >    DLINK_FOREACH(node, client_p->channel.head)
699 >      if (((struct Membership *)node->data)->chptr == chptr)
700 >        return node->data;
701 >  }
702  
703    return NULL;
704   }
705  
706 < /*!
707 < * \param chptr    pointer to Channel struct
708 < * \param source_p pointer to Client struct
709 < * \param ms       pointer to Membership struct (can be NULL)
706 > /*! Checks if a message contains control codes
707 > * \param message The actual message string the client wants to send
708 > * \return 1 if the message does contain any control codes, 0 otherwise
709 > */
710 > static int
711 > msg_has_ctrls(const char *message)
712 > {
713 >  const unsigned char *p = (const unsigned char *)message;
714 >
715 >  for (; *p; ++p)
716 >  {
717 >    if (*p > 31 || *p == 1)
718 >      continue;  /* No control code or CTCP */
719 >
720 >    if (*p == 27)  /* Escape */
721 >    {
722 >      /* ISO 2022 charset shift sequence */
723 >      if (*(p + 1) == '$' ||
724 >          *(p + 1) == '(')
725 >      {
726 >        ++p;
727 >        continue;
728 >      }
729 >    }
730 >
731 >    return 1;  /* Control code */
732 >  }
733 >
734 >  return 0;  /* No control code found */
735 > }
736 >
737 > /*! Tests if a client can send to a channel
738 > * \param chptr    Pointer to Channel struct
739 > * \param source_p Pointer to Client struct
740 > * \param member   Pointer to Membership struct (can be NULL)
741 > * \param message  The actual message string the client wants to send
742   * \return CAN_SEND_OPV if op or voiced on channel\n
743   *         CAN_SEND_NONOP if can send to channel but is not an op\n
744   *         ERR_CANNOTSENDTOCHAN or ERR_NEEDREGGEDNICK if they cannot send to channel\n
745   */
746   int
747 < can_send(struct Channel *chptr, struct Client *source_p, struct Membership *ms)
747 > can_send(struct Channel *chptr, struct Client *source_p,
748 >         struct Membership *member, const char *message)
749   {
750 +  const struct MaskItem *conf = NULL;
751 +
752    if (IsServer(source_p) || HasFlag(source_p, FLAGS_SERVICE))
753      return CAN_SEND_OPV;
754  
755    if (MyClient(source_p) && !IsExemptResv(source_p))
756 <    if (!(HasUMode(source_p, UMODE_OPER) && ConfigFileEntry.oper_pass_resv))
757 <      if (!hash_find_resv(chptr->chname) == ConfigChannel.restrict_channels)
756 >    if (!(HasUMode(source_p, UMODE_OPER) && ConfigGeneral.oper_pass_resv))
757 >      if ((conf = match_find_resv(chptr->name)) && !resv_find_exempt(source_p, conf))
758          return ERR_CANNOTSENDTOCHAN;
759  
760 <  if (ms != NULL || (ms = find_channel_link(source_p, chptr)))
761 <  {
762 <    if (ms->flags & (CHFL_CHANOP|CHFL_HALFOP|CHFL_VOICE))
760 >  if ((chptr->mode.mode & MODE_NOCTRL) && msg_has_ctrls(message))
761 >    return ERR_NOCTRLSONCHAN;
762 >
763 >  if (chptr->mode.mode & MODE_NOCTCP)
764 >    if (*message == '\001' && strncmp(message + 1, "ACTION ", 7))
765 >      return ERR_NOCTCP;
766 >
767 >  if (member || (member = find_channel_link(source_p, chptr)))
768 >    if (member->flags & (CHFL_CHANOP | CHFL_HALFOP | CHFL_VOICE))
769        return CAN_SEND_OPV;
770  
771 <    /* cache can send if quiet_on_ban and banned */
772 <    if (ConfigChannel.quiet_on_ban && MyClient(source_p))
771 >  if (!member && (chptr->mode.mode & MODE_NOPRIVMSGS))
772 >    return ERR_CANNOTSENDTOCHAN;
773 >
774 >  if (chptr->mode.mode & MODE_MODERATED)
775 >    return ERR_CANNOTSENDTOCHAN;
776 >
777 >  if ((chptr->mode.mode & MODE_MODREG) && !HasUMode(source_p, UMODE_REGISTERED))
778 >    return ERR_NEEDREGGEDNICK;
779 >
780 >  /* Cache can send if banned */
781 >  if (MyClient(source_p))
782 >  {
783 >    if (member)
784      {
785 <      if (ms->flags & CHFL_BAN_SILENCED)
785 >      if (member->flags & CHFL_BAN_SILENCED)
786          return ERR_CANNOTSENDTOCHAN;
787  
788 <      if (!(ms->flags & CHFL_BAN_CHECKED))
788 >      if (!(member->flags & CHFL_BAN_CHECKED))
789        {
790          if (is_banned(chptr, source_p))
791          {
792 <          ms->flags |= (CHFL_BAN_CHECKED|CHFL_BAN_SILENCED);
792 >          member->flags |= (CHFL_BAN_CHECKED | CHFL_BAN_SILENCED);
793            return ERR_CANNOTSENDTOCHAN;
794          }
795  
796 <        ms->flags |= CHFL_BAN_CHECKED;
796 >        member->flags |= CHFL_BAN_CHECKED;
797        }
798      }
799 +    else if (is_banned(chptr, source_p))
800 +      return ERR_CANNOTSENDTOCHAN;
801    }
747  else if (chptr->mode.mode & MODE_NOPRIVMSGS)
748    return ERR_CANNOTSENDTOCHAN;
749
750  if (chptr->mode.mode & MODE_MODERATED)
751    return ERR_CANNOTSENDTOCHAN;
802  
803    return CAN_SEND_NONOP;
804   }
# Line 756 | Line 806 | can_send(struct Channel *chptr, struct C
806   /*! \brief Updates the client's oper_warn_count_down, warns the
807   *         IRC operators if necessary, and updates
808   *         join_leave_countdown as needed.
809 < * \param source_p pointer to struct Client to check
810 < * \param name     channel name or NULL if this is a part.
809 > * \param source_p Pointer to struct Client to check
810 > * \param name     Channel name or NULL if this is a part.
811   */
812   void
813   check_spambot_warning(struct Client *source_p, const char *name)
# Line 766 | Line 816 | check_spambot_warning(struct Client *sou
816    int decrement_count = 0;
817  
818    if ((GlobalSetOptions.spam_num &&
819 <       (source_p->localClient->join_leave_count >=
819 >       (source_p->connection->join_leave_count >=
820          GlobalSetOptions.spam_num)))
821    {
822 <    if (source_p->localClient->oper_warn_count_down > 0)
823 <      source_p->localClient->oper_warn_count_down--;
822 >    if (source_p->connection->oper_warn_count_down > 0)
823 >      source_p->connection->oper_warn_count_down--;
824      else
825 <      source_p->localClient->oper_warn_count_down = 0;
825 >      source_p->connection->oper_warn_count_down = 0;
826  
827 <    if (source_p->localClient->oper_warn_count_down == 0)
827 >    if (source_p->connection->oper_warn_count_down == 0)
828      {
829 <      /* Its already known as a possible spambot */
830 <      if (name != NULL)
829 >      /* It's already known as a possible spambot */
830 >      if (name)
831          sendto_realops_flags(UMODE_BOTS, L_ALL, SEND_NOTICE,
832                               "User %s (%s@%s) trying to join %s is a possible spambot",
833                               source_p->name, source_p->username,
# Line 787 | Line 837 | check_spambot_warning(struct Client *sou
837                               "User %s (%s@%s) is a possible spambot",
838                               source_p->name, source_p->username,
839                               source_p->host);
840 <      source_p->localClient->oper_warn_count_down = OPER_SPAM_COUNTDOWN;
840 >      source_p->connection->oper_warn_count_down = OPER_SPAM_COUNTDOWN;
841      }
842    }
843    else
844    {
845 <    if ((t_delta = (CurrentTime - source_p->localClient->last_leave_time)) >
845 >    if ((t_delta = (CurrentTime - source_p->connection->last_leave_time)) >
846           JOIN_LEAVE_COUNT_EXPIRE_TIME)
847      {
848        decrement_count = (t_delta / JOIN_LEAVE_COUNT_EXPIRE_TIME);
849 <      if (decrement_count > source_p->localClient->join_leave_count)
850 <        source_p->localClient->join_leave_count = 0;
849 >
850 >      if (decrement_count > source_p->connection->join_leave_count)
851 >        source_p->connection->join_leave_count = 0;
852        else
853 <        source_p->localClient->join_leave_count -= decrement_count;
853 >        source_p->connection->join_leave_count -= decrement_count;
854      }
855      else
856      {
857 <      if ((CurrentTime - (source_p->localClient->last_join_time)) <
857 >      if ((CurrentTime - (source_p->connection->last_join_time)) <
858            GlobalSetOptions.spam_time)
859 <      {
809 <        /* oh, its a possible spambot */
810 <        source_p->localClient->join_leave_count++;
811 <      }
859 >        source_p->connection->join_leave_count++;  /* It's a possible spambot */
860      }
861  
862 <    if (name != NULL)
863 <      source_p->localClient->last_join_time = CurrentTime;
862 >    if (name)
863 >      source_p->connection->last_join_time = CurrentTime;
864      else
865 <      source_p->localClient->last_leave_time = CurrentTime;
865 >      source_p->connection->last_leave_time = CurrentTime;
866    }
867   }
868  
869 < /*! \brief compares usercount and servercount against their split
869 > /*! \brief Compares usercount and servercount against their split
870   *         values and adjusts splitmode accordingly
871   * \param unused Unused address pointer
872   */
# Line 828 | Line 876 | check_splitmode(void *unused)
876    if (splitchecking && (ConfigChannel.no_join_on_split ||
877                          ConfigChannel.no_create_on_split))
878    {
879 <    const unsigned int server = dlink_list_length(&global_serv_list);
879 >    const unsigned int server = dlink_list_length(&global_server_list);
880  
881      if (!splitmode && ((server < split_servers) || (Count.total < split_users)))
882      {
# Line 836 | Line 884 | check_splitmode(void *unused)
884  
885        sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
886                             "Network split, activating splitmode");
887 <      eventAddIsh("check_splitmode", check_splitmode, NULL, 10);
887 >      event_add(&splitmode_event, NULL);
888      }
889 <    else if (splitmode && (server > split_servers) && (Count.total > split_users))
889 >    else if (splitmode && (server >= split_servers) && (Count.total >= split_users))
890      {
891        splitmode = 0;
892  
893        sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
894                             "Network rejoined, deactivating splitmode");
895 <      eventDelete(check_splitmode, NULL);
895 >      event_delete(&splitmode_event);
896      }
897    }
898   }
899  
900 < /*! \brief Sets the channel topic for chptr
900 > /*! \brief Sets the channel topic for a certain channel
901   * \param chptr      Pointer to struct Channel
902   * \param topic      The topic string
903   * \param topic_info n!u\@h formatted string of the topic setter
904 < * \param topicts    timestamp on the topic
904 > * \param topicts    Timestamp on the topic
905 > * \param local      Whether the topic is set by a local client
906   */
907   void
908 < set_channel_topic(struct Channel *chptr, const char *topic,
909 <                  const char *topic_info, time_t topicts)
908 > channel_set_topic(struct Channel *chptr, const char *topic,
909 >                  const char *topic_info, time_t topicts, int local)
910   {
911 <  strlcpy(chptr->topic, topic, sizeof(chptr->topic));
911 >  if (local)
912 >    strlcpy(chptr->topic, topic, IRCD_MIN(sizeof(chptr->topic), ConfigServerInfo.max_topic_length + 1));
913 >  else
914 >    strlcpy(chptr->topic, topic, sizeof(chptr->topic));
915 >
916    strlcpy(chptr->topic_info, topic_info, sizeof(chptr->topic_info));
917 <  chptr->topic_time = topicts;
917 >  chptr->topic_time = topicts;
918 > }
919 >
920 > /* do_join_0()
921 > *
922 > * inputs       - pointer to client doing join 0
923 > * output       - NONE
924 > * side effects - Use has decided to join 0. This is legacy
925 > *                from the days when channels were numbers not names. *sigh*
926 > *                There is a bunch of evilness necessary here due to
927 > *                anti spambot code.
928 > */
929 > void
930 > channel_do_join_0(struct Client *source_p)
931 > {
932 >  dlink_node *node = NULL, *node_next = NULL;
933 >
934 >  if (source_p->channel.head)
935 >    if (MyConnect(source_p) && !HasUMode(source_p, UMODE_OPER))
936 >      check_spambot_warning(source_p, NULL);
937 >
938 >  DLINK_FOREACH_SAFE(node, node_next, source_p->channel.head)
939 >  {
940 >    struct Channel *chptr = ((struct Membership *)node->data)->chptr;
941 >
942 >    sendto_server(source_p, 0, 0, ":%s PART %s",
943 >                  source_p->id, chptr->name);
944 >    sendto_channel_local(0, chptr, ":%s!%s@%s PART %s",
945 >                         source_p->name, source_p->username,
946 >                         source_p->host, chptr->name);
947 >
948 >    remove_user_from_channel(node->data);
949 >  }
950 > }
951 >
952 > static char *
953 > channel_find_last0(struct Client *source_p, char *chanlist)
954 > {
955 >  int join0 = 0;
956 >
957 >  for (char *p = chanlist; *p; ++p)  /* Find last "JOIN 0" */
958 >  {
959 >    if (*p == '0' && (*(p + 1) == ',' || *(p + 1) == '\0'))
960 >    {
961 >      if (*(p + 1) == ',')
962 >        ++p;
963 >
964 >      chanlist = p + 1;
965 >      join0 = 1;
966 >    }
967 >    else
968 >    {
969 >      while (*p != ',' && *p != '\0')  /* Skip past channel name */
970 >        ++p;
971 >
972 >      if (*p == '\0')  /* Hit the end */
973 >        break;
974 >    }
975 >  }
976 >
977 >  if (join0)
978 >    channel_do_join_0(source_p);
979 >
980 >  return chanlist;
981 > }
982 >
983 > void
984 > channel_do_join(struct Client *source_p, char *channel, char *key_list)
985 > {
986 >  char *p = NULL;
987 >  char *chan = NULL;
988 >  char *chan_list = NULL;
989 >  struct Channel *chptr = NULL;
990 >  struct MaskItem *conf = NULL;
991 >  const struct ClassItem *const class = get_class_ptr(&source_p->connection->confs);
992 >  int i = 0;
993 >  unsigned int flags = 0;
994 >
995 >  assert(IsClient(source_p));
996 >
997 >  chan_list = channel_find_last0(source_p, channel);
998 >
999 >  for (chan = strtoken(&p, chan_list, ","); chan;
1000 >       chan = strtoken(&p,      NULL, ","))
1001 >  {
1002 >    const char *key = NULL;
1003 >
1004 >    /* If we have any more keys, take the first for this channel. */
1005 >    if (!EmptyString(key_list) && (key_list = strchr(key = key_list, ',')))
1006 >      *key_list++ = '\0';
1007 >
1008 >    /* Empty keys are the same as no keys. */
1009 >    if (key && *key == '\0')
1010 >      key = NULL;
1011 >
1012 >    if (!check_channel_name(chan, 1))
1013 >    {
1014 >      sendto_one_numeric(source_p, &me, ERR_BADCHANNAME, chan);
1015 >      continue;
1016 >    }
1017 >
1018 >    if (!IsExemptResv(source_p) &&
1019 >        !(HasUMode(source_p, UMODE_OPER) && ConfigGeneral.oper_pass_resv) &&
1020 >        ((conf = match_find_resv(chan)) && !resv_find_exempt(source_p, conf)))
1021 >    {
1022 >      ++conf->count;
1023 >      sendto_one_numeric(source_p, &me, ERR_CHANBANREASON, chan, conf->reason);
1024 >      sendto_realops_flags(UMODE_REJ, L_ALL, SEND_NOTICE,
1025 >                           "Forbidding reserved channel %s from user %s",
1026 >                           chan, get_client_name(source_p, HIDE_IP));
1027 >      continue;
1028 >    }
1029 >
1030 >    if (dlink_list_length(&source_p->channel) >=
1031 >        ((class->max_channels) ? class->max_channels : ConfigChannel.max_channels))
1032 >    {
1033 >      sendto_one_numeric(source_p, &me, ERR_TOOMANYCHANNELS, chan);
1034 >      break;
1035 >    }
1036 >
1037 >    if ((chptr = hash_find_channel(chan)))
1038 >    {
1039 >      if (IsMember(source_p, chptr))
1040 >        continue;
1041 >
1042 >      if (splitmode && !HasUMode(source_p, UMODE_OPER) &&
1043 >          ConfigChannel.no_join_on_split)
1044 >      {
1045 >        sendto_one_numeric(source_p, &me, ERR_UNAVAILRESOURCE, chptr->name);
1046 >        continue;
1047 >      }
1048 >
1049 >      /*
1050 >       * can_join checks for +i key, bans.
1051 >       */
1052 >      if ((i = can_join(source_p, chptr, key)))
1053 >      {
1054 >        sendto_one_numeric(source_p, &me, i, chptr->name);
1055 >        continue;
1056 >      }
1057 >
1058 >      /*
1059 >       * This should never be the case unless there is some sort of
1060 >       * persistant channels.
1061 >       */
1062 >      if (!dlink_list_length(&chptr->members))
1063 >        flags = CHFL_CHANOP;
1064 >      else
1065 >        flags = 0;
1066 >    }
1067 >    else
1068 >    {
1069 >      if (splitmode && !HasUMode(source_p, UMODE_OPER) &&
1070 >          (ConfigChannel.no_create_on_split || ConfigChannel.no_join_on_split))
1071 >      {
1072 >        sendto_one_numeric(source_p, &me, ERR_UNAVAILRESOURCE, chan);
1073 >        continue;
1074 >      }
1075 >
1076 >      flags = CHFL_CHANOP;
1077 >      chptr = make_channel(chan);
1078 >    }
1079 >
1080 >    if (!HasUMode(source_p, UMODE_OPER))
1081 >      check_spambot_warning(source_p, chptr->name);
1082 >
1083 >    add_user_to_channel(chptr, source_p, flags, 1);
1084 >
1085 >    /*
1086 >     * Set timestamp if appropriate, and propagate
1087 >     */
1088 >    if (flags == CHFL_CHANOP)
1089 >    {
1090 >      chptr->creationtime = CurrentTime;
1091 >      chptr->mode.mode |= MODE_TOPICLIMIT;
1092 >      chptr->mode.mode |= MODE_NOPRIVMSGS;
1093 >
1094 >      sendto_server(source_p, 0, 0, ":%s SJOIN %lu %s +nt :@%s",
1095 >                    me.id, (unsigned long)chptr->creationtime,
1096 >                    chptr->name, source_p->id);
1097 >
1098 >      /*
1099 >       * Notify all other users on the new channel
1100 >       */
1101 >      sendto_channel_local_butone(NULL, CAP_EXTENDED_JOIN, 0, chptr, ":%s!%s@%s JOIN %s %s :%s",
1102 >                                  source_p->name, source_p->username,
1103 >                                  source_p->host, chptr->name,
1104 >                                  (!IsDigit(source_p->account[0]) && source_p->account[0] != '*') ? source_p->account : "*",
1105 >                                  source_p->info);
1106 >      sendto_channel_local_butone(NULL, 0, CAP_EXTENDED_JOIN, chptr, ":%s!%s@%s JOIN :%s",
1107 >                                  source_p->name, source_p->username,
1108 >                                  source_p->host, chptr->name);
1109 >      sendto_channel_local(0, chptr, ":%s MODE %s +nt",
1110 >                           me.name, chptr->name);
1111 >
1112 >      if (source_p->away[0])
1113 >        sendto_channel_local_butone(source_p, CAP_AWAY_NOTIFY, 0, chptr,
1114 >                                    ":%s!%s@%s AWAY :%s",
1115 >                                    source_p->name, source_p->username,
1116 >                                    source_p->host, source_p->away);
1117 >    }
1118 >    else
1119 >    {
1120 >      sendto_server(source_p, 0, 0, ":%s JOIN %lu %s +",
1121 >                    source_p->id, (unsigned long)chptr->creationtime,
1122 >                    chptr->name);
1123 >
1124 >      sendto_channel_local_butone(NULL, CAP_EXTENDED_JOIN, 0, chptr, ":%s!%s@%s JOIN %s %s :%s",
1125 >                                  source_p->name, source_p->username,
1126 >                                  source_p->host, chptr->name,
1127 >                                  (!IsDigit(source_p->account[0]) && source_p->account[0] != '*') ? source_p->account : "*",
1128 >                                  source_p->info);
1129 >      sendto_channel_local_butone(NULL, 0, CAP_EXTENDED_JOIN, chptr, ":%s!%s@%s JOIN :%s",
1130 >                                  source_p->name, source_p->username,
1131 >                                  source_p->host, chptr->name);
1132 >
1133 >      if (source_p->away[0])
1134 >        sendto_channel_local_butone(source_p, CAP_AWAY_NOTIFY, 0, chptr,
1135 >                                    ":%s!%s@%s AWAY :%s",
1136 >                                    source_p->name, source_p->username,
1137 >                                    source_p->host, source_p->away);
1138 >    }
1139 >
1140 >    del_invite(chptr, source_p);
1141 >
1142 >    if (chptr->topic[0])
1143 >    {
1144 >      sendto_one_numeric(source_p, &me, RPL_TOPIC, chptr->name, chptr->topic);
1145 >      sendto_one_numeric(source_p, &me, RPL_TOPICWHOTIME, chptr->name,
1146 >                         chptr->topic_info, chptr->topic_time);
1147 >    }
1148 >
1149 >    channel_member_names(source_p, chptr, 1);
1150 >
1151 >    source_p->connection->last_join_time = CurrentTime;
1152 >  }
1153 > }
1154 >
1155 > /*! \brief Removes a client from a specific channel
1156 > * \param source_p Pointer to source client to remove
1157 > * \param name     Name of channel to remove from
1158 > * \param reason   Part reason to show
1159 > */
1160 > static void
1161 > channel_part_one_client(struct Client *source_p, const char *name, const char *reason)
1162 > {
1163 >  struct Channel *chptr = NULL;
1164 >  struct Membership *member = NULL;
1165 >
1166 >  if ((chptr = hash_find_channel(name)) == NULL)
1167 >  {
1168 >    sendto_one_numeric(source_p, &me, ERR_NOSUCHCHANNEL, name);
1169 >    return;
1170 >  }
1171 >
1172 >  if ((member = find_channel_link(source_p, chptr)) == NULL)
1173 >  {
1174 >    sendto_one_numeric(source_p, &me, ERR_NOTONCHANNEL, chptr->name);
1175 >    return;
1176 >  }
1177 >
1178 >  if (MyConnect(source_p) && !HasUMode(source_p, UMODE_OPER))
1179 >    check_spambot_warning(source_p, NULL);
1180 >
1181 >  /*
1182 >   * Remove user from the old channel (if any)
1183 >   * only allow /part reasons in -m chans
1184 >   */
1185 >  if (*reason && (!MyConnect(source_p) ||
1186 >      ((can_send(chptr, source_p, member, reason) &&
1187 >       (source_p->connection->firsttime + ConfigGeneral.anti_spam_exit_message_time)
1188 >        < CurrentTime))))
1189 >  {
1190 >    sendto_server(source_p, 0, 0, ":%s PART %s :%s",
1191 >                  source_p->id, chptr->name, reason);
1192 >    sendto_channel_local(0, chptr, ":%s!%s@%s PART %s :%s",
1193 >                         source_p->name, source_p->username,
1194 >                         source_p->host, chptr->name, reason);
1195 >  }
1196 >  else
1197 >  {
1198 >    sendto_server(source_p, 0, 0, ":%s PART %s",
1199 >                  source_p->id, chptr->name);
1200 >    sendto_channel_local(0, chptr, ":%s!%s@%s PART %s",
1201 >                         source_p->name, source_p->username,
1202 >                         source_p->host, chptr->name);
1203 >  }
1204 >
1205 >  remove_user_from_channel(member);
1206 > }
1207 >
1208 > void
1209 > channel_do_part(struct Client *source_p, char *channel, const char *reason)
1210 > {
1211 >  char *p = NULL, *name = NULL;
1212 >  char buf[KICKLEN + 1] = "";
1213 >
1214 >  assert(IsClient(source_p));
1215 >
1216 >  if (!EmptyString(reason))
1217 >    strlcpy(buf, reason, sizeof(buf));
1218 >
1219 >  for (name = strtoken(&p, channel, ","); name;
1220 >       name = strtoken(&p,    NULL, ","))
1221 >    channel_part_one_client(source_p, name, buf);
1222   }

Diff Legend

Removed lines
+ Added lines
< Changed lines (old)
> Changed lines (new)