Skip to content

Instantly share code, notes, and snippets.

@philnelson
Forked from gavinblair/minetweet.php
Created August 3, 2011 18:12
Show Gist options
  • Save philnelson/1123366 to your computer and use it in GitHub Desktop.
Save philnelson/1123366 to your computer and use it in GitHub Desktop.
script that periodically checks the minecraft server log, and tweets when players log in/out
<?php
//run via cron every 2 minutes
$log_location = "/users/minecraft/minecraft/";
$web_viewable_location = "/var/www/gotham.extrafuture.com/public_html/";
$bot_email = "gotham@extrafuture.com";
$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