Class: Kowala::Browser

Inherits:
Object
  • Object
show all
Defined in:
lib/kowala.rb

Instance Method Summary collapse

Constructor Details

#initialize(url = "http://mail.emea.microsoftonline.com") ⇒ Browser

Returns a new instance of Browser.



10
11
12
13
14
15
# File 'lib/kowala.rb', line 10

def initialize ( url = "http://mail.emea.microsoftonline.com" )
  @agent = Mechanize.new
  @agent.user_agent_alias = "Mac Safari"
  @url = url
  @coder = HTMLEntities.new
end

Instance Method Details

#getAddress(id, name) ⇒ Object



84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/kowala.rb', line 84

def getAddress(id, name)
  id = CGI::escape(id)
  url = "https://red002.mail.emea.microsoftonline.com/owa/?ae=Item&t=AD.RecipientType.User&id=" + id
  page = @agent.get(url)
  email = false
  (page/"td").each do |veld|
    if veld.inner_html.strip == "E-mail" then
      email = veld.next_sibling.inner_html.to_s
    end
  end
  return "\"#{name}\" <#{email}>"
end

#getMail(id) ⇒ Object



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
# File 'lib/kowala.rb', line 48

def getMail(id)
  m = {:id => id}
  m[:url] = "https://red002.mail.emea.microsoftonline.com/owa/?ae=Item&t=IPM.Note&id=" + m[:id]

  page = @agent.get(m[:url])

  m[:from] = (page/"td[@class=\"frm\"]/span").inner_html.gsub(/<\/?[^>]*>/, "").strip
  if (page/"td[@class=\"frm\"]/span/a").count > 0 then
    m[:from] = getAddress((page/"td[@class=\"frm\"]/span/a").first.attributes["id"].to_s, m[:from]) 
  else
    m[:from] = parseAddress(m[:from])
  end

  m[:to] = parseRecipientList((page/"div[@id=\"divTo\"]/span"))
  m[:cc] = parseRecipientList((page/"div[@id=\"divCc\"]/span"))
  
  m[:subject] = (page/"td[@class=\"sub\"]").inner_html.strip
  m[:body] = (page/"div[@class=\"bdy\"]").first.inner_html.gsub(/<\/?[^>]*>/, "")

  (page/"td[@class=\"hdtxt\"]").each do |veld|
     if veld.inner_html.strip == "Verzonden:" then
       m[:date] = veld.next_element.inner_html.strip
     end
  end

  m[:source] = ""
  m[:source] += "From: " + m[:from] + "\n"
  m[:source] += "Subject: " + m[:subject] + "\n"
  m[:source] += "To: #{m[:to]}\n"
  m[:source] += "Date: #{m[:date]}\n" 
  m[:source] += "\n"
  m[:source] += m[:body]
  m[:source] = @coder.decode(m[:source])
  return m
end

#login(username, password) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/kowala.rb', line 17

def (username, password)
  doc = @agent.get(@url)
  inlog_form = doc.forms.first
  @username = username
  inlog_form.username = username
  inlog_form.password = password
  @page = @agent.submit(inlog_form)
end

#parseAddress(raw) ⇒ Object



97
98
99
100
101
102
103
# File 'lib/kowala.rb', line 97

def parseAddress(raw)
  if raw =~ /(.*) \[(.*)\]/
    return "\"#{$1}\" <#{$2}>"
  else
    return raw
  end
end

#parseRecipientList(list) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/kowala.rb', line 35

def parseRecipientList(list)
  parsedList = ""
  list.each do |t|
    parsedList += ", " unless parsedList == ""
    if(t/"a").count > 0 then
      parsedList += getAddress((t/"a").first.attributes["id"].to_s, t.inner_html.gsub(/<\/?[^>]*>/, "").strip)
    else
      parsedList += t.inner_html.gsub(/<\/?[^>]*>/, "").strip
    end
  end
  return parsedList
end

#unreadObject



26
27
28
29
30
31
32
33
# File 'lib/kowala.rb', line 26

def unread
  list = []
  (@page/"tr[@style=\"font-weight:bold;\"]").each do |tr|
    id = CGI::escape((tr/"input[@type=\"checkbox\"]").first.attributes["value"].to_s)
    list << getMail(id)
  end
  return list
end