Complete MailComponent tests
darcs-hash:20070321170603-86b55-109cde21f4ff2e4220c3cc5c7297e0aef755978f.gz
This commit is contained in:
20
src/jmc.py
20
src/jmc.py
@@ -30,17 +30,31 @@ del sys.setdefaultencoding
|
||||
from sqlobject import *
|
||||
from pyxmpp.message import Message
|
||||
|
||||
from jcl.model import account
|
||||
from jcl.model.account import Account, PresenceAccount
|
||||
|
||||
from jmc.jabber.component import MailComponent
|
||||
from jmc.model.account import MailPresenceAccount
|
||||
from jmc.model.account import MailAccount, IMAPAccount, POP3Account
|
||||
|
||||
DB_PATH = "/tmp/jmc.db"
|
||||
DB_URL = DB_PATH# + "?debug=1&debugThreading=1"
|
||||
|
||||
logger = logging.getLogger()
|
||||
logger.addHandler(logging.StreamHandler())
|
||||
logger.setLevel(logging.DEBUG)
|
||||
|
||||
account.hub.threadConnection = connectionForURI('sqlite://' + DB_URL)
|
||||
Account.createTable(ifNotExists = True)
|
||||
PresenceAccount.createTable(ifNotExists = True)
|
||||
MailAccount.createTable(ifNotExists = True)
|
||||
IMAPAccount.createTable(ifNotExists = True)
|
||||
POP3Account.createTable(ifNotExists = True)
|
||||
del account.hub.threadConnection
|
||||
|
||||
component = MailComponent("jmc.localhost", \
|
||||
"secret", \
|
||||
"127.0.0.1", \
|
||||
5349, \
|
||||
"sqlite:///tmp/jmc_test.db")
|
||||
component.account_class = MailPresenceAccount
|
||||
"sqlite://" + DB_URL)
|
||||
component.run()
|
||||
logger.debug("JMC is exiting")
|
||||
|
||||
@@ -1,2 +1,5 @@
|
||||
"""JMC module"""
|
||||
__revision__ = ""
|
||||
|
||||
version = "0.3.0"
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -21,31 +21,11 @@
|
||||
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
##
|
||||
|
||||
class Lang:
|
||||
def __init__(self, default_lang = "en"):
|
||||
self.default_lang = default_lang
|
||||
import jcl.lang
|
||||
|
||||
def get_lang_from_node(self, node):
|
||||
lang = node.getLang()
|
||||
if lang is None:
|
||||
print "Using default lang " + self.default_lang
|
||||
lang = self.default_lang
|
||||
return lang
|
||||
|
||||
def get_lang_class(self, lang):
|
||||
if lang is not None:
|
||||
lang = lang[:2]
|
||||
if hasattr(Lang, lang):
|
||||
return getattr(Lang, lang)
|
||||
return getattr(Lang, self.default_lang)
|
||||
|
||||
def get_lang_class_from_node(self, node):
|
||||
return self.get_lang_class(self.get_lang_from_node(node))
|
||||
|
||||
class en:
|
||||
register_title = u"Jabber Mail connection registration"
|
||||
register_instructions = u"Enter connection parameters"
|
||||
account_name = u"Connection name"
|
||||
class Lang(jcl.lang.Lang):
|
||||
class en(jcl.lang.Lang.en):
|
||||
account_login = u"Login"
|
||||
account_password = u"Password"
|
||||
account_password_store = u"Store password on Jabber server?"
|
||||
@@ -70,15 +50,6 @@ class Lang:
|
||||
update_account_message_subject = u"Updated %s connection '%s'"
|
||||
update_account_message_body = u"Registered with username '%s' and " \
|
||||
"password '%s' on '%s'"
|
||||
new_account_message_subject = u"New %s connection '%s' created"
|
||||
new_account_message_body = u"Registered with " \
|
||||
"username '%s' and password '%s' on '%s'"
|
||||
ask_password_subject = u"Password request"
|
||||
ask_password_body = u"Reply to this message with the password " \
|
||||
"for the following account: \n" \
|
||||
"\thost = %s\n" \
|
||||
"\tlogin = %s\n"
|
||||
password_saved_for_session = u"Password will be kept during your Jabber session"
|
||||
check_error_subject = u"Error while checking emails."
|
||||
check_error_body = u"An error appears while checking emails:\n\t%s"
|
||||
new_mail_subject = u"New email from %s"
|
||||
|
||||
@@ -33,7 +33,8 @@ import socket
|
||||
from sqlobject.inheritance import InheritableSQLObject
|
||||
from sqlobject.col import StringCol, IntCol, BoolCol
|
||||
|
||||
from jcl.model.account import PresenceAccount
|
||||
from jcl.model import account
|
||||
from jcl.model.account import Account, PresenceAccount
|
||||
from jmc.lang import Lang
|
||||
|
||||
IMAP4_TIMEOUT = 10
|
||||
@@ -148,15 +149,15 @@ class MailAccount(PresenceAccount):
|
||||
|
||||
lastcheck = IntCol(default = 0)
|
||||
waiting_password_reply = BoolCol(default = False)
|
||||
in_error = BoolCol(default = False)
|
||||
first_check = BoolCol(default = True)
|
||||
|
||||
def _init(self, *args, **kw):
|
||||
"""MailAccount init
|
||||
Initialize class attributes"""
|
||||
InheritableSQLObject._init(self, *args, **kw)
|
||||
PresenceAccount._init(self, *args, **kw)
|
||||
self.__logger = logging.getLogger("jmc.model.account.MailAccount")
|
||||
self.connection = None
|
||||
self.connected = False
|
||||
self.default_lang_class = Lang.en # TODO: use String
|
||||
|
||||
def _get_register_fields(cls):
|
||||
@@ -167,7 +168,7 @@ class MailAccount(PresenceAccount):
|
||||
return None
|
||||
return password
|
||||
|
||||
return Account.get_register_fields() + \
|
||||
return PresenceAccount.get_register_fields() + \
|
||||
[("login", "text-single", None, account.string_not_null_post_func, \
|
||||
account.mandatory_field), \
|
||||
("password", "text-private", None, password_post_func, \
|
||||
@@ -314,22 +315,12 @@ class MailAccount(PresenceAccount):
|
||||
def get_next_mail_index(self, mail_list):
|
||||
raise NotImplementedError
|
||||
|
||||
def is_mail_list_valid(self, mail_list):
|
||||
return (mail_list and mail_list != [] and mail_list[0] != '')
|
||||
|
||||
# Does not modify server state but just internal JMC state
|
||||
def mark_all_as_read(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def get_action(self):
|
||||
mapping = {"online": self.online_action,
|
||||
"chat": self.chat_action,
|
||||
"away": self.away_action,
|
||||
"xa": self.xa_action,
|
||||
"dnd": self.dnd_action,
|
||||
"offline": self.offline_action}
|
||||
if mapping.has_key(self.status):
|
||||
return mapping[self.status]
|
||||
return PresenceAccount.DO_NOTHING
|
||||
|
||||
action = property(get_action)
|
||||
|
||||
class IMAPAccount(MailAccount):
|
||||
mailbox = StringCol(default = "INBOX") # TODO : set default INBOX in reg_form (use get_register_fields last field ?)
|
||||
@@ -344,11 +335,7 @@ class IMAPAccount(MailAccount):
|
||||
|
||||
return MailAccount.get_register_fields() + \
|
||||
[("mailbox", "text-single", None, account.string_not_null_post_func, \
|
||||
(lambda field_name: "INBOX")), \
|
||||
("password", "text-private", None, password_post_func, \
|
||||
(lambda field_name: None)), \
|
||||
("store_password", "boolean", None, account.boolean_post_func, \
|
||||
lambda field_name: True)]
|
||||
(lambda field_name: "INBOX"))]
|
||||
|
||||
get_register_fields = classmethod(_get_register_fields)
|
||||
|
||||
@@ -375,11 +362,13 @@ class IMAPAccount(MailAccount):
|
||||
else:
|
||||
self.connection = MYIMAP4(self.host, self.port)
|
||||
self.connection.login(self.login, self.password)
|
||||
self.connected = True
|
||||
|
||||
def disconnect(self):
|
||||
self.__logger.debug("Disconnecting from IMAP server " \
|
||||
+ self.host)
|
||||
self.connection.logout()
|
||||
self.connected = False
|
||||
|
||||
def get_mail_list(self):
|
||||
self.__logger.debug("Getting mail list")
|
||||
@@ -406,9 +395,10 @@ class IMAPAccount(MailAccount):
|
||||
return u"Error while fetching mail " + str(index)
|
||||
|
||||
def get_next_mail_index(self, mail_list):
|
||||
if not mail_list or mail_list[0] == '':
|
||||
if self.is_mail_list_valid(mail_list):
|
||||
return mail_list.pop(0)
|
||||
else:
|
||||
return None
|
||||
return mail_list.pop(0)
|
||||
|
||||
def mark_all_as_read(self):
|
||||
self.get_mail_list()
|
||||
@@ -443,12 +433,14 @@ class POP3Account(MailAccount):
|
||||
except:
|
||||
self.connection.user(self.login)
|
||||
self.connection.pass_(self.password)
|
||||
self.connected = True
|
||||
|
||||
|
||||
def disconnect(self):
|
||||
self.__logger.debug("Disconnecting from POP3 server " \
|
||||
+ self.host)
|
||||
self.connection.quit()
|
||||
self.connected = False
|
||||
|
||||
def get_mail_list(self):
|
||||
self.__logger.debug("Getting mail list")
|
||||
@@ -479,13 +471,16 @@ class POP3Account(MailAccount):
|
||||
return u"Error while fetching mail " + str(index)
|
||||
|
||||
def get_next_mail_index(self, mail_list):
|
||||
if self.nb_mail == self.lastmail:
|
||||
if self.is_mail_list_valid(mail_list):
|
||||
if self.nb_mail == self.lastmail:
|
||||
return None
|
||||
if self.nb_mail < self.lastmail:
|
||||
self.lastmail = 0
|
||||
result = int(mail_list[self.lastmail])
|
||||
self.lastmail += 1
|
||||
return result
|
||||
else:
|
||||
return None
|
||||
if self.nb_mail < self.lastmail:
|
||||
self.lastmail = 0
|
||||
result = int(mail_list[self.lastmail])
|
||||
self.lastmail += 1
|
||||
return result
|
||||
|
||||
def mark_all_as_read(self):
|
||||
self.get_mail_list()
|
||||
|
||||
Reference in New Issue
Block a user