2009-10-02 22:10:40 +02:00
|
|
|
from config import hostname, port, username, password
|
|
|
|
import webdavlib
|
|
|
|
import urllib
|
|
|
|
import base64
|
2009-11-26 22:14:56 +01:00
|
|
|
import simplejson
|
2009-10-02 22:10:40 +02:00
|
|
|
|
2011-04-01 21:43:11 +02:00
|
|
|
import sogoLogin
|
|
|
|
|
2012-02-15 21:27:00 +01:00
|
|
|
|
2015-04-28 17:02:43 +02:00
|
|
|
DEBUG=False
|
|
|
|
#DEBUG=True
|
|
|
|
|
2012-02-15 21:27:00 +01:00
|
|
|
|
|
|
|
# must be kept in sync with SoObjects/SOGo/SOGoDefaults.plist
|
|
|
|
# this should probably be fetched magically...
|
2016-01-25 16:50:07 +01:00
|
|
|
SOGoSupportedLanguages = [ "Arabic", "Basque", "Catalan", "ChineseTaiwan", "Croatian", "Czech", "Dutch", "Danish", "Welsh", "English", "Finnish",
|
2017-01-27 03:40:52 +01:00
|
|
|
"SpanishSpain", "SpanishArgentina", "French", "German", "Hebrew",
|
2016-03-14 15:08:25 +01:00
|
|
|
"Icelandic", "Italian", "Lithuanian", "Macedonian", "Hungarian", "Portuguese", "BrazilianPortuguese",
|
2016-09-14 18:13:00 +02:00
|
|
|
"NorwegianBokmal", "NorwegianNynorsk", "Polish", "Russian", "Serbian", "Slovak",
|
2016-05-16 19:28:12 +02:00
|
|
|
"Slovenian", "Swedish", "TurkishTurkey", "Ukrainian" ];
|
2011-05-03 14:37:34 +02:00
|
|
|
daysBetweenResponseList=[1,2,3,5,7,14,21,30]
|
2009-10-02 22:10:40 +02:00
|
|
|
|
|
|
|
class HTTPPreferencesPOST (webdavlib.HTTPPOST):
|
2015-04-28 17:02:43 +02:00
|
|
|
cookie = None
|
|
|
|
|
|
|
|
def prepare_headers (self):
|
|
|
|
headers = webdavlib.HTTPPOST.prepare_headers(self)
|
|
|
|
if self.cookie:
|
|
|
|
headers["Cookie"] = self.cookie
|
|
|
|
return headers
|
2009-10-02 22:10:40 +02:00
|
|
|
|
|
|
|
class HTTPPreferencesGET (webdavlib.HTTPGET):
|
2015-04-28 17:02:43 +02:00
|
|
|
cookie = None
|
2009-10-02 22:10:40 +02:00
|
|
|
|
2015-04-28 17:02:43 +02:00
|
|
|
def prepare_headers (self):
|
|
|
|
headers = webdavlib.HTTPGET.prepare_headers(self)
|
|
|
|
if self.cookie:
|
|
|
|
headers["Cookie"] = self.cookie
|
|
|
|
return headers
|
2009-10-02 22:10:40 +02:00
|
|
|
|
|
|
|
class preferences:
|
2015-04-28 17:02:43 +02:00
|
|
|
login = username
|
|
|
|
passw = password
|
|
|
|
|
|
|
|
def __init__(self, otherLogin = None, otherPassword = None):
|
|
|
|
if otherLogin and otherPassword:
|
|
|
|
self.login = otherLogin
|
|
|
|
self.passw = otherPassword
|
|
|
|
|
|
|
|
self.client = webdavlib.WebDAVClient(hostname, port)
|
|
|
|
|
|
|
|
authCookie = sogoLogin.getAuthCookie(hostname, port, self.login, self.passw)
|
|
|
|
self.cookie = authCookie
|
|
|
|
|
|
|
|
#- If this is not set, we CAN'T save preferences
|
|
|
|
self.preferences = None
|
|
|
|
|
|
|
|
def find_key(self, d, key):
|
|
|
|
if key in d:
|
|
|
|
return d
|
|
|
|
subdicts = [a[1] for a in d.iteritems() if type(a[1]) == dict]
|
|
|
|
for subd in subdicts:
|
|
|
|
ret = self.find_key(subd, key)
|
|
|
|
if ret:
|
|
|
|
return ret
|
|
|
|
return None
|
|
|
|
|
|
|
|
def load_preferences(self):
|
|
|
|
defaults = self.get_defaults()
|
|
|
|
settings = self.get_settings()
|
|
|
|
self.preferences = {'defaults': defaults, 'settings': settings}
|
|
|
|
#print "LOAD PREFS:", self.preferences
|
|
|
|
|
|
|
|
def get(self, preference=None):
|
|
|
|
if not self.preferences:
|
|
|
|
self.load_preferences()
|
|
|
|
#- Want the whole thing
|
|
|
|
if not preference:
|
|
|
|
return self.preferences
|
|
|
|
else:
|
|
|
|
tmpdict = self.find_key(self.preferences, preference)
|
2015-05-04 19:48:55 +02:00
|
|
|
if tmpdict:
|
|
|
|
return tmpdict[preference]
|
|
|
|
else:
|
|
|
|
return None
|
2015-04-28 17:02:43 +02:00
|
|
|
|
|
|
|
def _get(self, subtype='jsonDefault', preference=None):
|
|
|
|
url = "/SOGo/so/%s/%s" % (self.login, subtype)
|
|
|
|
get = HTTPPreferencesGET(url)
|
|
|
|
get.cookie = self.cookie
|
|
|
|
self.client.execute(get)
|
2015-08-26 21:07:11 +02:00
|
|
|
if DEBUG: print "DEBUG (url):", url
|
|
|
|
if DEBUG: print "DEBUG (status):", get.response["status"]
|
|
|
|
if DEBUG: print "DEBUG (body):", get.response['body']
|
2015-04-28 17:02:43 +02:00
|
|
|
content = simplejson.loads(get.response['body'])
|
|
|
|
result = None
|
|
|
|
try:
|
|
|
|
if preference:
|
|
|
|
result = content[preference]
|
|
|
|
else:
|
|
|
|
result = content
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
return result
|
|
|
|
|
|
|
|
def get_defaults(self, preference=None):
|
|
|
|
return self._get('jsonDefaults', preference)
|
|
|
|
|
|
|
|
def get_settings(self, preference=None):
|
|
|
|
return self._get('jsonSettings', preference)
|
|
|
|
|
|
|
|
def set_nosave(self, preference, value=None):
|
|
|
|
# First check if we did a get, if not, must get first
|
|
|
|
if not self.preferences:
|
|
|
|
self.load_preferences()
|
|
|
|
|
|
|
|
# Get the right sub-dict and change the key/value
|
|
|
|
subdict = self.find_key(self.preferences, preference)
|
|
|
|
if not subdict:
|
2015-08-26 21:07:11 +02:00
|
|
|
raise AttributeError("ERROR(nosubdict): looking for %s in: %s" %(preference, str(self.preferences)))
|
2015-04-28 17:02:43 +02:00
|
|
|
subdict[preference] = value
|
|
|
|
|
|
|
|
def set(self, preference, value=None):
|
|
|
|
self.set_nosave(preference, value)
|
|
|
|
self.save()
|
|
|
|
|
|
|
|
def set_multiple(self, preferences={}):
|
|
|
|
for key, value in preferences.iteritems():
|
|
|
|
self.set_nosave(key, value)
|
|
|
|
self.save()
|
|
|
|
|
2015-05-04 19:48:55 +02:00
|
|
|
def set_or_create(self, preference, value, paths=['defaults']):
|
|
|
|
if not self.preferences:
|
|
|
|
self.load_preferences()
|
|
|
|
subdict = self.find_key(self.preferences, preference)
|
|
|
|
#- Pref is not set
|
|
|
|
if not subdict:
|
|
|
|
subdict = self.preferences
|
|
|
|
for path in paths:
|
|
|
|
subdict = subdict.setdefault(path, {})
|
|
|
|
subdict[preference] = value
|
|
|
|
|
2015-04-28 17:02:43 +02:00
|
|
|
def save(self):
|
|
|
|
url = "/SOGo/so/%s/Preferences/save" % self.login
|
|
|
|
|
|
|
|
post = HTTPPreferencesPOST(url, simplejson.dumps(self.preferences))
|
|
|
|
post.content_type = "application/json"
|
|
|
|
post.cookie = self.cookie
|
|
|
|
self.client.execute(post)
|
|
|
|
|
|
|
|
# Raise an exception if the pref wasn't properly set
|
|
|
|
if post.response["status"] != 200:
|
|
|
|
raise Exception ("failure setting prefs, (code = %d)" \
|
2009-10-02 22:10:40 +02:00
|
|
|
% post.response["status"])
|
|
|
|
|
|
|
|
|
|
|
|
# Simple main to test this class
|
|
|
|
if __name__ == "__main__":
|
2015-04-28 17:02:43 +02:00
|
|
|
p = preferences ()
|
|
|
|
print p.get ("SOGoLanguage")
|
|
|
|
p.set ("SOGoLanguage", SOGoSupportedLanguages.index("French"))
|
|
|
|
print p.get ("SOGoLanguage")
|