It is an autoresponder script in which all the sending and receiving messages are done by automatically.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.3 KiB

#!/usr/bin/python3
"""Bitmessage AutoResponder"""
import base64
import configparser
import json
import os
import time
import xmlrpc.client
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/autoresponder.conf')
config.read(db_var)
apiport = config['sqlite3']['apiport']
apiinterface = config['sqlite3']['apiinterface']
apiusername = config['sqlite3']['apiusername']
apipassword = config['sqlite3']['apipassword']
api = xmlrpc.client.ServerProxy(
"http://%s:%s@%s:%s/"
% (apiusername, apipassword, apiinterface, apiport))
expire = 300
label = "Autoresponder"
def __init__(self):
self.addresses = {}
self.track = {}
def init_address(self):
"""generate random address if there isn't one"""
try:
2 years ago
addresses = json.loads(self.api.listAddresses())['addresses']
if addresses:
return
except KeyError:
pass
self.api.createRandomAddress(base64.b64encode(
self.label.encode()).decode('ascii'))
def loop(self):
"""Main loop"""
2 years ago
for i in json.loads(self.api.listAddresses())['addresses']:
self.addresses[i['address']] = True
while True:
self.process_inbox_messages()
self.cleanup_tracking()
self.delete_outbox_messages()
time.sleep(10)
def process_inbox_messages(self):
"""Loop inbox and process messages"""
inboxmessages = json.loads(self.api.getAllInboxMessages())
inbox_var = inboxmessages.get('inboxMessages')
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] = time.time() + self.expire
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:
status_ack_rcvd = values.get('status')
ackdata = values.get('ackData')
if status_ack_rcvd in ('ackreceived', 'msgsentnoackexpected'):
self.api.trashSentMessageByAckData(ackdata)
def cleanup_tracking(self):
"""Expire old tracking data"""
currenttime = time.time()
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)
if __name__ == '__main__':
BitmessageAutoresponder().init_address()
BitmessageAutoresponder().loop()