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
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
156
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
|
# -*- coding: utf-8 -*-
##
## feeder.py
## Login : David Rousselie <dax@happycoders.org>
## Started on Wed Mar 5 19:15:04:42 2008 David Rousselie
## $Id$
##
## Copyright (C) 2006 David Rousselie
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##
"""
FeederComponent with JMC Feeder and Sender implementation
"""
__revision__ = "$Id: feeder.py,v 1.1 2008/03/05 20:24:07 dax Exp $"
import logging
from pyxmpp.jid import JID
from jcl.model.account import PresenceAccount
from jcl.jabber.feeder import Feeder, MessageSender, \
HeadlineSender, FeederHandler
from jcl.model import account
from jmc.model.account import MailAccount
class MailFeeder(Feeder):
"""Email check"""
def __init__(self, component):
"""MailFeeder constructor"""
Feeder.__init__(self, component)
self.__logger = logging.getLogger("jmc.jabber.component.MailFeeder")
def initialize_live_email(self, _account):
"""For live email checking account, mark emails received while
offline as read.
Return a boolean to continue mail checking or not
(if waiting for password).
"""
if _account.password is None:
if not _account.waiting_password_reply:
account_manager = self.component.account_manager
self.component.send_stanzas(\
account_manager.ask_password(_account,
_account.default_lang_class))
return False
try:
_account.connect()
_account.mark_all_as_read()
_account.disconnect()
_account.first_check = False
_account.error = None
return True
except Exception, e:
if _account.connected:
try:
_account.disconnect()
except:
# We have done everything we could
_account.connected = False
self.component.send_error(_account, e)
return False
def feed(self, _account):
"""Check for new emails for given MailAccount and return a list of
those emails or a summary.
"""
self.__logger.debug("MailFeeder.feed")
result = []
if _account.first_check and _account.live_email_only:
continue_checking = self.initialize_live_email(_account)
if not continue_checking:
return result
_account.lastcheck += 1
if _account.lastcheck == _account.interval:
_account.lastcheck = 0
action = _account.action
if action != PresenceAccount.DO_NOTHING:
try:
if _account.password is None:
account_manager = self.component.account_manager
self.component.send_stanzas(\
account_manager.ask_password(_account,
_account.default_lang_class))
return result
self.__logger.debug("Checking " + _account.name)
self.__logger.debug("\t" + _account.login \
+ "@" + _account.host)
_account.connect()
mail_list = _account.get_new_mail_list()
default_lang_class = _account.default_lang_class
if action == MailAccount.RETRIEVE:
for mail_index in _account.get_next_mail_index(mail_list):
(body, email_from) = _account.get_mail(mail_index)
result.append((email_from,
default_lang_class.new_mail_subject\
% (email_from),
body))
elif action == MailAccount.DIGEST:
body = ""
new_mail_count = 0
for mail_index in _account.get_next_mail_index(mail_list):
(tmp_body, from_email) = \
_account.get_mail_summary(mail_index)
body += tmp_body
body += "\n----------------------------------\n"
new_mail_count += 1
if body != "":
result.append((None,
default_lang_class.new_digest_subject\
% (new_mail_count),
body))
else:
raise Exception("Unkown action: " + str(action) \
+ "\nPlease reconfigure account.")
_account.disconnect()
_account.error = None
self.__logger.debug("\nCHECK_MAIL ends " + _account.jid)
except Exception, e:
if _account.connected:
try:
_account.disconnect()
except:
# We have done everything we could
_account.connected = False
self.component.send_error(_account, e)
return result
class MailSender(HeadlineSender):
"""Send emails messages to jabber users"""
def create_full_email_message(self, email_from, email_subject,
email_body, to_account):
"""
Create a jabber message with email data and XEP-0033 addresses
"""
message = MessageSender.create_message(self, to_account,
(email_subject, email_body))
msg_node = message.get_node()
addresses_node = msg_node.newChild(None, "addresses", None)
address_ns = addresses_node.newNs("http://jabber.org/protocol/address",
None)
addresses_node.setNs(address_ns)
replyto_address_node = addresses_node.newChild(address_ns, "address",
None)
replyto_address_node.setProp("type", "replyto")
replyto_jid = email_from.replace('@', '%', 1) + "@" \
+ unicode(JID(to_account.jid).domain)
replyto_address_node.setProp("jid", replyto_jid)
return message
def create_message(self, to_account, data):
"""Send given emails (in data) as Jabber messages"""
email_from, subject, body = data
if to_account.action == MailAccount.RETRIEVE:
message = self.create_full_email_message(email_from, subject, body,
to_account)
elif to_account.action == MailAccount.DIGEST:
message = HeadlineSender.create_message(self, to_account,
(subject, body))
else:
message = None
return message
class MailFeederHandler(FeederHandler):
def filter(self, stanza, lang_class):
"""
Return only email account type to check mail from
"""
accounts = account.get_all_accounts(account_class=MailAccount)
return accounts
|