modules/iplogger.php
author Dan
Sat, 15 Nov 2008 15:38:21 -0500
changeset 28 a7e537c0b549
parent 21 d86ea89358ec
permissions -rw-r--r--
Fix undefined $total in stats-fe
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
21
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
     1
<?php
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
     2
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
     3
eb_hook('event_raw_message', 'iplogger_log_join($message);');
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
     4
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
     5
function iplogger_log_join($message)
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
     6
{
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
     7
  if ( $message['action'] == 'JOIN' )
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
     8
  {
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
     9
    $nick = mysql_real_escape_string($message['nick']);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    10
    $basenick = basenick($nick);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    11
    $host = mysql_real_escape_string($message['host']);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    12
    $ip = ( $_ = @resolve_ip($message['host'], $message['user']) ) ? mysql_real_escape_string($_) : '0.0.0.0';
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    13
    $channel = mysql_real_escape_string($message['message']);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    14
    $time = time();
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    15
    $query = "DELETE FROM ip_log WHERE
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    16
                nick = '$nick' AND
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    17
                basenick = '$basenick' AND
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    18
                ip = '$ip' AND
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    19
                hostname = '$host' AND
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    20
                channel = '$channel';";
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    21
    eb_mysql_query($query);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    22
    
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    23
    $query = "INSERT INTO ip_log ( nick, basenick, ip, hostname, channel, time ) VALUES(
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    24
                '$nick',
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    25
                '$basenick',
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    26
                '$ip',
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    27
                '$host',
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    28
                '$channel',
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    29
                $time
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    30
              );";
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    31
    eb_mysql_query($query);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    32
  }
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    33
  else if ( $message['ACTION'] == 'NICK' )
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    34
  {
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    35
    // this is for cross-referencing purposes.
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    36
    $nick = mysql_real_escape_string($message['message']);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    37
    $basenick = basenick($message['nick']);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    38
    $host = mysql_real_escape_string($message['host']);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    39
    $ip = ( $_ = @resolve_ip($message['host'], $message['user']) ) ? mysql_real_escape_string($_) : '0.0.0.0';
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    40
    $channel = '__nickchange';
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    41
    $time = time();
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    42
    $query = "DELETE FROM ip_log WHERE
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    43
                nick = '$nick' AND
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    44
                basenick = '$basenick' AND
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    45
                ip = '$ip' AND
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    46
                hostname = '$host' AND
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    47
                channel = '$channel';";
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    48
    eb_mysql_query($query);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    49
    
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    50
    $query = "INSERT INTO ip_log ( nick, basenick, ip, hostname, channel, time ) VALUES(
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    51
                '$nick',
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    52
                '$basenick',
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    53
                '$ip',
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    54
                '$host',
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    55
                '$channel',
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    56
                $time
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    57
              );";
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    58
    eb_mysql_query($query);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    59
  }
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    60
}
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    61
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    62
/**
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    63
 * Attempt to eliminate mini-statuses and such from nicknames.
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    64
 * @example
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    65
 <code>
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    66
 $basenick = basenick('enanobot|debug');
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    67
 // $basenick = 'enanobot'
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    68
 </code>
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    69
 * @param string Nickname
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    70
 * @return string
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    71
 */
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    72
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    73
function basenick($nick)
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    74
{
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    75
  if ( preg_match('/^`/', $nick) )
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    76
  {
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    77
    $nick = substr($nick, 1);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    78
  }
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    79
  return preg_replace('/(`|\|)(.+?)$/', '', $nick);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    80
}
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    81
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    82
/**
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    83
 * Resolve an IP address. First goes by checking if it's a mibbit or CGI-IRC IP/user, then performs lookups accordingly.
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    84
 * @param string Hostname
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    85
 * @param string Username
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    86
 * @return string IP address
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    87
 */
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    88
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    89
function resolve_ip($host, $user)
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    90
{
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    91
  if ( $host == 'webchat.mibbit.com' )
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    92
  {
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    93
    return hex2ipv4($user);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    94
  }
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    95
  return gethostbyname($host);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    96
}
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    97
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    98
function hex2ipv4($ip)
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
    99
{
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
   100
  $ip = preg_replace('/^0x/', '', $ip);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
   101
  $ip = str_split($ip, 2);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
   102
  foreach ( $ip as &$byte )
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
   103
  {
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
   104
    $byte = hexdec($byte);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
   105
  }
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
   106
  return implode('.', $ip);
d86ea89358ec Added IP logger module.
Dan
parents:
diff changeset
   107
}