diff --git a/states/maildb/init.sls b/states/maildb/init.sls index 2c44435..1ee670c 100644 --- a/states/maildb/init.sls +++ b/states/maildb/init.sls @@ -3,10 +3,9 @@ maildb-pkg: pkg.latest: - pkgs: - - python - - python-mysqldb - python3 - python3-mysqldb + - python3-psycopg2 maildb-user-vmail: user.present: @@ -50,4 +49,4 @@ maildb-config: - template: jinja - require: - file: maildb-apps-dir - - file: maildb-data-dir \ No newline at end of file + - file: maildb-data-dir diff --git a/states/maildb/maildb.py b/states/maildb/maildb.py index da29254..e6d617e 100644 --- a/states/maildb/maildb.py +++ b/states/maildb/maildb.py @@ -1,12 +1,12 @@ #!/usr/bin/env python3 -#-*- coding: utf-8 -*- import argparse import sys -import MySQLdb import configparser -import email from email.utils import parseaddr +import email +#import MySQLdb +import psycopg2 def DBConfig(configfile): @@ -24,7 +24,9 @@ def DBConfig(configfile): def ConnectMysqlDB(dbconfig): - conn = MySQLdb.connect(**dbconfig) + connstr = f"host={dbconfig['host']} dbname={dbconfig['db']} user={dbconfig['user']} password={dbconfig['passwd']}" + conn = psycopg2.connect(connstr) + #conn = MySQLdb.connect(**dbconfig) return conn, conn.cursor() @@ -35,7 +37,14 @@ def CloseDB(conn): def CreateDB(conn, cursor): try: - cursor.execute('''CREATE TABLE IF NOT EXISTS mail (id int(10) PRIMARY KEY AUTO_INCREMENT, sender varchar(50), recipient varchar(50), date datetime NOT NULL DEFAULT current_timestamp(), content mediumtext);''') + cursor.execute('''CREATE TABLE IF NOT EXISTS mail ( + id serial NOT NULL, + sender text NULL, + recipient text NULL, + "date" timestamp NOT NULL DEFAULT CURRENT_DATE, + "content" text NULL, + CONSTRAINT mail_pkey PRIMARY KEY (id) +)''') conn.commit() except Exception as e: print(e) @@ -43,7 +52,7 @@ def CreateDB(conn, cursor): def InsertMail(conn, cursor, sender, recipient, content): try: - cursor.execute("INSERT INTO mail (sender, recipient, content) values ('%s','%s','%s')" % (sender, recipient, content)) + cursor.execute(f"INSERT INTO mail (sender, recipient, content) values ('{sender}','{recipient}','{content}');") conn.commit() except Exception as e: print(e) @@ -54,10 +63,10 @@ def ParseMail(input_data): for line in input_data: if line != "": content += line - + msgobj = email.message_from_string(content) - sender = email.utils.parseaddr(msgobj['From'])[1] - recipient = email.utils.parseaddr(msgobj['To'])[1] + sender = parseaddr(msgobj['From'])[1] + recipient = parseaddr(msgobj['To'])[1] return sender, recipient, content