mirror of
https://github.com/inverse-inc/sogo.git
synced 2026-08-18 13:13:31 +00:00
Style
This commit is contained in:
+121
-125
@@ -25,7 +25,7 @@ sogoUserDefaultsFile = "/home/sogo/GNUstep/Defaults/.GNUstepDefaults"
|
||||
# - Delete the socfs_ table for the username.
|
||||
|
||||
def usage():
|
||||
print """
|
||||
print """
|
||||
%s [-i imaphost] ] [-p imapport] [-s sambaprivate] username [password]
|
||||
-i imaphost IMAP host to connect to [%s]
|
||||
-p imappost IMAP port to use [%d]
|
||||
@@ -33,60 +33,59 @@ def usage():
|
||||
""" % (os.path.basename(sys.argv[0]), imaphost, imapport, sambaprivate)
|
||||
|
||||
def main():
|
||||
global sambaprivate
|
||||
global mapistorefolder
|
||||
global imaphost
|
||||
global imapport
|
||||
try:
|
||||
opts, args = getopt.getopt(sys.argv[1:], "i:p:s:")
|
||||
except getopt.GetoptError, err:
|
||||
print str(err)
|
||||
usage()
|
||||
sys.exit(2)
|
||||
global sambaprivate
|
||||
global mapistorefolder
|
||||
global imaphost
|
||||
global imapport
|
||||
try:
|
||||
opts, args = getopt.getopt(sys.argv[1:], "i:p:s:")
|
||||
except getopt.GetoptError, err:
|
||||
print str(err)
|
||||
usage()
|
||||
sys.exit(2)
|
||||
|
||||
for o, a in opts:
|
||||
if o == "-i":
|
||||
imaphost = a
|
||||
elif o == "-p":
|
||||
imapport = a
|
||||
elif o == "-s":
|
||||
sambaprivate = a
|
||||
mapistorefolder = "%s/mapistore" % (sambaprivate)
|
||||
else:
|
||||
assert False, "unhandled option"
|
||||
for o, a in opts:
|
||||
if o == "-i":
|
||||
imaphost = a
|
||||
elif o == "-p":
|
||||
imapport = a
|
||||
elif o == "-s":
|
||||
sambaprivate = a
|
||||
mapistorefolder = "%s/mapistore" % (sambaprivate)
|
||||
else:
|
||||
assert False, "unhandled option"
|
||||
|
||||
argslen = len(args)
|
||||
if (argslen == 2):
|
||||
username = args[0]
|
||||
userpass = args[1]
|
||||
elif (argslen == 1):
|
||||
username = args[0]
|
||||
userpass = username
|
||||
print "Using username as password"
|
||||
else:
|
||||
usage()
|
||||
print "Specify a user (and optionally the password)"
|
||||
sys.exit(2)
|
||||
argslen = len(args)
|
||||
if (argslen == 2):
|
||||
username = args[0]
|
||||
userpass = args[1]
|
||||
elif (argslen == 1):
|
||||
username = args[0]
|
||||
userpass = username
|
||||
print "Using username as password"
|
||||
else:
|
||||
usage()
|
||||
print "Specify a user (and optionally the password)"
|
||||
sys.exit(2)
|
||||
|
||||
# cleanup starts here
|
||||
try:
|
||||
imapCleanup(imaphost, imapport, username, userpass)
|
||||
except Exception as e:
|
||||
print "Error during imapCleanup, continuing: %s" % str(e)
|
||||
# cleanup starts here
|
||||
try:
|
||||
imapCleanup(imaphost, imapport, username, userpass)
|
||||
except Exception as e:
|
||||
print "Error during imapCleanup, continuing: %s" % str(e)
|
||||
|
||||
try:
|
||||
mapistoreCleanup(mapistorefolder, username)
|
||||
except (shutil.Error, OSError) as e:
|
||||
print "Error during mapistoreCleanup, continuing: %s" % str(e)
|
||||
try:
|
||||
mapistoreCleanup(mapistorefolder, username)
|
||||
except (shutil.Error, OSError) as e:
|
||||
print "Error during mapistoreCleanup, continuing: %s" % str(e)
|
||||
|
||||
# try:
|
||||
# pass
|
||||
# #ldbCleanup(sambaprivate, username)
|
||||
# except ldb.LdbError as e:
|
||||
# print "Error during ldbCleanup, continuing: %s" % str(e)
|
||||
|
||||
sqlCleanup(username)
|
||||
# try:
|
||||
# pass
|
||||
# #ldbCleanup(sambaprivate, username)
|
||||
# except ldb.LdbError as e:
|
||||
# print "Error during ldbCleanup, continuing: %s" % str(e)
|
||||
|
||||
sqlCleanup(username)
|
||||
|
||||
def extractmb(si):
|
||||
inparen = False
|
||||
@@ -135,8 +134,7 @@ def cleanupmb(mb, client):
|
||||
else:
|
||||
print "mailbox '%s' coult NOT be deleted (code = '%s')" % (mb, code)
|
||||
|
||||
def imapCleanup(imaphost, imapport, username, userpass) :
|
||||
|
||||
def imapCleanup(imaphost, imapport, username, userpass):
|
||||
client = imaplib.IMAP4(imaphost, imapport)
|
||||
(code, data) = client.login(username, userpass)
|
||||
if code != "OK":
|
||||
@@ -144,7 +142,7 @@ def imapCleanup(imaphost, imapport, username, userpass) :
|
||||
|
||||
print "Logged in as '%s'" % username
|
||||
|
||||
for foldername in [ "Sync Issues", "Junk E-mail" ]:
|
||||
for foldername in ("Sync Issues", "Junk E-mail"):
|
||||
(code, data) = client.list(foldername, "%")
|
||||
if code == "OK":
|
||||
for si in data:
|
||||
@@ -154,105 +152,103 @@ def imapCleanup(imaphost, imapport, username, userpass) :
|
||||
client.logout()
|
||||
|
||||
def mapistoreCleanup(mapistorefolder, username):
|
||||
|
||||
# delete the user's folder under the mapistore and under mapistore/SOGo
|
||||
shutil.rmtree("%s/%s" % (mapistorefolder, username), ignore_errors=True)
|
||||
shutil.rmtree("%s/SOGo/%s" % (mapistorefolder, username), ignore_errors=True)
|
||||
|
||||
# NOTYET
|
||||
#def ldbCleanup(sambaprivate, username):
|
||||
# conn = ldb.Ldb("%s/openchange.ldb" % (sambaprivate))
|
||||
#### entries = conn.search(None, expression="(|(cn=%s)(MAPIStoreURI=sogo://%s:*)(MAPIStoreURI=sogo://%s@*))" % (username,username,username),
|
||||
# entries = conn.search(None, expression="cn=%s" % (username),
|
||||
# scope=ldb.SCOPE_SUBTREE)
|
||||
# for entry in entries:
|
||||
# print "Deleting %s" % (entry.dn)
|
||||
# conn.delete(entry.dn)
|
||||
# NOTYET
|
||||
#def ldbCleanup(sambaprivate, username):
|
||||
# conn = ldb.Ldb("%s/openchange.ldb" % (sambaprivate))
|
||||
#### entries = conn.search(None, expression="(|(cn=%s)(MAPIStoreURI=sogo://%s:*)(MAPIStoreURI=sogo://%s@*))" % (username,username,username),
|
||||
# entries = conn.search(None, expression="cn=%s" % (username),
|
||||
# scope=ldb.SCOPE_SUBTREE)
|
||||
# for entry in entries:
|
||||
# print "Deleting %s" % (entry.dn)
|
||||
# conn.delete(entry.dn)
|
||||
|
||||
def mysqlCleanup(dbhost, dbport, dbuser, dbpass, dbname, username):
|
||||
import MySQLdb
|
||||
import MySQLdb
|
||||
|
||||
conn= MySQLdb.connect(host=dbhost, port=int(dbport), user=dbuser, passwd=dbpass, db=dbname)
|
||||
c=conn.cursor()
|
||||
tablename="socfs_%s" % (username)
|
||||
c.execute("TRUNCATE TABLE %s" % tablename)
|
||||
print "Table %s emptied"
|
||||
conn= MySQLdb.connect(host=dbhost, port=int(dbport), user=dbuser, passwd=dbpass, db=dbname)
|
||||
c=conn.cursor()
|
||||
tablename="socfs_%s" % (username)
|
||||
c.execute("TRUNCATE TABLE %s" % tablename)
|
||||
print "Table %s emptied"
|
||||
|
||||
|
||||
def postgresqlCleanup(dbhost, dbport, dbuser, dbpass, dbname, username):
|
||||
import pg
|
||||
conn = pg.connect(host=dbhost, port=int(dbport), user=dbuser, passwd=dbpass, dbname=dbname)
|
||||
tablename = "socfs_%s" % username
|
||||
conn.query("DELETE FROM %s" % tablename)
|
||||
print "table '%s' emptied" % tablename
|
||||
import pg
|
||||
conn = pg.connect(host=dbhost, port=int(dbport), user=dbuser, passwd=dbpass, dbname=dbname)
|
||||
tablename = "socfs_%s" % username
|
||||
conn.query("DELETE FROM %s" % tablename)
|
||||
print "table '%s' emptied" % tablename
|
||||
|
||||
def getOCSFolderInfoURL():
|
||||
global sogoSysDefaultsFile, sogoUserDefaultsFile
|
||||
global sogoSysDefaultsFile, sogoUserDefaultsFile
|
||||
|
||||
OCSFolderInfoURL = ""
|
||||
OCSFolderInfoURL = ""
|
||||
|
||||
# read defaults from /etc/sogo/sogo.conf
|
||||
if os.path.exists(sogoSysDefaultsFile):
|
||||
sogoDefaults = plistlib.readPlist(sogoSysDefaultsFile)
|
||||
if "OCSFolderInfoURL" in sogoDefaults:
|
||||
OCSFolderInfoURL = sogoDefaults["OCaSFolderInfoURL"]
|
||||
# read defaults from /etc/sogo/sogo.conf
|
||||
if os.path.exists(sogoSysDefaultsFile):
|
||||
sogoDefaults = plistlib.readPlist(sogoSysDefaultsFile)
|
||||
if "OCSFolderInfoURL" in sogoDefaults:
|
||||
OCSFolderInfoURL = sogoDefaults["OCaSFolderInfoURL"]
|
||||
|
||||
# defaults from user directory must have precedence
|
||||
if os.path.exists(sogoUserDefaultsFile):
|
||||
sogoDefaults = plistlib.readPlist(sogoUserDefaultsFile)
|
||||
if "sogo" in sogoDefaults and "OCSFolderInfoURL" in sogoDefaults["sogo"]:
|
||||
OCSFolderInfoURL = sogoDefaults['sogod']['OCaSFolderInfoURL']
|
||||
# defaults from user directory must have precedence
|
||||
if os.path.exists(sogoUserDefaultsFile):
|
||||
sogoDefaults = plistlib.readPlist(sogoUserDefaultsFile)
|
||||
if "sogo" in sogoDefaults and "OCSFolderInfoURL" in sogoDefaults["sogo"]:
|
||||
OCSFolderInfoURL = sogoDefaults['sogod']['OCaSFolderInfoURL']
|
||||
|
||||
return OCSFolderInfoURL
|
||||
return OCSFolderInfoURL
|
||||
|
||||
def asCSSIdentifier(inputString):
|
||||
cssEscapingCharMap = {"_" : "_U_",
|
||||
"." : "_D_",
|
||||
"#" : "_H_",
|
||||
"@" : "_A_",
|
||||
"*" : "_S_",
|
||||
":" : "_C_",
|
||||
"," : "_CO_",
|
||||
" " : "_SP_",
|
||||
"'" : "_SQ_",
|
||||
"&" : "_AM_",
|
||||
"+" : "_P_"}
|
||||
cssEscapingCharMap = {"_" : "_U_",
|
||||
"." : "_D_",
|
||||
"#" : "_H_",
|
||||
"@" : "_A_",
|
||||
"*" : "_S_",
|
||||
":" : "_C_",
|
||||
"," : "_CO_",
|
||||
" " : "_SP_",
|
||||
"'" : "_SQ_",
|
||||
"&" : "_AM_",
|
||||
"+" : "_P_"}
|
||||
|
||||
newChars = []
|
||||
newChars = []
|
||||
|
||||
for c in inputString:
|
||||
if c in cssEscapingCharMap:
|
||||
newChars.append(cssEscapingCharMap[c])
|
||||
else:
|
||||
newChars.append(c)
|
||||
for c in inputString:
|
||||
if c in cssEscapingCharMap:
|
||||
newChars.append(cssEscapingCharMap[c])
|
||||
else:
|
||||
newChars.append(c)
|
||||
|
||||
return "".join(newChars)
|
||||
return "".join(newChars)
|
||||
|
||||
def sqlCleanup(username):
|
||||
OCSFolderInfoURL = getOCSFolderInfoURL()
|
||||
if OCSFolderInfoURL is None:
|
||||
raise Exception("Couldn't fetch OCSFolderInfoURL or it is not set. the socfs_%s table should be truncated manually" % (username))
|
||||
OCSFolderInfoURL = getOCSFolderInfoURL()
|
||||
if OCSFolderInfoURL is None:
|
||||
raise Exception("Couldn't fetch OCSFolderInfoURL or it is not set. the socfs_%s table should be truncated manually" % (username))
|
||||
|
||||
# postgresql://sogo:sogo@127.0.0.1:5432/sogo/sogo_folder_info
|
||||
m = re.search("(.+)://(.+):(.+)@(.+):(\d+)/(.+)/(.+)", OCSFolderInfoURL)
|
||||
# postgresql://sogo:sogo@127.0.0.1:5432/sogo/sogo_folder_info
|
||||
m = re.search("(.+)://(.+):(.+)@(.+):(\d+)/(.+)/(.+)", OCSFolderInfoURL)
|
||||
|
||||
proto = m.group(1)
|
||||
dbuser = m.group(2)
|
||||
dbpass = m.group(3)
|
||||
dbhost = m.group(4)
|
||||
dbport = m.group(5)
|
||||
dbname = m.group(6)
|
||||
# 7 is folderinfo table
|
||||
proto = m.group(1)
|
||||
dbuser = m.group(2)
|
||||
dbpass = m.group(3)
|
||||
dbhost = m.group(4)
|
||||
dbport = m.group(5)
|
||||
dbname = m.group(6)
|
||||
# 7 is folderinfo table
|
||||
|
||||
encodedUserName = asCSSIdentifier(username)
|
||||
|
||||
if (proto == "postgresql"):
|
||||
postgresqlCleanup(dbhost, dbport, dbuser, dbpass, dbname, encodedUserName)
|
||||
elif (proto == "mysql"):
|
||||
mysqlCleanup(dbhost, dbport, dbuser, dbpass, dbname, encodedUserName)
|
||||
else:
|
||||
raise Exception("Unknown sql proto: " % (proto))
|
||||
encodedUserName = asCSSIdentifier(username)
|
||||
|
||||
if (proto == "postgresql"):
|
||||
postgresqlCleanup(dbhost, dbport, dbuser, dbpass, dbname, encodedUserName)
|
||||
elif (proto == "mysql"):
|
||||
mysqlCleanup(dbhost, dbport, dbuser, dbpass, dbname, encodedUserName)
|
||||
else:
|
||||
raise Exception("Unknown sql proto: " % (proto))
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
main()
|
||||
|
||||
Reference in New Issue
Block a user