diff --git a/fucking_coffee.rb b/fucking_coffee.rb index 20e37d6..29564be 100755 --- a/fucking_coffee.rb +++ b/fucking_coffee.rb @@ -11,8 +11,10 @@ require 'net/telnet' coffee_machine_ip = '10.10.42.42' password = '1234' password_prompt = 'Password: ' +delay_before_brew = 17 delay = 24 +sleep delay_before_brew con = Net::Telnet.new('Host' => coffee_machine_ip) con.cmd('String' => password, 'Match' => /#{password_prompt}/) con.cmd('sys brew') diff --git a/hangover.rb b/hangover.rb index 2bc4053..400c0d8 100755 --- a/hangover.rb +++ b/hangover.rb @@ -3,13 +3,6 @@ # Skip on weekends exit if Time.now.saturday? || Time.now.sunday? -log_file_name = File.dirname(__FILE__) + '/logs/hangover.txt' - -# Be sure that logs dir always exists -Dir.mkdir('logs') unless File.exists?(log_file_name) - -LOG_FILE = File.open(log_file_name, 'a+') - # Exit early if sessions with my username are found exit if `who -q`.include? ENV['USER'] @@ -27,19 +20,18 @@ TWILIO_AUTH_TOKEN = ENV['TWILIO_AUTH_TOKEN'] my_number = '+xxx' number_of_boss = '+xxx' -excuses = [ +excuse = [ 'Locked out', 'Pipes broke', 'Food poisoning', 'Not feeling well' -] +].sample # Send a text message @twilio.messages.create( from: my_number, to: number_of_boss, - body: 'Gonna work from home. ' + excuses.sample + body: "Gonna work from home. #{excuse}" ) # Log this -LOG_FILE.puts("Message sent at: #{Time.now}") -LOG_FILE.close +puts "Message sent at: #{Time.now} | Excuse: #{excuse}" diff --git a/kumar_asshole.rb b/kumar_asshole.rb index 0494fa3..ca5b9f6 100755 --- a/kumar_asshole.rb +++ b/kumar_asshole.rb @@ -9,21 +9,30 @@ GMAIL_USERNAME = ENV['GMAIL_USERNAME'] GMAIL_PASSWORD = ENV['GMAIL_PASSWORD'] gmail = Gmail.connect(GMAIL_USERNAME, GMAIL_PASSWORD) +kumars_email = 'kumar.a@example.com' +DB_NAME_REGEX = /\S+_staging/ KEYWORDS_REGEX = /sorry|help|wrong/i -gmail.inbox.find(:unread, from: 'kumar.a@example.com').each do |email| - if email.body[KEYWORDS_REGEX] - # Restore DB and send a reply +gmail.inbox.find(:unread, from: kumars_email).each do |email| + if email.body[KEYWORDS_REGEX] && (db_name = email.body[DB_NAME_REGEX]) + backup_file = "/home/backups/databases/#{db_name}-" + (Date.today - 1).strftime('%Y%m%d') + '.gz' + abort 'ERROR: Backup file not found' unless File.exist?(backup_file) + + # Restore DB + `gunzip -c #{backup_file} | psql #{db_name}` + + # Mark as read, add label and reply + email.read! email.label('Database fixes') - reply = reply_to(email.subject) + reply = create_reply(email.subject) gmail.deliver(reply) end end -def reply_to(subject) +def create_reply(subject) gmail.compose do - to "email@example.com" + to kumars_email subject "RE: #{subject}" body "No problem. I've fixed it. \n\n Please be careful next time." end diff --git a/php/Lib/Telnet.php b/php/Lib/Telnet.php new file mode 100644 index 0000000..3409dc3 --- /dev/null +++ b/php/Lib/Telnet.php @@ -0,0 +1,264 @@ + + * Based on the code originally written by Marc Ennaji and extended by + * Matthias Blaser + * + * Extended by Christian Hammers + * Modified by Frederik Sauer + * + */ + +class Telnet { + + private $host; + private $port; + private $timeout; + private $stream_timeout_sec; + private $stream_timeout_usec; + + private $socket = NULL; + private $buffer = NULL; + private $prompt; + private $errno; + private $errstr; + private $strip_prompt = TRUE; + + private $NULL; + private $DC1; + private $WILL; + private $WONT; + private $DO; + private $DONT; + private $IAC; + + private $global_buffer = ''; + + const TELNET_ERROR = FALSE; + const TELNET_OK = TRUE; + + public function __construct($host = '127.0.0.1', $port = '23', $timeout = 10, $prompt = '$', $stream_timeout = 1) { + $this->host = $host; + $this->port = $port; + $this->timeout = $timeout; + $this->setPrompt($prompt); + $this->setStreamTimeout($stream_timeout); + + // set some telnet special characters + $this->NULL = chr(0); + $this->DC1 = chr(17); + $this->WILL = chr(251); + $this->WONT = chr(252); + $this->DO = chr(253); + $this->DONT = chr(254); + $this->IAC = chr(255); + + $this->connect(); + } + + public function __destruct() { + // clean up resources + $this->disconnect(); + $this->buffer = NULL; + $this->global_buffer = NULL; + } + + public function connect() { + // check if we need to convert host to IP + if (!preg_match('/([0-9]{1,3}\\.){3,3}[0-9]{1,3}/', $this->host)) { + $ip = gethostbyname($this->host); + + if ($this->host == $ip) { + throw new Exception("Cannot resolve $this->host"); + } else { + $this->host = $ip; + } + } + + // attempt connection - suppress warnings + $this->socket = @fsockopen($this->host, $this->port, $this->errno, $this->errstr, $this->timeout); + + if (!$this->socket) { + throw new Exception("Cannot connect to $this->host on port $this->port"); + } + + if (!empty($this->prompt)) { + $this->waitPrompt(); + } + + return self::TELNET_OK; + } + + public function disconnect() { + if ($this->socket) { + if (! fclose($this->socket)) { + throw new Exception("Error while closing telnet socket"); + } + $this->socket = NULL; + } + return self::TELNET_OK; + } + + public function exec($command, $add_newline = TRUE) { + $this->write($command, $add_newline); + $this->waitPrompt(); + return $this->getBuffer(); + } + + public function login($username, $password) { + try { + $this->setPrompt('login:'); + $this->waitPrompt(); + $this->write($username); + $this->setPrompt('Password:'); + $this->waitPrompt(); + $this->write($password); + $this->setPrompt(); + $this->waitPrompt(); + } catch (Exception $e) { + throw new Exception("Login failed."); + } + + return self::TELNET_OK; + } + + public function setPrompt($str = '$') { + return $this->setRegexPrompt(preg_quote($str, '/')); + } + + public function setRegexPrompt($str = '\$') { + $this->prompt = $str; + return self::TELNET_OK; + } + + public function setStreamTimeout($timeout) { + $this->stream_timeout_usec = (int)(fmod($timeout, 1) * 1000000); + $this->stream_timeout_sec = (int)$timeout; + } + + public function stripPromptFromBuffer($strip) { + $this->strip_prompt = $strip; + } // function stripPromptFromBuffer + + protected function getc() { + stream_set_timeout($this->socket, $this->stream_timeout_sec, $this->stream_timeout_usec); + $c = fgetc($this->socket); + $this->global_buffer .= $c; + return $c; + } + + public function clearBuffer() { + $this->buffer = ''; + } + + public function readTo($prompt) { + if (!$this->socket) { + throw new Exception("Telnet connection closed"); + } + + // clear the buffer + $this->clearBuffer(); + + $until_t = time() + $this->timeout; + do { + // time's up (loop can be exited at end or through continue!) + if (time() > $until_t) { + throw new Exception("Couldn't find the requested : '$prompt' within {$this->timeout} seconds"); + } + + $c = $this->getc(); + + if ($c === FALSE) { + if (empty($prompt)) { + return self::TELNET_OK; + } + throw new Exception("Couldn't find the requested : '" . $prompt . "', it was not in the data returned from server: " . $this->buffer); + } + + // Interpreted As Command + if ($c == $this->IAC) { + if ($this->negotiateTelnetOptions()) { + continue; + } + } + + // append current char to global buffer + $this->buffer .= $c; + + // we've encountered the prompt. Break out of the loop + if (!empty($prompt) && preg_match("/{$prompt}$/", $this->buffer)) { + return self::TELNET_OK; + } + + } while ($c != $this->NULL || $c != $this->DC1); + } + + public function write($buffer, $add_newline = TRUE) { + if (!$this->socket) { + throw new Exception("Telnet connection closed"); + } + + // clear buffer from last command + $this->clearBuffer(); + + if ($add_newline == TRUE) { + $buffer .= "\n"; + } + + $this->global_buffer .= $buffer; + if (!fwrite($this->socket, $buffer) < 0) { + throw new Exception("Error writing to socket"); + } + + return self::TELNET_OK; + } + + protected function getBuffer() { + // Remove all carriage returns from line breaks + $buf = preg_replace('/\r\n|\r/', "\n", $this->buffer); + // Cut last line from buffer (almost always prompt) + if ($this->strip_prompt) { + $buf = explode("\n", $buf); + unset($buf[count($buf) - 1]); + $buf = implode("\n", $buf); + } + return trim($buf); + } + + public function getGlobalBuffer() { + return $this->global_buffer; + } + + protected function negotiateTelnetOptions() { + $c = $this->getc(); + + if ($c != $this->IAC) { + if (($c == $this->DO) || ($c == $this->DONT)) { + $opt = $this->getc(); + fwrite($this->socket, $this->IAC . $this->WONT . $opt); + } else if (($c == $this->WILL) || ($c == $this->WONT)) { + $opt = $this->getc(); + fwrite($this->socket, $this->IAC . $this->DONT . $opt); + } else { + throw new Exception('Error: unknown control character ' . ord($c)); + } + } else { + throw new Exception('Error: Something Wicked Happened'); + } + + return self::TELNET_OK; + } + + protected function waitPrompt() { + return $this->readTo($this->prompt); + } +} \ No newline at end of file diff --git a/php/fucking_coffee.php b/php/fucking_coffee.php new file mode 100644 index 0000000..5ab70c7 --- /dev/null +++ b/php/fucking_coffee.php @@ -0,0 +1,170 @@ +IsWeekend( date('m.d.y') ) == false ) + { + + return false; + } + + /** + * Create a new telnet class + */ + + $this->telnet = new Telnet( $this->host, $this->port ); + + /** + * Once we have completed this, we can brew our coffee! + */ + + $this->BrewCoffee( function(){ + + /** + * Echo out a message + */ + + echo "coffee has been poured"; + + /** + * Unset + */ + + unset( $this->telnet ); + }); + + /** + * Return tue + */ + + return true; + } + + /** + * Brews our coffee + * + * @param $callback + */ + + public function BrewCoffee( $callback ) + { + + if( $this->telnet != null ) + { + + /** + * Execute and enter the password + */ + + $this->telnet->exec('Password: ' . $this->password); + + /** + * Brew the coffee + */ + + $this->telnet->exec('sys brew'); + + /** + * Wait + */ + + sleep( $this->delay ); + + /** + * Pour the coffee + */ + + $this->telnet->exec('sys pour'); + + /** + * Execute our callback + */ + + call_user_func( $callback ); + } + } + + /** + * Is this currently the weekend? + * + * @param $date + * + * @return bool + */ + + public function IsWeekend( $date ) + { + + if( date('N', strtotime( $date ) ) >= 6 ) + { + + return true; + } + + return false; + } + +} \ No newline at end of file diff --git a/powershell/smack_my_bitch_up.ps1 b/powershell/smack_my_bitch_up.ps1 new file mode 100644 index 0000000..46c3d8f --- /dev/null +++ b/powershell/smack_my_bitch_up.ps1 @@ -0,0 +1,39 @@ +$DAYOFWEEK = (Get-Date).DayOfWeek.value__; + +# Skip on weekends +if ($DAYOFWEEK -eq 6 -or $DAYOFWEEK -eq 7) { + return +} + +# Exit early if no sessions with my username are found +if ((QWINSTA $env:USERNAME | measure).Count -gt 0){ + return +} + +# Phone numbers +$MY_NUMBER='+xxx' +$HER_NUMBER='+xxx' + +$TWILIO_ACCOUNT_SID = 'xxx' +$TWILIO_AUTH_TOKEN = 'xxx' + +$REASONS= + 'Working hard', + 'Gotta ship this feature', + 'Someone fucked the system again' + +$RAND = Get-Random -Maximum $REASONS.Count + +$MSG="Late at work. $REASONS[$RAND]" + + +$BASE64AUTHINFO = [Convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $TWILIO_ACCOUNT_SID,$TWILIO_AUTH_TOKEN))) + +#Send a text messag and Log errors +try{ + Invoke-RestMethod -Method Post -Headers @{Authorization=("Basic {0}" -f $BASE64AUTHINFO)} "https://api.twilio.com/2010-04-01/Accounts/$TWILIO_ACCOUNT_SID/Messages" -Body "From=$MY_NUMBER&To=$HER_NUMBER&Body=$MSG" > $null +} +catch{ + Write-Host "Failed to send SMS: $_" +} + diff --git a/python/hangover.py b/python/hangover.py index 77958f3..1daab65 100755 --- a/python/hangover.py +++ b/python/hangover.py @@ -13,9 +13,9 @@ today = datetime.date.today() if today.strftime('%A') in ('Saturday', 'Sunday'): sys.exit() -# exit if no sessions with my username are found +# exit if sessions with my username are found output = subprocess.check_output('who') -if 'my_username' not in output: +if 'my_username' in output: sys.exit() # returns 'None' if the key doesn't exist @@ -37,21 +37,8 @@ client = TwilioRestClient(TWILIO_ACCOUNT_SID, TWILIO_AUTH_TOKEN) client.messages.create( to=number_of_boss, - from=my_number, + from_=my_number, body="Gonna work from home. " + random.choice(excuses) ) -try: - f = open('logs/file.txt', 'a') -except IOError as e: - # dir & file don't exist; create them - os.mkdir('logs') - f = open('logs/file.txt', 'a') -except Exception as e: - print e -else: - pass - -# log it -f.write("Message sent at " + strftime("%a, %d %b %Y %H:%M:%S") + "\n") -f.close() +print "Message sent at " + strftime("%a, %d %b %Y %H:%M:%S") diff --git a/python/smack_my_bitch_up.py b/python/smack_my_bitch_up.py index 398c881..0884416 100755 --- a/python/smack_my_bitch_up.py +++ b/python/smack_my_bitch_up.py @@ -42,17 +42,4 @@ client.messages.create( body="Late at work. " + random.choice(reasons) ) -try: - f = open('logs/file.txt', 'a') -except IOError as e: - # dir & file don't exist; create them - os.mkdir('logs') - f = open('logs/file.txt', 'a') -except Exception as e: - print e -else: - pass - -# log it -f.write("Message sent at " + strftime("%a, %d %b %Y %H:%M:%S") + "\n") -f.close() +print "Message sent at " + strftime("%a, %d %b %Y %H:%M:%S") diff --git a/smack_my_bitch_up.rb b/smack_my_bitch_up.rb index 8cddd7d..06596e8 100755 --- a/smack_my_bitch_up.rb +++ b/smack_my_bitch_up.rb @@ -3,13 +3,6 @@ # Skip on weekends exit if Time.now.saturday? || Time.now.sunday? -log_file_name = File.dirname(__FILE__) + '/logs/smack_my_bitch_up.txt' - -# Be sure that logs dir always exists -Dir.mkdir('logs') unless File.exists?(log_file_name) - -LOG_FILE = File.open(log_file_name, 'a+') - # Exit early if no sessions with my username are found exit if `who -q`.include? ENV['USER'] @@ -27,17 +20,16 @@ TWILIO_AUTH_TOKEN = ENV['TWILIO_AUTH_TOKEN'] my_number = '+xxx' her_number = '+xxx' -reasons = [ +reason = [ 'Working hard', 'Gotta ship this feature', 'Someone fucked the system again' -] +].sample # Send a text message @twilio.messages.create( - from: my_number, to: her_number, body: 'Late at work. ' + reasons.sample + from: my_number, to: her_number, body: "Late at work. #{reason}" ) # Log this -LOG_FILE.puts("Message sent at: #{Time.now}") -LOG_FILE.close +puts "Message sent at: #{Time.now} | Reason: #{reason}"