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