Hi all!

	I wanted to be notified in a dramatic way when an e-mail comes in
from a particular domain.  I use Mozilla for my e-mail on WinXP.  The
following Ruby script checks my Mozilla Inbox every 30 seconds and alerts
me by playing a MP3 file whenever a new message from that specific domain
comes in.  I thought someone else might find this useful, so here it is...
Enjoy.  Public Domain.  No guarantees.  No warranties.  Etc.

-- Glenn

PS: I start it from a shell using "rubyw check_email.rb".  If you use "rubyw"
     then it starts in the background and has no console... so you won't even
     know if it is running unless you check the Task Manager for a "rubyw"
     process.  I like running it that way because I can start it and forget
     about it, and I don't need a dedicated console window opened to it.

#############################################################################
#!/usr/bin/env ruby

file = "c:/Documents and Settings/MyName/Application Data/Mozilla/Profiles/default/mutw1w.slt/Mail/pop.com/Inbox"
name = "yahoo.com"
musicdir = 'C:\Documents and Settings\MyName\My Documents\My Music'
musicfile = 'Vladimir_Horowitz-Etude_In_C_Minor_Op_25_No_12_Chopin.mp3'

unless Dir.chdir(musicdir)
   puts "Could not change directory to: #{musicdir}"
   exit -1
end

unless File.exists?(file)
   puts "Could not find file: #{file}"
   exit -1
end

def get_recent_message_id(from, file)
   res = File.open(file) {|f| f.read }
   savemsgid = nil
   msgid = nil
   flag = false
   res.split(/\n/).each do |line|
     if line =~ /^From:.*#{from}/i
       # puts "flag=true: #{line}"
       flag = true
       savemsgid = msgid if msgid
       next
     end
     if line =~ /^\s*$/
       # puts "flag=false: #{line}"
       msgid = nil
       flag = false
       next
     end
     if line =~ /^Message-ID:/
       msgid = line
       # puts "found msgid=#{msgid}"
       savemsgid = msgid if flag
       next
     end
   end
   # puts "returning msgid=#{savemsgid}"
   return savemsgid
end

modified = File.stat(file).mtime

msgid = get_recent_message_id(name, file)

while 1
   puts "#{Time.now}: Checking Inbox..."
   if (File.stat(file).mtime != modified)
     puts "#{Time.now}: Inbox has changed!"
     modified = File.stat(file).mtime
     newmsgid = get_recent_message_id(name, file)
     if msgid != newmsgid
       puts "#{Time.now}: ALERT!!! NEW MESSAGE FROM #{name}!!!"
       system("start #{musicfile}")
       msgid = newmsgid
     end
   end
   sleep 30
end