Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/php
- <?php
- /****** ULTRA LAZY CODE GOOOOOOO ******/
- /* Database structure:
- *
- * 'messages':
- * - 'enabled' - BOOLEAN
- * - 'user' ---- VARCHAR(64)
- * - 'notice' -- INT
- * - 'message' - LONGVARCHAR(1000)
- *
- * 'admins':
- * - 'id' ------ INT
- */
- // Allowed arguments & their defaults
- $runmode = array(
- 'no-daemon' => false,
- 'help' => false,
- 'write-initd' => false,
- );
- // Scan command line attributes for allowed arguments
- foreach($argv as $k=>$arg) {
- if (substr($arg, 0, 2) == '--' && isset($runmode[substr($arg, 2)])) {
- $runmode[substr($arg, 2)] = true;
- }
- }
- // Help mode. Shows allowed argumentents and quit directly
- if($runmode['help'] == true) {
- echo 'Usage: '.$argv[0].' [runmode]' . "\n";
- echo 'Available runmodes:' . "\n";
- foreach ($runmode as $runmod=>$val) {
- echo ' --'.$runmod . "\n";
- }
- die();
- }
- // Include Class
- error_reporting(E_STRICT);
- require_once 'System/Daemon.php';
- // Setup
- $options = array(
- 'appName' => 'welcomed',
- 'appDir' => dirname(__FILE__),
- 'appDescription' => 'Parses StatusNet public events and welcomes users with 1 notice.',
- 'authorName' => 'Tylian',
- 'authorEmail' => 'immatyger@gmail.com',
- 'sysMaxExecutionTime' => '0',
- 'sysMaxInputTime' => '0',
- 'sysMemoryLimit' => '512M',
- 'appRunAsGID' => 1000,
- 'appRunAsUID' => 1000,
- );
- System_Daemon::setOptions($options);
- // This program can also be run in the forground with runmode --no-daemon
- if(!$runmode['no-daemon']) {
- System_Daemon::start();
- } else {
- System_Daemon::info('running in no-daemon mode');
- }
- // With the runmode --write-initd, this program can automatically write a
- // system startup file called: 'init.d'
- // This will make sure your daemon will be started on reboot
- if(!$runmode['write-initd']) {
- System_Daemon::info('not writing an init.d script this time');
- } else {
- if(($initd_location = System_Daemon::writeAutoRun()) === false) {
- System_Daemon::notice('unable to write init.d script');
- } else {
- System_Daemon::info('sucessfully written startup script: %s',
- $initd_location);
- }
- }
- // ----------------------------------------------------------------------
- // Bot config
- // Site configuration
- $apiBase = 'http://rainbowdash.net/api';
- $username = 'welcomepony';
- $password = 'YOURFACEISLIKEABUTT';
- // MySQL configuration
- $mysqlHost = 'localhost';
- $mysqlUsername = 'MAGICFRIENDSHIPHORSES';
- $mysqlPassword = 'IUSEDTOWONDERWHATFRIENDSHIPCOULDBE';
- $mysqlDatabase = 'welcomebot';
- $admins = array();/*
- $admins[] = 'minti';
- $admins[] = 'colfax';
- $admins[] = 'redenchilada';
- $admins[] = 'nerthos';
- $admins[] = 'ceruleanspark';
- $admins[] = 'widget';*/
- initAdmins();
- $admins[798] = 'a';
- // Time to wait between each check
- $refreshDelay = 30;
- $commandDelay = 60;
- // Global variables
- $lastNoticeId = 0;
- $lastCommandTime = 0;
- // Random crap
- $welcomeToggle = true; // Toggle on/off for welcome me command
- $uniqueIds = array(); // Array of unique users
- $lastReset = ""; // Last day that the unique users thing was reset. It's text cause I can, and did.
- $postingFrom = "Gravity Falls, OR"; // Source
- // -----------------------------------------------------------------------
- // Bot entry point
- // Prepare posts to get the last notice ID.
- $posts = getTimeline();
- foreach($posts as $post) {
- if($post->id > $lastNoticeId) {
- $lastNoticeId = $post->id;
- }
- }
- while(!System_Daemon::isDying()) {
- $posts = getTimeline();
- foreach($posts as $post) {
- // Store the ID so we don't reparse notices we've already seen
- if($post->id > $lastNoticeId)
- $lastNoticeId = $post->id;
- // Check if we need to empty the unique users array
- if(date('j') != $lastReset) {
- $lastReset = date('j');
- System_Daemon::info('$s users posted today.', count($uniqueIds);
- $uniqueIds = array();
- }
- // Check if the user is unique, if so add them to the unique user array
- if(!in_array($post->user->id, $uniqueIds)) {
- $uniqueIds[] = $post->user->id;
- }
- // Send a welcome if they have only one notice
- if($post->user->statuses_count == 1) {
- System_Daemon::info('new user found: %s', $post->user->screen_name);
- postNotice(getMessage($post->user->screen_name), $post->id);
- continue;
- }
- $postText = strtolower($post->text);
- // This message doesn't reference us, ignore it!
- if(strpos($postText, strtolower("@$username")) === false)
- continue;
- // Store if they're an admin
- $isAdmin = isset($admins[$post->user->id]);
- // SUPER HAPPY CRAZY LET EVERYONE DO EVERYTHING MODE
- $isAdmin = true;
- // Add message to Queue if they request it
- if(substr($postText, 0, strlen($username) + 6) == "@$username add ") {
- System_Daemon::info('adding message from %s to database', $post->user->screen_name);
- $message = substr($post->text, strlen($username) + 6);
- $reply = sprintf('@%s Thank you for the suggestion! The message has been sent to a queue to be approved, and once it is you\'ll see it being sent to newponies!', $post->user->screen_name);
- addMessage($message, $post->user->screen_name, $post->id);
- postNotice($reply, $post->id);
- continue;
- }
- // Commands that have a limit
- if((time() - $lastCommandTime >= $commandDelay) || $isAdmin) {
- if(strpos($postText, 'ping') !== false) {
- System_Daemon::info('responding to ping from %s', $post->user->screen_name);
- $message = sprintf('@%s pong!', $post->user->screen_name);
- postNotice($message, $post->id);
- $lastCommandTime = time();
- continue;
- }
- if(strpos($postText, 'ell me a story') !== false) {
- System_Daemon::info('telling %s the story of welcomepony', $post->user->screen_name);
- $message = sprintf('@%s Once upon a time there was a pony who felt very sad because she didn\'t know what to do with her life. Then she found out that her special talent was welcoming people! So she began welcoming all the newponies who showed up around town. The end!', $post->user->screen_name);
- postNotice($message, $post->id);
- $lastCommandTime = time();
- continue;
- }
- if(strpos($postText, 'welcome me') !== false) {
- // Deny the welcome if a mod has turned welcomes off
- if(!$welcomeToggle) continue;
- System_Daemon::info('sending a forced welcome to %s', $post->user->screen_name);
- postNotice(getMessage($post->user->screen_name), $post->id);
- $lastCommandTime = time();
- continue;
- }
- if(strpos($postText, 'users') !== false) {
- System_Daemon::info('sending unique user stats to %s', $post->user->screen_name);
- $message = sprintf('@%s Wowie, %d unique users have posted today!!', $post->user->screen_name, count($uniqueIds));
- postNotice($message, $post->id);
- $lastCommandTime = time();
- continue;
- }
- if(strpos($postText, 'You\'re ugly') !== false ||
- strpos($postText, 'you\'re ugly') !== false ||
- strpos($postText, 'Your ugly') !== false ||
- strpos($postText, 'your ugly') !== false) {
- System_Daemon::info('getting mad at %s', $post->user->screen_name);
- $message = sprintf('@%s #banned', $post->user->screen_name);
- postNotice($message, $post->id);
- $lastCommandTime = time();
- continue;
- }
- }
- // The following commands are admin only.
- if(!$isAdmin) continue;
- if(strpos($postText, 'approve welcome') !== false) {
- System_Daemon::info('%s approved welcome notice %s', $post->user->screen_name, $post->in_reply_to_status_id);
- $message = sprintf('@%s Okey dokey lokey! I\'ll start sending this message to newponies!', $post->user->screen_name);
- postNotice($message, $post->id);
- approveMessage($post->in_reply_to_status_id);
- continue;
- }
- if(strpos($postText, 'welcome off') !== false) {
- System_Daemon::info('%s disabled welcome messages', $post->user->screen_name);
- $message = sprintf('@%s Okey dokey lokey! I won\'t welcome ponies who want me to!', $post->user->screen_name);
- postNotice($message, $post->id);
- $welcomeToggle = false;
- continue;
- }
- if(strpos($postText, 'welcome on') !== false) {
- System_Daemon::info('%s enabled welcome messages', $post->user->screen_name);
- $message = sprintf('@%s Okey dokey lokey! I\'ll welcome ponies who want me too!', $post->user->screen_name);
- postNotice($message, $post->id);
- $welcomeToggle = true;
- continue;
- }
- if(strpos($postText, 'unique reset') !== false) {
- System_Daemon::info('%s resetted unique user list manually.', $post->user->screen_name);
- $message = sprintf('@%s Okey dokey lokey! I\'ll reset my list of unique ponies!', $post->user->screen_name);
- postNotice($message, $post->id);
- $uniqueIds = array();
- continue;
- }
- if(strpos($postText, 'shut down') !== false) {
- System_Daemon::info('stopping due to command from '. $post->user->screen_name);
- $message = sprintf('@%s Okay.. If you insist.. Goodbye cruel Equestria!!', $post->user->screen_name);
- postNotice($message, $post->id);
- System_Daemon::stop();
- exit();
- }
- if(substr($postText, 0, strlen($username) + 10) == "@$username promote ") {
- $promoting = intval(substr($post->text, strlen($username) + 10));
- System_Daemon::info('%s is promoting user ID %s to bot admin', $post->user->screen_name, $promoting);
- $message = "";
- if(addAdmin($promoting))
- $message = '@%s This user has been promoted.';
- else
- $message = '@%s This user could not be promoted.';
- $reply = sprintf($message, $post->user->screen_name);
- postNotice($reply, $post->id);
- continue;
- }
- if(substr($postText, 0, strlen($username) + 9) == "@$username demote ") {
- $demoting = intval(substr($post->text, strlen($username) + 9));
- System_Daemon::info('%s is demoting user ID %s from bot admin', $post->user->screen_name, $demoting);
- $message = "";
- if(removeAdmin($demoting))
- $message = '@%s This user has been demoted.';
- else
- $message = '@%s This user could not be demoted.';
- $reply = sprintf($message, $post->user->screen_name);
- postNotice($reply, $post->id);
- continue;
- }
- /*
- if(strpos($postText, 'debug') !== false) {
- $message = sprintf('@%s Debug information has been printed to the log.', $post->user->screen_name);
- postNotice($message, $post->id);
- System_Daemon::info('Bot admins:');
- foreach($admins as $k => $v)
- System_Daemon::info($k . $v);
- System_Daemon::info('Unique users:');
- foreach($uniqueIds as $i)
- System_Daemon::info($i);
- continue;
- }
- */
- if(substr($postText, 0, strlen($username) + 9) == "@$username source ") {
- $postingFrom = substr($post->text, strlen($username) + 9);
- System_Daemon::info('%s changed source to %s', $post->user->screen_name, $postingFrom);
- $reply = sprintf('@%s Okey, I\'ll start talking from "%s"!', $post->user->screen_name, $postingFrom);
- postNotice($reply, $post->id);
- continue;
- }
- }
- System_Daemon::iterate($refreshDelay);
- }
- System_Daemon::info('stopping');
- System_Daemon::stop();
- // ----------------------------------------------------------------------
- // Bot helper functions below this line
- // Gets the public timeline as an object
- function getTimeline() {
- global $lastNoticeId, $apiBase;
- $curl = curl_init();
- curl_setopt($curl, CURLOPT_URL, "$apiBase/statuses/public_timeline.json?since_id=$lastNoticeId");
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
- $data = curl_exec($curl);
- curl_close($curl);
- return json_decode($data);
- }
- // Gets a random welcome message from the database.
- function getMessage($name) {
- global $mysqlHost, $mysqlUsername, $mysqlPassword, $mysqlDatabase;
- mysql_connect($mysqlHost, $mysqlUsername, $mysqlPassword);
- mysql_select_db($mysqlDatabase);
- $result = mysql_query('SELECT * FROM messages WHERE enabled=1 ORDER BY RAND() LIMIT 1');
- $row = mysql_fetch_object($result);
- mysql_close();
- return sprintf($row->message, $name);
- }
- // Adds a welcome message to the database
- function addMessage($message, $user, $id) {
- global $mysqlHost, $mysqlUsername, $mysqlPassword, $mysqlDatabase;
- mysql_connect($mysqlHost, $mysqlUsername, $mysqlPassword);
- mysql_select_db($mysqlDatabase);
- $query = sprintf("INSERT INTO `%s`.`messages` (`enabled`, `user`, `notice`, `message`)
- VALUES (0, '%s', '%s', '%s')",
- $mysqlDatabase,
- mysql_real_escape_string($user),
- mysql_real_escape_string($id),
- mysql_real_escape_string($message));
- $result = mysql_query($query);
- mysql_close();
- return true;
- }
- function approveMessage($id) {
- global $mysqlHost, $mysqlUsername, $mysqlPassword, $mysqlDatabase;
- mysql_connect($mysqlHost, $mysqlUsername, $mysqlPassword);
- mysql_select_db($mysqlDatabase);
- $query = sprintf("UPDATE `%s`.`messages` SET `enabled`=1 WHERE notice=%s",
- $mysqlDatabase,
- mysql_real_escape_string($id));
- $result = mysql_query($query);
- mysql_close();
- return true;
- }
- // Posts a notice to the service using the auth provided on config
- function postNotice($notice, $replyId) {
- global $apiBase, $username, $password, $postingFrom;
- $curl = curl_init();
- System_Daemon::info('sending notice: %s', $notice);
- $data = array('status' => $notice, 'source' => $postingFrom, 'in_reply_to_status_id' => $replyId);
- curl_setopt($curl, CURLOPT_URL, "$apiBase/statuses/update.json");
- curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 5);
- curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($curl, CURLOPT_POST, 1);
- curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($data, '&'));
- curl_setopt($curl, CURLOPT_USERPWD, "$username:$password");
- curl_exec($curl);
- curl_close($curl);
- return true;
- }
- function initAdmins() {
- global $mysqlHost, $mysqlUsername, $mysqlPassword, $mysqlDatabase, $admins;
- mysql_connect($mysqlHost, $mysqlUsername, $mysqlPassword);
- mysql_select_db($mysqlDatabase);
- $result = mysql_query('SELECT * FROM admins');
- while($row = mysql_fetch_array($result))
- $admins[intval($row['id'])] = 'm';
- mysql_close();
- }
- function addAdmin($id) {
- global $mysqlHost, $mysqlUsername, $mysqlPassword, $mysqlDatabase, $admins;
- if(isset($admins[$id]))
- return false;
- mysql_connect($mysqlHost, $mysqlUsername, $mysqlPassword);
- mysql_select_db($mysqlDatabase);
- $query = sprintf("INSERT INTO `%s`.`admins` (`id`)
- VALUES ('%s')",
- $mysqlDatabase,
- mysql_real_escape_string($id));
- $result = mysql_query($query);
- $admins[$id] = 'm';
- mysql_close();
- return true;
- }
- function removeAdmin($id) {
- global $mysqlHost, $mysqlUsername, $mysqlPassword, $mysqlDatabase, $admins;
- if(!isset($admins[$id]) || $admins[$id] == 'a')
- return false;
- mysql_connect($mysqlHost, $mysqlUsername, $mysqlPassword);
- mysql_select_db($mysqlDatabase);
- $query = sprintf("DELETE FROM `%s`.`admins` WHERE `id`=`%s`",
- $mysqlDatabase,
- mysql_real_escape_string($id));
- $result = mysql_query($query);
- unset($admins[$id]);
- mysql_close();
- return true;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement