Report abuse

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#!/usr/bin/env ruby

require 'rubygems'
require 'mechanize'
require 'breakpoint'
require 'pp'
require 'vpim'
require 'cgi'

def new_contact(card)
  p = {}
  p[:addrid] = -1
  p[:page] = "/addrbook.jsp"

  p[:title] = card.name.prefix
  p[:firstname] = card.name.given
  p[:middlename] = card.name.additional
  p[:lastname] = card.name.family
  p[:nickname] = card.nickname
  p[:displayname] = card.name.fullname
  p[:email1] = card.emails[0]
  p[:email2] = card.emails[1]
  p[:email3] = card.emails[2]
  p[:defemail] = card.emails.index(card.emails.find {|e| e.preferred})+1 rescue 1
  p[:webpage] = card.url rescue nil
  p[:workwebpage] = nil
  p[:company] = card.org.first rescue nil
  p[:jobtitle] = nil
  p[:department] = nil

  work = card.addresses.find {|a| a.location == ["work"]}
  p[:workaddress] = work.street rescue nil
  p[:workcity] = work.locality rescue nil
  p[:workcountry] = work.country rescue nil
  p[:workstate] = work.region rescue nil
  p[:workzipcode] = work.postalcode rescue nil

  home = card.addresses.find {|a| a.location == ["home"]}
  p[:address] = home.street rescue nil
  p[:city] = home.locality rescue nil
  p[:country] = home.country rescue nil
  p[:state] = home.region rescue nil
  p[:zipcode] = home.postalcode rescue nil

  p[:defaultaddress] = {"home" => 0, "work" => 1}[addresses.find {|a| a.preferred}.location.first] rescue 0
  
  p[:categoryid] = 0
  
  p[:birthday] = nil
  p[:bmonth] = card.birthday.month rescue nil
  p[:bdate] = card.birthday.day rescue nil
  p[:byear] = card.birthday.year rescue nil

  p[:anniversary] = nil
  p[:amonth] = nil
  p[:adate] = nil
  p[:ayear] = nil

  p[:spousename] = nil
  p[:familyname] = nil
  p[:notes] = card.note

  p[:homephone] = card.telephones.find {|t| t.location == ["home"]}
  p[:workphone] = card.telephones.find {|t| t.location == ["work"]}
  p[:pager] = card.telephones.find {|t| t.location == ["pager"]}
  p[:mobile] = card.telephones.find {|t| t.location == ["cell"]}
  p[:fax] = card.telephones.find {|t| t.capability == ["fax"]}
  p[:other] = nil
  p[:defaultphone] = {"home" => 1, "office" => 2, "pager" => 3, "cell" => 4, "fax" => 5, "other" => 0}[telephones.find {|t| (t.capability + t.location).first}] rescue 1

  {"X-JABBER" => "Other", "X-AIM" => "AOL Messenger", "X-MSN" => "MSN Messenger", "X-YAHOO" => "Yahoo Messenger", "X-ICQ" => "ICQ"}.inject(1) do |c,(k,v)|
    break if c > 3
    if card[k]
      p["im#{c}"] = card[k]
      p["im#{c}domain"] = v
      c+=1
    end
    c
  end
  p[:defim] = 1
  
  p
end

username = 'MYEMAIL' # without the @myhelio.com
password = 'MYPASSWORD'

agent = WWW::Mechanize.new
agent.user_agent = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.1) Gecko/20061010 Firefox/2.0"

cards = Vpim::Vcard.decode(STDIN.read)

agent.get "https://webmail.myhelio.com/wam/Login?page=%2Findex.jsp&start=1&tzoffset=-8&email=#{username}%40myhelio.com&password=#{password}"

cards.each do |card|
  puts "Creating contact for #{card.name.fullname || 'unknown'}"
  params = new_contact(card)
  qs = params.map { |k,v| "#{CGI.escape(k.to_s)}=#{CGI.escape(v.to_s)}" }.join('&')
  url = "http://webmail.myhelio.com/wam/AddrUpdate?#{qs}"
  # `open "#{url}"`
  agent.post url # params.inject({}) {|h,(k,v)| h[k.to_s] = v.to_s; h}
  # File.open('/tmp/helio.html', 'w') {|f| f<<agent.page.body}
end

agent.get '/wam/Logout'