From 1227a16b72b7accc6dbcb33fba7496c1ac4542cc Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Sat, 1 Jan 2022 16:39:36 +0000 Subject: [PATCH] Implement helper function to send messages on IRC --- handler/agora.py | 6 +++--- handler/irc.py | 10 ++++++++++ handler/transactions.py | 27 +++++++++------------------ 3 files changed, 22 insertions(+), 21 deletions(-) diff --git a/handler/agora.py b/handler/agora.py index b758571..4d3694f 100644 --- a/handler/agora.py +++ b/handler/agora.py @@ -94,7 +94,7 @@ class Agora(object): if reference not in current_trades: current_trades.append(reference) # Let us know there is a new trade - self.irc.client.msg(self.irc.client.channel, f"AUTO {reference}: {buyer} {amount}{currency} {amount_xmr}XMR") + self.irc.sendmsg(f"AUTO {reference}: {buyer} {amount}{currency} {amount_xmr}XMR") # Note that we have seen this reference self.last_dash.add(reference) @@ -161,13 +161,13 @@ class Agora(object): 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}") + self.irc.sendmsg(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}") + self.irc.sendmsg(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 diff --git a/handler/irc.py b/handler/irc.py index 2ac97d4..834fb46 100644 --- a/handler/irc.py +++ b/handler/irc.py @@ -188,6 +188,16 @@ class IRCBotFactory(protocol.ClientFactory): def set_tx(self, tx): self.tx = tx + def sendmsg(self, msg): + """ + Passthrough function to send a message to the channel. + """ + if self.client: + self.client.msg(self.client.channel, msg) + else: + self.log.error("Trying to send a message without connected client: {msg}", msg=msg) + return + def buildProtocol(self, addr): """ Custom override for the Twisted buildProtocol so we can access the Protocol instance. diff --git a/handler/transactions.py b/handler/transactions.py index ff92462..7aef36a 100644 --- a/handler/transactions.py +++ b/handler/transactions.py @@ -75,14 +75,12 @@ class Transactions(object): self.log.info("Transaction processed: {formatted}", formatted=dumps(to_store, indent=2)) r.hmset(f"tx.{txid}", to_store) - self.irc.client.msg( - self.irc.client.channel, f"AUTO Incoming transaction: {amount}{currency} ({reference}) - {state} - {description}" - ) + self.irc.sendmsg(f"AUTO Incoming transaction: {amount}{currency} ({reference}) - {state} - {description}") # Try getting the trade by the reference ID given stored_trade = self.get_ref(reference) if not stored_trade: self.log.info(f"No reference in DB for {reference}", reference=reference) - self.irc.client.msg(self.irc.client.channel, f"No reference in DB for {reference}") + self.irc.sendmsg(f"No reference in DB for {reference}") return amount = float(amount) @@ -90,30 +88,25 @@ class Transactions(object): # Make sure it was sent in the expected currency if not stored_trade["currency"] == currency: - self.irc.client.msg(self.irc.client.channel, f"Currency mismatch, Agora: {stored_trade['currency']} / Revolut: {currency}") + self.irc.sendmsg(f"Currency mismatch, Agora: {stored_trade['currency']} / Revolut: {currency}") return # Make sure the expected amount was sent if not stored_trade["amount"] == amount: # If the amount does not match exactly, get the min and max values for our given acceptable margins for trades min_amount, max_amount = self.agora.get_acceptable_margins(currency, amount) - self.irc.client.msg( - self.irc.client.channel, f"Amount does not match exactly, trying with margins: min: {min_amount} / max: {max_amount}" - ) + self.irc.sendmsg(f"Amount does not match exactly, trying with margins: min: {min_amount} / max: {max_amount}") if not min_amount < stored_trade["amount"] < max_amount: - self.irc.client.msg( - self.irc.client.channel, - f"Amount mismatch - not in margins: {stored_trade['amount']} (min: {min_amount} / max: {max_amount}", - ) + self.irc.sendmsg(f"Amount mismatch - not in margins: {stored_trade['amount']} (min: {min_amount} / max: {max_amount}") return # Make sure the account type was Revolut, as these are completed instantly if not account_type == "revolut": - self.irc.client.msg(self.irc.client.channel, f"Account type is not Revolut: {account_type}") + self.irc.sendmsg(f"Account type is not Revolut: {account_type}") return - self.irc.client.msg(self.irc.client.channel, f"All checks passed, releasing funds for {stored_trade['id']} / {reference}") + self.irc.sendmsg(f"All checks passed, releasing funds for {stored_trade['id']} / {reference}") rtrn = self.agora.release_funds(stored_trade["id"]) self.agora.agora.contact_message_post(stored_trade["id"], "Thanks! Releasing now :)") - self.irc.client.msg(self.irc.client.channel, dumps(rtrn)) + self.irc.sendmsg(dumps(rtrn)) def new_trade(self, trade_id, buyer, currency, amount, amount_xmr): """ @@ -123,8 +116,6 @@ class Transactions(object): reference = "".join(choices(ascii_uppercase, k=5)) reference = f"XMR-{reference}" existing_ref = r.get(f"trade.{trade_id}.reference") - # if existing_ref: - # self.irc.client.msg(self.irc.client.channel, f"Existing reference for {trade_id}: {existing_ref.decode('utf-8')}") if not existing_ref: r.set(f"trade.{trade_id}.reference", reference) to_store = { @@ -137,7 +128,7 @@ class Transactions(object): } self.log.info("Storing trade information: {info}", info=str(to_store)) r.hmset(f"trade.{reference}", to_store) - self.irc.client.msg(self.irc.client.channel, f"Generated reference for {trade_id}: {reference}") + self.irc.sendmsg(f"Generated reference for {trade_id}: {reference}") if settings.Agora.Send == "1": self.agora.agora.contact_message_post(trade_id, f"Hi! When sending the payment please use reference code: {reference}") if existing_ref: