modules/stats.php
author Dan
Sun, 31 Aug 2008 10:13:20 +0000
changeset 11 b3fec7467c05
parent 8 0acb8d9a3194
child 15 5e2d1514ccd0
permissions -rw-r--r--
Fixed $nick being overwritten in index
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     1
<?php
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     2
0acb8d9a3194 Welcome, modularization and stats.
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.
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     4
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     5
$stats_memberlist = array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     6
$stats_prefixes = array(
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     7
  'o' => '@',
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     8
  'v' => '+'
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
     9
);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    10
$stats_data = array('anonymous' => array(), 'messages' => array());
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    11
@include('./stats-data.php');
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    12
unset($stats_data['members']);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    13
$stats_data['members'] =& $stats_memberlist;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    14
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    15
eb_hook('event_self_join', 'stats_init_channel($this);');
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    16
eb_hook('event_raw_message', 'stats_process_message($chan, $message);');
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    17
eb_hook('snippet_dynamic', 'if ( $snippet === "memberlist" ) return stats_list_members($chan, $message); if ( $snippet === "deluser" ) return stats_del_user($chan, $message);');
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    18
eb_hook('event_other', 'stats_handle_other_event($message);');
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    19
eb_hook('event_privmsg', 'stats_handle_privmsg($message);');
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    20
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    21
function stats_init_channel(&$chan)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    22
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    23
  global $stats_memberlist, $stats_prefixes, $stats_data;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    24
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    25
  $channel_name = $chan->get_channel_name();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    26
  $stats_memberlist[$channel_name] = array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    27
  $prefixes_regexp = '/^([' . preg_quote(implode('', $stats_prefixes)) . '])+/';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    28
  $prefixes_flipped = array_flip($stats_prefixes);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    29
  $prefixes_regexp_notlist = '/[^' . preg_quote(implode('', $prefixes_flipped)) . ']/';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    30
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    31
  if ( !isset($stats_data['messages'][$channel_name]) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    32
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    33
    $stats_data['messages'][$channel_name] = array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    34
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    35
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    36
  // read list of members from channel
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    37
  @stream_set_timeout($chan->parent->sock, 3);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    38
  while ( $msg = $chan->parent->get() )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    39
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    40
    if ( $ml = strstr($msg, ' 353 ') )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    41
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    42
      $memberlist = trim(substr(strstr($ml, ':'), 1));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    43
      $stats_memberlist[$channel_name] = explode(' ', $memberlist);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    44
      $stats_memberlist[$channel_name] = array_flip($stats_memberlist[$channel_name]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    45
      foreach ( $stats_memberlist[$channel_name] as $nick => $_ )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    46
      {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    47
        $stats_memberlist[$channel_name][$nick] = '';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    48
        while ( preg_match($prefixes_regexp, $nick) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    49
        {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    50
          $prefix = substr($nick, 0, 1);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    51
          $add = preg_replace($prefixes_regexp_notlist, '', strval($stats_memberlist[$channel_name][$nick]));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    52
          unset($stats_memberlist[$channel_name][$nick]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    53
          $nick = substr($nick, 1);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    54
          $stats_memberlist[$channel_name][$nick] = $prefixes_flipped[$prefix] . $add;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    55
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    56
      }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    57
      break;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    58
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    59
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    60
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    61
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    62
function stats_process_message(&$chan, $message)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    63
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    64
  global $stats_memberlist, $stats_data;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    65
  $channel_name = $chan->get_channel_name();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    66
  if ( !isset($stats_memberlist[$channel_name]) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    67
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    68
    return false;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    69
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    70
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    71
  $ml =& $stats_memberlist[$channel_name];
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    72
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    73
  // we need to change statistics accordingly depending on the event
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    74
  if ( $message['action'] == 'JOIN' )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    75
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    76
    // member joined - init their flags and up the member count by one
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    77
    $ml[$message['nick']] = '';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    78
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    79
  else if ( $message['action'] == 'PART' )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    80
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    81
    // member left - clear flags and decrement the total member count
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    82
    unset($ml[$message['nick']]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    83
    $ml = array_values($ml);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    84
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    85
  else if ( $message['action'] == 'MODE' )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    86
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    87
    // update member list (not sure why this would be useful, but export it anyway - display scripts might find it useful)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    88
    list($mode, $target) = explode(' ', $message['message']);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    89
    $action = substr($mode, 0, 1);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    90
    
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    91
    global $stats_prefixes;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    92
    $ml[$target] = str_replace(substr($mode, 1), '', $ml[$target]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    93
    if ( $action == '+' )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    94
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    95
      $ml[$target] .= substr($mode, 1);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    96
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    97
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    98
  else if ( $message['action'] == 'PRIVMSG' )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    99
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   100
    // private message into $channel_name - mark the user active and log the message time
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   101
    if ( isset($stats_data['anonymous'][$message['nick']]) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   102
      $message['nick'] = 'Anonymous';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   103
    
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   104
    $messages =& $stats_data['messages'][$channel_name];
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   105
    
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   106
    $messages[] = array(
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   107
        'time' => time(),
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   108
        'nick' => $message['nick']
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   109
      );
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   110
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   111
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   112
  stats_cron();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   113
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   114
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   115
function stats_list_members(&$chan, &$message)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   116
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   117
  global $stats_memberlist;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   118
  $channel_name = $chan->get_channel_name();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   119
  if ( !isset($stats_memberlist[$channel_name]) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   120
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   121
    return false;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   122
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   123
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   124
  $ml =& $stats_memberlist[$channel_name];
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   125
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   126
  $chan->parent->privmsg($message['nick'], "memberlist:\n" . str_replace("\n", ' ', print_r($ml, true)));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   127
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   128
  return true;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   129
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   130
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   131
function stats_del_user(&$chan, &$message)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   132
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   133
  global $stats_memberlist, $privileged_list, $irc, $stats_data;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   134
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   135
  // remove a user from the DB
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   136
  $targetuser = trim(substr(strstr($message['message'], '|'), 1));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   137
  if ( empty($targetuser) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   138
    $targetuser = $message['nick'];
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   139
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   140
  if ( $targetuser != $message['nick'] && !in_array($message['nick'], $privileged_list) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   141
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   142
    $irc->privmsg($message['nick'], "Sorry, you need to be a moderator to delete statistics for users other than yourself.");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   143
    return true;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   144
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   145
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   146
  // we should be good - delete the user
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   147
  foreach ( $stats_data['messages'] as $channel => &$messages )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   148
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   149
    foreach ( $messages as $i => &$currentmessage )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   150
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   151
      if ( $currentmessage['nick'] == $targetuser )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   152
      {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   153
        unset($messages[$i]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   154
      }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   155
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   156
    $messages = array_values($messages);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   157
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   158
  unset($users, $currentmessage, $messages);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   159
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   160
  global $nick;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   161
  $greeting = ( $targetuser == $message['nick'] ) ? "All of your statistics data" : "All of {$targetuser}'s statistic data";
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   162
  $irc->privmsg($message['nick'], "$greeting has been removed from the database for all channels. The changes will show up in the next commit to disk, which is usually no more than once every two minutes.");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   163
  $irc->privmsg($message['nick'], "Want your stats to be anonymized in the future? Type /msg $nick anonymize to make me keep all your stats anonymous in the future. This only applies to your current nick though - for example if you change your nick to \"{$message['nick']}|sleep\" or similar your information will not be anonymous.");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   164
  $irc->privmsg($message['nick'], "You can't clear your logs if you're anonymous. Type /msg $nick denonymize to remove yourself from the anonymization list. Anonymized logs can't be converted back to their original nicks.");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   165
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   166
  return true;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   167
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   168
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   169
function stats_handle_privmsg(&$message)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   170
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   171
  global $irc, $stats_data, $nick;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   172
  static $poll_list = array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   173
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   174
  $message['message'] = strtolower($message['message']);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   175
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   176
  if ( trim($message['message']) === 'anonymize' )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   177
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   178
    $stats_data['anonymous'][$message['nick']] = true;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   179
    $poll_list[$message['nick']] = true;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   180
    $irc->privmsg($message['nick'], "Anonymization complete. Any further statistics recorded about you will be anonymous.");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   181
    $irc->privmsg($message['nick'], "Do you want to also anonymize any past statistics about you? (type \"yes\" or \"no\")");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   182
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   183
  else if ( trim($message['message']) === 'denonymize' )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   184
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   185
    $stats_data['anonymous'][$message['nick']] = false;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   186
    unset($stats_data['anonymous'][$message['nick']]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   187
    $irc->privmsg($message['nick'], "Denonymization complete. Any further statistics recorded about you will bear your nick. Remember that you can always change this with /msg $nick anonymize.");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   188
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   189
  else if ( trim($message['message']) === 'yes' && isset($poll_list[$message['nick']]) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   190
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   191
    // anonymize logs for this user
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   192
    // we should be good - delete the user
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   193
    $targetuser = $message['nick'];
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   194
    
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   195
    foreach ( $stats_data['messages'] as $channel => &$messages )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   196
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   197
      foreach ( $messages as $i => &$currentmessage )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   198
      {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   199
        if ( $currentmessage['nick'] == $targetuser )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   200
        {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   201
          $currentmessage['nick'] = 'Anonymous';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   202
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   203
      }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   204
      $messages = array_values($messages);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   205
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   206
    unset($users, $currentmessage, $messages);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   207
    $irc->privmsg($message['nick'], "Anonymization complete. All past statistics on your nick are now anonymous.");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   208
    
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   209
    unset($poll_list[$message['nick']]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   210
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   211
  stats_cron();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   212
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   213
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   214
function stats_handle_other_event(&$message)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   215
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   216
  global $stats_memberlist;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   217
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   218
  if ( $message['action'] == 'NICK' )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   219
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   220
    // we have a nick change; go through all channels and replace the old nick with the new
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   221
    foreach ( $stats_memberlist as &$ml )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   222
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   223
      if ( isset($ml[$message['nick']]) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   224
      {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   225
        $ml[$message['message']] = $ml[$message['nick']];
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   226
        unset($ml[$message['nick']]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   227
      }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   228
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   229
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   230
  stats_cron();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   231
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   232
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   233
function stats_cron()
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   234
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   235
  static $commit_time = 0;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   236
  $now = time();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   237
  // commit to disk every 1 minute
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   238
  if ( $commit_time + 60 < $now )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   239
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   240
    $commit_time = $now;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   241
    stats_commit();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   242
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   243
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   244
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   245
function stats_commit()
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   246
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   247
  global $stats_data;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   248
  ob_start();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   249
  var_export($stats_data);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   250
  $stats_data_exported = ob_get_contents();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   251
  ob_end_clean();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   252
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   253
  $fp = @fopen('./stats-data.php', 'w');
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   254
  if ( !$fp )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   255
    return false;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   256
  fwrite($fp, "<?php\n\$stats_data = $stats_data_exported;\n");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   257
  fclose($fp);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   258
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   259