IRC Metadata draft support for moods

This commit is contained in:
Dpeta 2022-06-05 01:08:38 +02:00
parent 5f6cda821b
commit 298dd8eb5d
3 changed files with 60 additions and 11 deletions

55
irc.py
View file

@ -209,6 +209,13 @@ class PesterIRC(QtCore.QThread):
@QtCore.pyqtSlot() @QtCore.pyqtSlot()
def updateMood(self): def updateMood(self):
me = self.mainwindow.profile() me = self.mainwindow.profile()
# Moods via metadata
try:
helpers.metadata(self.cli, '*', "set", "mood", str(me.mood.value()))
except socket.error as e:
PchumLog.warning(e)
self.setConnectionBroken()
# Backwards compatibility
try: try:
helpers.msg(self.cli, "#pesterchum", "MOOD >%d" % (me.mood.value())) helpers.msg(self.cli, "#pesterchum", "MOOD >%d" % (me.mood.value()))
except socket.error as e: except socket.error as e:
@ -385,6 +392,13 @@ class PesterHandler(DefaultCommandHandler):
self.parent.memoReceived.emit(msg[1:msg.index("]")], handle, msg) self.parent.memoReceived.emit(msg[1:msg.index("]")], handle, msg)
else: else:
self.parent.noticeReceived.emit(handle, msg) self.parent.noticeReceived.emit(handle, msg)
def metadata(self, target, nick, key, visibility, value):
# The format of the METADATA server notication is:
# METADATA <Target> <Key> <Visibility> <Value>
if key == "mood":
mood = Mood(int(value))
self.parent.moodUpdated.emit(nick, mood)
def privmsg(self, nick, chan, msg): def privmsg(self, nick, chan, msg):
handle = nick[0:nick.find("!")] handle = nick[0:nick.find("!")]
if len(msg) == 0: if len(msg) == 0:
@ -477,12 +491,38 @@ class PesterHandler(DefaultCommandHandler):
from time import sleep from time import sleep
sleep(0.5) # To prevent TLS from dying </3 sleep(0.5) # To prevent TLS from dying </3
helpers.join(self.client, "#pesterchum") helpers.join(self.client, "#pesterchum")
# Moods via metadata
helpers.metadata(self.client, '*', 'sub', 'mood')
helpers.metadata(self.client, '*', "set", "mood", str(mymood))
# Backwards compatible moods
helpers.msg(self.client, "#pesterchum", "MOOD >%d" % (mymood)) helpers.msg(self.client, "#pesterchum", "MOOD >%d" % (mymood))
def keyvalue(self, target, handle_us, handle_owner, key, visibility, *value):
# The format of the METADATA server notication is:
# METADATA <Target> <Key> <Visibility> <Value>
if key == "mood":
mood = Mood(int(value[0]))
self.parent.moodUpdated.emit(handle_owner, mood)
def metadatasubok(self, *params):
PchumLog.info("metadatasubok: " + str(params))
def nomatchingkey(self, target, our_handle, failed_handle, key, *error):
# Try to get moods the old way if metadata fails.
PchumLog.info("nomatchingkey: " + failed_handle)
chumglub = "GETMOOD "
try:
helpers.msg(self.client, "#pesterchum", chumglub + failed_handle)
except socket.error as e:
PchumLog.warning(e)
self.parent.setConnectionBroken()
def nicknameinuse(self, server, cmd, nick, msg): def nicknameinuse(self, server, cmd, nick, msg):
newnick = "pesterClient%d" % (random.randint(100,999)) newnick = "pesterClient%d" % (random.randint(100,999))
helpers.nick(self.client, newnick) helpers.nick(self.client, newnick)
self.parent.nickCollision.emit(nick, newnick) self.parent.nickCollision.emit(nick, newnick)
def quit(self, nick, reason): def quit(self, nick, reason):
handle = nick[0:nick.find("!")] handle = nick[0:nick.find("!")]
PchumLog.info("---> recv \"QUIT %s: %s\"" % (handle, reason)) PchumLog.info("---> recv \"QUIT %s: %s\"" % (handle, reason))
@ -513,6 +553,7 @@ class PesterHandler(DefaultCommandHandler):
if handle == self.parent.mainwindow.randhandler.randNick: if handle == self.parent.mainwindow.randhandler.randNick:
self.parent.mainwindow.randhandler.setRunning(True) self.parent.mainwindow.randhandler.setRunning(True)
self.parent.moodUpdated.emit(handle, Mood("chummy")) self.parent.moodUpdated.emit(handle, Mood("chummy"))
def mode(self, op, channel, mode, *handles): def mode(self, op, channel, mode, *handles):
PchumLog.debug("op=" + str(op)) PchumLog.debug("op=" + str(op))
PchumLog.debug("channel=" + str(channel)) PchumLog.debug("channel=" + str(channel))
@ -683,20 +724,12 @@ class PesterHandler(DefaultCommandHandler):
self.client.send('PONG', server) self.client.send('PONG', server)
def getMood(self, *chums): def getMood(self, *chums):
chumglub = "GETMOOD " # Try to get mood via metadata get.
# If it fails the old code is excecuted.
for c in chums: for c in chums:
chandle = c.handle chandle = c.handle
if len(chumglub+chandle) >= 350:
try: try:
helpers.msg(self.client, "#pesterchum", chumglub) helpers.metadata(self.client, chandle, "get", "mood")
except socket.error as e:
PchumLog.warning(e)
self.parent.setConnectionBroken()
chumglub = "GETMOOD "
chumglub += chandle
if chumglub != "GETMOOD ":
try:
helpers.msg(self.client, "#pesterchum", chumglub)
except socket.error as e: except socket.error as e:
PchumLog.warning(e) PchumLog.warning(e)
self.parent.setConnectionBroken() self.parent.setConnectionBroken()

View file

@ -66,6 +66,11 @@ def ctcp(cli, handle, cmd, msg=""):
def ctcp_reply(cli, handle, cmd, msg=""): def ctcp_reply(cli, handle, cmd, msg=""):
notice(cli, str(handle), "\x01%s %s\x01" % (cmd.upper(), msg)) notice(cli, str(handle), "\x01%s %s\x01" % (cmd.upper(), msg))
def metadata(cli, target, subcommand, *params):
# IRC metadata draft specification
# https://gist.github.com/k4bek4be/92c2937cefd49990fbebd001faf2b237
cli.send("METADATA", target, subcommand, *params)
def msgrandom(cli, choices, dest, user=None): def msgrandom(cli, choices, dest, user=None):
o = "%s: " % user if user else "" o = "%s: " % user if user else ""
o += random.choice(choices) o += random.choice(choices)

View file

@ -180,6 +180,16 @@ numeric_events = {
"502": "usersdontmatch", "502": "usersdontmatch",
} }
# IRC metadata draft specification
# https://gist.github.com/k4bek4be/92c2937cefd49990fbebd001faf2b237
metadata_numeric_events = {
"761": "keyvalue",
"762": "metadataend",
"766": "nomatchingkey",
"770": "metadatasubok",
}
numeric_events.update(metadata_numeric_events)
generated_events = [ generated_events = [
# Generated events # Generated events
"dcc_connect", "dcc_connect",
@ -205,6 +215,7 @@ protocol_events = [
"quit", "quit",
"invite", "invite",
"pong", "pong",
"metadata" # Metadata specification
] ]
all_events = generated_events + protocol_events + list(numeric_events.values()) all_events = generated_events + protocol_events + list(numeric_events.values())