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

Diff Legend

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