Convert CFMail to sqlite3

master^2
Kevin Zheng 2024-03-21 20:14:53 -07:00
parent 0b0bc8440c
commit 6a26ab0f07
4 changed files with 61 additions and 46 deletions

View File

@ -1,57 +1,40 @@
# CFMail.py - CFMail class
#
# Copyright (C) 2002 Joris Bontje
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#
# The author can be reached via e-mail at jbontje@suespammers.org
#
#Updated to use new path functions in CFPython -Todd Mitchell
import os.path
import shelve
# Rewritten to use CFSqlDb
import Crossfire
import CFSqlDb as cfdb
class CFMail:
def __init__(self):
self.maildb = cfdb.open()
maildb_file = os.path.join(Crossfire.LocalDirectory(),'crossfiremail')
maildb = {}
total = 0
def init_schema(self):
self.maildb.execute("CREATE TABLE IF NOT EXISTS mail ('recipient' TEXT, 'sender' TEXT, 'date' DATE, 'type' INT, 'message' TEXT);")
def __init__(self):
self.maildb = shelve.open(self.maildb_file)
def __enter__(self):
return self
def send(self, type, toname, fromname, message):
# type: 1=mailscoll, 2=newsletter, 3=mailwarning
if not toname in self.maildb:
self.maildb[toname]=[[type,fromname,message]]
else:
temp=self.maildb[toname]
temp.append([type,fromname,message])
self.maildb[toname]=temp
def __exit__(self, exc_type, exc_value, traceback):
self.close()
def receive(self, toname):
if toname in self.maildb:
elements=self.maildb[toname]
del self.maildb[toname]
return elements
def send(self, type, toname, fromname, message):
# type: 1=mailscoll, 2=newsletter, 3=mailwarning
self.maildb.execute("INSERT INTO mail VALUES (?, ?, datetime('now'), ?, ?);", (toname, fromname, type, message))
def receive(self, toname):
c = self.maildb.cursor()
c.execute("SELECT type, sender, message FROM mail WHERE recipient=?;", (toname,))
mail = list()
for el in c.fetchall():
mail.append(el)
c.execute("DELETE FROM mail WHERE recipient=?;", (toname,))
return mail
def countmail(self, toname):
if toname in self.maildb:
return len(self.maildb[toname])
else:
return 0
def countmail(self, toname):
c = self.maildb.cursor()
c.execute("SELECT COUNT(*) FROM mail WHERE recipient=?;", (toname,))
return c.fetchone()[0]
def close(self):
self.maildb.commit()
self.maildb.close()

View File

@ -41,3 +41,5 @@ if total > 0:
Crossfire.Log(Crossfire.LogError, 'ERROR: unknown mailtype\n')
else:
activator.Write("You haven't got any mail.")
mail.close()

View File

@ -57,3 +57,5 @@ while inv:
for inv in idlist:
inv.Remove()
mail.close()

View File

@ -0,0 +1,28 @@
import os.path
import Crossfire
import CFMail
def convert_bdb(mail):
filename = 'crossfiremail.db'
path = os.path.join(Crossfire.LocalDirectory(), filename)
if os.path.isfile(path):
Crossfire.Log(Crossfire.LogInfo, "Converting crossfiremail.db (BDB)")
import berkeleydb.dbshelve as shelve
old_db = shelve.open(path, 'r')
for to in old_db.keys():
allmail = old_db[to]
toname = to.decode('ascii')
for (t, fromname, msg) in allmail:
mail.send(t, toname, fromname, msg)
old_db.close()
bak_file = os.path.join(Crossfire.LocalDirectory(), filename + '.bak')
os.rename(path, bak_file)
def main():
Crossfire.Log(Crossfire.LogInfo, "Initializing CFMail")
with CFMail.CFMail() as mail:
mail.init_schema()
convert_bdb(mail)
main()