Move to Apache License
[amazing.git] / lib / amazing / widgets / gmail.rb
blobe90a8e2433af199a15b0cbf1bf785779bb88e0c5
1 # Copyright 2008 Dag Odenhall <dag.odenhall@gmail.com>
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 #    http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 require 'amazing/widget'
17 module Amazing
18   module Widgets
19     class Gmail < Widget
20       description "GMail checker"
21       dependency "net/https", "Ruby standard library"
22       dependency "rexml/document", "Ruby standard library"
23       dependency "time", "Ruby standard library"
24       option :username, "Username"
25       option :password, "Password"
26       option :certificates, "Path to SSL certificates", "/etc/ssl/certs"
27       option :verify, "Verify certificates", false
28       field :messages, "List of new messages"
29       field :count, "Number of new messages"
30       default { @count }
32       init do
33         http = Net::HTTP.new("mail.google.com", 443)
34         http.use_ssl = true
35         if @verify
36           http.verify_mode = OpenSSL::SSL::VERIFY_PEER
37           http.ca_path = @certificates
38         else
39           http.verify_mode = OpenSSL::SSL::VERIFY_NONE
40         end
41         request = Net::HTTP::Get.new("/mail/feed/atom")
42         request.basic_auth(@username, @password)
43         doc = REXML::Document.new(http.request(request).body)
44         begin
45           @messages = doc.elements.to_a("//entry").map do |e|
46             {:subject => e.elements["title"].text,
47              :summary => e.elements["summary"].text,
48              :from => e.elements.to_a("author").map do |a|
49                {:name => a.elements["name"].text,
50                 :email => a.elements["email"].text}
51              end[0],
52              :date => Time.xmlschema(e.elements["issued"].text),
53              :link => e.elements["link"].attributes["href"]}
54           end
55         rescue
56         end
57         @count = doc.root.elements["fullcount"].text.to_i
58       end
59     end
60   end
61 end