Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #
- # this isn't the best possible implementation, but it was written
- # quickly and seems to work
- #
- # for info on what fb.php is check out:
- # http://someboringsite.com/2009/09/facebook-php-application-fun.html
- #
- require 'net/imap'
- require 'net/smtp'
- require 'net/ftp'
- require 'openssl'
- $debugMode = 1
- # 0 = off: do everything as intended and w/o unnecessary output
- # 1 = execute everything except sending emails to proper recipients
- # 2 = no email routines really executed
- $isAlive = true
- # credentials for things we need to update
- $gmail_username=''
- $gmail_password=''
- $host_username=''
- $host_password=''
- $twitter_username=''
- $twitter_password=''
- $blogger_username=''
- $blogger_password=''
- # path info that we may need to play with in the scrit
- $script_path = File.expand_path(File.dirname(__FILE__))
- $script_file = File.basename(__FILE__)
- $script_etc = $script_path + '/etc/'
- $script_lib = $script_path + '/lib/'
- $script_tmp = $script_path + '/tmp/'
- # debug info
- if $debugMode
- puts '== running in debug mode =='
- puts " path = #{$script_path}"
- puts " file = #{$script_file}"
- puts " etc = #{$script_etc}"
- puts " lib = #{$script_lib}"
- puts ''
- end
- # this function doesn't always work correctly (e.g. w/ large #s of msgs)
- # doesn't always empty the trash either. too lazy to fix it though :(
- def wipe_gmail_account
- mboxAll = '[Gmail]/All Mail'
- mboxSpam = '[Gmail]/Spam'
- mboxTrash = '[Gmail]/Trash'
- # connect to the server
- imap = Net::IMAP.new('imap.gmail.com', '993', true)
- imap.login($gmail_username, $gmail_password)
- # move messages to trash
- imap.select(mboxAll)
- result = imap.search(["BEFORE", '1-Jan-3000'])
- puts "#{result.length} messages found in archives"
- if result.length > 0 and $debugMode < 2
- imap.copy(1..result.length, mboxTrash)
- imap.store(1..result.length, "+FLAGS", [:Deleted])
- puts "Attempting to move #{result.length} messages to trash..."
- imap.expunge
- end
- # empty spam
- imap.select(mboxSpam)
- result = imap.search(["BEFORE", '1-Jan-3000'])
- puts "#{result.length} messages found in spam"
- if result.length > 0 and $debugMode < 2
- puts "Attempting to delete all #{result.length} messages in spam folder..."
- imap.copy(1..result.length, mboxTrash)
- imap.store(1..result.length, "+FLAGS", [:Deleted])
- imap.expunge
- end
- # empty trash
- imap.select(mboxTrash)
- result = imap.search(["SINCE", '1-Jan-1900'])
- puts "#{result.length} messages found in trash"
- if result.length > 0 and $debugMode < 2
- puts "Attempting to delete all #{result.length} messages in trash..."
- imap.store(1..result.length, "+FLAGS", [:Deleted])
- imap.expunge
- end
- # end imap session
- imap.logout
- end
- # we use this to implement smtp
- # stolen from http://d.hatena.ne.jp/zorio/20060416
- Net::SMTP.class_eval do
- private
- def do_start(helodomain, user, secret, authtype)
- raise IOError, 'SMTP session already started' if @started
- #check_auth_args user, secret, authtype if user or secret
- sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
- @socket = Net::InternetMessageIO.new(sock)
- @socket.read_timeout = 60 #@read_timeout
- #@socket.debug_output = STDERR #@debug_output
- check_response(critical { recv_response() })
- do_helo(helodomain)
- raise 'openssl library not installed' unless defined?(OpenSSL)
- starttls
- ssl = OpenSSL::SSL::SSLSocket.new(sock)
- ssl.sync_close = true
- ssl.connect
- @socket = Net::InternetMessageIO.new(ssl)
- @socket.read_timeout = 60 #@read_timeout
- #@socket.debug_output = STDERR #@debug_output
- do_helo(helodomain)
- authenticate user, secret, authtype if user
- @started = true
- ensure
- unless @started
- # authentication failed, cancel connection.
- @socket.close if not @started and @socket and not @socket.closed?
- @socket = nil
- end
- end
- def do_helo(helodomain)
- begin
- if @esmtp
- ehlo helodomain
- else
- helo helodomain
- end
- rescue Net::ProtocolError
- if @esmtp
- @esmtp = false
- @error_occured = false
- retry
- end
- raise
- end
- end
- def starttls
- getok('STARTTLS')
- end
- end
- # make life easier when it comes to sending emails via our gmail account
- def send_via_gmail(sendto, msg)
- smtp = Net::SMTP.start('smtp.gmail.com', 587, 'localhost.localdomain', $gmail_username, $gmail_password, 'plain')
- smtp.send_message(msg, $gmail_username, sendto)
- smtp.finish
- end
- # mass email function
- def send_emails
- # if not dead, send messages to myself for testing
- if $isAlive
- list_send_emails = {
- "test+1@example.com" => "msg_to_user1.txt",
- "test+2@example.com" => "msg_to_user2.txt",
- "test+3@example.com" => "msg_to_user3.txt",
- '1234567890@txt.att.net' => "This is a test of sending SMS via email."
- }
- # when i am dead, send it to proper recipients
- else
- list_send_emails = {
- "user1@example.com" => "msg_to_user1.txt",
- "user2@example.com" => "msg_to_user2.txt",
- "user3@example.com" => "msg_to_user3.txt",
- '1234567890@txt.att.net' => "Actual SMS message when not testing."
- }
- end
- # process the hashes
- list_send_emails.each { |person,file|
- msg = ''
- # if the file exists, use the text within the file
- # otherwise, the "filename" is probably not a file name but the actual message
- if File.exist?($script_etc+file)
- if $debugMode
- puts "sending #{person} message from #{file}"
- end
- File.open($script_etc+file).each_line { |s| msg = msg + s }
- else
- if $debugMode
- puts "sending #{person} message: #{file}"
- end
- msg = file
- end
- # ensure we don't accidentally send these emails out while testing the script
- if $debugMode == 0 or ($debugMode == 1 and $isAlive == true)
- send_via_gmail(person, msg)
- end
- }
- end
- # update twitter using curl on the command line via their api
- def update_twitter
- if $debugMode
- puts "updating twitter via curl..."
- end
- cmd = "curl -u #{$twitter_username}:#{$twitter_password}"
- if $isAlive
- cmd = cmd + ' -d status="testing a script"'
- else
- cmd = cmd + ' -d status="check out amidead.com"'
- end
- system("#{cmd} http://twitter.com/statuses/update.xml")
- end
- # use the fb.php script to update facebook
- # it's basically a stripped FBCMD with the facebook client library
- # crushed into a single file and added to it
- def update_facebook
- if $debugMode
- puts "updating facebook using fb.php..."
- end
- if $isAlive
- status="testing a script"
- else
- status="is dead (check out amidead.com if you don't believe me)"
- end
- output = `php #{$script_lib+'fb.php'} "#{status}"`
- if $debugMode
- puts output
- end
- end
- # update blogger via email
- def update_blogger
- if $debugMode
- puts "updating blogger (mybloggersite.com) via email..."
- end
- if $isAlive
- msg = "Subject: Is Scripting Cool?\n\n\nYes, scripting is cool."
- else
- msg = "Subject: Am I Dead?\n\n\nCheck out amidead.com for the most recent answer to the question: Am I Dead?"
- end
- blogger = "#{$blogger_username}.#{$blogger_password}@blogger.com"
- send_via_gmail(blogger, msg)
- end
- # update the amidead.com site via FTP
- # (realistically it'll route through localhost since this script is on the same box)
- # the site is basically 1 file: index.php
- # it outputs the question, reads 'word.txt' for the answer and outputs whatever is in 'word.txt'
- # not exactly complex/elegant stuff but it gets the job done
- def update_amidead
- # create a 'word.txt' to send
- file = File.new("#{$script_tmp+'word.txt'}", 'w+')
- if $isAlive
- file.puts "No"
- else
- file.puts "Yes"
- end
- file.close
- # write a shell script to script a ftp session in order to upload
- # net::ftp irritated me and i don't feel like installing net::sftp, etc.
- # don't want to worry about public key authentication w/ ssh either
- # this is as easy as it gets and lets me kick it old school
- script = File.new("#{$script_tmp+'word.sh'}", 'w+')
- str = "#!/bin/sh\nftp -n amidead.com <<END_SCRIPT\n"
- str = str + "quote USER #{$host_username}\n"
- str = str + "quote PASS #{$host_password}\n"
- str = str + "lcd #{$script_tmp}\n"
- str = str + "put word.txt\n"
- str = str + "quit\nEND_SCRIPT\nexit 0\n"
- script.puts str
- script.close
- # execute the script and clean up
- system("sh #{$script_tmp+'word.sh'}")
- File.unlink("#{$script_tmp+'word.sh'}")
- File.unlink("#{$script_tmp+'word.txt'}")
- end
- # call all update functions i wrote
- def update_all
- update_amidead
- update_blogger
- update_twitter
- update_facebook
- end
- # overall flow is determined here
- def main
- unless $isAlive
- puts "== RUNNING IN _NOT_ ALIVE MODE =="
- # we may just be testing "dead mode" and not need the 3 hour delay
- unless $debugMode
- sleep (60*60*3)
- end
- end
- # do all updates, send emails, wipe gmail accout
- update_all
- send_emails
- wipe_gmail_account
- end
- # this script will be run with the argument 'NO' for the main use
- # during testing, etc. it'll run with anything else
- if ARGV[0] == 'NO'
- $isAlive = false
- else
- $isAlive = true
- end
- main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement