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 2148 by michael, Fri May 31 17:18:20 2013 UTC vs.
Revision 7012 by michael, Fri Jan 1 13:52:27 2016 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-2016 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 client_p   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,
74 > add_user_to_channel(struct Channel *chptr, struct Client *client_p,
75                      unsigned int flags, int flood_ctrl)
76   {
77 <  struct Membership *ms = NULL;
77 >  struct Membership *member = NULL;
78 >
79 >  assert(IsClient(client_p));
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 106 | Line 101 | add_user_to_channel(struct Channel *chpt
101          SetJoinFloodNoticed(chptr);
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);
104 >                             get_client_name(client_p, HIDE_IP),
105 >                             client_p->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 = client_p;
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(client_p))
120 >    dlinkAdd(member, &member->locchannode, &chptr->locmembers);
121 >
122 >  dlinkAdd(member, &member->usernode, &client_p->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 */
163 >  start = t = buf + snprintf(buf, sizeof(buf), ":%s SJOIN %ju %s %s %s:",
164 >                             me.id, 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 (!dlink_list_length(list))
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 %ju %s %c :", me.id,
231 >                  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 >  chptr->name_len = strlcpy(chptr->name, name, sizeof(chptr->name));
350 >  if (chptr->name_len >= sizeof(chptr->name))
351 >    chptr->name_len = sizeof(chptr->name) - 1;
352  
353 +  dlinkAdd(chptr, &chptr->node, &channel_list);
354    hash_add_channel(chptr);
355  
356    return chptr;
357   }
358  
359 < /*! \brief walk through this channel, and destroy it.
360 < * \param chptr channel pointer
359 > /*! \brief Walk through this channel, and destroy it.
360 > * \param chptr Channel pointer
361   */
362   void
363 < destroy_channel(struct Channel *chptr)
363 > channel_free(struct Channel *chptr)
364   {
365 <  dlink_node *ptr = NULL, *ptr_next = NULL;
395 <
396 <  DLINK_FOREACH_SAFE(ptr, ptr_next, chptr->invites.head)
397 <    del_invite(chptr, ptr->data);
365 >  clear_invites_channel(chptr);
366  
367 <  /* free ban/exception/invex lists */
368 <  free_channel_list(&chptr->banlist);
369 <  free_channel_list(&chptr->exceptlist);
370 <  free_channel_list(&chptr->invexlist);
367 >  /* Free ban/exception/invex lists */
368 >  channel_free_mask_list(&chptr->banlist);
369 >  channel_free_mask_list(&chptr->exceptlist);
370 >  channel_free_mask_list(&chptr->invexlist);
371  
372 <  dlinkDelete(&chptr->node, &global_channel_list);
372 >  dlinkDelete(&chptr->node, &channel_list);
373    hash_del_channel(chptr);
374  
375    mp_pool_release(chptr);
376   }
377  
378   /*!
379 < * \param chptr pointer to channel
380 < * \return string pointer "=" if public, "@" if secret else "*"
379 > * \param chptr Pointer to channel
380 > * \return String pointer "=" if public, "@" if secret else "*"
381   */
382   static const char *
383   channel_pub_or_secret(const struct Channel *chptr)
# Line 422 | Line 390 | channel_pub_or_secret(const struct Chann
390   }
391  
392   /*! \brief lists all names on given channel
393 < * \param source_p pointer to client struct requesting names
394 < * \param chptr    pointer to channel block
395 < * \param show_eon show ENDOFNAMES numeric or not
393 > * \param client_p Pointer to client struct requesting names
394 > * \param chptr    Pointer to channel block
395 > * \param show_eon Show RPL_ENDOFNAMES numeric or not
396   *                 (don't want it with /names with no params)
397   */
398   void
399 < channel_member_names(struct Client *source_p, struct Channel *chptr,
399 > channel_member_names(struct Client *client_p, struct Channel *chptr,
400                       int show_eon)
401   {
402 <  const dlink_node *ptr = NULL;
403 <  char lbuf[IRCD_BUFSIZE + 1];
402 >  const dlink_node *node = NULL;
403 >  char buf[IRCD_BUFSIZE + 1] = "";
404    char *t = NULL, *start = NULL;
405    int tlen = 0;
406 <  int is_member = IsMember(source_p, chptr);
407 <  int multi_prefix = HasCap(source_p, CAP_MULTI_PREFIX) != 0;
406 >  const int is_member = IsMember(client_p, chptr);
407 >  const int multi_prefix = HasCap(client_p, CAP_MULTI_PREFIX) != 0;
408 >  const int uhnames = HasCap(client_p, CAP_UHNAMES) != 0;
409 >
410 >  assert(IsClient(client_p));
411  
412    if (PubChannel(chptr) || is_member)
413    {
414 <    t = lbuf + snprintf(lbuf, sizeof(lbuf), form_str(RPL_NAMREPLY),
415 <                        me.name, source_p->name,
416 <                        channel_pub_or_secret(chptr), chptr->chname);
414 >    t = buf + snprintf(buf, sizeof(buf), numeric_form(RPL_NAMREPLY),
415 >                       me.name, client_p->name,
416 >                       channel_pub_or_secret(chptr), chptr->name);
417      start = t;
418  
419 <    DLINK_FOREACH(ptr, chptr->members.head)
419 >    DLINK_FOREACH(node, chptr->members.head)
420      {
421 <      const struct Membership *ms = ptr->data;
421 >      const struct Membership *member = node->data;
422  
423 <      if (HasUMode(ms->client_p, UMODE_INVISIBLE) && !is_member)
423 >      if (HasUMode(member->client_p, UMODE_INVISIBLE) && !is_member)
424          continue;
425  
426 <      tlen = strlen(ms->client_p->name) + 1;  /* nick + space */
426 >      if (!uhnames)
427 >        tlen = strlen(member->client_p->name) + 1;  /* +1 for space */
428 >      else
429 >        tlen = strlen(member->client_p->name) + strlen(member->client_p->username) +
430 >               strlen(member->client_p->host) + 3;  /* +3 for ! + @ + space */
431  
432        if (!multi_prefix)
433        {
434 <        if (ms->flags & (CHFL_CHANOP | CHFL_HALFOP | CHFL_VOICE))
434 >        if (member->flags & (CHFL_CHANOP | CHFL_HALFOP | CHFL_VOICE))
435            ++tlen;
436        }
437        else
438        {
439 <        if (ms->flags & CHFL_CHANOP)
439 >        if (member->flags & CHFL_CHANOP)
440            ++tlen;
441 <        if (ms->flags & CHFL_HALFOP)
441 >        if (member->flags & CHFL_HALFOP)
442            ++tlen;
443 <        if (ms->flags & CHFL_VOICE)
443 >        if (member->flags & CHFL_VOICE)
444            ++tlen;
445        }
446  
447 <      if (t + tlen - lbuf > IRCD_BUFSIZE - 2)
447 >      if (t + tlen - buf > IRCD_BUFSIZE - 2)
448        {
449          *(t - 1) = '\0';
450 <        sendto_one(source_p, "%s", lbuf);
450 >        sendto_one(client_p, "%s", buf);
451          t = start;
452        }
453  
454 <      t += sprintf(t, "%s%s ", get_member_status(ms, multi_prefix),
455 <                   ms->client_p->name);
454 >      if (!uhnames)
455 >        t += sprintf(t, "%s%s ", get_member_status(member, multi_prefix),
456 >                     member->client_p->name);
457 >      else
458 >        t += sprintf(t, "%s%s!%s@%s ", get_member_status(member, multi_prefix),
459 >                     member->client_p->name, member->client_p->username,
460 >                     member->client_p->host);
461      }
462  
463 <    if (tlen != 0)
463 >    if (tlen)
464      {
465        *(t - 1) = '\0';
466 <      sendto_one(source_p, "%s", lbuf);
466 >      sendto_one(client_p, "%s", buf);
467      }
468    }
469  
470    if (show_eon)
471 <    sendto_one(source_p, form_str(RPL_ENDOFNAMES),
492 <               me.name, source_p->name, chptr->chname);
471 >    sendto_one_numeric(client_p, &me, RPL_ENDOFNAMES, chptr->name);
472   }
473  
474 < /*! \brief adds client to invite list
475 < * \param chptr pointer to channel block
476 < * \param who   pointer to client to add invite to
474 > /*! \brief Adds client to invite list
475 > * \param chptr    Pointer to channel block
476 > * \param client_p Pointer to client to add invite to
477   */
478   void
479 < add_invite(struct Channel *chptr, struct Client *who)
479 > add_invite(struct Channel *chptr, struct Client *client_p)
480   {
481 <  del_invite(chptr, who);
481 >  assert(IsClient(client_p));
482 >
483 >  del_invite(chptr, client_p);
484  
485    /*
486 <   * delete last link in chain if the list is max length
486 >   * Delete last link in chain if the list is max length
487     */
488 <  if (dlink_list_length(&who->localClient->invited) >=
489 <      ConfigChannel.max_chans_per_user)
490 <    del_invite(who->localClient->invited.tail->data, who);
488 >  if (dlink_list_length(&client_p->connection->invited) >=
489 >      ConfigChannel.max_channels)
490 >    del_invite(client_p->connection->invited.tail->data, client_p);
491  
492 <  /* add client to channel invite list */
493 <  dlinkAdd(who, make_dlink_node(), &chptr->invites);
492 >  /* Add client to channel invite list */
493 >  dlinkAdd(client_p, make_dlink_node(), &chptr->invites);
494  
495 <  /* add channel to the end of the client invite list */
496 <  dlinkAdd(chptr, make_dlink_node(), &who->localClient->invited);
495 >  /* Add channel to the end of the client invite list */
496 >  dlinkAdd(chptr, make_dlink_node(), &client_p->connection->invited);
497   }
498  
499   /*! \brief Delete Invite block from channel invite list
500   *         and client invite list
501 < * \param chptr pointer to Channel struct
502 < * \param who   pointer to client to remove invites from
501 > * \param chptr    Pointer to Channel struct
502 > * \param client_p Pointer to client to remove invites from
503 > */
504 > void
505 > del_invite(struct Channel *chptr, struct Client *client_p)
506 > {
507 >  dlink_node *node = NULL;
508 >
509 >  if ((node = dlinkFindDelete(&client_p->connection->invited, chptr)))
510 >    free_dlink_node(node);
511 >
512 >  if ((node = dlinkFindDelete(&chptr->invites, client_p)))
513 >    free_dlink_node(node);
514 > }
515 >
516 > /*! \brief Removes all invites of a specific channel
517 > * \param chptr Pointer to Channel struct
518   */
519   void
520 < del_invite(struct Channel *chptr, struct Client *who)
520 > clear_invites_channel(struct Channel *chptr)
521   {
522 <  dlink_node *ptr = NULL;
522 >  dlink_node *node = NULL, *node_next = NULL;
523 >
524 >  DLINK_FOREACH_SAFE(node, node_next, chptr->invites.head)
525 >    del_invite(chptr, node->data);
526 > }
527  
528 <  if ((ptr = dlinkFindDelete(&who->localClient->invited, chptr)))
529 <    free_dlink_node(ptr);
528 > /*! \brief Removes all invites of a specific client
529 > * \param client_p Pointer to Client struct
530 > */
531 > void
532 > clear_invites_client(struct Client *client_p)
533 > {
534 >  dlink_node *node = NULL, *node_next = NULL;
535  
536 <  if ((ptr = dlinkFindDelete(&chptr->invites, who)))
537 <    free_dlink_node(ptr);
536 >  DLINK_FOREACH_SAFE(node, node_next, client_p->connection->invited.head)
537 >    del_invite(node->data, client_p);
538   }
539  
540   /* get_member_status()
# Line 544 | Line 549 | del_invite(struct Channel *chptr, struct
549   * (like in get_client_name)
550   */
551   const char *
552 < get_member_status(const struct Membership *ms, const int combine)
552 > get_member_status(const struct Membership *member, const int combine)
553   {
554 <  static char buffer[4];
554 >  static char buffer[CMEMBER_STATUS_FLAGS_LEN + 1];  /* +1 for \0 */
555    char *p = buffer;
556  
557 <  if (ms->flags & CHFL_CHANOP)
557 >  if (member->flags & CHFL_CHANOP)
558    {
559      if (!combine)
560        return "@";
561      *p++ = '@';
562    }
563  
564 < #ifdef HALFOPS
560 <  if (ms->flags & CHFL_HALFOP)
564 >  if (member->flags & CHFL_HALFOP)
565    {
566      if (!combine)
567        return "%";
568      *p++ = '%';
569    }
566 #endif
570  
571 <  if (ms->flags & CHFL_VOICE)
571 >  if (member->flags & CHFL_VOICE)
572      *p++ = '+';
573    *p = '\0';
574  
# Line 573 | Line 576 | get_member_status(const struct Membershi
576   }
577  
578   /*!
579 < * \param who  pointer to Client to check
580 < * \param list pointer to ban list to search
579 > * \param client_p Pointer to Client to check
580 > * \param list     Pointer to ban list to search
581   * \return 1 if ban found for given n!u\@h mask, 0 otherwise
579 *
582   */
583   static int
584 < find_bmask(const struct Client *who, const dlink_list *const list)
584 > find_bmask(const struct Client *client_p, const dlink_list *const list)
585   {
586 <  const dlink_node *ptr = NULL;
586 >  const dlink_node *node = NULL;
587  
588 <  DLINK_FOREACH(ptr, list->head)
588 >  DLINK_FOREACH(node, list->head)
589    {
590 <    const struct Ban *bp = ptr->data;
590 >    const struct Ban *ban = node->data;
591  
592 <    if (!match(bp->name, who->name) && !match(bp->username, who->username))
592 >    if (!match(ban->name, client_p->name) && !match(ban->user, client_p->username))
593      {
594 <      switch (bp->type)
594 >      switch (ban->type)
595        {
596          case HM_HOST:
597 <          if (!match(bp->host, who->host) || !match(bp->host, who->sockhost))
597 >          if (!match(ban->host, client_p->host) || !match(ban->host, client_p->sockhost))
598              return 1;
599            break;
600          case HM_IPV4:
601 <          if (who->localClient->aftype == AF_INET)
602 <            if (match_ipv4(&who->localClient->ip, &bp->addr, bp->bits))
601 >          if (client_p->connection->aftype == AF_INET)
602 >            if (match_ipv4(&client_p->connection->ip, &ban->addr, ban->bits))
603                return 1;
604            break;
603 #ifdef IPV6
605          case HM_IPV6:
606 <          if (who->localClient->aftype == AF_INET6)
607 <            if (match_ipv6(&who->localClient->ip, &bp->addr, bp->bits))
606 >          if (client_p->connection->aftype == AF_INET6)
607 >            if (match_ipv6(&client_p->connection->ip, &ban->addr, ban->bits))
608                return 1;
609            break;
609 #endif
610          default:
611            assert(0);
612        }
# Line 617 | Line 617 | find_bmask(const struct Client *who, con
617   }
618  
619   /*!
620 < * \param chptr pointer to channel block
621 < * \param who   pointer to client to check access fo
620 > * \param chptr    Pointer to channel block
621 > * \param client_p Pointer to client to check access fo
622   * \return 0 if not banned, 1 otherwise
623   */
624   int
625 < is_banned(const struct Channel *chptr, const struct Client *who)
625 > is_banned(const struct Channel *chptr, const struct Client *client_p)
626   {
627 <  if (find_bmask(who, &chptr->banlist))
628 <    if (!find_bmask(who, &chptr->exceptlist))
627 >  if (find_bmask(client_p, &chptr->banlist))
628 >    if (!find_bmask(client_p, &chptr->exceptlist))
629        return 1;
630  
631    return 0;
632   }
633  
634 < /*!
635 < * \param source_p pointer to client attempting to join
636 < * \param chptr    pointer to channel
637 < * \param key      key sent by client attempting to join if present
634 > /*! Tests if a client can join a certain channel
635 > * \param client_p Pointer to client attempting to join
636 > * \param chptr    Pointer to channel
637 > * \param key      Key sent by client attempting to join if present
638   * \return ERR_BANNEDFROMCHAN, ERR_INVITEONLYCHAN, ERR_CHANNELISFULL
639   *         or 0 if allowed to join.
640   */
641 < int
642 < can_join(struct Client *source_p, struct Channel *chptr, const char *key)
641 > static int
642 > can_join(struct Client *client_p, const struct Channel *chptr, const char *key)
643   {
644 <  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)
649 <    return ERR_SSLONLYCHAN;
650 < #else
651 <  if ((chptr->mode.mode & MODE_SSLONLY))
644 >  if ((chptr->mode.mode & MODE_SSLONLY) && !HasUMode(client_p, UMODE_SSL))
645      return ERR_SSLONLYCHAN;
653 #endif
646  
647 <  if ((chptr->mode.mode & MODE_REGONLY) && !HasUMode(source_p, UMODE_REGISTERED))
647 >  if ((chptr->mode.mode & MODE_REGONLY) && !HasUMode(client_p, UMODE_REGISTERED))
648      return ERR_NEEDREGGEDNICK;
649  
650 <  if ((chptr->mode.mode & MODE_OPERONLY) && !HasUMode(source_p, UMODE_OPER))
650 >  if ((chptr->mode.mode & MODE_OPERONLY) && !HasUMode(client_p, UMODE_OPER))
651      return ERR_OPERONLYCHAN;
652  
653    if (chptr->mode.mode & MODE_INVITEONLY)
654 <    if (!dlinkFind(&source_p->localClient->invited, chptr))
655 <      if (!find_bmask(source_p, &chptr->invexlist))
654 >    if (!dlinkFind(&client_p->connection->invited, chptr))
655 >      if (!find_bmask(client_p, &chptr->invexlist))
656          return ERR_INVITEONLYCHAN;
657  
658    if (chptr->mode.key[0] && (!key || strcmp(chptr->mode.key, key)))
# Line 670 | Line 662 | can_join(struct Client *source_p, struct
662        chptr->mode.limit)
663      return ERR_CHANNELISFULL;
664  
665 +  if (is_banned(chptr, client_p))
666 +    return ERR_BANNEDFROMCHAN;
667 +
668    return 0;
669   }
670  
671   int
672 < has_member_flags(const struct Membership *ms, const unsigned int flags)
672 > has_member_flags(const struct Membership *member, const unsigned int flags)
673   {
674 <  if (ms != NULL)
680 <    return ms->flags & flags;
681 <  return 0;
674 >  return member && (member->flags & flags);
675   }
676  
677   struct Membership *
678   find_channel_link(struct Client *client_p, struct Channel *chptr)
679   {
680 <  dlink_node *ptr = NULL;
680 >  dlink_node *node = NULL;
681  
682    if (!IsClient(client_p))
683      return NULL;
684  
685 <  DLINK_FOREACH(ptr, client_p->channel.head)
686 <    if (((struct Membership *)ptr->data)->chptr == chptr)
687 <      return ptr->data;
685 >  if (dlink_list_length(&chptr->members) < dlink_list_length(&client_p->channel))
686 >  {
687 >    DLINK_FOREACH(node, chptr->members.head)
688 >      if (((struct Membership *)node->data)->client_p == client_p)
689 >        return node->data;
690 >  }
691 >  else
692 >  {
693 >    DLINK_FOREACH(node, client_p->channel.head)
694 >      if (((struct Membership *)node->data)->chptr == chptr)
695 >        return node->data;
696 >  }
697  
698    return NULL;
699   }
700  
701 < /*
702 < * Basically the same functionality as in bahamut
701 > /*! Checks if a message contains control codes
702 > * \param message The actual message string the client wants to send
703 > * \return 1 if the message does contain any control codes, 0 otherwise
704   */
705   static int
706   msg_has_ctrls(const char *message)
# Line 707 | Line 710 | msg_has_ctrls(const char *message)
710    for (; *p; ++p)
711    {
712      if (*p > 31 || *p == 1)
713 <      continue;
713 >      continue;  /* No control code or CTCP */
714  
715 <    if (*p == 27)
715 >    if (*p == 27)  /* Escape */
716      {
717 +      /* ISO 2022 charset shift sequence */
718        if (*(p + 1) == '$' ||
719            *(p + 1) == '(')
720        {
# Line 719 | Line 723 | msg_has_ctrls(const char *message)
723        }
724      }
725  
726 <    return 1;
726 >    return 1;  /* Control code */
727    }
728  
729 <  return 0;
729 >  return 0;  /* No control code found */
730   }
731  
732 < /*!
733 < * \param chptr    pointer to Channel struct
734 < * \param source_p pointer to Client struct
735 < * \param ms       pointer to Membership struct (can be NULL)
732 > /*! Tests if a client can send to a channel
733 > * \param chptr    Pointer to Channel struct
734 > * \param client_p Pointer to Client struct
735 > * \param member   Pointer to Membership struct (can be NULL)
736 > * \param message  The actual message string the client wants to send
737   * \return CAN_SEND_OPV if op or voiced on channel\n
738   *         CAN_SEND_NONOP if can send to channel but is not an op\n
739   *         ERR_CANNOTSENDTOCHAN or ERR_NEEDREGGEDNICK if they cannot send to channel\n
740   */
741   int
742 < can_send(struct Channel *chptr, struct Client *source_p,
743 <         struct Membership *ms, const char *message)
742 > can_send(struct Channel *chptr, struct Client *client_p,
743 >         struct Membership *member, const char *message, int notice)
744   {
745 <  struct MaskItem *conf = NULL;
745 >  const struct MaskItem *conf = NULL;
746  
747 <  if (IsServer(source_p) || HasFlag(source_p, FLAGS_SERVICE))
747 >  if (IsServer(client_p) || HasFlag(client_p, FLAGS_SERVICE))
748      return CAN_SEND_OPV;
749  
750 <  if (MyClient(source_p) && !IsExemptResv(source_p))
751 <    if (!(HasUMode(source_p, UMODE_OPER) && ConfigFileEntry.oper_pass_resv))
752 <      if ((conf = match_find_resv(chptr->chname)) && !resv_find_exempt(source_p, conf))
750 >  if (MyClient(client_p) && !HasFlag(client_p, FLAGS_EXEMPTRESV))
751 >    if (!(HasUMode(client_p, UMODE_OPER) && HasOFlag(client_p, OPER_FLAG_JOIN_RESV)))
752 >      if ((conf = match_find_resv(chptr->name)) && !resv_find_exempt(client_p, conf))
753          return ERR_CANNOTSENDTOCHAN;
754  
755    if ((chptr->mode.mode & MODE_NOCTRL) && msg_has_ctrls(message))
756      return ERR_NOCTRLSONCHAN;
757 <  if (ms || (ms = find_channel_link(source_p, chptr)))
758 <    if (ms->flags & (CHFL_CHANOP|CHFL_HALFOP|CHFL_VOICE))
757 >
758 >  if (chptr->mode.mode & MODE_NOCTCP)
759 >    if (*message == '\001' && strncmp(message + 1, "ACTION ", 7))
760 >      return ERR_NOCTCP;
761 >
762 >  if (member || (member = find_channel_link(client_p, chptr)))
763 >    if (member->flags & (CHFL_CHANOP | CHFL_HALFOP | CHFL_VOICE))
764        return CAN_SEND_OPV;
765 +
766 +  if (!member && (chptr->mode.mode & MODE_NOPRIVMSGS))
767 +    return ERR_CANNOTSENDTOCHAN;
768 +
769    if (chptr->mode.mode & MODE_MODERATED)
770      return ERR_CANNOTSENDTOCHAN;
771 <  if ((chptr->mode.mode & MODE_MODREG) && !HasUMode(source_p, UMODE_REGISTERED))
771 >
772 >  if ((chptr->mode.mode & MODE_MODREG) && !HasUMode(client_p, UMODE_REGISTERED))
773      return ERR_NEEDREGGEDNICK;
774  
775 <  /* cache can send if banned */
776 <  if (MyClient(source_p))
775 >  if ((chptr->mode.mode & MODE_NONOTICE) && notice)
776 >    return ERR_CANNOTSENDTOCHAN;
777 >
778 >  /* Cache can send if banned */
779 >  if (MyClient(client_p))
780    {
781 <    if (ms)
781 >    if (member)
782      {
783 <      if (ms->flags & CHFL_BAN_SILENCED)
783 >      if (member->flags & CHFL_BAN_SILENCED)
784          return ERR_CANNOTSENDTOCHAN;
785  
786 <      if (!(ms->flags & CHFL_BAN_CHECKED))
786 >      if (!(member->flags & CHFL_BAN_CHECKED))
787        {
788 <        if (is_banned(chptr, source_p))
788 >        if (is_banned(chptr, client_p))
789          {
790 <          ms->flags |= (CHFL_BAN_CHECKED|CHFL_BAN_SILENCED);
790 >          member->flags |= (CHFL_BAN_CHECKED | CHFL_BAN_SILENCED);
791            return ERR_CANNOTSENDTOCHAN;
792          }
793  
794 <        ms->flags |= CHFL_BAN_CHECKED;
794 >        member->flags |= CHFL_BAN_CHECKED;
795        }
796      }
797 <    else if (is_banned(chptr, source_p))
797 >    else if (is_banned(chptr, client_p))
798        return ERR_CANNOTSENDTOCHAN;
799    }
800  
# Line 786 | Line 804 | can_send(struct Channel *chptr, struct C
804   /*! \brief Updates the client's oper_warn_count_down, warns the
805   *         IRC operators if necessary, and updates
806   *         join_leave_countdown as needed.
807 < * \param source_p pointer to struct Client to check
808 < * \param name     channel name or NULL if this is a part.
807 > * \param client_p Pointer to struct Client to check
808 > * \param name     Channel name or NULL if this is a part.
809   */
810   void
811 < check_spambot_warning(struct Client *source_p, const char *name)
811 > check_spambot_warning(struct Client *client_p, const char *name)
812   {
813    int t_delta = 0;
814    int decrement_count = 0;
815  
816    if ((GlobalSetOptions.spam_num &&
817 <       (source_p->localClient->join_leave_count >=
817 >       (client_p->connection->join_leave_count >=
818          GlobalSetOptions.spam_num)))
819    {
820 <    if (source_p->localClient->oper_warn_count_down > 0)
821 <      source_p->localClient->oper_warn_count_down--;
820 >    if (client_p->connection->oper_warn_count_down > 0)
821 >      client_p->connection->oper_warn_count_down--;
822      else
823 <      source_p->localClient->oper_warn_count_down = 0;
823 >      client_p->connection->oper_warn_count_down = 0;
824  
825 <    if (source_p->localClient->oper_warn_count_down == 0)
825 >    if (client_p->connection->oper_warn_count_down == 0)
826      {
827 <      /* Its already known as a possible spambot */
828 <      if (name != NULL)
827 >      /* It's already known as a possible spambot */
828 >      if (name)
829          sendto_realops_flags(UMODE_BOTS, L_ALL, SEND_NOTICE,
830                               "User %s (%s@%s) trying to join %s is a possible spambot",
831 <                             source_p->name, source_p->username,
832 <                             source_p->host, name);
831 >                             client_p->name, client_p->username,
832 >                             client_p->host, name);
833        else
834          sendto_realops_flags(UMODE_BOTS, L_ALL, SEND_NOTICE,
835                               "User %s (%s@%s) is a possible spambot",
836 <                             source_p->name, source_p->username,
837 <                             source_p->host);
838 <      source_p->localClient->oper_warn_count_down = OPER_SPAM_COUNTDOWN;
836 >                             client_p->name, client_p->username,
837 >                             client_p->host);
838 >      client_p->connection->oper_warn_count_down = OPER_SPAM_COUNTDOWN;
839      }
840    }
841    else
842    {
843 <    if ((t_delta = (CurrentTime - source_p->localClient->last_leave_time)) >
843 >    if ((t_delta = (CurrentTime - client_p->connection->last_leave_time)) >
844           JOIN_LEAVE_COUNT_EXPIRE_TIME)
845      {
846        decrement_count = (t_delta / JOIN_LEAVE_COUNT_EXPIRE_TIME);
847 <      if (decrement_count > source_p->localClient->join_leave_count)
848 <        source_p->localClient->join_leave_count = 0;
847 >
848 >      if (decrement_count > client_p->connection->join_leave_count)
849 >        client_p->connection->join_leave_count = 0;
850        else
851 <        source_p->localClient->join_leave_count -= decrement_count;
851 >        client_p->connection->join_leave_count -= decrement_count;
852      }
853      else
854      {
855 <      if ((CurrentTime - (source_p->localClient->last_join_time)) <
855 >      if ((CurrentTime - (client_p->connection->last_join_time)) <
856            GlobalSetOptions.spam_time)
857 <      {
839 <        /* oh, its a possible spambot */
840 <        source_p->localClient->join_leave_count++;
841 <      }
857 >        client_p->connection->join_leave_count++;  /* It's a possible spambot */
858      }
859  
860 <    if (name != NULL)
861 <      source_p->localClient->last_join_time = CurrentTime;
860 >    if (name)
861 >      client_p->connection->last_join_time = CurrentTime;
862      else
863 <      source_p->localClient->last_leave_time = CurrentTime;
863 >      client_p->connection->last_leave_time = CurrentTime;
864    }
865   }
866  
867 < /*! \brief compares usercount and servercount against their split
868 < *         values and adjusts splitmode accordingly
869 < * \param unused Unused address pointer
867 > /*! \brief Sets the channel topic for a certain channel
868 > * \param chptr      Pointer to struct Channel
869 > * \param topic      The topic string
870 > * \param topic_info n!u\@h formatted string of the topic setter
871 > * \param topicts    Timestamp on the topic
872 > * \param local      Whether the topic is set by a local client
873 > */
874 > void
875 > channel_set_topic(struct Channel *chptr, const char *topic,
876 >                  const char *topic_info, time_t topicts, int local)
877 > {
878 >  if (local)
879 >    strlcpy(chptr->topic, topic, IRCD_MIN(sizeof(chptr->topic), ConfigServerInfo.max_topic_length + 1));
880 >  else
881 >    strlcpy(chptr->topic, topic, sizeof(chptr->topic));
882 >
883 >  strlcpy(chptr->topic_info, topic_info, sizeof(chptr->topic_info));
884 >  chptr->topic_time = topicts;
885 > }
886 >
887 > /* do_join_0()
888 > *
889 > * inputs       - pointer to client doing join 0
890 > * output       - NONE
891 > * side effects - Use has decided to join 0. This is legacy
892 > *                from the days when channels were numbers not names. *sigh*
893 > *                There is a bunch of evilness necessary here due to
894 > *                anti spambot code.
895   */
896   void
897 < check_splitmode(void *unused)
897 > channel_do_join_0(struct Client *client_p)
898 > {
899 >  dlink_node *node = NULL, *node_next = NULL;
900 >
901 >  if (client_p->channel.head)
902 >    if (MyConnect(client_p) && !HasUMode(client_p, UMODE_OPER))
903 >      check_spambot_warning(client_p, NULL);
904 >
905 >  DLINK_FOREACH_SAFE(node, node_next, client_p->channel.head)
906 >  {
907 >    struct Channel *chptr = ((struct Membership *)node->data)->chptr;
908 >
909 >    sendto_server(client_p, 0, 0, ":%s PART %s",
910 >                  client_p->id, chptr->name);
911 >    sendto_channel_local(NULL, chptr, 0, 0, 0, ":%s!%s@%s PART %s",
912 >                         client_p->name, client_p->username,
913 >                         client_p->host, chptr->name);
914 >
915 >    remove_user_from_channel(node->data);
916 >  }
917 > }
918 >
919 > static char *
920 > channel_find_last0(struct Client *client_p, char *chanlist)
921 > {
922 >  int join0 = 0;
923 >
924 >  for (char *p = chanlist; *p; ++p)  /* Find last "JOIN 0" */
925 >  {
926 >    if (*p == '0' && (*(p + 1) == ',' || *(p + 1) == '\0'))
927 >    {
928 >      if (*(p + 1) == ',')
929 >        ++p;
930 >
931 >      chanlist = p + 1;
932 >      join0 = 1;
933 >    }
934 >    else
935 >    {
936 >      while (*p != ',' && *p != '\0')  /* Skip past channel name */
937 >        ++p;
938 >
939 >      if (*p == '\0')  /* Hit the end */
940 >        break;
941 >    }
942 >  }
943 >
944 >  if (join0)
945 >    channel_do_join_0(client_p);
946 >
947 >  return chanlist;
948 > }
949 >
950 > void
951 > channel_do_join(struct Client *client_p, char *channel, char *key_list)
952   {
953 <  if (splitchecking && (ConfigChannel.no_join_on_split ||
954 <                        ConfigChannel.no_create_on_split))
953 >  char *p = NULL;
954 >  char *chan = NULL;
955 >  char *chan_list = NULL;
956 >  struct Channel *chptr = NULL;
957 >  struct MaskItem *conf = NULL;
958 >  const struct ClassItem *const class = get_class_ptr(&client_p->connection->confs);
959 >  int i = 0;
960 >  unsigned int flags = 0;
961 >
962 >  assert(IsClient(client_p));
963 >
964 >  chan_list = channel_find_last0(client_p, channel);
965 >
966 >  for (chan = strtok_r(chan_list, ",", &p); chan;
967 >       chan = strtok_r(NULL,      ",", &p))
968    {
969 <    const unsigned int server = dlink_list_length(&global_serv_list);
969 >    const char *key = NULL;
970 >
971 >    /* If we have any more keys, take the first for this channel. */
972 >    if (!EmptyString(key_list) && (key_list = strchr(key = key_list, ',')))
973 >      *key_list++ = '\0';
974 >
975 >    /* Empty keys are the same as no keys. */
976 >    if (key && *key == '\0')
977 >      key = NULL;
978 >
979 >    if (!channel_check_name(chan, 1))
980 >    {
981 >      sendto_one_numeric(client_p, &me, ERR_BADCHANNAME, chan);
982 >      continue;
983 >    }
984 >
985 >    if (!HasFlag(client_p, FLAGS_EXEMPTRESV) &&
986 >        !(HasUMode(client_p, UMODE_OPER) && HasOFlag(client_p, OPER_FLAG_JOIN_RESV)) &&
987 >        ((conf = match_find_resv(chan)) && !resv_find_exempt(client_p, conf)))
988 >    {
989 >      ++conf->count;
990 >      sendto_one_numeric(client_p, &me, ERR_CHANBANREASON, chan, conf->reason);
991 >      sendto_realops_flags(UMODE_REJ, L_ALL, SEND_NOTICE,
992 >                           "Forbidding reserved channel %s from user %s",
993 >                           chan, get_client_name(client_p, HIDE_IP));
994 >      continue;
995 >    }
996 >
997 >    if (dlink_list_length(&client_p->channel) >=
998 >        ((class->max_channels) ? class->max_channels : ConfigChannel.max_channels))
999 >    {
1000 >      sendto_one_numeric(client_p, &me, ERR_TOOMANYCHANNELS, chan);
1001 >      break;
1002 >    }
1003 >
1004 >    if ((chptr = hash_find_channel(chan)))
1005 >    {
1006 >      if (IsMember(client_p, chptr))
1007 >        continue;
1008 >
1009 >      /*
1010 >       * can_join checks for +i key, bans.
1011 >       */
1012 >      if ((i = can_join(client_p, chptr, key)))
1013 >      {
1014 >        sendto_one_numeric(client_p, &me, i, chptr->name);
1015 >        continue;
1016 >      }
1017  
1018 <    if (!splitmode && ((server < split_servers) || (Count.total < split_users)))
1018 >      /*
1019 >       * This should never be the case unless there is some sort of
1020 >       * persistant channels.
1021 >       */
1022 >      if (!dlink_list_length(&chptr->members))
1023 >        flags = CHFL_CHANOP;
1024 >      else
1025 >        flags = 0;
1026 >    }
1027 >    else
1028      {
1029 <      splitmode = 1;
1029 >      flags = CHFL_CHANOP;
1030 >      chptr = channel_make(chan);
1031 >    }
1032 >
1033 >    if (!HasUMode(client_p, UMODE_OPER))
1034 >      check_spambot_warning(client_p, chptr->name);
1035 >
1036 >    add_user_to_channel(chptr, client_p, flags, 1);
1037  
1038 <      sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1039 <                           "Network split, activating splitmode");
1040 <      eventAddIsh("check_splitmode", check_splitmode, NULL, 10);
1038 >    /*
1039 >     * Set timestamp if appropriate, and propagate
1040 >     */
1041 >    if (flags == CHFL_CHANOP)
1042 >    {
1043 >      chptr->creationtime = CurrentTime;
1044 >      chptr->mode.mode |= MODE_TOPICLIMIT;
1045 >      chptr->mode.mode |= MODE_NOPRIVMSGS;
1046 >
1047 >      sendto_server(client_p, 0, 0, ":%s SJOIN %ju %s +nt :@%s",
1048 >                    me.id, chptr->creationtime,
1049 >                    chptr->name, client_p->id);
1050 >
1051 >      /*
1052 >       * Notify all other users on the new channel
1053 >       */
1054 >      sendto_channel_local(NULL, chptr, 0, CAP_EXTENDED_JOIN, 0, ":%s!%s@%s JOIN %s %s :%s",
1055 >                           client_p->name, client_p->username,
1056 >                           client_p->host, chptr->name, client_p->account, client_p->info);
1057 >      sendto_channel_local(NULL, chptr, 0, 0, CAP_EXTENDED_JOIN, ":%s!%s@%s JOIN :%s",
1058 >                           client_p->name, client_p->username,
1059 >                           client_p->host, chptr->name);
1060 >      sendto_channel_local(NULL, chptr, 0, 0, 0, ":%s MODE %s +nt",
1061 >                           me.name, chptr->name);
1062 >
1063 >      if (client_p->away[0])
1064 >        sendto_channel_local(client_p, chptr, 0, CAP_AWAY_NOTIFY, 0,
1065 >                             ":%s!%s@%s AWAY :%s",
1066 >                             client_p->name, client_p->username,
1067 >                             client_p->host, client_p->away);
1068      }
1069 <    else if (splitmode && (server > split_servers) && (Count.total > split_users))
1069 >    else
1070      {
1071 <      splitmode = 0;
1071 >      sendto_server(client_p, 0, 0, ":%s JOIN %ju %s +",
1072 >                    client_p->id, chptr->creationtime,
1073 >                    chptr->name);
1074 >
1075 >      sendto_channel_local(NULL, chptr, 0, CAP_EXTENDED_JOIN, 0, ":%s!%s@%s JOIN %s %s :%s",
1076 >                           client_p->name, client_p->username,
1077 >                           client_p->host, chptr->name, client_p->account, client_p->info);
1078 >      sendto_channel_local(NULL, chptr, 0, 0, CAP_EXTENDED_JOIN, ":%s!%s@%s JOIN :%s",
1079 >                           client_p->name, client_p->username,
1080 >                           client_p->host, chptr->name);
1081 >
1082 >      if (client_p->away[0])
1083 >        sendto_channel_local(client_p, chptr, 0, CAP_AWAY_NOTIFY, 0,
1084 >                             ":%s!%s@%s AWAY :%s",
1085 >                             client_p->name, client_p->username,
1086 >                             client_p->host, client_p->away);
1087 >    }
1088 >
1089 >    del_invite(chptr, client_p);
1090  
1091 <      sendto_realops_flags(UMODE_ALL, L_ALL, SEND_NOTICE,
1092 <                           "Network rejoined, deactivating splitmode");
1093 <      eventDelete(check_splitmode, NULL);
1091 >    if (chptr->topic[0])
1092 >    {
1093 >      sendto_one_numeric(client_p, &me, RPL_TOPIC, chptr->name, chptr->topic);
1094 >      sendto_one_numeric(client_p, &me, RPL_TOPICWHOTIME, chptr->name,
1095 >                         chptr->topic_info, chptr->topic_time);
1096      }
1097 +
1098 +    channel_member_names(client_p, chptr, 1);
1099 +
1100 +    client_p->connection->last_join_time = CurrentTime;
1101    }
1102   }
1103  
1104 < /*! \brief Sets the channel topic for chptr
1105 < * \param chptr      Pointer to struct Channel
1106 < * \param topic      The topic string
1107 < * \param topic_info n!u\@h formatted string of the topic setter
886 < * \param topicts    timestamp on the topic
1104 > /*! \brief Removes a client from a specific channel
1105 > * \param client_p Pointer to source client to remove
1106 > * \param name     Name of channel to remove from
1107 > * \param reason   Part reason to show
1108   */
1109 < void
1110 < set_channel_topic(struct Channel *chptr, const char *topic,
890 <                  const char *topic_info, time_t topicts, int local)
1109 > static void
1110 > channel_part_one_client(struct Client *client_p, const char *name, const char *reason)
1111   {
1112 <  if (local)
1113 <    strlcpy(chptr->topic, topic, IRCD_MIN(sizeof(chptr->topic), ServerInfo.max_topic_length + 1));
1112 >  struct Channel *chptr = NULL;
1113 >  struct Membership *member = NULL;
1114 >
1115 >  if ((chptr = hash_find_channel(name)) == NULL)
1116 >  {
1117 >    sendto_one_numeric(client_p, &me, ERR_NOSUCHCHANNEL, name);
1118 >    return;
1119 >  }
1120 >
1121 >  if ((member = find_channel_link(client_p, chptr)) == NULL)
1122 >  {
1123 >    sendto_one_numeric(client_p, &me, ERR_NOTONCHANNEL, chptr->name);
1124 >    return;
1125 >  }
1126 >
1127 >  if (MyConnect(client_p) && !HasUMode(client_p, UMODE_OPER))
1128 >    check_spambot_warning(client_p, NULL);
1129 >
1130 >  /*
1131 >   * Remove user from the old channel (if any)
1132 >   * only allow /part reasons in -m chans
1133 >   */
1134 >  if (*reason && (!MyConnect(client_p) ||
1135 >      ((client_p->connection->firsttime +
1136 >        ConfigGeneral.anti_spam_exit_message_time) < CurrentTime &&
1137 >       can_send(chptr, client_p, member, reason, 0) < 0)))
1138 >  {
1139 >    sendto_server(client_p, 0, 0, ":%s PART %s :%s",
1140 >                  client_p->id, chptr->name, reason);
1141 >    sendto_channel_local(NULL, chptr, 0, 0, 0, ":%s!%s@%s PART %s :%s",
1142 >                         client_p->name, client_p->username,
1143 >                         client_p->host, chptr->name, reason);
1144 >  }
1145    else
1146 <    strlcpy(chptr->topic, topic, sizeof(chptr->topic));
1146 >  {
1147 >    sendto_server(client_p, 0, 0, ":%s PART %s",
1148 >                  client_p->id, chptr->name);
1149 >    sendto_channel_local(NULL, chptr, 0, 0, 0, ":%s!%s@%s PART %s",
1150 >                         client_p->name, client_p->username,
1151 >                         client_p->host, chptr->name);
1152 >  }
1153  
1154 <  strlcpy(chptr->topic_info, topic_info, sizeof(chptr->topic_info));
1155 <  chptr->topic_time = topicts;
1154 >  remove_user_from_channel(member);
1155 > }
1156 >
1157 > void
1158 > channel_do_part(struct Client *client_p, char *channel, const char *reason)
1159 > {
1160 >  char *p = NULL, *name = NULL;
1161 >  char buf[KICKLEN + 1] = "";
1162 >
1163 >  assert(IsClient(client_p));
1164 >
1165 >  if (!EmptyString(reason))
1166 >    strlcpy(buf, reason, sizeof(buf));
1167 >
1168 >  for (name = strtok_r(channel, ",", &p); name;
1169 >       name = strtok_r(NULL,    ",", &p))
1170 >    channel_part_one_client(client_p, name, buf);
1171   }

Diff Legend

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