Reimplement messaging and implement bruteforce

This commit is contained in:
Mark Veidemanis 2021-12-30 22:41:28 +00:00
parent 1ee39989af
commit 21fce0e685
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
2 changed files with 80 additions and 61 deletions

View File

@ -61,7 +61,7 @@ class Agora(object):
contact_id = contact["data"]["contact_id"]
dash_tmp[contact_id] = contact
fmt = self.dashboard_hook(dash_tmp)
self.get_all_messages_hook(dash_tmp)
self.get_recent_messages()
if formatted:
return fmt
return dash_tmp
@ -134,64 +134,39 @@ class Agora(object):
return dash_tmp
def get_messages(self, contact_id, send_irc=True):
def get_recent_messages(self, send_irc=True):
"""
Get messages for a certain trade ID.
Post new messages to IRC and cache messages for the future.
:param contact_id: trade/contact ID
:return: list of messages
:rtype: list
"""
messages = self.agora.contact_messages(contact_id)
messages_tmp = []
reference = self.tx.tx_to_ref(contact_id)
if not reference:
reference = "not_set"
if "data" not in messages["response"]:
self.log.error("Malformed messages: {messages}", messages=messages)
return False
for message in messages["response"]["data"]["message_list"]:
messages_tmp.append(f"({message['sender']['username']}): {message['msg']}")
if send_irc:
if reference in self.last_messages:
if not len(self.last_messages[reference]) == len(messages_tmp):
difference = set(messages_tmp) ^ set(self.last_messages[reference])
for x in difference:
self.irc.client.msg(self.irc.client.channel, f"AUTO {reference}: {x}")
self.last_messages[reference] = messages_tmp
else:
self.last_messages[reference] = messages_tmp
for x in messages_tmp:
self.irc.client.msg(self.irc.client.channel, f"NEW {reference}: {x}")
return messages_tmp
def get_all_messages(self, send_irc=True):
"""
Get all messages for all open trades.
Wrapper for passing in dashboard.
:return: dict of lists keyed by trade/contact ID
:rtype: dict with lists
"""
dash = self.dashboard()
if dash is None:
return False
return self.get_all_messages_hook(dash, send_irc=send_irc)
def get_all_messages_hook(self, dash, send_irc=True):
"""
Get all messages for all open trades.
:return: dict of lists keyed by trade/contact ID
:rtype: dict with lists
Get recent messages.
"""
messages_tmp = {}
for contact_id in dash:
messages = self.agora.recent_messages()
if not messages["success"]:
return False
open_tx = self.tx.get_ref_map().keys()
for message in messages["response"]["data"]["message_list"]:
contact_id = message["contact_id"]
username = message["sender"]["username"]
msg = message["msg"]
if contact_id not in open_tx:
continue
reference = self.tx.tx_to_ref(contact_id)
if not reference:
reference = "not_set"
messages = self.get_messages(contact_id, send_irc=send_irc)
if not messages:
return False
messages_tmp[reference] = messages
if reference in messages_tmp:
messages_tmp[reference].append([username, msg])
else:
messages_tmp[reference] = [[username, msg]]
# Send new messages on IRC
if send_irc:
for user, message in messages_tmp[reference]:
if reference in self.last_messages:
if not [user, message] in self.last_messages[reference]:
self.irc.client.msg(self.irc.client.channel, f"AUTO {reference}: ({user}) {message}")
# Append sent messages to last_messages so we don't send them again
self.last_messages[reference].append([user, message])
else:
self.last_messages[reference] = [[user, message]]
for x in messages_tmp[reference]:
self.irc.client.msg(self.irc.client.channel, f"NEW {reference}: ({user}) {message}")
# Purge old trades from cache
for ref in list(self.last_messages): # We're removing from the list on the fly
@ -265,6 +240,8 @@ class Agora(object):
:return: data about created object or error
:rtype: dict
"""
ad = settings.Agora.Ad
ad = ad.replace("$CURRENCY$", currency)
rates = self.get_rates_all()
if currency == "USD":
min_amount = float(settings.Agora.MinUSD)
@ -298,8 +275,9 @@ class Agora(object):
def dist_countries(self):
"""
Distribute our advert into all countries listed in the config.
:return: True or False
:rtype: bool
Exits on errors.
:return: False or dict with response
:rtype: bool or dict
"""
for currency, countrycode in loads(settings.Agora.DistList):
rtrn = self.create_ad(countrycode, currency)
@ -307,6 +285,22 @@ class Agora(object):
return False
yield rtrn
def dist_bruteforce(self):
"""
Bruteforce all possible ads from the currencies and countries in the config.
Does not exit on errors.
:return: False or dict with response
:rtype: bool or dict
"""
currencies = loads(settings.Agora.BruteCurrencies)
countries = loads(settings.Agora.BruteCountries)
combinations = [[country, currency] for country in countries for currency in currencies]
for country, currency in combinations:
rtrn = self.create_ad(country, currency)
if not rtrn:
yield False
yield rtrn
def release_funds(self, contact_id):
"""
Release funds for a contact_id.

View File

@ -47,16 +47,16 @@ class IRCCommands(object):
Get all messages for all open trades or a given trade.
"""
if length == 1:
messages = agora.get_all_messages(send_irc=False)
messages = agora.get_recent_messages()
if messages is False:
msg("Error getting messages.")
return
if not messages:
msg("No messages.")
return
for message_id in messages:
for message in messages[message_id]:
msg(f"{message_id}: {message}")
for reference in messages:
for message in messages[reference]:
msg(f"{reference}: {message[0]} {message[1]}")
msg("---")
elif length == 2:
@ -83,6 +83,18 @@ class IRCCommands(object):
else:
msg(x["response"]["data"]["message"])
class brute(object):
name = "brute"
authed = True
@staticmethod
def run(cmd, spl, length, authed, msg, agora, revolut, tx):
for x in agora.dist_bruteforce():
if x["success"]:
msg(f"{x['response']['data']['message']}: {x['response']['data']['ad_id']}")
else:
msg(x["response"]["data"]["message"])
class find(object):
name = "find"
authed = True
@ -228,3 +240,16 @@ class IRCCommands(object):
def run(cmd, spl, length, authed, msg, agora, revolut, tx):
rtrn = agora.nuke_ads()
msg(dumps(rtrn))
class wallet(object):
name = "wallet"
authed = True
@staticmethod
def run(cmd, spl, length, authed, msg, agora, revolut, tx):
rtrn = agora.agora.wallet_balance_xmr()
if not rtrn["success"]:
msg("Error getting wallet details.")
return
balance = rtrn["response"]["data"]["total"]["balance"]
msg(f"Wallet balance: {balance}XMR")