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

Diff Legend

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