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'
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"
33 http = Net::HTTP.new("mail.google.com", 443)
36 http.verify_mode = OpenSSL::SSL::VERIFY_PEER
37 http.ca_path = @certificates
39 http.verify_mode = OpenSSL::SSL::VERIFY_NONE
41 request = Net::HTTP::Get.new("/mail/feed/atom")
42 request.basic_auth(@username, @password)
43 doc = REXML::Document.new(http.request(request).body)
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}
52 :date => Time.xmlschema(e.elements["issued"].text),
53 :link => e.elements["link"].attributes["href"]}
57 @count = doc.root.elements["fullcount"].text.to_i