modules/stats.php
author Dan
Sat, 15 Nov 2008 14:59:51 -0500
changeset 19 eb92dc5d9fb4
parent 17 b07b23187b17
child 20 e2f6e3af9959
permissions -rw-r--r--
Can't believe I never put graphs.php in there.
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());
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    11
$stats_day = gmdate('Ymd');
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
    12
@include("./stats/stats-data-$stats_day.php");
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    13
unset($stats_data['members']);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    14
$stats_data['members'] =& $stats_memberlist;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    15
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    16
eb_hook('event_self_join', 'stats_init_channel($this);');
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    17
eb_hook('event_raw_message', 'stats_process_message($chan, $message);');
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    18
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
    19
eb_hook('event_other', 'stats_handle_other_event($message);');
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    20
eb_hook('event_privmsg', 'stats_handle_privmsg($message);');
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    21
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    22
function stats_init_channel(&$chan)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    23
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    24
  global $stats_memberlist, $stats_prefixes, $stats_data;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    25
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    26
  $channel_name = $chan->get_channel_name();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    27
  $stats_memberlist[$channel_name] = array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    28
  $prefixes_regexp = '/^([' . preg_quote(implode('', $stats_prefixes)) . '])+/';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    29
  $prefixes_flipped = array_flip($stats_prefixes);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    30
  $prefixes_regexp_notlist = '/[^' . preg_quote(implode('', $prefixes_flipped)) . ']/';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    31
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    32
  if ( !isset($stats_data['messages'][$channel_name]) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    33
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    34
    $stats_data['messages'][$channel_name] = array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    35
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    36
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    37
  // read list of members from channel
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    38
  @stream_set_timeout($chan->parent->sock, 3);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    39
  while ( $msg = $chan->parent->get() )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    40
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    41
    if ( $ml = strstr($msg, ' 353 ') )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    42
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    43
      $memberlist = trim(substr(strstr($ml, ':'), 1));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    44
      $stats_memberlist[$channel_name] = explode(' ', $memberlist);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    45
      $stats_memberlist[$channel_name] = array_flip($stats_memberlist[$channel_name]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    46
      foreach ( $stats_memberlist[$channel_name] as $nick => $_ )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    47
      {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    48
        $stats_memberlist[$channel_name][$nick] = '';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    49
        while ( preg_match($prefixes_regexp, $nick) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    50
        {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    51
          $prefix = substr($nick, 0, 1);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    52
          $add = preg_replace($prefixes_regexp_notlist, '', strval($stats_memberlist[$channel_name][$nick]));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    53
          unset($stats_memberlist[$channel_name][$nick]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    54
          $nick = substr($nick, 1);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    55
          $stats_memberlist[$channel_name][$nick] = $prefixes_flipped[$prefix] . $add;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    56
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    57
      }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    58
      break;
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
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    63
function stats_process_message(&$chan, $message)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    64
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    65
  global $stats_memberlist, $stats_data;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    66
  $channel_name = $chan->get_channel_name();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    67
  if ( !isset($stats_memberlist[$channel_name]) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    68
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    69
    return false;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    70
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    71
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    72
  $ml =& $stats_memberlist[$channel_name];
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    73
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    74
  // we need to change statistics accordingly depending on the event
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    75
  if ( $message['action'] == 'JOIN' )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    76
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    77
    // member joined - init their flags and up the member count by one
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    78
    $ml[$message['nick']] = '';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    79
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    80
  else if ( $message['action'] == 'PART' )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    81
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    82
    // member left - clear flags and decrement the total member count
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    83
    unset($ml[$message['nick']]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    84
    $ml = array_values($ml);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    85
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    86
  else if ( $message['action'] == 'MODE' )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    87
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    88
    // 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
    89
    list($mode, $target) = explode(' ', $message['message']);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    90
    $action = substr($mode, 0, 1);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    91
    
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    92
    global $stats_prefixes;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    93
    $ml[$target] = str_replace(substr($mode, 1), '', $ml[$target]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    94
    if ( $action == '+' )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    95
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    96
      $ml[$target] .= substr($mode, 1);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    97
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    98
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
    99
  else if ( $message['action'] == 'PRIVMSG' )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   100
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   101
    // private message into $channel_name - mark the user active and log the message time
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   102
    if ( isset($stats_data['anonymous'][$message['nick']]) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   103
      $message['nick'] = 'Anonymous';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   104
    
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   105
    $messages =& $stats_data['messages'][$channel_name];
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   106
    
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   107
    $messages[] = array(
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   108
        'time' => time(),
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   109
        'nick' => $message['nick']
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
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   113
  stats_cron();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   114
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   115
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   116
function stats_list_members(&$chan, &$message)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   117
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   118
  global $stats_memberlist;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   119
  $channel_name = $chan->get_channel_name();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   120
  if ( !isset($stats_memberlist[$channel_name]) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   121
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   122
    return false;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   123
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   124
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   125
  $ml =& $stats_memberlist[$channel_name];
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   126
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   127
  $chan->parent->privmsg($message['nick'], "memberlist:\n" . str_replace("\n", ' ', print_r($ml, true)));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   128
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   129
  return true;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   130
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   131
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   132
function stats_del_user(&$chan, &$message)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   133
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   134
  global $stats_memberlist, $privileged_list, $irc, $stats_data;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   135
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   136
  // remove a user from the DB
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   137
  $targetuser = trim(substr(strstr($message['message'], '|'), 1));
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   138
  if ( empty($targetuser) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   139
    $targetuser = $message['nick'];
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   140
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   141
  if ( $targetuser != $message['nick'] && !in_array($message['nick'], $privileged_list) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   142
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   143
    $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
   144
    return true;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   145
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   146
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   147
  // we should be good - delete the user
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   148
  foreach ( $stats_data['messages'] as $channel => &$messages )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   149
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   150
    foreach ( $messages as $i => &$currentmessage )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   151
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   152
      if ( $currentmessage['nick'] == $targetuser )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   153
      {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   154
        unset($messages[$i]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   155
      }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   156
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   157
    $messages = array_values($messages);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   158
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   159
  unset($users, $currentmessage, $messages);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   160
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   161
  global $nick;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   162
  $greeting = ( $targetuser == $message['nick'] ) ? "All of your statistics data" : "All of {$targetuser}'s statistic data";
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   163
  $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
   164
  $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
   165
  $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
   166
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   167
  return true;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   168
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   169
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   170
function stats_handle_privmsg(&$message)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   171
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   172
  global $irc, $stats_data, $nick;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   173
  static $poll_list = array();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   174
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   175
  $message['message'] = strtolower($message['message']);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   176
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   177
  if ( trim($message['message']) === 'anonymize' )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   178
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   179
    $stats_data['anonymous'][$message['nick']] = true;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   180
    $poll_list[$message['nick']] = true;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   181
    $irc->privmsg($message['nick'], "Anonymization complete. Any further statistics recorded about you will be anonymous.");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   182
    $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
   183
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   184
  else if ( trim($message['message']) === 'denonymize' )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   185
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   186
    $stats_data['anonymous'][$message['nick']] = false;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   187
    unset($stats_data['anonymous'][$message['nick']]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   188
    $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
   189
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   190
  else if ( trim($message['message']) === 'yes' && isset($poll_list[$message['nick']]) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   191
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   192
    // anonymize logs for this user
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   193
    // we should be good - delete the user
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   194
    $targetuser = $message['nick'];
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   195
    
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   196
    foreach ( $stats_data['messages'] as $channel => &$messages )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   197
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   198
      foreach ( $messages as $i => &$currentmessage )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   199
      {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   200
        if ( $currentmessage['nick'] == $targetuser )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   201
        {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   202
          $currentmessage['nick'] = 'Anonymous';
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   203
        }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   204
      }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   205
      $messages = array_values($messages);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   206
    }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   207
    unset($users, $currentmessage, $messages);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   208
    $irc->privmsg($message['nick'], "Anonymization complete. All past statistics on your nick are now anonymous.");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   209
    
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   210
    unset($poll_list[$message['nick']]);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   211
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   212
  stats_cron();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   213
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   214
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   215
function stats_handle_other_event(&$message)
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   216
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   217
  global $stats_memberlist;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   218
  
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   219
  if ( $message['action'] == 'NICK' )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   220
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   221
    // 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
   222
    foreach ( $stats_memberlist as &$ml )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   223
    {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   224
      if ( isset($ml[$message['nick']]) )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   225
      {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   226
        $ml[$message['message']] = $ml[$message['nick']];
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   227
        unset($ml[$message['nick']]);
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
  }
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   231
  stats_cron();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   232
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   233
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   234
function stats_cron()
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   235
{
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   236
  static $commit_time = 0;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   237
  $now = time();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   238
  // commit to disk every 1 minute
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   239
  if ( $commit_time + 60 < $now )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   240
  {
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   241
    $commit_time = $now;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   242
    stats_commit();
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
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   246
function stats_commit()
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   247
{
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   248
  global $stats_data, $stats_day;
17
b07b23187b17 OK, *now* it should actually cycle the data once a day
Dan
parents: 15
diff changeset
   249
  
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   250
  ob_start();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   251
  var_export($stats_data);
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   252
  $stats_data_exported = ob_get_contents();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   253
  ob_end_clean();
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   254
  
15
5e2d1514ccd0 Made stats use multiple files to reduce memory usage
Dan
parents: 8
diff changeset
   255
  $fp = @fopen("./stats/stats-data-$stats_day.php", 'w');
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   256
  if ( !$fp )
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   257
    return false;
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   258
  fwrite($fp, "<?php\n\$stats_data = $stats_data_exported;\n");
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   259
  fclose($fp);
17
b07b23187b17 OK, *now* it should actually cycle the data once a day
Dan
parents: 15
diff changeset
   260
  
b07b23187b17 OK, *now* it should actually cycle the data once a day
Dan
parents: 15
diff changeset
   261
  if ( $stats_day != gmdate('Ymd') )
b07b23187b17 OK, *now* it should actually cycle the data once a day
Dan
parents: 15
diff changeset
   262
  {
b07b23187b17 OK, *now* it should actually cycle the data once a day
Dan
parents: 15
diff changeset
   263
    // it's a new day! flush all our logs
b07b23187b17 OK, *now* it should actually cycle the data once a day
Dan
parents: 15
diff changeset
   264
    foreach ( $stats_data['messages'] as &$data )
b07b23187b17 OK, *now* it should actually cycle the data once a day
Dan
parents: 15
diff changeset
   265
    {
b07b23187b17 OK, *now* it should actually cycle the data once a day
Dan
parents: 15
diff changeset
   266
      $data = array();
b07b23187b17 OK, *now* it should actually cycle the data once a day
Dan
parents: 15
diff changeset
   267
    }
b07b23187b17 OK, *now* it should actually cycle the data once a day
Dan
parents: 15
diff changeset
   268
  }
b07b23187b17 OK, *now* it should actually cycle the data once a day
Dan
parents: 15
diff changeset
   269
  
b07b23187b17 OK, *now* it should actually cycle the data once a day
Dan
parents: 15
diff changeset
   270
  $stats_day = gmdate('Ymd');
8
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   271
}
0acb8d9a3194 Welcome, modularization and stats.
Dan
parents:
diff changeset
   272