Advertisement
Guest User

lazy

a guest
Sep 29th, 2009
622
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 9.21 KB | None | 0 0
  1. #
  2. # this isn't the best possible implementation, but it was written
  3. # quickly and seems to work
  4. #
  5. # for info on what fb.php is check out:
  6. # http://someboringsite.com/2009/09/facebook-php-application-fun.html
  7. #
  8.  
  9. require 'net/imap'
  10. require 'net/smtp'
  11. require 'net/ftp'
  12. require 'openssl'
  13.  
  14. $debugMode = 1
  15. # 0 = off: do everything as intended and w/o unnecessary output
  16. # 1 = execute everything except sending emails to proper recipients
  17. # 2 = no email routines really executed
  18. $isAlive = true
  19.  
  20. # credentials for things we need to update
  21. $gmail_username=''
  22. $gmail_password=''
  23. $host_username=''
  24. $host_password=''
  25. $twitter_username=''
  26. $twitter_password=''
  27. $blogger_username=''
  28. $blogger_password=''
  29.  
  30. # path info that we may need to play with in the scrit
  31. $script_path = File.expand_path(File.dirname(__FILE__))
  32. $script_file = File.basename(__FILE__)
  33. $script_etc = $script_path + '/etc/'
  34. $script_lib = $script_path + '/lib/'
  35. $script_tmp = $script_path + '/tmp/'
  36.  
  37. # debug info
  38. if $debugMode
  39.   puts '== running in debug mode =='
  40.   puts "  path = #{$script_path}"
  41.   puts "  file = #{$script_file}"
  42.   puts "   etc = #{$script_etc}"
  43.   puts "   lib = #{$script_lib}"
  44.   puts ''
  45. end
  46.  
  47. # this function doesn't always work correctly (e.g. w/ large #s of msgs)
  48. # doesn't always empty the trash either. too lazy to fix it though :(
  49. def wipe_gmail_account
  50.   mboxAll = '[Gmail]/All Mail'
  51.   mboxSpam = '[Gmail]/Spam'
  52.   mboxTrash = '[Gmail]/Trash'
  53.  
  54.   # connect to the server
  55.   imap = Net::IMAP.new('imap.gmail.com', '993', true)
  56.   imap.login($gmail_username, $gmail_password)
  57.  
  58.   # move messages to trash
  59.   imap.select(mboxAll)
  60.   result = imap.search(["BEFORE", '1-Jan-3000'])
  61.   puts "#{result.length} messages found in archives"
  62.   if result.length > 0 and $debugMode < 2
  63.     imap.copy(1..result.length, mboxTrash)
  64.     imap.store(1..result.length, "+FLAGS", [:Deleted])
  65.     puts "Attempting to move #{result.length} messages to trash..."
  66.     imap.expunge
  67.   end
  68.  
  69.   # empty spam
  70.   imap.select(mboxSpam)
  71.   result = imap.search(["BEFORE", '1-Jan-3000'])
  72.   puts "#{result.length} messages found in spam"
  73.   if result.length > 0 and $debugMode < 2
  74.     puts "Attempting to delete all #{result.length} messages in spam folder..."
  75.     imap.copy(1..result.length, mboxTrash)
  76.     imap.store(1..result.length, "+FLAGS", [:Deleted])
  77.     imap.expunge
  78.   end
  79.  
  80.   # empty trash
  81.   imap.select(mboxTrash)
  82.   result = imap.search(["SINCE", '1-Jan-1900'])
  83.   puts "#{result.length} messages found in trash"
  84.   if result.length > 0 and $debugMode < 2
  85.     puts "Attempting to delete all #{result.length} messages in trash..."
  86.     imap.store(1..result.length, "+FLAGS", [:Deleted])
  87.     imap.expunge
  88.   end
  89.  
  90.   # end imap session
  91.   imap.logout
  92. end
  93.  
  94. # we use this to implement smtp
  95. # stolen from http://d.hatena.ne.jp/zorio/20060416
  96. Net::SMTP.class_eval do
  97.   private
  98.   def do_start(helodomain, user, secret, authtype)
  99.     raise IOError, 'SMTP session already started' if @started
  100.     #check_auth_args user, secret, authtype if user or secret
  101.  
  102.     sock = timeout(@open_timeout) { TCPSocket.open(@address, @port) }
  103.     @socket = Net::InternetMessageIO.new(sock)
  104.     @socket.read_timeout = 60 #@read_timeout
  105.     #@socket.debug_output = STDERR #@debug_output
  106.  
  107.     check_response(critical { recv_response() })
  108.     do_helo(helodomain)
  109.  
  110.     raise 'openssl library not installed' unless defined?(OpenSSL)
  111.     starttls
  112.     ssl = OpenSSL::SSL::SSLSocket.new(sock)
  113.     ssl.sync_close = true
  114.     ssl.connect
  115.     @socket = Net::InternetMessageIO.new(ssl)
  116.     @socket.read_timeout = 60 #@read_timeout
  117.     #@socket.debug_output = STDERR #@debug_output
  118.     do_helo(helodomain)
  119.  
  120.     authenticate user, secret, authtype if user
  121.     @started = true
  122.   ensure
  123.     unless @started
  124.       # authentication failed, cancel connection.
  125.         @socket.close if not @started and @socket and not @socket.closed?
  126.       @socket = nil
  127.     end
  128.   end
  129.  
  130.   def do_helo(helodomain)
  131.      begin
  132.       if @esmtp
  133.         ehlo helodomain
  134.       else
  135.         helo helodomain
  136.       end
  137.     rescue Net::ProtocolError
  138.       if @esmtp
  139.         @esmtp = false
  140.         @error_occured = false
  141.         retry
  142.       end
  143.       raise
  144.     end
  145.   end
  146.  
  147.   def starttls
  148.     getok('STARTTLS')
  149.   end
  150. end
  151.  
  152. # make life easier when it comes to sending emails via our gmail account
  153. def send_via_gmail(sendto, msg)
  154.   smtp = Net::SMTP.start('smtp.gmail.com', 587, 'localhost.localdomain', $gmail_username, $gmail_password, 'plain')
  155.   smtp.send_message(msg, $gmail_username, sendto)
  156.   smtp.finish
  157. end
  158.  
  159. # mass email function
  160. def send_emails
  161.   # if not dead, send messages to myself for testing
  162.   if $isAlive
  163.     list_send_emails = {
  164.       "test+1@example.com" => "msg_to_user1.txt",
  165.       "test+2@example.com" => "msg_to_user2.txt",
  166.       "test+3@example.com" => "msg_to_user3.txt",
  167.       '1234567890@txt.att.net' => "This is a test of sending SMS via email."
  168.     }
  169.   # when i am dead, send it to proper recipients
  170.   else
  171.     list_send_emails = {
  172.       "user1@example.com" => "msg_to_user1.txt",
  173.       "user2@example.com" => "msg_to_user2.txt",
  174.       "user3@example.com" => "msg_to_user3.txt",
  175.       '1234567890@txt.att.net' => "Actual SMS message when not testing."
  176.     }
  177.   end
  178.  
  179.   # process the hashes
  180.   list_send_emails.each { |person,file|
  181.     msg = ''
  182.     # if the file exists, use the text within the file
  183.     # otherwise, the "filename" is probably not a file name but the actual message
  184.     if File.exist?($script_etc+file)
  185.       if $debugMode
  186.         puts "sending #{person} message from #{file}"
  187.       end
  188.       File.open($script_etc+file).each_line { |s| msg = msg + s }
  189.     else
  190.       if $debugMode
  191.         puts "sending #{person} message: #{file}"
  192.       end
  193.       msg = file
  194.     end
  195.  
  196.     # ensure we don't accidentally send these emails out while testing the script
  197.     if $debugMode == 0 or ($debugMode == 1 and $isAlive == true)
  198.       send_via_gmail(person, msg)
  199.     end
  200.   }
  201. end
  202.  
  203. # update twitter using curl on the command line via their api
  204. def update_twitter
  205.   if $debugMode
  206.     puts "updating twitter via curl..."
  207.   end
  208.   cmd = "curl -u #{$twitter_username}:#{$twitter_password}"
  209.   if $isAlive
  210.     cmd = cmd + ' -d status="testing a script"'
  211.   else
  212.     cmd = cmd + ' -d status="check out amidead.com"'
  213.   end
  214.   system("#{cmd} http://twitter.com/statuses/update.xml")
  215. end
  216.  
  217. # use the fb.php script to update facebook
  218. # it's basically a stripped FBCMD with the facebook client library
  219. # crushed into a single file and added to it
  220. def update_facebook
  221.   if $debugMode
  222.     puts "updating facebook using fb.php..."
  223.   end
  224.   if $isAlive
  225.     status="testing a script"
  226.   else
  227.     status="is dead (check out amidead.com if you don't believe me)"
  228.   end
  229.   output = `php #{$script_lib+'fb.php'} "#{status}"`
  230.   if $debugMode
  231.     puts output
  232.   end
  233. end
  234.  
  235. # update blogger via email
  236. def update_blogger
  237.   if $debugMode
  238.     puts "updating blogger (mybloggersite.com) via email..."
  239.   end
  240.   if $isAlive
  241.     msg = "Subject: Is Scripting Cool?\n\n\nYes, scripting is cool."
  242.   else
  243.     msg = "Subject: Am I Dead?\n\n\nCheck out amidead.com for the most recent answer to the question: Am I Dead?"
  244.   end
  245.   blogger = "#{$blogger_username}.#{$blogger_password}@blogger.com"
  246.   send_via_gmail(blogger, msg)
  247. end
  248.  
  249. # update the amidead.com site via FTP
  250. # (realistically it'll route through localhost since this script is on the same box)
  251. # the site is basically 1 file: index.php
  252. # it outputs the question, reads 'word.txt' for the answer and outputs whatever is in 'word.txt'
  253. # not exactly complex/elegant stuff but it gets the job done
  254. def update_amidead
  255.   # create a 'word.txt' to send
  256.   file = File.new("#{$script_tmp+'word.txt'}", 'w+')
  257.   if $isAlive
  258.     file.puts "No"
  259.   else
  260.     file.puts "Yes"
  261.   end
  262.   file.close
  263.  
  264.   # write a shell script to script a ftp session in order to upload
  265.   # net::ftp irritated me and i don't feel like installing net::sftp, etc.
  266.   # don't want to worry about public key authentication w/ ssh either
  267.   # this is as easy as it gets and lets me kick it old school
  268.   script = File.new("#{$script_tmp+'word.sh'}", 'w+')
  269.   str = "#!/bin/sh\nftp -n amidead.com <<END_SCRIPT\n"
  270.   str = str + "quote USER #{$host_username}\n"
  271.   str = str + "quote PASS #{$host_password}\n"
  272.   str = str + "lcd #{$script_tmp}\n"
  273.   str = str + "put word.txt\n"
  274.   str = str + "quit\nEND_SCRIPT\nexit 0\n"
  275.   script.puts str
  276.   script.close
  277.  
  278.   # execute the script and clean up
  279.   system("sh #{$script_tmp+'word.sh'}")
  280.   File.unlink("#{$script_tmp+'word.sh'}")
  281.   File.unlink("#{$script_tmp+'word.txt'}")
  282. end
  283.  
  284. # call all update functions i wrote
  285. def update_all
  286.   update_amidead
  287.   update_blogger
  288.   update_twitter
  289.   update_facebook
  290. end
  291.  
  292. # overall flow is determined here
  293. def main
  294.   unless $isAlive
  295.     puts "== RUNNING IN _NOT_ ALIVE MODE =="
  296.     # we may just be testing "dead mode" and not need the 3 hour delay
  297.     unless $debugMode
  298.       sleep (60*60*3)
  299.     end
  300.   end
  301.  
  302.   # do all updates, send emails, wipe gmail accout
  303.   update_all
  304.   send_emails
  305.   wipe_gmail_account
  306. end
  307.  
  308. # this script will be run with the argument 'NO' for the main use
  309. # during testing, etc. it'll run with anything else
  310. if ARGV[0] == 'NO'
  311.   $isAlive = false
  312. else
  313.   $isAlive = true
  314. end
  315. main
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement