226 lines
7.6 KiB
Python
226 lines
7.6 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
"""
|
|
SAT: a jabber client
|
|
Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org)
|
|
|
|
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 3 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, see <http://www.gnu.org/licenses/>.
|
|
"""
|
|
|
|
from __future__ import with_statement
|
|
|
|
import os.path
|
|
import time
|
|
import pickle
|
|
from xml.dom import minidom
|
|
from logging import debug, info, error
|
|
import pdb
|
|
|
|
const_SAVEFILE_PARAM=os.path.expanduser("~/.sat_param.save")
|
|
const_SAVEFILE_HISTORY=os.path.expanduser("~/.sat_history.save")
|
|
|
|
class Param():
|
|
"""This class manage parameter with xml"""
|
|
|
|
#TODO: mettre Watched dans un plugin
|
|
default_xml = """
|
|
<params>
|
|
<category name="Connection">
|
|
<param name="JabberID" value="goffi@necton2.int/TestScript" type="string" />
|
|
<param name="Password" value="toto" type="password" />
|
|
<param name="Server" value="necton2.int" type="string" />
|
|
</category>
|
|
<category name="File Transfert">
|
|
<param name="IP" value="192.168.0.10" type="string" />
|
|
<param name="Port" value="28915" type="string" />
|
|
</category>
|
|
<category name="Misc">
|
|
<param name="Watched" value="test@Jabber.goffi.int" type="string" />
|
|
</category>
|
|
</params>
|
|
"""
|
|
|
|
def load_default_params(self):
|
|
self.dom = minidom.parseString(Param.default_xml)
|
|
|
|
def __init__(self):
|
|
debug("Parameters init")
|
|
self.load_default_params()
|
|
|
|
|
|
def getParamV(self, name, category):
|
|
"""Helper method to get the value in the good type
|
|
getParamV stands for GetParamValue"""
|
|
node = self.__getParamNode(name, category)
|
|
if not node:
|
|
error("Requested param [%s] in category [%s] doesn't exist !" , name, category)
|
|
return none
|
|
return node.getAttribute("value")
|
|
|
|
def getParams(self):
|
|
"""Return the whole params XML"""
|
|
return self.dom.toxml()
|
|
|
|
def getParamsForCategory(self, category):
|
|
"""Return node's xml for selected category"""
|
|
for node in self.dom.documentElement.childNodes:
|
|
if node.nodeName == "category" and node.getAttribute("name") == category:
|
|
return node.toxml()
|
|
return "<category />"
|
|
|
|
def __getParamNode(self, name, category):
|
|
for node in self.dom.documentElement.childNodes:
|
|
if node.nodeName == "category" and node.getAttribute("name") == category:
|
|
params = node.getElementsByTagName("param")
|
|
for param in params:
|
|
if param.getAttribute("name") == name:
|
|
return param
|
|
return None
|
|
|
|
def getParamsCategories(self):
|
|
"""return the categories availables"""
|
|
categories=[]
|
|
for cat in self.dom.getElementsByTagName("category"):
|
|
categories.append(cat.getAttribute("name"))
|
|
return categories
|
|
|
|
def setParam(self, name, value, category):
|
|
node = self.__getParamNode(name, category)
|
|
if not node:
|
|
return #TODO: throw an error
|
|
node.setAttribute("value", value)
|
|
|
|
"""def createParam (self, name, value, type, category):
|
|
### TODO: add desciption in params
|
|
if not self.params.has_key(category):
|
|
self.params[category]={}
|
|
self.params[category][name]=[value,type];"""
|
|
|
|
class Memory:
|
|
"""This class manage all persistent informations"""
|
|
|
|
def __init__(self):
|
|
info ("Memory manager init")
|
|
self.contact={}
|
|
self.presenceStatus={}
|
|
self.params=Param()
|
|
self.history={}
|
|
self.disco={} #XXX: maybe best in a separate class
|
|
self.features={}
|
|
self.load()
|
|
|
|
def load(self):
|
|
"""Load parameters and all memory things from file/db"""
|
|
|
|
#first parameters
|
|
if os.path.exists(const_SAVEFILE_PARAM):
|
|
try:
|
|
with open(const_SAVEFILE_PARAM, 'r') as params_pickle:
|
|
self.params=pickle.load(params_pickle)
|
|
debug("params loaded")
|
|
except:
|
|
error ("Can't load params !")
|
|
self.params.load_default_params()
|
|
else:
|
|
self.params.load_default_params()
|
|
|
|
#history
|
|
if os.path.exists(const_SAVEFILE_HISTORY):
|
|
try:
|
|
with open(const_SAVEFILE_HISTORY, 'r') as history_pickle:
|
|
self.history=pickle.load(history_pickle)
|
|
debug("history loaded")
|
|
except:
|
|
error ("Can't load history !")
|
|
|
|
|
|
def save(self):
|
|
"""Save parameters and all memory things to file/db"""
|
|
with open(const_SAVEFILE_PARAM, 'w') as params_pickle:
|
|
pickle.dump(self.params, params_pickle)
|
|
with open(const_SAVEFILE_HISTORY, 'w') as history_pickle:
|
|
pickle.dump(self.history, history_pickle)
|
|
|
|
def addToHistory(self, me_jid, from_jid, to_jid, type, message):
|
|
me_short=me_jid.userhost()
|
|
from_short=from_jid.userhost()
|
|
to_short=to_jid.userhost()
|
|
|
|
if from_jid==me_jid:
|
|
key=to_short
|
|
else:
|
|
key=from_short
|
|
|
|
if not self.history.has_key(me_short):
|
|
self.history[me_short]={}
|
|
if not self.history[me_short].has_key(key):
|
|
self.history[me_short][key]={}
|
|
|
|
self.history[me_short][key][int(time.time())] = (from_short, message)
|
|
|
|
def getHistory(self, from_jid, to_jid, size):
|
|
ret={}
|
|
if not self.history.has_key(from_jid):
|
|
error("source JID not found !")
|
|
#TODO: throw an error here
|
|
return {}
|
|
if not self.history[from_jid].has_key(to_jid):
|
|
error("dest JID not found !")
|
|
#TODO: throw an error here
|
|
return {}
|
|
stamps=self.history[from_jid][to_jid].keys()
|
|
stamps.sort()
|
|
for stamp in stamps[-size:]:
|
|
ret[stamp]=self.history[from_jid][to_jid][stamp]
|
|
|
|
return ret
|
|
|
|
def addContact(self, JID, attributes, groups):
|
|
debug("Memory addContact: %s",JID)
|
|
assert(isinstance(attributes,dict))
|
|
assert(isinstance(groups,set))
|
|
self.contact[JID]=[attributes, groups]
|
|
|
|
def addPresenceStatus(self, jid, type, show, status, priority):
|
|
self.presenceStatus[jid]=[type, show, status, priority]
|
|
|
|
def getContacts(self):
|
|
debug ("Memory getContact OK (%s)", self.contact)
|
|
ret=[]
|
|
for contact in self.contact:
|
|
ret.append([contact] + [self.contact[contact][0]] + [self.contact[contact][1]]) #very ugly I know !
|
|
return ret
|
|
|
|
def getPresenceStatus(self):
|
|
status=[]
|
|
for contact, contactStatus in self.presenceStatus.items():
|
|
status.append([contact]+contactStatus)
|
|
debug ("Memory getPresenceStatus (%s)", status)
|
|
return status
|
|
|
|
def getParamV(self, name, category):
|
|
return self.params.getParamV(name, category)
|
|
|
|
def getParams(self):
|
|
return self.params.getParams()
|
|
|
|
def getParamsForCategory(self, category):
|
|
return self.params.getParamsForCategory(category)
|
|
|
|
def getParamsCategories(self):
|
|
return self.params.getParamsCategories()
|
|
|
|
def setParam(self, name, value, category):
|
|
return self.params.setParam(name, value, category)
|