- local address check fix
- autocreate an address if there are none
- minor reshuffling
- code quality
This commit is contained in:
Peter Šurda 2020-05-21 11:04:35 +08:00
parent 203bcdba5e
commit a059dcff30
Signed by untrusted user: PeterSurda
GPG Key ID: 0C5F50C0B5F37D87
2 changed files with 68 additions and 49 deletions

View File

@ -16,7 +16,7 @@ Installation
Clone the repo: 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. And.. you're done.
@ -26,7 +26,7 @@ Before running this script please run the bitmessages application, because if yo
Configuration Steps 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: 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 --> apienabled = true
@ -36,9 +36,4 @@ Configuration Steps
--> apipassword = abc@123 --> apipassword = abc@123
3.For running the application use 3.For running the application use
--> Python autoresponder_mailing.py --> python3 autoresponder_mailing.py

View File

@ -1,61 +1,83 @@
#!/usr/bin/python3
"""Bitmessage AutoResponder""" """Bitmessage AutoResponder"""
import base64
import configparser import configparser
import json import json
import os import os
import time import time
import xmlrpclib import xmlrpc.client
class BitmessageAutoresponder():
class BitmessageAutoresponder(): # pylint: disable=too-many-locals
"""Sending and receiving messages through autoresponder""" """Sending and receiving messages through autoresponder"""
config = configparser.ConfigParser() 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) config.read(db_var)
apiport = config['sqlite3']['apiport'] apiport = config['sqlite3']['apiport']
apiinterface = config['sqlite3']['apiinterface'] apiinterface = config['sqlite3']['apiinterface']
apiusername = config['sqlite3']['apiusername'] apiusername = config['sqlite3']['apiusername']
apipassword = config['sqlite3']['apipassword'] 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 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: while True:
inboxmessages = json.loads(self.api.getAllInboxMessages()) self.process_inbox_messages()
inbox_var = inboxmessages.get('inboxMessages') self.delete_outbox_messages()
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()
time.sleep(10) time.sleep(10)
def delete_outbox_message(self): def process_inbox_messages(self):
"""Deleting Messages from Outbox Having Status ackreceived or msgsentnoackexpected""" """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()) sentmessage = json.loads(self.api.getAllSentMessages())
sent_var = sentmessage.get('sentMessages') sent_var = sentmessage.get('sentMessages')
for values in sent_var: for values in sent_var:
@ -64,5 +86,7 @@ class BitmessageAutoresponder():
if status_ack_rcvd in ('ackreceived', 'msgsentnoackexpected'): if status_ack_rcvd in ('ackreceived', 'msgsentnoackexpected'):
self.api.trashSentMessageByAckData(ackdata) self.api.trashSentMessageByAckData(ackdata)
if __name__ == '__main__': if __name__ == '__main__':
BitmessageAutoresponder().send_autorepond_inbox_message() BitmessageAutoresponder().init_address()
BitmessageAutoresponder().loop()