Migration to JCL
JMC migration to JCL: - Use SQLObject for persistence - Use PyXMPP DataForm implementation - test packages reorganisation Need to update component.py and config.py to finish the migration darcs-hash:20070221173604-86b55-17fb4a530f378b51b6b62a117a6f93c73c5be796.gz
This commit is contained in:
85
src/jmc.py
85
src/jmc.py
@@ -1,12 +1,10 @@
|
||||
#!/usr/bin/python -u
|
||||
##
|
||||
## Jabber Mail Component
|
||||
## jmc.py
|
||||
## Login : David Rousselie <david.rousselie@happycoders.org>
|
||||
## Started on Fri Jan 7 11:06:42 2005
|
||||
## $Id: jmc.py,v 1.3 2005/07/11 20:39:31 dax Exp $
|
||||
## Login : <dax@happycoders.org>
|
||||
## Started on Fri Jan 19 18:14:41 2007 David Rousselie
|
||||
## $Id$
|
||||
##
|
||||
## Copyright (C) 2005
|
||||
## Copyright (C) 2007 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
|
||||
@@ -22,70 +20,27 @@
|
||||
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
##
|
||||
|
||||
import sys
|
||||
import os.path
|
||||
import logging
|
||||
import sys
|
||||
|
||||
reload(sys)
|
||||
sys.setdefaultencoding('utf-8')
|
||||
del sys.setdefaultencoding
|
||||
from jmc.email import mailconnection
|
||||
from jmc.jabber.component import MailComponent, ComponentFatalError
|
||||
from jmc.utils.config import Config
|
||||
|
||||
def main(config_file = "jmc.xml", isDebug = 0):
|
||||
try:
|
||||
logger = logging.getLogger()
|
||||
logger.addHandler(logging.StreamHandler())
|
||||
if isDebug > 0:
|
||||
logger.setLevel(logging.DEBUG)
|
||||
try:
|
||||
logger.debug("Loading config file " + config_file)
|
||||
config = Config(config_file)
|
||||
except:
|
||||
print >>sys.stderr, "Couldn't load config file:", \
|
||||
str(sys.exc_value)
|
||||
sys.exit(1)
|
||||
from sqlobject import *
|
||||
from pyxmpp.message import Message
|
||||
|
||||
pidfile = open(config.get_content("config/pidfile"), "w")
|
||||
pidfile.write(str(os.getpid()))
|
||||
pidfile.close()
|
||||
mailconnection.default_encoding = config.get_content("config/mail_default_encoding")
|
||||
print "creating component..."
|
||||
mailcomp = MailComponent(config.get_content("config/jabber/service"), \
|
||||
config.get_content("config/jabber/secret"), \
|
||||
config.get_content("config/jabber/server"), \
|
||||
int(config.get_content("config/jabber/port")), \
|
||||
config.get_content("config/jabber/language"), \
|
||||
int(config.get_content("config/check_interval")), \
|
||||
config.get_content("config/spooldir"), \
|
||||
config.get_content("config/storage"), \
|
||||
config.get_content("config/jabber/vCard/FN"))
|
||||
|
||||
print "starting..."
|
||||
mailcomp.run(1)
|
||||
os.remove(config.get_content("config/pidfile"))
|
||||
except ComponentFatalError,e:
|
||||
print e
|
||||
print "Aborting."
|
||||
sys.exit(1)
|
||||
|
||||
if __name__ == "__main__":
|
||||
var_option = 0
|
||||
file_num = 0
|
||||
index = 0
|
||||
debug_level = 0
|
||||
for opt in sys.argv:
|
||||
if var_option == 0 and len(opt) == 2 and opt == "-c":
|
||||
var_option += 1
|
||||
elif (var_option & 1) == 1 and len(opt) > 0:
|
||||
var_option += 1
|
||||
file_num = index
|
||||
if len(opt) == 2 and opt == "-D":
|
||||
debug_level = 1
|
||||
index += 1
|
||||
if (var_option & 2) == 2:
|
||||
main(sys.argv[file_num], debug_level)
|
||||
else:
|
||||
main("/etc/jabber/jmc.xml", debug_level)
|
||||
from jmc.jabber.component import MailComponent
|
||||
from jmc.model.account import MailPresenceAccount
|
||||
|
||||
logger = logging.getLogger()
|
||||
logger.addHandler(logging.StreamHandler())
|
||||
logger.setLevel(logging.DEBUG)
|
||||
component = MailComponent("jmc.localhost", \
|
||||
"secret", \
|
||||
"127.0.0.1", \
|
||||
5349, \
|
||||
"sqlite:///tmp/jmc_test.db")
|
||||
component.account_class = MailPresenceAccount
|
||||
component.run()
|
||||
logger.debug("JMC is exiting")
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
"""JMC module"""
|
||||
__revision__ = ""
|
||||
|
||||
@@ -1,142 +0,0 @@
|
||||
##
|
||||
## mailconnection_factory.py
|
||||
## Login : David Rousselie <david.rousselie@happycoders.org>
|
||||
## Started on Fri May 20 10:41:46 2005 David Rousselie
|
||||
## $Id: mailconnection_factory.py,v 1.2 2005/09/18 20:24:07 dax Exp $
|
||||
##
|
||||
## Copyright (C) 2005
|
||||
## 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
|
||||
##
|
||||
|
||||
import jmc.email.mailconnection as mailconnection
|
||||
from jmc.email.mailconnection import IMAPConnection, POP3Connection
|
||||
|
||||
def get_new_mail_connection(type):
|
||||
""" Static method to return an empty MailConnection object of given type
|
||||
:Parameters:
|
||||
- 'type': type of connection to return : 'imap', 'imaps', 'pop3', 'pop3s'
|
||||
|
||||
:return: MailConnection of given type in parameter, None if unknown type
|
||||
|
||||
:returntype: 'MailConnection'
|
||||
"""
|
||||
if type == "imap":
|
||||
return IMAPConnection()
|
||||
elif type == "imaps":
|
||||
return IMAPConnection(ssl = True)
|
||||
elif type == "pop3":
|
||||
return POP3Connection()
|
||||
elif type == "pop3s":
|
||||
return POP3Connection(ssl = True)
|
||||
raise Exception, "Connection type \"" + type + "\" unknown"
|
||||
|
||||
def str_to_mail_connection(connection_string):
|
||||
""" Static methode to create a MailConnection object filled from string
|
||||
|
||||
:Parameters:
|
||||
- 'connection_string': string containing MailConnection parameters separated
|
||||
by '#'. ex: 'pop3#login#password#host#110#chat_action#online_action#away_action#xa_action#dnd_action#offline_action#check_interval#liv_email_only(#Mailbox)'
|
||||
|
||||
:Types:
|
||||
- 'connection_string': string
|
||||
|
||||
:return: MailConnection of given type found in string parameter
|
||||
|
||||
:returntype: 'MailConnection'
|
||||
"""
|
||||
arg_list = connection_string.split("#")
|
||||
# optionals values must be the at the beginning of the list to pop them
|
||||
# last
|
||||
arg_list.reverse()
|
||||
type = arg_list.pop()
|
||||
login = arg_list.pop()
|
||||
password = arg_list.pop()
|
||||
if password == "/\\":
|
||||
password = None
|
||||
store_password = False
|
||||
else:
|
||||
store_password = True
|
||||
host = arg_list.pop()
|
||||
port = int(arg_list.pop())
|
||||
chat_action = None
|
||||
online_action = None
|
||||
away_action = None
|
||||
xa_action = None
|
||||
dnd_action = None
|
||||
offline_action = None
|
||||
interval = None
|
||||
live_email_only = False
|
||||
result = None
|
||||
if type[0:4] == "imap":
|
||||
if len(arg_list) == 9:
|
||||
chat_action = int(arg_list.pop())
|
||||
online_action = int(arg_list.pop())
|
||||
away_action = int(arg_list.pop())
|
||||
xa_action = int(arg_list.pop())
|
||||
dnd_action = int(arg_list.pop())
|
||||
offline_action = int(arg_list.pop())
|
||||
interval = int(arg_list.pop())
|
||||
live_email_only = (arg_list.pop().lower() == "true")
|
||||
else:
|
||||
retrieve = bool(arg_list.pop() == "True")
|
||||
if retrieve:
|
||||
chat_action = online_action = away_action = xa_action = dnd_action = mailconnection.RETRIEVE
|
||||
else:
|
||||
chat_action = online_action = away_action = xa_action = dnd_action = mailconnection.DIGEST
|
||||
offline_action = mailconnection.DO_NOTHING
|
||||
mailbox = arg_list.pop()
|
||||
result = IMAPConnection(login = login, \
|
||||
password = password, \
|
||||
host = host, \
|
||||
ssl = (len(type) == 5), \
|
||||
port = port, \
|
||||
mailbox = mailbox)
|
||||
elif type[0:4] == "pop3":
|
||||
if len(arg_list) == 8:
|
||||
chat_action = int(arg_list.pop())
|
||||
online_action = int(arg_list.pop())
|
||||
away_action = int(arg_list.pop())
|
||||
xa_action = int(arg_list.pop())
|
||||
dnd_action = int(arg_list.pop())
|
||||
offline_action = int(arg_list.pop())
|
||||
interval = int(arg_list.pop())
|
||||
live_email_only = (arg_list.pop().lower() == "true")
|
||||
else:
|
||||
retrieve = bool(arg_list.pop() == "True")
|
||||
if retrieve:
|
||||
chat_action = online_action = away_action = xa_action = dnd_action = mailconnection.RETRIEVE
|
||||
else:
|
||||
chat_action = online_action = away_action = xa_action = dnd_action = mailconnection.DIGEST
|
||||
offline_action = mailconnection.DO_NOTHING
|
||||
result = POP3Connection(login = login, \
|
||||
password = password, \
|
||||
host = host, \
|
||||
port = port, \
|
||||
ssl = (len(type) == 5))
|
||||
if result is None:
|
||||
raise Exception, "Connection type \"" + type + "\" unknown"
|
||||
result.store_password = store_password
|
||||
result.chat_action = chat_action
|
||||
result.online_action = online_action
|
||||
result.away_action = away_action
|
||||
result.xa_action = xa_action
|
||||
result.dnd_action = dnd_action
|
||||
result.offline_action = offline_action
|
||||
if interval is not None:
|
||||
result.interval = interval
|
||||
result.live_email_only = live_email_only
|
||||
return result
|
||||
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
"""Jabber component classes"""
|
||||
__revision__ = ""
|
||||
|
||||
@@ -1,129 +0,0 @@
|
||||
##
|
||||
## x.py
|
||||
## Login : David Rousselie <dax@happycoders.org>
|
||||
## Started on Fri Jan 7 11:06:42 2005
|
||||
## $Id: x.py,v 1.3 2005/09/18 20:24:07 dax Exp $
|
||||
##
|
||||
## Copyright (C) 2005
|
||||
## 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
|
||||
##
|
||||
|
||||
import sys
|
||||
from pyxmpp.stanza import common_doc
|
||||
|
||||
class Option(object):
|
||||
def __init__(self, label, value):
|
||||
self.__label = label
|
||||
self.__value = value
|
||||
|
||||
def get_xml(self, parent):
|
||||
if parent is None:
|
||||
option = common_doc.newChild(None, "option", None)
|
||||
else:
|
||||
option = parent.newChild(None, "option", None)
|
||||
option.setProp("label", self.__label)
|
||||
option.newChild(None, "value", self.__value)
|
||||
return option
|
||||
|
||||
class Field(object):
|
||||
def __init__(self, type, label, var, value):
|
||||
self.__type = type
|
||||
self.__label = label
|
||||
self.__var = var
|
||||
self.value = value
|
||||
self.__options = []
|
||||
|
||||
def add_option(self, label, value):
|
||||
option = Option(label, value)
|
||||
self.__options.append(option)
|
||||
return option
|
||||
|
||||
def get_xml(self, parent):
|
||||
if parent is None:
|
||||
raise Exception, "parent field should not be None"
|
||||
else:
|
||||
field = parent.newChild(None, "field", None)
|
||||
field.setProp("type", self.__type)
|
||||
if not self.__label is None:
|
||||
field.setProp("label", self.__label)
|
||||
if not self.__var is None:
|
||||
field.setProp("var", self.__var)
|
||||
if self.value:
|
||||
field.newChild(None, "value", self.value)
|
||||
for option in self.__options:
|
||||
option.get_xml(field)
|
||||
return field
|
||||
|
||||
class X(object):
|
||||
def __init__(self):
|
||||
self.fields = {}
|
||||
self.fields_tab = []
|
||||
self.title = None
|
||||
self.instructions = None
|
||||
self.type = None
|
||||
self.xmlns = None
|
||||
|
||||
def add_field(self, type = "fixed", label = None, var = None, value = ""):
|
||||
field = Field(type, label, var, value)
|
||||
self.fields[var] = field
|
||||
# fields_tab exist to keep added fields order
|
||||
self.fields_tab.append(field)
|
||||
return field
|
||||
|
||||
def attach_xml(self, iq):
|
||||
node = iq.newChild(None, "x", None)
|
||||
_ns = node.newNs(self.xmlns, None)
|
||||
node.setNs(_ns)
|
||||
if not self.title is None:
|
||||
node.newTextChild(None, "title", self.title)
|
||||
if not self.instructions is None:
|
||||
node.newTextChild(None, "instructions", self.instructions)
|
||||
for field in self.fields_tab:
|
||||
field.get_xml(node)
|
||||
return node
|
||||
|
||||
def from_xml(self, node):
|
||||
## TODO : test node type and ns and clean that loop !!!!
|
||||
while node and node.type != "element":
|
||||
node = node.next
|
||||
child = node.children
|
||||
while child:
|
||||
## TODO : test child type (element) and ns (jabber:x:data)
|
||||
if child.type == "element" and child.name == "field":
|
||||
if child.hasProp("type"):
|
||||
type = child.prop("type")
|
||||
else:
|
||||
type = ""
|
||||
|
||||
if child.hasProp("label"):
|
||||
label = child.prop("label")
|
||||
else:
|
||||
label = ""
|
||||
|
||||
if child.hasProp("var"):
|
||||
var = child.prop("var")
|
||||
else:
|
||||
var = ""
|
||||
|
||||
xval = child.children
|
||||
while xval and xval.name != "value":
|
||||
xval = xval.next
|
||||
if xval:
|
||||
value = xval.getContent()
|
||||
else:
|
||||
value = ""
|
||||
field = Field(type, label, var, value)
|
||||
self.fields[var] = field
|
||||
child = child.next
|
||||
2
src/jmc/model/__init__.py
Normal file
2
src/jmc/model/__init__.py
Normal file
@@ -0,0 +1,2 @@
|
||||
"""Contains data model classes"""
|
||||
__revision__ = ""
|
||||
@@ -1,10 +1,10 @@
|
||||
##
|
||||
## mailconnection.py
|
||||
## Login : David Rousselie <dax@happycoders.org>
|
||||
## Started on Fri Jan 7 11:06:42 2005
|
||||
## $Id: mailconnection.py,v 1.11 2005/09/18 20:24:07 dax Exp $
|
||||
## account.py
|
||||
## Login : <dax@happycoders.org>
|
||||
## Started on Fri Jan 19 18:21:44 2007 David Rousselie
|
||||
## $Id$
|
||||
##
|
||||
## Copyright (C) 2005
|
||||
## Copyright (C) 2007 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
|
||||
@@ -20,7 +20,6 @@
|
||||
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
##
|
||||
|
||||
|
||||
import sys
|
||||
import logging
|
||||
import email
|
||||
@@ -31,16 +30,15 @@ import poplib
|
||||
import imaplib
|
||||
import socket
|
||||
|
||||
from jmc.utils.lang import Lang
|
||||
from sqlobject.inheritance import InheritableSQLObject
|
||||
from sqlobject.col import StringCol, IntCol, BoolCol
|
||||
|
||||
from jcl.model.account import PresenceAccount
|
||||
from jmc.lang import Lang
|
||||
|
||||
IMAP4_TIMEOUT = 10
|
||||
POP3_TIMEOUT = 10
|
||||
|
||||
DO_NOTHING = 0
|
||||
DIGEST = 1
|
||||
RETRIEVE = 2
|
||||
default_encoding = "iso-8859-1"
|
||||
|
||||
## All MY* classes are implemented to add a timeout (settimeout)
|
||||
## while connecting
|
||||
class MYIMAP4(imaplib.IMAP4):
|
||||
@@ -127,77 +125,84 @@ class MYPOP3_SSL(poplib.POP3_SSL):
|
||||
self._debugging = 0
|
||||
self.welcome = self._getresp()
|
||||
|
||||
class MailConnection(object):
|
||||
class MailAccount(PresenceAccount):
|
||||
""" Wrapper to mail connection and action.
|
||||
Abstract class, do not represent real mail connection type"""
|
||||
|
||||
_logger = logging.getLogger("jmc.MailConnection")
|
||||
# Define constants
|
||||
DIGEST = 1
|
||||
RETRIEVE = 2
|
||||
default_encoding = "iso-8859-1"
|
||||
possibles_actions = [PresenceAccount.DO_NOTHING, \
|
||||
DIGEST, \
|
||||
RETRIEVE]
|
||||
|
||||
def __init__(self, login = "", password = "", host = "", \
|
||||
port = 110, ssl = False):
|
||||
""" Initialize MailConnection object for common parameters of all
|
||||
connections types
|
||||
|
||||
:Parameters:
|
||||
- 'login': login used to connect mail server
|
||||
- 'password': password associated with 'login' to connect mail server
|
||||
- 'host': mail server hostname
|
||||
- 'port': mail server port
|
||||
- 'ssl': activate ssl to connect server
|
||||
|
||||
:Types:
|
||||
- 'login': string
|
||||
- 'password': string
|
||||
- 'host': string
|
||||
- 'port': int
|
||||
- 'ssl': boolean"""
|
||||
self.login = login
|
||||
self.password = password
|
||||
self.store_password = True
|
||||
self.host = host
|
||||
self.port = port
|
||||
self.ssl = ssl
|
||||
self.status = "offline"
|
||||
self.connection = None
|
||||
self.chat_action = RETRIEVE
|
||||
self.online_action = RETRIEVE
|
||||
self.away_action = RETRIEVE
|
||||
self.xa_action = RETRIEVE
|
||||
self.dnd_action = RETRIEVE
|
||||
self.offline_action = DO_NOTHING
|
||||
self.interval = 5
|
||||
self.lastcheck = 0
|
||||
self.default_lang_class = Lang.en
|
||||
self.waiting_password_reply = False
|
||||
self.in_error = False
|
||||
self.live_email_only = False
|
||||
self.first_check = True
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.get_type() == other.get_type() \
|
||||
and self.login == other.login \
|
||||
and (not self.store_password or self.password == other.password) \
|
||||
and self.store_password == other.store_password \
|
||||
and self.host == other.host \
|
||||
and self.port == other.port \
|
||||
and self.ssl == other.ssl \
|
||||
and self.chat_action == other.chat_action \
|
||||
and self.online_action == other.online_action \
|
||||
and self.away_action == other.away_action \
|
||||
and self.xa_action == other.xa_action \
|
||||
and self.dnd_action == other.dnd_action \
|
||||
and self.offline_action == other.offline_action \
|
||||
and self.interval == other.interval \
|
||||
and self.live_email_only == other.live_email_only
|
||||
login = StringCol(default = "")
|
||||
password = StringCol(default = None)
|
||||
host = StringCol(default = "localhost")
|
||||
port = IntCol(default = 110)
|
||||
ssl = BoolCol(default = False)
|
||||
interval = IntCol(default = 5)
|
||||
store_password = BoolCol(default = True)
|
||||
live_email_only = BoolCol(default = False)
|
||||
|
||||
def __str__(self):
|
||||
return self.get_type() + "#" + self.login + "#" + \
|
||||
(self.store_password and self.password or "/\\") + "#" \
|
||||
+ self.host + "#" + str(self.port) + "#" + str(self.chat_action) + "#" \
|
||||
+ str(self.online_action) + "#" + str(self.away_action) + "#" + \
|
||||
str(self.xa_action) + "#" + str(self.dnd_action) + "#" + \
|
||||
str(self.offline_action) + "#" + str(self.interval) + "#" + str(self.live_email_only)
|
||||
|
||||
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)
|
||||
self.__logger = logging.getLogger("jmc.model.account.MailAccount")
|
||||
self.connection = None
|
||||
self.default_lang_class = Lang.en # TODO: use String
|
||||
|
||||
def _get_register_fields(cls):
|
||||
"""See Account._get_register_fields
|
||||
"""
|
||||
def password_post_func(password):
|
||||
if password is None or password == "":
|
||||
return None
|
||||
return password
|
||||
|
||||
return Account.get_register_fields() + \
|
||||
[("login", "text-single", None, account.string_not_null_post_func, \
|
||||
account.mandatory_field), \
|
||||
("password", "text-private", None, password_post_func, \
|
||||
(lambda field_name: None)), \
|
||||
("host", "text-single", None, account.string_not_null_post_func, \
|
||||
account.mandatory_field), \
|
||||
("port", "text-single", None, account.int_post_func, \
|
||||
account.mandatory_field), \
|
||||
("ssl", "boolean", None, account.default_post_func, \
|
||||
(lambda field_name: None)), \
|
||||
("store_password", "boolean", None, account.default_post_func, \
|
||||
(lambda field_name: True)), \
|
||||
("live_email_only", "boolean", None, account.default_post_func, \
|
||||
lambda field_name: False)]
|
||||
|
||||
get_register_fields = classmethod(_get_register_fields)
|
||||
|
||||
def _get_presence_actions_fields(cls):
|
||||
"""See PresenceAccount._get_presence_actions_fields
|
||||
"""
|
||||
return {'chat_action': (cls.possibles_actions, \
|
||||
RETRIEVE), \
|
||||
'online_action': (cls.possibles_actions, \
|
||||
RETRIEVE), \
|
||||
'away_action': (cls.possibles_actions, \
|
||||
RETRIEVE), \
|
||||
'xa_action': (cls.possibles_actions, \
|
||||
RETRIEVE), \
|
||||
'dnd_action': (cls.possibles_actions, \
|
||||
RETRIEVE), \
|
||||
'offline_action': (cls.possibles_actions, \
|
||||
PresenceAccount.DO_NOTHING)}
|
||||
|
||||
get_presence_actions_fields = classmethod(_get_presence_actions_fields)
|
||||
|
||||
def get_decoded_part(self, part, charset_hint):
|
||||
content_charset = part.get_content_charset()
|
||||
result = u""
|
||||
@@ -292,26 +297,26 @@ class MailConnection(object):
|
||||
unicode(self.port)
|
||||
|
||||
def connect(self):
|
||||
pass
|
||||
raise NotImplementedError
|
||||
|
||||
def disconnect(self):
|
||||
pass
|
||||
raise NotImplementedError
|
||||
|
||||
def get_mail_list(self):
|
||||
return 0
|
||||
raise NotImplementedError
|
||||
|
||||
def get_mail(self, index):
|
||||
return None
|
||||
raise NotImplementedError
|
||||
|
||||
def get_mail_summary(self, index):
|
||||
return None
|
||||
raise NotImplementedError
|
||||
|
||||
def get_next_mail_index(self, mail_list):
|
||||
pass
|
||||
raise NotImplementedError
|
||||
|
||||
# Does not modify server state but just internal JMC state
|
||||
def mark_all_as_read(self):
|
||||
pass
|
||||
raise NotImplementedError
|
||||
|
||||
def get_action(self):
|
||||
mapping = {"online": self.online_action,
|
||||
@@ -322,29 +327,35 @@ class MailConnection(object):
|
||||
"offline": self.offline_action}
|
||||
if mapping.has_key(self.status):
|
||||
return mapping[self.status]
|
||||
return DO_NOTHING
|
||||
return PresenceAccount.DO_NOTHING
|
||||
|
||||
action = property(get_action)
|
||||
|
||||
class IMAPConnection(MailConnection):
|
||||
_logger = logging.getLogger("jmc.IMAPConnection")
|
||||
|
||||
def __init__(self, login = "", password = "", host = "", \
|
||||
port = None, ssl = False, mailbox = "INBOX"):
|
||||
if not port:
|
||||
if ssl:
|
||||
port = 993
|
||||
else:
|
||||
port = 143
|
||||
MailConnection.__init__(self, login, password, host, port, ssl)
|
||||
self.mailbox = mailbox
|
||||
class IMAPAccount(MailAccount):
|
||||
mailbox = StringCol(default = "INBOX") # TODO : set default INBOX in reg_form (use get_register_fields last field ?)
|
||||
|
||||
def __eq__(self, other):
|
||||
return MailConnection.__eq__(self, other) \
|
||||
and self.mailbox == other.mailbox
|
||||
def _get_register_fields(cls):
|
||||
"""See Account._get_register_fields
|
||||
"""
|
||||
def password_post_func(password):
|
||||
if password is None or password == "":
|
||||
return None
|
||||
return password
|
||||
|
||||
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)]
|
||||
|
||||
def __str__(self):
|
||||
return MailConnection.__str__(self) + "#" + self.mailbox
|
||||
get_register_fields = classmethod(_get_register_fields)
|
||||
|
||||
|
||||
def _init(self, *args, **kw):
|
||||
MailAccount._init(self, *args, **kw)
|
||||
self.__logger = logging.getLogger("jmc.IMAPConnection")
|
||||
|
||||
def get_type(self):
|
||||
if self.ssl:
|
||||
@@ -352,10 +363,10 @@ class IMAPConnection(MailConnection):
|
||||
return "imap"
|
||||
|
||||
def get_status(self):
|
||||
return MailConnection.get_status(self) + "/" + self.mailbox
|
||||
return MailAccount.get_status(self) + "/" + self.mailbox
|
||||
|
||||
def connect(self):
|
||||
IMAPConnection._logger.debug("Connecting to IMAP server " \
|
||||
self.__logger.debug("Connecting to IMAP server " \
|
||||
+ self.login + "@" + self.host + ":" + str(self.port) \
|
||||
+ " (" + self.mailbox + "). SSL=" \
|
||||
+ str(self.ssl))
|
||||
@@ -366,12 +377,12 @@ class IMAPConnection(MailConnection):
|
||||
self.connection.login(self.login, self.password)
|
||||
|
||||
def disconnect(self):
|
||||
IMAPConnection._logger.debug("Disconnecting from IMAP server " \
|
||||
self.__logger.debug("Disconnecting from IMAP server " \
|
||||
+ self.host)
|
||||
self.connection.logout()
|
||||
|
||||
def get_mail_list(self):
|
||||
IMAPConnection._logger.debug("Getting mail list")
|
||||
self.__logger.debug("Getting mail list")
|
||||
typ, data = self.connection.select(self.mailbox)
|
||||
typ, data = self.connection.search(None, 'RECENT')
|
||||
if typ == 'OK':
|
||||
@@ -379,7 +390,7 @@ class IMAPConnection(MailConnection):
|
||||
return None
|
||||
|
||||
def get_mail(self, index):
|
||||
IMAPConnection._logger.debug("Getting mail " + str(index))
|
||||
self.__logger.debug("Getting mail " + str(index))
|
||||
typ, data = self.connection.select(self.mailbox, True)
|
||||
typ, data = self.connection.fetch(index, '(RFC822)')
|
||||
if typ == 'OK':
|
||||
@@ -387,7 +398,7 @@ class IMAPConnection(MailConnection):
|
||||
return u"Error while fetching mail " + str(index)
|
||||
|
||||
def get_mail_summary(self, index):
|
||||
IMAPConnection._logger.debug("Getting mail summary " + str(index))
|
||||
self.__logger.debug("Getting mail summary " + str(index))
|
||||
typ, data = self.connection.select(self.mailbox, True)
|
||||
typ, data = self.connection.fetch(index, '(RFC822)')
|
||||
if typ == 'OK':
|
||||
@@ -404,29 +415,25 @@ class IMAPConnection(MailConnection):
|
||||
|
||||
type = property(get_type)
|
||||
|
||||
class POP3Connection(MailConnection):
|
||||
_logger = logging.getLogger("jmc.POP3Connection")
|
||||
|
||||
def __init__(self, login = "", password = "", host = "", \
|
||||
port = None, ssl = False):
|
||||
if not port:
|
||||
if ssl:
|
||||
port = 995
|
||||
else:
|
||||
port = 110
|
||||
MailConnection.__init__(self, login, password, host, port, ssl)
|
||||
self.__nb_mail = 0
|
||||
self.__lastmail = 0
|
||||
class POP3Account(MailAccount):
|
||||
nb_mail = IntCol(default = 0)
|
||||
lastmail = IntCol(default = 0)
|
||||
|
||||
def _init(self, *args, **kw):
|
||||
MailAccount._init(self, *args, **kw)
|
||||
self.__logger = logging.getLogger("jmc.model.account.POP3Account")
|
||||
|
||||
def get_type(self):
|
||||
if self.ssl:
|
||||
return "pop3s"
|
||||
return "pop3"
|
||||
|
||||
type = property(get_type)
|
||||
|
||||
def connect(self):
|
||||
POP3Connection._logger.debug("Connecting to POP3 server " \
|
||||
+ self.login + "@" + self.host + ":" + str(self.port)\
|
||||
+ ". SSL=" + str(self.ssl))
|
||||
self.__logger.debug("Connecting to POP3 server " \
|
||||
+ self.login + "@" + self.host + ":" + str(self.port)\
|
||||
+ ". SSL=" + str(self.ssl))
|
||||
if self.ssl:
|
||||
self.connection = MYPOP3_SSL(self.host, self.port)
|
||||
else:
|
||||
@@ -439,18 +446,18 @@ class POP3Connection(MailConnection):
|
||||
|
||||
|
||||
def disconnect(self):
|
||||
POP3Connection._logger.debug("Disconnecting from POP3 server " \
|
||||
+ self.host)
|
||||
self.__logger.debug("Disconnecting from POP3 server " \
|
||||
+ self.host)
|
||||
self.connection.quit()
|
||||
|
||||
def get_mail_list(self):
|
||||
POP3Connection._logger.debug("Getting mail list")
|
||||
self.__logger.debug("Getting mail list")
|
||||
count, size = self.connection.stat()
|
||||
self.__nb_mail = count
|
||||
self.nb_mail = count
|
||||
return [str(i) for i in range(1, count + 1)]
|
||||
|
||||
def get_mail(self, index):
|
||||
POP3Connection._logger.debug("Getting mail " + str(index))
|
||||
self.__logger.debug("Getting mail " + str(index))
|
||||
ret, data, size = self.connection.retr(index)
|
||||
try:
|
||||
self.connection.rset()
|
||||
@@ -461,7 +468,7 @@ class POP3Connection(MailConnection):
|
||||
return u"Error while fetching mail " + str(index)
|
||||
|
||||
def get_mail_summary(self, index):
|
||||
POP3Connection._logger.debug("Getting mail summary " + str(index))
|
||||
self.__logger.debug("Getting mail summary " + str(index))
|
||||
ret, data, size = self.connection.retr(index)
|
||||
try:
|
||||
self.connection.rset()
|
||||
@@ -472,16 +479,15 @@ class POP3Connection(MailConnection):
|
||||
return u"Error while fetching mail " + str(index)
|
||||
|
||||
def get_next_mail_index(self, mail_list):
|
||||
if self.__nb_mail == self.__lastmail:
|
||||
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
|
||||
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()
|
||||
self.__lastmail = self.__nb_mail
|
||||
self.lastmail = self.nb_mail
|
||||
|
||||
type = property(get_type)
|
||||
@@ -1,33 +0,0 @@
|
||||
##
|
||||
## release.py
|
||||
## Login : David Rousselie <dax@happycoders.org>
|
||||
## Started on Mon Jul 24 22:37:00 2006 dax
|
||||
## $Id$
|
||||
##
|
||||
## Copyright (C) 2006 dax
|
||||
## 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
|
||||
##
|
||||
|
||||
version = "0.2.2"
|
||||
author = "David Rousselie"
|
||||
email = "dax@happycoders.org"
|
||||
license = "GPL"
|
||||
long_description = """Jabber Mail Component
|
||||
|
||||
JMC is a jabber service to check email from POP3 and IMAP4 server and retrieve
|
||||
them or just a notification of new emails. Jabber users can register multiple
|
||||
email accounts.
|
||||
|
||||
"""
|
||||
@@ -1,297 +0,0 @@
|
||||
##
|
||||
## storage.py
|
||||
## Login : David Rousselie <dax@happycoders.org>
|
||||
## Started on Wed Jul 20 20:26:53 2005 dax
|
||||
## $Id: storage.py,v 1.1 2005/09/18 20:24:07 dax Exp $
|
||||
##
|
||||
## Copyright (C) 2005 dax
|
||||
## 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
|
||||
##
|
||||
|
||||
import os
|
||||
import re
|
||||
import os.path
|
||||
import sys
|
||||
import anydbm
|
||||
import logging
|
||||
from UserDict import UserDict
|
||||
|
||||
import jmc.email.mailconnection_factory as mailconnection_factory
|
||||
|
||||
|
||||
class Storage(UserDict):
|
||||
def __init__(self, nb_pk_fields = 1, spool_dir = ".", db_file = None):
|
||||
UserDict.__init__(self)
|
||||
self.nb_pk_fields = nb_pk_fields
|
||||
if db_file is None:
|
||||
self._spool_dir = ""
|
||||
self.set_spool_dir(spool_dir)
|
||||
self.file = self._spool_dir + "/registered.db"
|
||||
else:
|
||||
spool_dir = os.path.dirname(db_file) or "."
|
||||
self.set_spool_dir(spool_dir)
|
||||
self.file = db_file
|
||||
self._registered = self.load()
|
||||
|
||||
def __setitem__(self, pk_tuple, obj):
|
||||
# print "Adding " + "#".join(map(str, pk_tuple)) + " = " + str(obj)
|
||||
self._registered[str("#".join(map(str, pk_tuple)))] = obj
|
||||
|
||||
def __getitem__(self, pk_tuple):
|
||||
# print "Getting " + "#".join(map(str, pk_tuple))
|
||||
if len(pk_tuple) == self.nb_pk_fields:
|
||||
return self._registered[str("#".join(map(str, pk_tuple)))]
|
||||
else:
|
||||
partial_key = str("#".join(map(str, pk_tuple)))
|
||||
regexp = re.compile(partial_key)
|
||||
return [self._registered[key]
|
||||
for key in self._registered.keys()
|
||||
if regexp.search(key)]
|
||||
|
||||
def __delitem__(self, pk_tuple):
|
||||
#print "Deleting " + "#".join(map(str, pk_tuple))
|
||||
del self._registered[str("#".join(map(str, pk_tuple)))]
|
||||
|
||||
def get_spool_dir(self):
|
||||
return self._spool_dir
|
||||
|
||||
def set_spool_dir(self, spool_dir):
|
||||
self._spool_dir = spool_dir
|
||||
if not os.path.isdir(self._spool_dir):
|
||||
os.makedirs(self._spool_dir)
|
||||
|
||||
spool_dir = property(get_spool_dir, set_spool_dir)
|
||||
|
||||
def has_key(self, pk_tuple):
|
||||
if len(pk_tuple) == self.nb_pk_fields:
|
||||
return self._registered.has_key(str("#".join(map(str, pk_tuple))))
|
||||
else:
|
||||
partial_key = str("#".join(map(str, pk_tuple)))
|
||||
regexp = re.compile("^" + partial_key)
|
||||
for key in self._registered.keys():
|
||||
if regexp.search(key):
|
||||
return True
|
||||
return False
|
||||
|
||||
def keys(self, pk_tuple = None):
|
||||
if pk_tuple is None:
|
||||
return [tuple(key.split("#")) for key in self._registered.keys()]
|
||||
else:
|
||||
level = len(pk_tuple)
|
||||
partial_key = str("#".join(map(str, pk_tuple)))
|
||||
regexp = re.compile("^" + partial_key)
|
||||
result = {}
|
||||
for key in self._registered.keys():
|
||||
if regexp.search(key):
|
||||
result[key.split("#")[level]] = None
|
||||
return result.keys()
|
||||
|
||||
def dump(self):
|
||||
for pk in self._registered.keys():
|
||||
print pk + " = " + str(self._registered[pk])
|
||||
|
||||
def load(self):
|
||||
pass
|
||||
|
||||
class DBMStorage(Storage):
|
||||
_logger = logging.getLogger("jmc.utils.DBMStorage")
|
||||
|
||||
def __init__(self, nb_pk_fields = 1, spool_dir = ".", db_file = None):
|
||||
# print "DBM INIT"
|
||||
Storage.__init__(self, nb_pk_fields, spool_dir, db_file)
|
||||
|
||||
def __del__(self):
|
||||
# print "DBM STOP"
|
||||
self.sync()
|
||||
|
||||
def load(self):
|
||||
str_registered = anydbm.open(self.file, \
|
||||
'c')
|
||||
result = {}
|
||||
try:
|
||||
for pk in str_registered.keys():
|
||||
result[pk] = mailconnection_factory.str_to_mail_connection(str_registered[pk])
|
||||
except Exception, e:
|
||||
print >>sys.stderr, "Cannot load registered.db : "
|
||||
print >>sys.stderr, e
|
||||
str_registered.close()
|
||||
return result
|
||||
|
||||
def sync(self):
|
||||
#print "DBM SYNC"
|
||||
self.store()
|
||||
|
||||
def __store(self, nb_pk_fields, registered, pk):
|
||||
if nb_pk_fields > 0:
|
||||
for key in registered.keys():
|
||||
if pk:
|
||||
self.__store(nb_pk_fields - 1, \
|
||||
registered[key], pk + "#" + key)
|
||||
else:
|
||||
self.__store(nb_pk_fields - 1, \
|
||||
registered[key], key)
|
||||
else:
|
||||
self.__str_registered[pk] = str(registered)
|
||||
# print "STORING : " + pk + " = " + str(registered)
|
||||
|
||||
def store(self):
|
||||
# print "DBM STORE"
|
||||
try:
|
||||
str_registered = anydbm.open(self.file, \
|
||||
'n')
|
||||
for pk in self._registered.keys():
|
||||
str_registered[pk] = str(self._registered[pk])
|
||||
except Exception, e:
|
||||
print >>sys.stderr, "Cannot save to registered.db : "
|
||||
print >>sys.stderr, e
|
||||
str_registered.close()
|
||||
|
||||
def __setitem__(self, pk_tuple, obj):
|
||||
Storage.__setitem__(self, pk_tuple, obj)
|
||||
self.sync()
|
||||
|
||||
def __delitem__(self, pk_tuple):
|
||||
Storage.__delitem__(self, pk_tuple)
|
||||
self.sync()
|
||||
|
||||
# Do not fail if pysqlite is not installed
|
||||
try:
|
||||
from pysqlite2 import dbapi2 as sqlite
|
||||
|
||||
class SQLiteStorage(Storage):
|
||||
_logger = logging.getLogger("jmc.utils.SQLiteStorage")
|
||||
|
||||
def __init__(self, nb_pk_fields = 1, spool_dir = ".", db_file = None):
|
||||
self.__connection = None
|
||||
Storage.__init__(self, nb_pk_fields, spool_dir, db_file)
|
||||
|
||||
def create(self):
|
||||
SQLiteStorage._logger.debug("creating new Table")
|
||||
cursor = self.__connection.cursor()
|
||||
cursor.execute("""
|
||||
create table account(
|
||||
jid STRING,
|
||||
name STRING,
|
||||
type STRING,
|
||||
login STRING,
|
||||
password STRING,
|
||||
host STRING,
|
||||
port INTEGER,
|
||||
chat_action INTEGER,
|
||||
online_action INTEGER,
|
||||
away_action INTEGER,
|
||||
xa_action INTEGER,
|
||||
dnd_action INTEGER,
|
||||
offline_action INTEGER,
|
||||
interval INTEGER,
|
||||
live_email_only BOOLEAN,
|
||||
mailbox STRING,
|
||||
PRIMARY KEY(jid, name)
|
||||
)
|
||||
""")
|
||||
self.__connection.commit()
|
||||
cursor.close()
|
||||
|
||||
def __del__(self):
|
||||
self.__connection.close()
|
||||
|
||||
def sync(self):
|
||||
pass
|
||||
|
||||
def load(self):
|
||||
if not os.path.exists(self.file):
|
||||
self.__connection = sqlite.connect(self.file)
|
||||
self.create()
|
||||
else:
|
||||
self.__connection = sqlite.connect(self.file)
|
||||
cursor = self.__connection.cursor()
|
||||
cursor.execute("""select * from account""")
|
||||
result = {}
|
||||
for row in cursor.fetchall():
|
||||
# print "Creating new " + row[self.nb_pk_fields] + " connection."
|
||||
account_type = row[self.nb_pk_fields]
|
||||
account = result["#".join(row[0:self.nb_pk_fields])] = mailconnection_factory.get_new_mail_connection(account_type)
|
||||
account.login = row[self.nb_pk_fields + 1]
|
||||
account.password = row[self.nb_pk_fields + 2]
|
||||
if account.password is None:
|
||||
account.store_password = False
|
||||
else:
|
||||
account.store_password = True
|
||||
account.host = row[self.nb_pk_fields + 3]
|
||||
account.port = int(row[self.nb_pk_fields + 4])
|
||||
account.chat_action = int(row[self.nb_pk_fields + 5])
|
||||
account.online_action = int(row[self.nb_pk_fields + 6])
|
||||
account.away_action = int(row[self.nb_pk_fields + 7])
|
||||
account.xa_action = int(row[self.nb_pk_fields + 8])
|
||||
account.dnd_action = int(row[self.nb_pk_fields + 9])
|
||||
account.offline_action = int(row[self.nb_pk_fields + 10])
|
||||
account.interval = int(row[self.nb_pk_fields + 11])
|
||||
account.live_email_only = (row[self.nb_pk_fields + 12] == 1)
|
||||
if account_type[0:4] == "imap":
|
||||
account.mailbox = row[self.nb_pk_fields + 13]
|
||||
# for field_index in range(self.nb_pk_fields + 1, len(row)):
|
||||
# print "\tSetting " + str(cursor.description[field_index][0]) + \
|
||||
# " to " + str(row[field_index])
|
||||
# setattr(account,
|
||||
# cursor.description[field_index][0],
|
||||
# row[field_index])
|
||||
cursor.close()
|
||||
return result
|
||||
|
||||
def __setitem__(self, pk_tuple, obj):
|
||||
Storage.__setitem__(self, pk_tuple, obj)
|
||||
cursor = self.__connection.cursor()
|
||||
mailbox = None
|
||||
password = None
|
||||
if obj.type[0:4] == "imap":
|
||||
mailbox = obj.mailbox
|
||||
if obj.store_password == True:
|
||||
password = obj.password
|
||||
cursor.execute("""
|
||||
insert or replace into account values
|
||||
(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)
|
||||
""",
|
||||
(pk_tuple[0],
|
||||
pk_tuple[1],
|
||||
obj.type,
|
||||
obj.login,
|
||||
password,
|
||||
obj.host,
|
||||
obj.port,
|
||||
obj.chat_action,
|
||||
obj.online_action,
|
||||
obj.away_action,
|
||||
obj.xa_action,
|
||||
obj.dnd_action,
|
||||
obj.offline_action,
|
||||
obj.interval,
|
||||
obj.live_email_only,
|
||||
mailbox))
|
||||
self.__connection.commit()
|
||||
cursor.close()
|
||||
|
||||
def __delitem__(self, pk_tuple):
|
||||
Storage.__delitem__(self, pk_tuple)
|
||||
cursor = self.__connection.cursor()
|
||||
cursor.execute("""
|
||||
delete from account where jid = ? and name = ?
|
||||
""",
|
||||
(pk_tuple[0],
|
||||
pk_tuple[1]))
|
||||
self.__connection.commit()
|
||||
cursor.close()
|
||||
except ImportError:
|
||||
pass
|
||||
|
||||
Reference in New Issue
Block a user