Skip to content

Instantly share code, notes, and snippets.

@aont
Created March 9, 2014 10:32
Show Gist options
  • Select an option

  • Save aont/9445767 to your computer and use it in GitHub Desktop.

Select an option

Save aont/9445767 to your computer and use it in GitHub Desktop.
PR-400のIPアドレス変化通知メールをリアルタイム検知
#!/usr/bin/ruby
# -*- coding: utf-8 -*-
require 'net/imap'
require 'kconv'
Net::IMAP::debug = true
imap = Net::IMAP.new('imap.gmail.com', 993, true)
imap.login('hoge@gmail.com', 'pass')
#imap.select 'INBOX'
imap.examine 'INBOX'
$last_ip_uid = -1
while true
imap.idle do | resp |
p resp
p resp.name
p resp.data
p resp.methods
if resp.kind_of?(Net::IMAP::UntaggedResponse) and resp.name == "EXISTS"
puts "Mailbox now has #{resp.data} messages"
imap.idle_done()
end
end
last_uid = imap.status('INBOX', ["MESSAGES"])["MESSAGES"]
puts "UID: #{last_uid}"
envelope = imap.fetch(last_uid, "ENVELOPE")[0].attr["ENVELOPE"]
from = envelope.from[0].mailbox + "@" + envelope.from[0].host
puts "From: #{from}"
puts "Subject: #{envelope.subject.toutf8}"
if $last_ip_uid != last_uid
if from=="haoki@diary.ocn.ne.jp"
if envelope.subject.toutf.index("【ひかり電話ルータ】 VPN接続用のIPアドレスが変更になりました。")
puts "changed!"
$last_ip_uid = last_uid
#
end
end
end
end
imap.logout
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment