modules/memberlist.php
author Dan
Wed, 31 Dec 2008 21:41:57 -0500
changeset 38 e6a4b7f91e91
parent 20 e2f6e3af9959
permissions -rw-r--r--
Built de-ghosting code into libirc core
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
20
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
     1
<?php
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
     2
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
     3
// most of the code in here goes towards keeping track of the list of members currently in the various channels we're in.
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
     4
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
     5
$eb_memberlist = array();
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
     6
$userflags = array(
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
     7
  'o' => '@',
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
     8
  'v' => '+'
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
     9
);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    10
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    11
eb_hook('event_self_join', 'mlist_init_channel($this);');
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    12
eb_hook('event_raw_message', 'mlist_process_message($chan, $message);');
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    13
eb_hook('snippet_dynamic', 'if ( $snippet === "memberlist" ) return mlist_list_members($chan, $message);');
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    14
eb_hook('event_other', 'mlist_handle_other_event($message);');
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    15
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    16
function mlist_init_channel(&$chan)
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    17
{
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    18
  global $eb_memberlist, $userflags;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    19
  
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    20
  $channel_name = $chan->get_channel_name();
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    21
  $eb_memberlist[$channel_name] = array();
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    22
  $prefixes_regexp = '/^([' . preg_quote(implode('', $userflags)) . '])+/';
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    23
  $prefixes_flipped = array_flip($userflags);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    24
  $prefixes_regexp_notlist = '/[^' . preg_quote(implode('', $prefixes_flipped)) . ']/';
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    25
  
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    26
  // read list of members from channel
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    27
  @stream_set_timeout($chan->parent->sock, 3);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    28
  while ( $msg = $chan->parent->get() )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    29
  {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    30
    if ( $ml = strstr($msg, ' 353 ') )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    31
    {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    32
      $memberlist = trim(substr(strstr($ml, ':'), 1));
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    33
      $eb_memberlist[$channel_name] = explode(' ', $memberlist);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    34
      $eb_memberlist[$channel_name] = array_flip($eb_memberlist[$channel_name]);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    35
      foreach ( $eb_memberlist[$channel_name] as $nick => $_ )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    36
      {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    37
        $eb_memberlist[$channel_name][$nick] = '';
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    38
        while ( preg_match($prefixes_regexp, $nick) )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    39
        {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    40
          $prefix = substr($nick, 0, 1);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    41
          $add = preg_replace($prefixes_regexp_notlist, '', strval($eb_memberlist[$channel_name][$nick]));
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    42
          unset($eb_memberlist[$channel_name][$nick]);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    43
          $nick = substr($nick, 1);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    44
          $eb_memberlist[$channel_name][$nick] = $prefixes_flipped[$prefix] . $add;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    45
        }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    46
      }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    47
      break;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    48
    }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    49
  }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    50
}
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    51
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    52
function mlist_process_message(&$chan, $message)
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    53
{
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    54
  global $eb_memberlist;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    55
  $channel_name = $chan->get_channel_name();
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    56
  if ( !isset($eb_memberlist[$channel_name]) )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    57
  {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    58
    return false;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    59
  }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    60
  
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    61
  $ml =& $eb_memberlist[$channel_name];
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    62
  
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    63
  // we need to change statistics accordingly depending on the event
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    64
  if ( $message['action'] == 'JOIN' )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    65
  {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    66
    // member joined - init their flags and up the member count by one
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    67
    $ml[$message['nick']] = '';
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    68
  }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    69
  else if ( $message['action'] == 'PART' )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    70
  {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    71
    // member left - clear flags and decrement the total member count
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    72
    unset($ml[$message['nick']]);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    73
  }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    74
  else if ( $message['action'] == 'MODE' )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    75
  {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    76
    // update member list (not sure why this would be useful, but export it anyway - display scripts might find it useful)
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    77
    list($mode, $target) = explode(' ', $message['message']);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    78
    $action = substr($mode, 0, 1);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    79
    
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    80
    global $userflags;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    81
    $ml[$target] = str_replace(substr($mode, 1), '', $ml[$target]);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    82
    if ( $action == '+' )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    83
    {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    84
      $ml[$target] .= substr($mode, 1);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    85
    }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    86
  }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    87
}
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    88
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    89
function mlist_list_members(&$chan, &$message)
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    90
{
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    91
  global $eb_memberlist;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    92
  $channel_name = $chan->get_channel_name();
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    93
  if ( !isset($eb_memberlist[$channel_name]) )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    94
  {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    95
    return false;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    96
  }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    97
  
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    98
  $ml =& $eb_memberlist[$channel_name];
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
    99
  
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   100
  $mlt = implode("\n", str_split(str_replace("\n", ' ', print_r($ml, true)), 400));
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   101
  $chan->parent->privmsg($message['nick'], "memberlist:\n" . $mlt);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   102
  
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   103
  return true;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   104
}
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   105
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   106
function mlist_handle_other_event(&$message)
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   107
{
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   108
  global $eb_memberlist;
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   109
  
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   110
  if ( $message['action'] == 'NICK' )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   111
  {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   112
    // we have a nick change; go through all channels and replace the old nick with the new
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   113
    foreach ( $eb_memberlist as &$ml )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   114
    {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   115
      if ( isset($ml[$message['nick']]) )
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   116
      {
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   117
        $ml[$message['message']] = $ml[$message['nick']];
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   118
        unset($ml[$message['nick']]);
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   119
      }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   120
    }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   121
  }
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   122
}
e2f6e3af9959 Stats now uses MySQL backend
Dan
parents:
diff changeset
   123