Changeset 20 in code for trunk/upstream.go


Ignore:
Timestamp:
Feb 6, 2020, 6:24:32 PM (5 years ago)
Author:
contact
Message:

Split IRC helpers to separate file

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/upstream.go

    r19 r20  
    1212        "gopkg.in/irc.v3"
    1313)
    14 
    15 const (
    16         rpl_localusers   = "265"
    17         rpl_globalusers  = "266"
    18         rpl_topicwhotime = "333"
    19 )
    20 
    21 type modeSet string
    22 
    23 func (ms modeSet) Has(c byte) bool {
    24         return strings.IndexByte(string(ms), c) >= 0
    25 }
    26 
    27 func (ms *modeSet) Add(c byte) {
    28         if !ms.Has(c) {
    29                 *ms += modeSet(c)
    30         }
    31 }
    32 
    33 func (ms *modeSet) Del(c byte) {
    34         i := strings.IndexByte(string(*ms), c)
    35         if i >= 0 {
    36                 *ms = (*ms)[:i] + (*ms)[i+1:]
    37         }
    38 }
    39 
    40 func (ms *modeSet) Apply(s string) error {
    41         var plusMinus byte
    42         for i := 0; i < len(s); i++ {
    43                 switch c := s[i]; c {
    44                 case '+', '-':
    45                         plusMinus = c
    46                 default:
    47                         switch plusMinus {
    48                         case '+':
    49                                 ms.Add(c)
    50                         case '-':
    51                                 ms.Del(c)
    52                         default:
    53                                 return fmt.Errorf("malformed modestring %q: missing plus/minus", s)
    54                         }
    55                 }
    56         }
    57         return nil
    58 }
    59 
    60 type channelStatus byte
    61 
    62 const (
    63         channelPublic  channelStatus = '='
    64         channelSecret  channelStatus = '@'
    65         channelPrivate channelStatus = '*'
    66 )
    67 
    68 func parseChannelStatus(s string) (channelStatus, error) {
    69         if len(s) > 1 {
    70                 return 0, fmt.Errorf("invalid channel status %q: more than one character", s)
    71         }
    72         switch cs := channelStatus(s[0]); cs {
    73         case channelPublic, channelSecret, channelPrivate:
    74                 return cs, nil
    75         default:
    76                 return 0, fmt.Errorf("invalid channel status %q: unknown status", s)
    77         }
    78 }
    79 
    80 type membership byte
    81 
    82 const (
    83         membershipFounder   membership = '~'
    84         membershipProtected membership = '&'
    85         membershipOperator  membership = '@'
    86         membershipHalfOp    membership = '%'
    87         membershipVoice     membership = '+'
    88 )
    89 
    90 const stdMembershipPrefixes = "~&@%+"
    91 
    92 func parseMembershipPrefix(s string) (prefix membership, nick string) {
    93         // TODO: any prefix from PREFIX RPL_ISUPPORT
    94         if strings.IndexByte(stdMembershipPrefixes, s[0]) >= 0 {
    95                 return membership(s[0]), s[1:]
    96         } else {
    97                 return 0, s
    98         }
    99 }
    10014
    10115type upstreamChannel struct {
Note: See TracChangeset for help on using the changeset viewer.