Message subject added

- Jabber messages now have a subject
- Some bugs corrected :
	- password storage true by default
	- better exception handling
	- storage.__setitem__ called after account object complete

darcs-hash:20060206133741-86b55-0a51e24fb65b16c1c942b088f7052ab88248b9ac.gz
This commit is contained in:
David Rousselie
2006-02-06 14:37:41 +01:00
parent 8dc5a2aaa5
commit e5e80b2b0a
4 changed files with 71 additions and 44 deletions

View File

@@ -30,6 +30,7 @@ import sys
import anydbm import anydbm
import os import os
import time import time
import traceback
import mailconnection import mailconnection
from mailconnection import * from mailconnection import *
@@ -112,7 +113,7 @@ class MailComponent(Component):
reg_form.add_field(type = "boolean", \ reg_form.add_field(type = "boolean", \
label = lang_class.account_password_store, \ label = lang_class.account_password_store, \
var = "store_password", var = "store_password",
value = "True") value = "1")
reg_form.add_field(type = "text-single", \ reg_form.add_field(type = "text-single", \
label = lang_class.account_host, \ label = lang_class.account_host, \
@@ -647,6 +648,7 @@ class MailComponent(Component):
else: else:
socket = host socket = host
if self.__storage.has_key((base_from_jid, name)): if self.__storage.has_key((base_from_jid, name)):
account = self.__storage[(base_from_jid, name)]
m = Message(from_jid = self.jid, to_jid = from_jid, \ m = Message(from_jid = self.jid, to_jid = from_jid, \
stanza_type = "normal", \ stanza_type = "normal", \
subject = lang_class.update_account_message_subject \ subject = lang_class.update_account_message_subject \
@@ -655,6 +657,7 @@ class MailComponent(Component):
% (login, password, socket)) % (login, password, socket))
self.stream.send(m) self.stream.send(m)
else: else:
account = mailconnection_factory.get_new_mail_connection(type)
m = Message(from_jid = self.jid, to_jid = from_jid, \ m = Message(from_jid = self.jid, to_jid = from_jid, \
stanza_type = "normal", \ stanza_type = "normal", \
subject = lang_class.new_account_message_subject \ subject = lang_class.new_account_message_subject \
@@ -666,9 +669,6 @@ class MailComponent(Component):
to_jid = base_from_jid, \ to_jid = base_from_jid, \
stanza_type="subscribe") stanza_type="subscribe")
self.stream.send(p) self.stream.send(p)
self.__storage[(base_from_jid, name)] = account = \
mailconnection_factory.get_new_mail_connection(type)
account.login = login account.login = login
account.password = password account.password = password
account.store_password = store_password account.store_password = store_password
@@ -687,6 +687,7 @@ class MailComponent(Component):
if type[0:4] == "imap": if type[0:4] == "imap":
account.mailbox = mailbox account.mailbox = mailbox
self.__storage[(base_from_jid, name)] = account
return 1 return 1
@@ -857,26 +858,30 @@ class MailComponent(Component):
account.lastmail = 0 account.lastmail = 0
if action == mailconnection.RETRIEVE: if action == mailconnection.RETRIEVE:
while account.lastmail < num: while account.lastmail < num:
body = account.get_mail(int(mail_list[account.lastmail])) (body, email_from) = account.get_mail(int(mail_list[account.lastmail]))
mesg = Message(from_jid = name + "@" + \ mesg = Message(from_jid = name + "@" + \
unicode(self.jid), \ unicode(self.jid), \
to_jid = jid, \ to_jid = jid, \
stanza_type = "normal", \ stanza_type = "normal", \
subject = account.default_lang_class.new_mail_subject % (email_from), \
body = body) body = body)
self.stream.send(mesg) self.stream.send(mesg)
account.lastmail += 1 account.lastmail += 1
else: else:
body = "" body = ""
new_mail_count = 0
while account.lastmail < num: while account.lastmail < num:
body += \ (tmp_body, from_email) = \
account.get_mail_summary(int(mail_list[account.lastmail])) \ account.get_mail_summary(int(mail_list[account.lastmail]))
+ "\n----------------------------------\n" body += tmp_body + "\n----------------------------------\n"
account.lastmail += 1 account.lastmail += 1
new_mail_count += 1
if body != "": if body != "":
mesg = Message(from_jid = name + "@" + \ mesg = Message(from_jid = name + "@" + \
unicode(self.jid), \ unicode(self.jid), \
to_jid = jid, \ to_jid = jid, \
stanza_type = "headline", \ stanza_type = "headline", \
subject = account.default_lang_class.new_digest_subject % (new_mail_count), \
body = body) body = body)
self.stream.send(mesg) self.stream.send(mesg)
account.disconnect() account.disconnect()
@@ -891,8 +896,10 @@ class MailComponent(Component):
body = account.default_lang_class.check_error_body \ body = account.default_lang_class.check_error_body \
% (e)) % (e))
self.stream.send(msg) self.stream.send(msg)
self.__logger.debug("Error while checking mail : %s" \ type, value, stack = sys.exc_info()
% (e)) self.__logger.debug("Error while checking mail : %s\n%s" \
% (e, "".join(traceback.format_exception
(type, value, stack, 5))))
""" check mail handler """ """ check mail handler """
def check_all_mail(self): def check_all_mail(self):
@@ -923,8 +930,10 @@ class MailComponent(Component):
body = account.default_lang_class.check_error_body \ body = account.default_lang_class.check_error_body \
% (e)) % (e))
self.stream.send(msg) self.stream.send(msg)
self.__logger.debug("Error while checking mail : %s" \ type, value, stack = sys.exc_info()
% (e)) self.__logger.debug("Error while first checking mail : %s\n%s" \
% (e, "".join(traceback.format_exception
(type, value, stack, 5))))
account.lastcheck += 1 account.lastcheck += 1
if account.lastcheck == account.interval: if account.lastcheck == account.interval:
account.lastcheck = 0 account.lastcheck = 0

View File

@@ -78,6 +78,8 @@ class Lang:
password_saved_for_session = u"Password will be kept during your jabber session" password_saved_for_session = u"Password will be kept during your jabber session"
check_error_subject = u"Error while checking emails." check_error_subject = u"Error while checking emails."
check_error_body = u"An error appears while checking emails :\n\t%s" check_error_body = u"An error appears while checking emails :\n\t%s"
new_mail_subject = u"New email from %s"
new_digest_subject = u"%i new email(s)"
class fr: class fr:
register_title = u"Enregistrement d'une nouvelle connexion à un serveur email." register_title = u"Enregistrement d'une nouvelle connexion à un serveur email."
@@ -119,3 +121,5 @@ class Lang:
check_error_subject = u"Erreur lors de la vérification des emails." check_error_subject = u"Erreur lors de la vérification des emails."
check_error_body = u"Une erreur est survenue lors de la vérification " \ check_error_body = u"Une erreur est survenue lors de la vérification " \
"des emails :\n\t%s" "des emails :\n\t%s"
new_mail_subject = u"Nouvel email de %s"
new_digest_subject = u"%i nouveau(x) email(s)"

View File

@@ -226,23 +226,26 @@ class MailConnection(object):
def format_message(self, email_msg, include_body = True): def format_message(self, email_msg, include_body = True):
from_decoded = email.Header.decode_header(email_msg["From"]) from_decoded = email.Header.decode_header(email_msg["From"])
charset_hint = None charset_hint = None
email_from = u""
result = u"From : " result = u"From : "
for i in range(len(from_decoded)): for i in range(len(from_decoded)):
if from_decoded[i][1]: if from_decoded[i][1]:
charset_hint = from_decoded[i][1] charset_hint = from_decoded[i][1]
result += unicode(from_decoded[i][0].decode(from_decoded[i][1])) email_from += unicode(from_decoded[i][0].decode(from_decoded[i][1]))
else: else:
try: try:
result += unicode(from_decoded[i][0]) email_from += unicode(from_decoded[i][0])
except Exception,e: except Exception,e:
try: try:
result += unicode(from_decoded[i][0].decode("iso-8859-1")) email_from += unicode(from_decoded[i][0].decode("iso-8859-1"))
except Exception, e: except Exception, e:
try: try:
result += unicode(from_decoded[i][0].decode(default_encoding)) email_from += unicode(from_decoded[i][0].decode(default_encoding))
except Exception, e: except Exception, e:
print e type, value, stack = sys.exc_info()
result += "\n" print >>sys.stderr, "".join(traceback.format_exception
(type, value, stack, 5))
result += email_from + u"\n"
subject_decoded = email.Header.decode_header(email_msg["Subject"]) subject_decoded = email.Header.decode_header(email_msg["Subject"])
result += u"Subject : " result += u"Subject : "
@@ -279,7 +282,7 @@ class MailConnection(object):
content_type = part.get_content_type() content_type = part.get_content_type()
if action.has_key(content_type): if action.has_key(content_type):
result += action[content_type](part) + u'\n' result += action[content_type](part) + u'\n'
return result return (result, email_from)
def format_message_summary(self, email_msg): def format_message_summary(self, email_msg):
return self.format_message(email_msg, False) return self.format_message(email_msg, False)

View File

@@ -59,13 +59,15 @@ class MailConnection_TestCase(unittest.TestCase):
test_format_message_summary_not_encoded = \ test_format_message_summary_not_encoded = \
make_test((False, False, True), \ make_test((False, False, True), \
lambda self, email: self.connection.format_message_summary(email), \ lambda self, email: self.connection.format_message_summary(email), \
u"From : not encoded from\nSubject : not encoded subject\n\n") (u"From : not encoded from\nSubject : not encoded subject\n\n", \
u"not encoded from"))
test_format_message_summary_encoded = \ test_format_message_summary_encoded = \
make_test((True, False, True), \ make_test((True, False, True), \
lambda self, email: self.connection.format_message_summary(email), \ lambda self, email: self.connection.format_message_summary(email), \
u"From : encoded from (éàê)\nSubject : encoded subject " + \ (u"From : encoded from (éàê)\nSubject : encoded subject " + \
u"(éàê)\n\n") u"(éàê)\n\n", \
u"encoded from (éàê)"))
test_format_message_summary_partial_encoded = \ test_format_message_summary_partial_encoded = \
make_test((True, False, True), \ make_test((True, False, True), \
@@ -77,35 +79,40 @@ class MailConnection_TestCase(unittest.TestCase):
"\"" + str(email["From"]) \ "\"" + str(email["From"]) \
+ "\" not encoded part") or \ + "\" not encoded part") or \
self.connection.format_message_summary(email), \ self.connection.format_message_summary(email), \
u"From : \"encoded from (éàê)\" not encoded part\nSubject " + \ (u"From : \"encoded from (éàê)\" not encoded part\nSubject " + \
u": \"encoded subject (éàê)\" not encoded part\n\n") u": \"encoded subject (éàê)\" not encoded part\n\n", \
u"\"encoded from (éàê)\" not encoded part"))
test_format_message_single_not_encoded = \ test_format_message_single_not_encoded = \
make_test((False, False, True), \ make_test((False, False, True), \
lambda self, email: self.connection.format_message(email), \ lambda self, email: self.connection.format_message(email), \
u"From : not encoded from\nSubject : not encoded subject" + \ (u"From : not encoded from\nSubject : not encoded subject" + \
u"\n\nNot encoded single part\n") u"\n\nNot encoded single part\n", \
u"not encoded from"))
test_format_message_single_encoded = \ test_format_message_single_encoded = \
make_test((True, False, True), \ make_test((True, False, True), \
lambda self, email: self.connection.format_message(email), \ lambda self, email: self.connection.format_message(email), \
u"From : encoded from (éàê)\nSubject : encoded subject " + \ (u"From : encoded from (éàê)\nSubject : encoded subject " + \
u"(éàê)\n\nEncoded single part with 'iso-8859-15' charset" + \ u"(éàê)\n\nEncoded single part with 'iso-8859-15' charset" + \
u" (éàê)\n") u" (éàê)\n", \
u"encoded from (éàê)"))
test_format_message_multi_not_encoded = \ test_format_message_multi_not_encoded = \
make_test((False, True, True), \ make_test((False, True, True), \
lambda self, email: self.connection.format_message(email), \ lambda self, email: self.connection.format_message(email), \
u"From : not encoded from\nSubject : not encoded subject" + \ (u"From : not encoded from\nSubject : not encoded subject" + \
u"\n\nNot encoded multipart1\nNot encoded multipart2\n") u"\n\nNot encoded multipart1\nNot encoded multipart2\n", \
u"not encoded from"))
test_format_message_multi_encoded = \ test_format_message_multi_encoded = \
make_test((True, True, True), \ make_test((True, True, True), \
lambda self, email: self.connection.format_message(email), \ lambda self, email: self.connection.format_message(email), \
u"From : encoded from (éàê)\nSubject : encoded subject (éà" + \ (u"From : encoded from (éàê)\nSubject : encoded subject (éà" + \
u"ê)\n\nutf-8 multipart1 with no charset (éàê)" + \ u"ê)\n\nutf-8 multipart1 with no charset (éàê)" + \
u"\nEncoded multipart2 with 'iso-8859-15' charset (éàê)\n" + \ u"\nEncoded multipart2 with 'iso-8859-15' charset (éàê)\n" + \
u"Encoded multipart3 with no charset (éàê)\n") u"Encoded multipart3 with no charset (éàê)\n", \
u"encoded from (éàê)"))
class POP3Connection_TestCase(unittest.TestCase): class POP3Connection_TestCase(unittest.TestCase):
@@ -161,8 +168,9 @@ class POP3Connection_TestCase(unittest.TestCase):
["RETR 1\r\n"], \ ["RETR 1\r\n"], \
lambda self: \ lambda self: \
self.assertEquals(self.pop3connection.get_mail_summary(1), \ self.assertEquals(self.pop3connection.get_mail_summary(1), \
"From : user@test.com\n" + \ (u"From : user@test.com\n" + \
"Subject : subject test\n\n")) u"Subject : subject test\n\n", \
u"user@test.com")))
test_get_mail = \ test_get_mail = \
make_test(["+OK 10 octets\r\n" + \ make_test(["+OK 10 octets\r\n" + \
@@ -172,9 +180,10 @@ class POP3Connection_TestCase(unittest.TestCase):
["RETR 1\r\n"], \ ["RETR 1\r\n"], \
lambda self: \ lambda self: \
self.assertEquals(self.pop3connection.get_mail(1), \ self.assertEquals(self.pop3connection.get_mail(1), \
"From : user@test.com\n" + \ (u"From : user@test.com\n" + \
"Subject : subject test\n\n" + \ u"Subject : subject test\n\n" + \
"mymessage\n")) u"mymessage\n", \
u"user@test.com")))
class IMAPConnection_TestCase(unittest.TestCase): class IMAPConnection_TestCase(unittest.TestCase):
@@ -245,7 +254,8 @@ class IMAPConnection_TestCase(unittest.TestCase):
"^[^ ]* FETCH 1 \(RFC822\)", \ "^[^ ]* FETCH 1 \(RFC822\)", \
"^[^ ]* STORE 1 FLAGS \(UNSEEN\)"], \ "^[^ ]* STORE 1 FLAGS \(UNSEEN\)"], \
lambda self: self.assertEquals(self.imap_connection.get_mail_summary(1), \ lambda self: self.assertEquals(self.imap_connection.get_mail_summary(1), \
"From : None\nSubject : None\n\n")) (u"From : None\nSubject : None\n\n", \
u"None")))
test_get_mail = make_test([lambda data: "* 42 EXISTS\r\n* 1 RECENT\r\n* OK" +\ test_get_mail = make_test([lambda data: "* 42 EXISTS\r\n* 1 RECENT\r\n* OK" +\
" [UNSEEN 9]\r\n* FLAGS (\Deleted \Seen\*)\r\n*" +\ " [UNSEEN 9]\r\n* FLAGS (\Deleted \Seen\*)\r\n*" +\
@@ -261,5 +271,6 @@ class IMAPConnection_TestCase(unittest.TestCase):
"^[^ ]* FETCH 1 \(RFC822\)", \ "^[^ ]* FETCH 1 \(RFC822\)", \
"^[^ ]* STORE 1 FLAGS \(UNSEEN\)"], \ "^[^ ]* STORE 1 FLAGS \(UNSEEN\)"], \
lambda self: self.assertEquals(self.imap_connection.get_mail(1), \ lambda self: self.assertEquals(self.imap_connection.get_mail(1), \
"From : None\nSubject : None\n\nbody text\r\n\n")) (u"From : None\nSubject : None\n\nbody text\r\n\n", \
u"None")))