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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
|
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
#
require 'net/pop'
require "rexml/document"
include REXML
# class POPChecker
#
# def initialize()
# print "POPCheckerImpl.initialize\n"
# end
def getNewAlerts(popserver, username, password, lastchecked)
print "POPCheckerImpl getting new POP e-mail alerts\n"
alertsXML = "<alerts xmlns=\"http://tuscany.apache.org/samples/alerter\">\n"
# initially set lastchecked to the epoch before trying to use the lastchecked string
lastcheckedtime = Time.at(0)
if lastchecked != ''
# lastchecked (if provided) is of form 2007-02-17T23:34:56
year , month, day, hour, min, sec = lastchecked.split(/[-T:]/)
lastcheckedtime = Time.gm(year , month, day, hour, min, sec, nil)
end
numberOfEmails = 0
Net::POP3.start(popserver, 110, username, password) do |pop|
if !pop.mails.empty?
pop.each_mail do |m|
msg = m.pop
header, body = msg.split("\r\n\r\n", 2)
date = getMessageField("Date", header)
# date is of form "Sun, 21 Jan 2007 13:51:53 -0500"
parts = date.split(' ')
timeparts = parts[4].split(':')
timezoneSecs = (parts[5].to_i()/100)*60*60 + (parts[5].slice(-2,0).to_i())*60
msgtime = Time.gm(timeparts[2], timeparts[1], timeparts[0], parts[1], parts[2], parts[3], nil, nil, nil, nil) + timezoneSecs
if msgtime >= lastcheckedtime
from = getMessageField("From", header)
subject = getMessageField("Subject", header)
alertsXML += "<alert><title>From: " + encodeXML(from) + "\nSubject: "+encodeXML(subject)+"</title>\n"
alertsXML += "<address></address>\n"
alertsXML += "<date>" + msgtime.strftime("%Y-%m-%dT%H:%M:%S") + "</date>\n"
alertsXML += "<summary>" + stripXML(body) + "</summary></alert>\n"
numberOfEmails += 1
end
end
end
end
alertsXML += "</alerts>"
# Print XML
# puts alertsXML
print "POPCheckerImpl retrieved "+numberOfEmails.to_s+" new POP e-mail alerts\n"
# Return data as REXML document
result = Document.new(alertsXML)
return result
end
def getMessageField(field, message)
# Use a regex to get the field
pattern = '^'+field+': (.*?)$'
re = Regexp.new(pattern)
re =~ message
if $1 == nil
return ''
end
return $1
end
def encodeXML(data)
data = data.gsub(/</,'<')
data = data.gsub(/>/,'>')
return data
end
def stripXML(data)
data = data.gsub(/<.*?>/m,'')
data = data.gsub(/&.*?;/, ' ')
data = data.gsub(/&/, 'and')
data = encodeXML(data)
return data
end
# # Testing
# pop = POPChecker.new()
# print pop.getNewAlerts("mail.eclipse.co.uk", "andrew@borley-hall.eclipse.co.uk", "app73sauc3", "2007-01-29T02:15:53")
|