[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[mhc:02151] Re: 添付 vCalendar の import



乃村です.

> ハンドラとして実装した方がいいのではないかと思います.
> 
> そうすると,guess する必要もないでしょうし,
> elisp じゃなくて,外部スクリプトに逃がせるので,
> メール受けたら,即登録のような用途にも転用できそうですし.

とりあえずサンプルスクリプト書いてみました.
このまま使うのはちと辛いですが時間切れ..

# 風呂入らないと怒られる

Mew だと,このスクリプトを | で呼んで,できたファイルを open して
M-x mhc-draft-mode C-cC-c で登録できました.

最後のコメントを外すと,そのまま登録できちゃいますが,
漢字コードなんかの問題がありそうですね.
--
nom


------------------------------------------------------------------------
#!/usr/bin/ruby

require 'mhc-schedule'
require 'mhc-date'

# iCalendar to MHC 
# 
# this is a TRUE quickhack brewed up from a glance.
# --
# nom
#
# BEGIN:VCALENDAR
# VERSION:1.0
# BEGIN:VEVENT
# CLASS:PUBLIC
# DTSTART:20050907T104000Z
# DTEND:20050908T114000Z
# AALARM:20050907T103500Z
# SUMMARY;ENCODING=QUOTED-PRINTABLE:=83e=83X=83g
# CATEGORIES:BUSINESS
# LOCATION;ENCODING=QUOTED-PRINTABLE:=83e=83X=83g=89=EF=8F=EA
# DESCRIPTION;ENCODING=QUOTED-PRINTABLE:=83e=83X=83g=83X=83P=83W=83=85=81[=
# =83=8B
# LAST-MODIFIED:20050907T015129Z
# END:VEVENT
# END:VCALENDAR

def decode_qp(string)
  trail = decode_qp(gets) if string =~ /=$/

  string = string .sub(/=$/, '') .sub(/^\s+/ , '')
  string = string .gsub(/=(..)/){ sprintf("%c", $1 .hex) }

  return string + (trail || '')
end

def parse_dt(str, tz = 9 * 3600 )
  if str =~ /(\d{4})(\d{2})(\d{2})T(\d{2})(\d{2})(\d{2})Z/
    t = Time .gm($1 .to_i, $2 .to_i, $3 .to_i, $4 .to_i, $5 .to_i) + tz
    return [MhcDate .new(t .year, t .month, t .day), 
            MhcTime .new(t .hour, t .min)]
  end
end

while line = gets

  line .chomp!

  case line
  when /DTSTART:(.*)/
    string = $1
    date_b, time_b = parse_dt(string)

  when /DTEND:(.*)/
    string = $1
    date_e, time_e = parse_dt(string)

  when /AALARM:(.*)/
    string = $1
    date_a, time_a = parse_dt(string)

  when /SUMMARY;ENCODING=QUOTED-PRINTABLE:(.*)/
    subject = decode_qp($1)

  when /CATEGORIES:(.*)/
    category = $1

  when /LOCATION;ENCODING=QUOTED-PRINTABLE:(.*)/
    location = decode_qp($1)

  when /DESCRIPTION;ENCODING=QUOTED-PRINTABLE:(.*)/
    description = decode_qp($1)
  end
end


s = MhcScheduleItem .new

s .set_subject(subject)
s .set_location(location)
s .add_category(category)
s .set_description(description)

## date_b == date_e assumed.
s .add_day(date_b)
s .set_time(time_b, time_e)
s .set_alarm(time_b .to_i - time_a .to_i) if time_a

f = File .open("/tmp/test", "w")
f .print s .dump
print s .dump

### add to db if you want -- risky for now.
# d = MhcScheduleDB .new
# d .add_sch(s)
------------------------------------------------------------------------