Skip to content

Commit

Permalink
Switched to Python3
Browse files Browse the repository at this point in the history
  • Loading branch information
rsmuc committed Nov 3, 2018
1 parent e2b07e9 commit 71ca3a1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 32 deletions.
18 changes: 9 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,30 +13,30 @@ RUN apt-get update && \
apt-get install -y --no-install-recommends \
cron \
nano \
python \
python-pip \
python-setuptools \
python3 \
python3-pip \
python3-setuptools \
rsyslog \
spamassassin \
spamc \
unzip \
wget \
python-sphinx \
python3-sphinx \
lighttpd \
logrotate \
unattended-upgrades && \
\
\
# install dependencies for isbg
pip install sphinx_rtd_theme html recommonmark typing imapclient && \
# install dependencies for pushtest
pip3 install imapclient && \
\
\
# download and install isbg
cd /root && \
wget https://github.com/rsmuc/isbg/archive/all_in.zip && \
unzip all_in.zip && \
cd isbg-all_in && \
python setup.py install && \
python3 setup.py install && \
cd .. ; \
rm -Rf /root/isbg-all_in ; \
rm /root/all_in.zip ; \
Expand Down Expand Up @@ -86,7 +86,7 @@ RUN apt-get update && \
\
\
# remove tools we don't need anymore
apt-get remove -y wget python-pip python-setuptools unzip make cpanminus && \
apt-get remove -y wget python3-pip python3-setuptools unzip make cpanminus && \
apt-get autoremove -y && \
apt-get clean && \
rm -rf /var/lib/apt/lists/*
Expand All @@ -98,4 +98,4 @@ VOLUME /root/accounts

EXPOSE 80/tcp

CMD python /root/startup.py && tail -n 0 -F /var/log/*.log
CMD python3 /root/startup.py && tail -n 0 -F /var/log/*.log
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,14 @@

## Status

**not productive**
**under development**

container should be working basically

[![CodeFactor](https://www.codefactor.io/repository/github/rsmuc/antispambox/badge)](https://www.codefactor.io/repository/github/rsmuc/antispambox)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/f3c14b25f77247e19bdb8fa59190d4d5)](https://www.codacy.com/app/mail_86/antispambox?utm_source=github.com&utm_medium=referral&utm_content=rsmuc/antispambox&utm_campaign=Badge_Grade)
[![Maintainability](https://api.codeclimate.com/v1/badges/c9ef8ccecca56e361aba/maintainability)](https://codeclimate.com/github/rsmuc/antispambox/maintainability)

currently under development

## About

Expand Down Expand Up @@ -59,6 +64,8 @@ I made some modifications to ISBG and the push requests are still pending. In An
* don't use tabs in configuration file. maybe switch to json or xml
* Move custom rules to own channel or seperate from users_conf(https://wiki.apache.org/spamassassin/PublishingRuleUpdates)
* don't save the password in text file
* fix logging of startup.py
* get rid of python2

## License
MIT
Expand Down
30 changes: 17 additions & 13 deletions files/pushtest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@
handler.setFormatter(formatter)
logger.addHandler(handler)


# TODO: will raise an exception, if default configuration is installed
# read account information
try:
account = list(csv.reader(open('/root/accounts/imap_accounts.txt', 'rb'), delimiter='\t'))
account = list(csv.reader(open('/root/accounts/imap_accounts.txt', 'r'), delimiter='\t'))
HOST = account[1][0]
USERNAME = account[1][1]
PASSWORD = account[1][2]
Expand All @@ -38,9 +38,10 @@
HAMTRAIN = account[1][5]
SPAMTRAIN = account[1][6]
except IndexError:
print ("ERROR: was not able to read imap_accounts.txt.")
print("ERROR: was not able to read imap_accounts.txt.")
sys.exit(1)


def scan_spam():
logger.info("Scanning for SPAM")
p = subprocess.Popen(['/usr/local/bin/isbg', '--spamc', '--imaphost',
Expand All @@ -51,8 +52,9 @@ def scan_spam():
'--delete', '--expunge'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
(output, err) = p.communicate()


def login():
#login to server
# login to server
while True:
try:
server = IMAPClient(HOST)
Expand All @@ -73,13 +75,14 @@ def logoff(server):
logger.info(("\nIDLE mode done"))
server.logout()


def pushing(server):
"""run IMAP idle until an exception (like no response) happens"""
count = 0
while True:
try:
# Wait for up to 30 seconds for an IDLE response
responses = server.idle_check(timeout=29)
responses = server.idle_check(timeout=29)

if responses:
logger.info(responses)
Expand All @@ -93,24 +96,26 @@ def pushing(server):
scan_spam()
count = 0
raise Exception("No response")

for response in responses:
count = 0
if response[1] == "RECENT" or response[1] == "EXISTS":
if response[1].decode('UTF-8') == "RECENT" or response[1].decode('UTF-8') == "EXISTS":
scan_spam()



except KeyboardInterrupt:
break

except Exception as e:
logger.info("Push error")
count = 0
logger.info(str(e.message))
# logger.info(str(e.message))
break


# run scan_spam once
scan_spam()


# run IMAP IDLE until CTRL-C is pressed.
while True:
try:
Expand All @@ -124,10 +129,9 @@ def pushing(server):
except KeyboardInterrupt:
break
except Exception as e:
logger.info("Exception in Mainloop:")
logger.info(str(e.message))
logger.info("Exception in Mainloop")
# logger.info(str(e.message))

# logoff
logoff(server)
logger.info("Pushtest exited")

17 changes: 9 additions & 8 deletions files/startup.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import csv
import sys


def cleanup_file(filename):
"""If file exists, delete it"""
if os.path.isfile(filename):
Expand Down Expand Up @@ -32,7 +33,7 @@ def start_service(servicename):

def check_imap_configuration():
""" check if the IMAP account has already been configured"""
account = list(csv.reader(open('/root/accounts/imap_accounts.txt', 'rb'), delimiter='\t'))
account = list(csv.reader(open('/root/accounts/imap_accounts.txt', 'r'), delimiter='\t'))

try:
HOST = account[1][0]
Expand Down Expand Up @@ -66,21 +67,21 @@ def download_spamassassin_rules():
p = subprocess.Popen(['/usr/bin/sa-update', '--no-gpg', '-v', '--channelfile', '/root/sa-channels'],
stdout=subprocess.PIPE)
(output, err) = p.communicate()
if p.returncode != 0:
print("sa-update failed")
if p.returncode != 0 and p.returncode != 1:
print("ERROR: sa-update failed")
print(err)
print(output)

p = subprocess.Popen(['/usr/bin/sa-update', '--no-gpg', '-v'], stdout=subprocess.PIPE)
(output, err) = p.communicate()
if p.returncode != 0:
print("sa-update failed")
if p.returncode != 0 and p.returncode != 1:
print("ERROR: sa-update failed")
print(err)
print(output)


def start_imap_idle():
p = subprocess.Popen(['python', '/root/pushtest.py'], stdout=subprocess.PIPE)
p = subprocess.Popen(['python3', '/root/pushtest.py'], stdout=subprocess.PIPE)
(output, err) = p.communicate()
# this will usually run endless
if p.returncode != 0:
Expand Down Expand Up @@ -113,5 +114,5 @@ def start_imap_idle():
print("\n\n *** check if the imap account configuration is available")
check_imap_configuration()

print ("\n\n *** start of IMAPIDLE / PUSH")
start_imap_idle()
print("\n\n *** start of IMAPIDLE / PUSH")
start_imap_idle()

0 comments on commit 71ca3a1

Please sign in to comment.