#!/usr/local/bin/ruby
#
# save.rb by N.Nakasato (SCL@nacci.org)
# Tmail URL => http://www.ruby-lang.org/en/raa-list.rhtml?name=TMail
#
# 2002/11 modified by T. Nakano
# (convert feel H"'s fucking BMP image to PNG)
require 'nkf'
require 'tmail'
require 'base64'
# ---------------------------------------------------
# HTML ヘッダ出力
#
def PrintHeader(f)
f.print NKF::nkf('-j', <
今日の画
今日の画
feel H" + Treva で撮った画像。
EOF
end
# ---------------------------------------------------
# HTML フッタ出力
#
def PrintFooter(f)
f.print NKF::nkf('-j', <
スクリプト
Thanks to 今日の画@nacci.org.
Nakano Taro
EOF
end
# ---------------------------------------------------
# main
#
save_dir = '/your/home/directory'
### Read mail file
body = $<.read
# price = body.size.to_f
# price = price*0.3/128.0
f = open("#{save_dir}/latest.txt", 'w')
f.write(body)
f.close()
mail = TMail::Mail.parse(body)
from = mail.from
if from =~ /@/
name, dum = from.split('@')
from = name
end
subject = ''
textpart = ''
body = ''
if mail.subject then
subject = NKF::nkf('-e', mail.subject)
if subject.downcase =~ /^(treva)(.*)/ then
subject = $2
end
end
filename = ''
filenamebase = Time.now.to_i.to_s
if mail.multipart? then
mail.parts.each do |m|
case m.main_type
when /text/i
# if subject == '' then
# textpart = NKF::nkf('-e', m.body.chop.chop)
textpart = NKF::nkf('-e', m.body)
# end
when /image/i
body = m.body
filename = "#{filenamebase}.jpg"
when /application/i # for DDIP
body = m.body
filename = "#{filenamebase}.bmp"
end
end
else
if mail.main_type =~ /image/i then
body = mail.body
filename = "#{filenamebase}.jpg"
else
exit
end
end
### Decode MIME attached image
fimage = open("#{save_dir}/data/#{filename}", 'w')
fimage.write decode64(body)
fimage.close
### Convert BMP -> PNG
# system( "/bin/cat #{save_dir}/data/#{filename} | /usr/local/bin/bmptoppm | /usr/local/bin/pnmtopng > #{save_dir}/data/#{filenamebase}.png" )
system( "/usr/local/bin/convert #{save_dir}/data/#{filename} #{save_dir}/data/#{filenamebase}.png" )
system( "/bin/chmod 644 #{save_dir}/data/#{filenamebase}.png" )
system( "/bin/rm #{save_dir}/data/#{filename}" )
### Old HTML body
flist = open("#{save_dir}/list.txt", 'r')
old = flist.read
flist.close()
### Add new lines to list.txt
flist = open("#{save_dir}/list.txt", 'w')
flist.write("「#{subject}」
(#{Time.now})
#{textpart}
\n")
flist.write('
')
flist.write(old)
flist.close()
### Output new HTML
fhtml = open("#{save_dir}/image.html", 'w')
flist = open("#{save_dir}/list.txt", 'r')
newlist= flist.read
flist.close()
PrintHeader(fhtml)
fhtml.write(NKF::nkf('-j', newlist))
PrintFooter(fhtml)
fhtml.close()