-
-
Save TarasDev/6324774 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
//run via cron every 2 minutes | |
$log_location = "/users/minecraft/minecraft/"; | |
$web_viewable_location = "/var/www/gotham.extrafuture.com/public_html/"; | |
$bot_email = "[email protected]"; | |
$twittermail_account = ""; | |
//Get the log file | |
$log = file_get_contents($log_location."server.log"); | |
$log = explode("\n", $log); | |
$loggedinusers = array(); | |
//only look at lines that mention logging in or logging out | |
foreach($log as $line) { | |
if(strpos($line, "logged in with entity id")){ | |
//logged in | |
$parts = explode("[INFO] ", $line); | |
$parts = $parts[1]; | |
$parts = explode(" ", $parts); | |
$username = $parts[0]; | |
$loggedinusers[$username] = $username; | |
} else if (strpos($line, " lost connection: ")) { | |
//logged out | |
$parts = explode("[INFO] ", $line); | |
$parts = $parts[1]; | |
$parts = explode(" ", $parts); | |
$username = $parts[0]; | |
unset($loggedinusers[$username]); | |
} | |
} | |
//create users.txt if not exists | |
if(!file_exists($log_location."users.txt")){ | |
$handle = fopen($log_location."users.txt", "w"); | |
foreach($loggedinusers as $user) { | |
fwrite($handle, $user."\n"); | |
} | |
fclose($handle); | |
//don't bother tweeting, wait until next run | |
} else { | |
//or get the contents, same type of array as from server.log | |
$oldusers = file_get_contents($log_location."users.txt"); | |
$oldusers = explode("\n", $oldusers); | |
$newusers = array(); | |
foreach($loggedinusers as $loggedinuser) { | |
//is this user in $oldusers? | |
if(!in_array($loggedinuser, $oldusers)) { | |
//new user! add to the new users array | |
$newusers[] = $loggedinuser; | |
} | |
} | |
$lostusers = array(); | |
foreach($oldusers as $olduser) { | |
//is this user in $loggedusers? | |
if(!in_array($olduser, $loggedinusers)) { | |
//lost a user. add to lost users array | |
$lostusers[] = $olduser; | |
} | |
} | |
//update users.txt if needed | |
if(count($newusers) || count($lostusers)) { | |
$handle = fopen($log_location."users.txt", "w"); | |
foreach($loggedinusers as $user) { | |
fwrite($handle, $user."\n"); | |
} | |
fclose($handle); | |
//copy users.txt to the web accessible directory | |
copy($log_location."users.txt", $web_viewable_location."users.txt"); | |
} | |
//check to see if this mc username is also a twitter username | |
//if so, prepend a ".@" to the tweet | |
$headers = 'From: '.$bot_email.'' . "\r\n" . | |
'Reply-To: '.$bot_email.'' . "\r\n" . | |
'X-Mailer: PHP/' . phpversion(); | |
//tweet! | |
foreach($newusers as $user) { | |
if(strlen($user) > 0) { | |
$test = false; | |
@$test = file_get_contents("http://twitter.com/$user"); | |
if($test) { $dotat=".@"; } else { $dotat=""; } | |
$text = "$dotat$user has logged in! http://minecraft.net/skin/skin.jsp?user=$user#".time(); | |
echo $text."\n"; | |
echo mail($twittermail_account, "tweet", $text, $headers); | |
} | |
} | |
foreach($lostusers as $user) { | |
if(strlen($user) > 0) { | |
@$test = file_get_contents("http://twitter.com/$user"); | |
if($test) { $dotat=".@"; } else { $dotat=""; } | |
$text = "$dotat$user has logged out. http://minecraft.net/skin/skin.jsp?user=$user#".time(); | |
echo $text."\n"; | |
echo mail($twittermail_account, "tweet", $text, $headers); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment