Class: Hotmailer

Inherits:
WWW::Mechanize
  • Object
show all
Defined in:
lib/hotmailer.rb

Defined Under Namespace

Classes: Message

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(username, passwd) ⇒ Hotmailer

Returns a new instance of Hotmailer.



10
11
12
13
14
15
16
17
18
# File 'lib/hotmailer.rb', line 10

def initialize(username,passwd)
  super()
  self.user_agent_alias  = "Windows IE 6"
  @username = username
  @passwd = passwd 
  @inbox = ''
  @contacts = []
  @messages = []
end

Instance Attribute Details

#inboxObject (readonly)

Returns the value of attribute inbox.



8
9
10
# File 'lib/hotmailer.rb', line 8

def inbox
  @inbox
end

#logged_inObject (readonly)

Returns the value of attribute logged_in.



8
9
10
# File 'lib/hotmailer.rb', line 8

def logged_in
  @logged_in
end

#passwdObject

Returns the value of attribute passwd.



7
8
9
# File 'lib/hotmailer.rb', line 7

def passwd
  @passwd
end

#usernameObject

Returns the value of attribute username.



7
8
9
# File 'lib/hotmailer.rb', line 7

def username
  @username
end

Instance Method Details

#add_contact(quickname, email, fname = '', lname = '') ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/hotmailer.rb', line 157

def add_contact(quickname,email,fname='',lname='')
#To add a contact

  self. unless self.logged_in
  
  main_url = '/cgi-bin/hmhome?'+self.inbox
  main_page = self.get(main_url)

  c_link =  (main_page/"a[@href=#]").find {|c| c.inner_html == "New Contact"}
  unless c_link['onclick'] =~ /G\('([^']+)'/
    raise "Error adding contact - hotmail format may have changed"
  end

  c_href = $1
  contact_page = self.get(c_href+"&"+self.inbox)
  contact_form = contact_page.form('addr')
  
  contact_form.alias = quickname
  contact_form.addrlist = email

  contact_form.aliasfname = fname
  contact_form.aliaslname = lname

  res_page = self.submit(contact_form)

  unless res_page.body =~ /Contact/
    raise "Error adding contact"
  end

  return true
end

#compose(to, subject, body) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/hotmailer.rb', line 82

def compose(to,subject,body)
 #Sends an email

 self. unless self.logged_in
 
 compose_url ="/cgi-bin/compose?"+self.inbox
 comp_page = self.get(compose_url)

 comp_form = comp_page.form("composeform")
 comp_form.to = to
 comp_form.subject = subject
 comp_form.body = body

 res_page = self.submit(comp_form)
 
 unless res_page.body =~ /Your message has been sent/
   raise "There was an error sending the message"
 end

 return true

end

#contacts(reload = false) ⇒ Object



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

def contacts(reload=false)
#Get user's contact list (returns an array of hashes, each has a record for one
# user, and contains :name and :email keys)

  unless reload
    return @contacts unless @contacts.empty?
  end

  self. unless self.logged_in

  #The URL to use to get contacts in printable view
  contacts_url = "/cgi-bin/addresses?fti=yes&PrintView=1&"+self.inbox
  
  all_contacts = self.get(contacts_url)

  #Get separate contacts [those within trs within trs]
  scontacts = all_contacts/"tr/tr"
  
  scontacts.each do |c|
    #Get each contact's name and email, and append it to @contacts 
    details = (c/"td")
    name = details[0].inner_html
    email = ""
    #Sometimes print view does not have an email
    #this checks whether it exists
    if (details[1]/"span")[0]
      email = (details[1]/"span")[0].inner_html
    end
    @contacts << {:name => name,:email => email}
  end

  return @contacts

end

#loginObject



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

def 
#Logs in the user
  first_page = self.get('http://hotmail.com')
   = first_page.form('f1')
  . = self.username
  .passwd = self.passwd

   = self.submit()
 
  if .body  =~ /window\.location\.replace\(\"([^"]+)\"/ 
      new_loc = $1
  else
    raise "Error logging in - check username and password and try again"
  end

  main_page = self.get(new_loc)
  
  if main_page.body =~  /_UM\s*=\s*"([^"]+)";?/
    @inbox = $1
  else
    raise "Error getting mailbox - hotmail format may have changed! (Please try logging in manually using your web browser, then retry this)."
  end

  @logged_in = true

end

#messages(reload = false) ⇒ Object



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/hotmailer.rb', line 105

def messages(reload=false)

  unless reload
    return @messages unless @messages.empty?
  end

  self. unless self.logged_in
  
  page = 1
  last_page = 1
  raw_messages = []
  messages = []

  while page <= last_page.to_i
    msg_url = "/cgi-bin/HoTMaiL?&Sort=rDate&page=#{page}&"+self.inbox
    msg_page = self.get(msg_url)
    
    #Get last page
    lastp = (msg_page/"a[@title='Last Page']")[0]
    if lastp
      lastp.attributes['href'] =~ /HM\('page=(\d+)/
      last_page = $1
    end
   
    msgs = msg_page/"tr[@name]"
    raw_messages += msgs
    
    page+=1
  end

  for msg in raw_messages
    fname = (msg/"a")[0].inner_html
    femail = msg.attributes['name']
    status = (msg/"img")[0].attributes['alt']

    mlink = (msg/"a")[0].attributes['href']
    mlink =~ /G\('([^']+)'/
    mlink = $1
    
    subject = (msg/"td")[6].inner_html
    date = (msg/"td")[7].inner_html
    size = (msg/"td")[8].inner_html
 
    m = Hotmailer::Message.new(self,:from_name=> fname, :from_email => femail, :status => status, :link => mlink, :subject => subject, :date => date, :size => size)

    messages << m
  end

  @messages = messages
  return @messages
end