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 1654 by michael, Fri Nov 16 19:39:37 2012 UTC vs.
Revision 3145 by michael, Wed Mar 12 20:12:46 2014 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-2014 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 33 | Line 33
33   #include "conf.h"
34   #include "hostmask.h"
35   #include "irc_string.h"
36 #include "sprintf_irc.h"
36   #include "ircd.h"
37   #include "numeric.h"
38 < #include "s_serv.h"             /* captab */
40 < #include "s_user.h"
38 > #include "s_serv.h"
39   #include "send.h"
40   #include "event.h"
41   #include "memory.h"
42   #include "mempool.h"
43 + #include "s_misc.h"
44 + #include "resv.h"
45  
46 struct config_channel_entry ConfigChannel;
46   dlink_list global_channel_list = { NULL, NULL, 0 };
47   mp_pool_t *ban_pool;    /*! \todo ban_pool shouldn't be a global var */
48  
# Line 51 | Line 50 | static mp_pool_t *member_pool = NULL;
50   static mp_pool_t *channel_pool = NULL;
51  
52   static char buf[IRCD_BUFSIZE];
54 static char modebuf[MODEBUFLEN];
55 static char parabuf[MODEBUFLEN];
53  
54  
55   /*! \brief Initializes the channel blockheap, adds known channel CAPAB
56   */
57   void
58 < init_channels(void)
58 > channel_init(void)
59   {
60    add_capability("EX", CAP_EX, 1);
61    add_capability("IE", CAP_IE, 1);
65  add_capability("CHW", CAP_CHW, 1);
62  
63    channel_pool = mp_pool_new(sizeof(struct Channel), MP_CHUNK_SIZE_CHANNEL);
64    ban_pool = mp_pool_new(sizeof(struct Ban), MP_CHUNK_SIZE_BAN);
# Line 151 | Line 147 | remove_user_from_channel(struct Membersh
147   */
148   static void
149   send_members(struct Client *client_p, struct Channel *chptr,
150 <             char *lmodebuf, char *lparabuf)
150 >             char *modebuf, char *parabuf)
151   {
152 <  struct Membership *ms;
157 <  dlink_node *ptr;
152 >  const dlink_node *ptr = NULL;
153    int tlen;              /* length of text to append */
154    char *t, *start;       /* temp char pointer */
155  
156 <  start = t = buf + ircsprintf(buf, ":%s SJOIN %lu %s %s %s:",
157 <                               ID_or_name(&me, client_p),
158 <                               (unsigned long)chptr->channelts,
159 <                               chptr->chname, lmodebuf, lparabuf);
156 >  start = t = buf + snprintf(buf, sizeof(buf), ":%s SJOIN %lu %s %s %s:",
157 >                             ID_or_name(&me, client_p),
158 >                             (unsigned long)chptr->channelts,
159 >                             chptr->chname, modebuf, parabuf);
160  
161    DLINK_FOREACH(ptr, chptr->members.head)
162    {
163 <    ms = ptr->data;
163 >    const struct Membership *ms = ptr->data;
164  
165 <    tlen = strlen(IsCapable(client_p, CAP_TS6) ?
171 <      ID(ms->client_p) : ms->client_p->name) + 1;  /* nick + space */
165 >    tlen = strlen(ID(ms->client_p)) + 1;  /* nick + space */
166  
167      if (ms->flags & CHFL_CHANOP)
168        tlen++;
169   #ifdef HALFOPS
170 <    else if (ms->flags & CHFL_HALFOP)
170 >    if (ms->flags & CHFL_HALFOP)
171        tlen++;
172   #endif
173      if (ms->flags & CHFL_VOICE)
# Line 189 | Line 183 | send_members(struct Client *client_p, st
183        t = start;
184      }
185  
186 <    if ((ms->flags & (CHFL_CHANOP | CHFL_HALFOP)))
187 <      *t++ = (!(ms->flags & CHFL_CHANOP) && IsCapable(client_p, CAP_HOPS)) ?
188 <        '%' : '@';
189 <    if ((ms->flags & CHFL_VOICE))
186 >    if (ms->flags & CHFL_CHANOP)
187 >      *t++ = '@';
188 >    if (ms->flags & CHFL_HALFOP)
189 >      *t++ = '%';
190 >    if (ms->flags & CHFL_VOICE)
191        *t++ = '+';
192  
193 <    if (IsCapable(client_p, CAP_TS6))
194 <      strcpy(t, ID(ms->client_p));
200 <    else
201 <      strcpy(t, ms->client_p->name);
193 >    strcpy(t, ID(ms->client_p));
194 >
195      t += strlen(t);
196      *t++ = ' ';
197    }
# Line 218 | Line 211 | send_members(struct Client *client_p, st
211   */
212   static void
213   send_mode_list(struct Client *client_p, struct Channel *chptr,
214 <               dlink_list *top, char flag)
214 >               const dlink_list *top, char flag)
215   {
216 <  int ts5 = !IsCapable(client_p, CAP_TS6);
224 <  dlink_node *lp;
225 <  struct Ban *banptr;
216 >  const dlink_node *lp = NULL;
217    char pbuf[IRCD_BUFSIZE];
218    int tlen, mlen, cur_len, count = 0;
219 <  char *mp = NULL, *pp = pbuf;
219 >  char *pp = pbuf;
220  
221    if (top == NULL || top->length == 0)
222      return;
223  
224 <  if (ts5)
225 <    mlen = ircsprintf(buf, ":%s MODE %s +", me.name, chptr->chname);
226 <  else
236 <    mlen = ircsprintf(buf, ":%s BMASK %lu %s %c :", me.id,
237 <                      (unsigned long)chptr->channelts, chptr->chname, flag);
238 <
239 <  /* MODE needs additional one byte for space between buf and pbuf */
240 <  cur_len = mlen + ts5;
241 <  mp = buf + mlen;
224 >  mlen = snprintf(buf, sizeof(buf), ":%s BMASK %lu %s %c :", me.id,
225 >                  (unsigned long)chptr->channelts, chptr->chname, flag);
226 >  cur_len = mlen;
227  
228    DLINK_FOREACH(lp, top->head)
229    {
230 <    banptr = lp->data;
230 >    const struct Ban *banptr = lp->data;
231  
232 <    /* must add another b/e/I letter if we use MODE */
248 <    tlen = banptr->len + 3 + ts5;
232 >    tlen = banptr->len + 3;
233  
234      /*
235       * send buffer and start over if we cannot fit another ban,
236       * or if the target is non-ts6 and we have too many modes in
237       * in this line.
238       */
239 <    if (cur_len + (tlen - 1) > IRCD_BUFSIZE - 2 ||
256 <        (!IsCapable(client_p, CAP_TS6) &&
257 <         (count >= MAXMODEPARAMS || pp - pbuf >= MODEBUFLEN)))
239 >    if (cur_len + (tlen - 1) > IRCD_BUFSIZE - 2)
240      {
241        *(pp - 1) = '\0';  /* get rid of trailing space on buffer */
242 <      sendto_one(client_p, "%s%s%s", buf, ts5 ? " " : "", pbuf);
242 >      sendto_one(client_p, "%s%s", buf, pbuf);
243  
244 <      cur_len = mlen + ts5;
263 <      mp = buf + mlen;
244 >      cur_len = mlen;
245        pp = pbuf;
246        count = 0;
247      }
248  
249      count++;
250 <    if (ts5)
251 <    {
271 <      *mp++ = flag;
272 <      *mp = '\0';
273 <    }
274 <
275 <    pp += ircsprintf(pp, "%s!%s@%s ", banptr->name, banptr->username,
276 <                     banptr->host);
250 >    pp += sprintf(pp, "%s!%s@%s ", banptr->name, banptr->user,
251 >                  banptr->host);
252      cur_len += tlen;
253    }
254  
255    *(pp - 1) = '\0';  /* get rid of trailing space on buffer */
256 <  sendto_one(client_p, "%s%s%s", buf, ts5 ? " " : "", pbuf);
256 >  sendto_one(client_p, "%s%s", buf, pbuf);
257   }
258  
259   /*! \brief send "client_p" a full list of the modes for channel chptr
# Line 288 | Line 263 | send_mode_list(struct Client *client_p,
263   void
264   send_channel_modes(struct Client *client_p, struct Channel *chptr)
265   {
266 <  *modebuf = *parabuf = '\0';
266 >  char modebuf[MODEBUFLEN] = "";
267 >  char parabuf[MODEBUFLEN] = "";
268 >
269    channel_modes(chptr, client_p, modebuf, parabuf);
270    send_members(client_p, chptr, modebuf, parabuf);
271  
272    send_mode_list(client_p, chptr, &chptr->banlist, 'b');
273 <
274 <  if (IsCapable(client_p, CAP_EX))
298 <    send_mode_list(client_p, chptr, &chptr->exceptlist, 'e');
299 <  if (IsCapable(client_p, CAP_IE))
300 <    send_mode_list(client_p, chptr, &chptr->invexlist, 'I');
273 >  send_mode_list(client_p, chptr, &chptr->exceptlist, 'e');
274 >  send_mode_list(client_p, chptr, &chptr->invexlist, 'I');
275   }
276  
277   /*! \brief check channel name for invalid characters
# Line 306 | Line 280 | send_channel_modes(struct Client *client
280   * \return 0 if invalid, 1 otherwise
281   */
282   int
283 < check_channel_name(const char *name, int local)
283 > check_channel_name(const char *name, const int local)
284   {
285    const char *p = name;
286    const int max_length = local ? LOCAL_CHANNELLEN : CHANNELLEN;
# Line 337 | Line 311 | remove_ban(struct Ban *bptr, dlink_list
311    dlinkDelete(&bptr->node, list);
312  
313    MyFree(bptr->name);
314 <  MyFree(bptr->username);
314 >  MyFree(bptr->user);
315    MyFree(bptr->host);
316    MyFree(bptr->who);
317  
# Line 435 | Line 409 | void
409   channel_member_names(struct Client *source_p, struct Channel *chptr,
410                       int show_eon)
411   {
412 <  struct Client *target_p = NULL;
439 <  struct Membership *ms = NULL;
440 <  dlink_node *ptr = NULL;
412 >  const dlink_node *ptr = NULL;
413    char lbuf[IRCD_BUFSIZE + 1];
414    char *t = NULL, *start = NULL;
415    int tlen = 0;
416    int is_member = IsMember(source_p, chptr);
417    int multi_prefix = HasCap(source_p, CAP_MULTI_PREFIX) != 0;
418 +  int uhnames = HasCap(source_p, CAP_UHNAMES) != 0;
419  
420    if (PubChannel(chptr) || is_member)
421    {
422 <    t = lbuf + ircsprintf(lbuf, form_str(RPL_NAMREPLY),
423 <                          me.name, source_p->name,
424 <                          channel_pub_or_secret(chptr),
452 <                          chptr->chname);
422 >    t = lbuf + snprintf(lbuf, sizeof(lbuf), numeric_form(RPL_NAMREPLY),
423 >                        me.name, source_p->name,
424 >                        channel_pub_or_secret(chptr), chptr->chname);
425      start = t;
426  
427      DLINK_FOREACH(ptr, chptr->members.head)
428      {
429 <      ms       = ptr->data;
458 <      target_p = ms->client_p;
429 >      const struct Membership *ms = ptr->data;
430  
431 <      if (HasUMode(target_p, UMODE_INVISIBLE) && !is_member)
431 >      if (HasUMode(ms->client_p, UMODE_INVISIBLE) && !is_member)
432          continue;
433  
434 <      tlen = strlen(target_p->name) + 1;  /* nick + space */
434 >      if (!uhnames)
435 >        tlen = strlen(ms->client_p->name) + 1;  /* nick + space */
436 >      else
437 >        tlen = strlen(ms->client_p->name) + strlen(ms->client_p->username) +
438 >               strlen(ms->client_p->host) + 3;
439  
440        if (!multi_prefix)
441        {
# Line 484 | Line 459 | channel_member_names(struct Client *sour
459          t = start;
460        }
461  
462 <      t += ircsprintf(t, "%s%s ", get_member_status(ms, multi_prefix),
463 <                      target_p->name);
462 >      if (!uhnames)
463 >        t += sprintf(t, "%s%s ", get_member_status(ms, multi_prefix),
464 >                     ms->client_p->name);
465 >      else
466 >        t += sprintf(t, "%s%s!%s@%s ", get_member_status(ms, multi_prefix),
467 >                     ms->client_p->name, ms->client_p->username,
468 >                     ms->client_p->host);
469      }
470  
471      if (tlen != 0)
# Line 496 | Line 476 | channel_member_names(struct Client *sour
476    }
477  
478    if (show_eon)
479 <    sendto_one(source_p, form_str(RPL_ENDOFNAMES),
500 <               me.name, source_p->name, chptr->chname);
479 >    sendto_one_numeric(source_p, &me, RPL_ENDOFNAMES, chptr->chname);
480   }
481  
482   /*! \brief adds client to invite list
# Line 552 | Line 531 | del_invite(struct Channel *chptr, struct
531   * (like in get_client_name)
532   */
533   const char *
534 < get_member_status(const struct Membership *ms, int combine)
534 > get_member_status(const struct Membership *ms, const int combine)
535   {
536    static char buffer[4];
537 <  char *p = NULL;
559 <
560 <  if (ms == NULL)
561 <    return "";
562 <  p = buffer;
537 >  char *p = buffer;
538  
539    if (ms->flags & CHFL_CHANOP)
540    {
# Line 599 | Line 574 | find_bmask(const struct Client *who, con
574    {
575      const struct Ban *bp = ptr->data;
576  
577 <    if (!match(bp->name, who->name) && !match(bp->username, who->username))
577 >    if (!match(bp->name, who->name) && !match(bp->user, who->username))
578      {
579        switch (bp->type)
580        {
# Line 645 | Line 620 | is_banned(const struct Channel *chptr, c
620  
621   /*!
622   * \param source_p pointer to client attempting to join
623 < * \param chptr    pointer to channel
623 > * \param chptr    pointer to channel
624   * \param key      key sent by client attempting to join if present
625   * \return ERR_BANNEDFROMCHAN, ERR_INVITEONLYCHAN, ERR_CHANNELISFULL
626   *         or 0 if allowed to join.
# Line 653 | Line 628 | is_banned(const struct Channel *chptr, c
628   int
629   can_join(struct Client *source_p, struct Channel *chptr, const char *key)
630   {
631 <  if (is_banned(chptr, source_p))
657 <    return ERR_BANNEDFROMCHAN;
658 <
659 < #ifdef HAVE_LIBCRYPTO
660 <  if ((chptr->mode.mode & MODE_SSLONLY) && !source_p->localClient->fd.ssl)
631 >  if ((chptr->mode.mode & MODE_SSLONLY) && !HasUMode(source_p, UMODE_SSL))
632      return ERR_SSLONLYCHAN;
662 #endif
633  
634    if ((chptr->mode.mode & MODE_REGONLY) && !HasUMode(source_p, UMODE_REGISTERED))
635      return ERR_NEEDREGGEDNICK;
# Line 679 | Line 649 | can_join(struct Client *source_p, struct
649        chptr->mode.limit)
650      return ERR_CHANNELISFULL;
651  
652 +  if (is_banned(chptr, source_p))
653 +    return ERR_BANNEDFROMCHAN;
654 +
655    return 0;
656   }
657  
658   int
659 < has_member_flags(struct Membership *ms, unsigned int flags)
659 > has_member_flags(const struct Membership *ms, const unsigned int flags)
660   {
661    if (ms != NULL)
662      return ms->flags & flags;
# Line 698 | Line 671 | find_channel_link(struct Client *client_
671    if (!IsClient(client_p))
672      return NULL;
673  
674 <  DLINK_FOREACH(ptr, client_p->channel.head)
675 <    if (((struct Membership *)ptr->data)->chptr == chptr)
676 <      return ptr->data;
674 >  if (dlink_list_length(&chptr->members) < dlink_list_length(&client_p->channel))
675 >  {
676 >    DLINK_FOREACH(ptr, chptr->members.head)
677 >      if (((struct Membership *)ptr->data)->client_p == client_p)
678 >        return ptr->data;
679 >  }
680 >  else
681 >  {
682 >    DLINK_FOREACH(ptr, client_p->channel.head)
683 >      if (((struct Membership *)ptr->data)->chptr == chptr)
684 >        return ptr->data;
685 >  }
686  
687    return NULL;
688   }
689  
690 + /*
691 + * Basically the same functionality as in bahamut
692 + */
693 + static int
694 + msg_has_ctrls(const char *message)
695 + {
696 +  const unsigned char *p = (const unsigned char *)message;
697 +
698 +  for (; *p; ++p)
699 +  {
700 +    if (*p > 31 || *p == 1)
701 +      continue;
702 +
703 +    if (*p == 27)
704 +    {
705 +      if (*(p + 1) == '$' ||
706 +          *(p + 1) == '(')
707 +      {
708 +        ++p;
709 +        continue;
710 +      }
711 +    }
712 +
713 +    return 1;
714 +  }
715 +
716 +  return 0;
717 + }
718 +
719   /*!
720   * \param chptr    pointer to Channel struct
721   * \param source_p pointer to Client struct
# Line 714 | Line 725 | find_channel_link(struct Client *client_
725   *         ERR_CANNOTSENDTOCHAN or ERR_NEEDREGGEDNICK if they cannot send to channel\n
726   */
727   int
728 < can_send(struct Channel *chptr, struct Client *source_p, struct Membership *ms)
728 > can_send(struct Channel *chptr, struct Client *source_p,
729 >         struct Membership *ms, const char *message)
730   {
731 +  struct MaskItem *conf = NULL;
732 +
733    if (IsServer(source_p) || HasFlag(source_p, FLAGS_SERVICE))
734      return CAN_SEND_OPV;
735  
736    if (MyClient(source_p) && !IsExemptResv(source_p))
737      if (!(HasUMode(source_p, UMODE_OPER) && ConfigFileEntry.oper_pass_resv))
738 <      if (!hash_find_resv(chptr->chname) == ConfigChannel.restrict_channels)
738 >      if ((conf = match_find_resv(chptr->chname)) && !resv_find_exempt(source_p, conf))
739          return ERR_CANNOTSENDTOCHAN;
740  
741 <  if (ms != NULL || (ms = find_channel_link(source_p, chptr)))
742 <  {
741 >  if ((chptr->mode.mode & MODE_NOCTRL) && msg_has_ctrls(message))
742 >    return ERR_NOCTRLSONCHAN;
743 >  if (ms || (ms = find_channel_link(source_p, chptr)))
744      if (ms->flags & (CHFL_CHANOP|CHFL_HALFOP|CHFL_VOICE))
745        return CAN_SEND_OPV;
746 +  if (!ms && (chptr->mode.mode & MODE_NOPRIVMSGS))
747 +    return ERR_CANNOTSENDTOCHAN;
748 +  if (chptr->mode.mode & MODE_MODERATED)
749 +    return ERR_CANNOTSENDTOCHAN;
750 +  if ((chptr->mode.mode & MODE_MODREG) && !HasUMode(source_p, UMODE_REGISTERED))
751 +    return ERR_NEEDREGGEDNICK;
752  
753 <    /* cache can send if quiet_on_ban and banned */
754 <    if (ConfigChannel.quiet_on_ban && MyClient(source_p))
753 >  /* cache can send if banned */
754 >  if (MyClient(source_p))
755 >  {
756 >    if (ms)
757      {
758        if (ms->flags & CHFL_BAN_SILENCED)
759          return ERR_CANNOTSENDTOCHAN;
# Line 746 | Line 769 | can_send(struct Channel *chptr, struct C
769          ms->flags |= CHFL_BAN_CHECKED;
770        }
771      }
772 +    else if (is_banned(chptr, source_p))
773 +      return ERR_CANNOTSENDTOCHAN;
774    }
750  else if (chptr->mode.mode & MODE_NOPRIVMSGS)
751    return ERR_CANNOTSENDTOCHAN;
752
753  if (chptr->mode.mode & MODE_MODERATED)
754    return ERR_CANNOTSENDTOCHAN;
775  
776    return CAN_SEND_NONOP;
777   }
# Line 860 | Line 880 | check_splitmode(void *unused)
880   */
881   void
882   set_channel_topic(struct Channel *chptr, const char *topic,
883 <                  const char *topic_info, time_t topicts)
883 >                  const char *topic_info, time_t topicts, int local)
884   {
885 <  strlcpy(chptr->topic, topic, sizeof(chptr->topic));
885 >  if (local)
886 >    strlcpy(chptr->topic, topic, IRCD_MIN(sizeof(chptr->topic), ServerInfo.max_topic_length + 1));
887 >  else
888 >    strlcpy(chptr->topic, topic, sizeof(chptr->topic));
889 >
890    strlcpy(chptr->topic_info, topic_info, sizeof(chptr->topic_info));
891 <  chptr->topic_time = topicts;
891 >  chptr->topic_time = topicts;
892   }

Diff Legend

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