From a059dcff30a6091e7f794ae796eb2b4fa4131289 Mon Sep 17 00:00:00 2001 From: Peter Surda Date: Thu, 21 May 2020 11:04:35 +0800 Subject: [PATCH] Fixes - local address check fix - autocreate an address if there are none - minor reshuffling - code quality --- README.md | 11 ++-- autoresponder_mailing.py | 106 ++++++++++++++++++++++++--------------- 2 files changed, 68 insertions(+), 49 deletions(-) diff --git a/README.md b/README.md index 4691665..41560ea 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,7 @@ Installation Clone the repo: -git clone https://git.bitmessage.org/prachiyadav/PyBitMessage_AutoResponder && cd PyBitMessage_AutoResponder +git clone https://git.bitmessage.org/Bitmessage/PyBitMessage_AutoResponder && cd PyBitMessage_AutoResponder And.. you're done. @@ -26,7 +26,7 @@ Before running this script please run the bitmessages application, because if yo Configuration Steps -1.Put the credentials.ini file into the .config directory which contains the database of bitmessage application. +1.Put the autoresponder.conf file into the .config directory which contains the database of bitmessage application. 2.To establish a connection, copy and paste these lines into the bitmessagesettings section of the keys.dat file.Note that the values "username" and "password" below are merely examples, and should be replaced by values that cannot feasibly be guessed: --> apienabled = true @@ -36,9 +36,4 @@ Configuration Steps --> apipassword = abc@123 3.For running the application use ---> Python autoresponder_mailing.py - - - - - +--> python3 autoresponder_mailing.py diff --git a/autoresponder_mailing.py b/autoresponder_mailing.py index f5879c7..f48725b 100644 --- a/autoresponder_mailing.py +++ b/autoresponder_mailing.py @@ -1,61 +1,83 @@ +#!/usr/bin/python3 """Bitmessage AutoResponder""" +import base64 import configparser import json import os import time -import xmlrpclib +import xmlrpc.client -class BitmessageAutoresponder(): + +class BitmessageAutoresponder(): # pylint: disable=too-many-locals """Sending and receiving messages through autoresponder""" config = configparser.ConfigParser() - db_var = os.path.join(os.environ["HOME"], '.config/PyBitmessage/credentials.ini') + db_var = os.path.join(os.environ["HOME"], + '.config/PyBitmessage/autoresponder.conf') config.read(db_var) apiport = config['sqlite3']['apiport'] apiinterface = config['sqlite3']['apiinterface'] apiusername = config['sqlite3']['apiusername'] apipassword = config['sqlite3']['apipassword'] - api = xmlrpclib.ServerProxy("http://%s:%s@%s:%s/"%(apiusername, apipassword, apiinterface, apiport)) + api = xmlrpc.client.ServerProxy( + "http://%s:%s@%s:%s/" + % (apiusername, apipassword, apiinterface, apiport)) expire = 300 - myaddress_path = os.path.join(os.environ["HOME"],'.config/PyBitmessage/keys.dat') + label = "Autoresponder" + + def __init__(self): + self.addresses = {} + self.track = {} + + def init_address(self): + """generate random address if there isn't one""" + try: + addresses = json.loads(self.api.listAddresses2())['addresses'] + if addresses: + return + except KeyError: + pass + self.api.createRandomAddress(base64.b64encode(self.label)) + + def loop(self): + """Main loop""" + for i in json.loads(self.api.listAddresses2())['addresses']: + self.addresses[i['address']] = True - def send_autorepond_inbox_message(self): - """Sending Auto Message To The Recipient""" - track = {} - open_file = open(self.myaddress_path) - get_file = open_file.read() - split_file = get_file.split("\n") - for lines in split_file: - if len(lines) == 39: - if "[" in lines: - set_address=lines.strip("[]") while True: - inboxmessages = json.loads(self.api.getAllInboxMessages()) - inbox_var = inboxmessages.get('inboxMessages') - currenttime = time.time() - for values in inbox_var: - msgid = values.get('msgid') - toaddress = values.get('fromAddress') - fromaddress = values.get('toAddress') - subject = values.get('subject') - message = values.get('message') - try: - if time.time() < track[toaddress]: - continue - except KeyError: - pass - if set_address == toaddress: - continue - self.api.sendMessage(toaddress, fromaddress, subject, message) - self.api.trashMessage(msgid) - track[toaddress] = currenttime + self.expire - list_of_time_track = list(key for (key, value) in track.items() if value < currenttime) - for address in list_of_time_track: - track.pop(address) - self.delete_outbox_message() + self.process_inbox_messages() + self.delete_outbox_messages() time.sleep(10) - def delete_outbox_message(self): - """Deleting Messages from Outbox Having Status ackreceived or msgsentnoackexpected""" + def process_inbox_messages(self): + """Loop inbox and process messages""" + inboxmessages = json.loads(self.api.getAllInboxMessages()) + inbox_var = inboxmessages.get('inboxMessages') + currenttime = time.time() + for values in inbox_var: + msgid = values.get('msgid') + toaddress = values.get('fromAddress') + fromaddress = values.get('toAddress') + subject = values.get('subject') + message = values.get('message') + try: + if time.time() < self.track[toaddress]: + continue + except KeyError: + pass + if toaddress in self.addresses: + continue + self.api.sendMessage(toaddress, fromaddress, subject, message) + self.api.trashMessage(msgid) + self.track[toaddress] = currenttime + self.expire + list_of_time_track = list( + key for (key, value) + in self.track.items() if value < currenttime) + for address in list_of_time_track: + self.track.pop(address) + + def delete_outbox_messages(self): + """Deleting Messages from outbox having status + ackreceived or msgsentnoackexpected""" sentmessage = json.loads(self.api.getAllSentMessages()) sent_var = sentmessage.get('sentMessages') for values in sent_var: @@ -64,5 +86,7 @@ class BitmessageAutoresponder(): if status_ack_rcvd in ('ackreceived', 'msgsentnoackexpected'): self.api.trashSentMessageByAckData(ackdata) + if __name__ == '__main__': - BitmessageAutoresponder().send_autorepond_inbox_message() \ No newline at end of file + BitmessageAutoresponder().init_address() + BitmessageAutoresponder().loop()