Skip to content

Commit

Permalink
fix: improve ping robustness and disconnect
Browse files Browse the repository at this point in the history
closes #79
  • Loading branch information
akrherz committed Mar 5, 2024
1 parent 8043ddf commit 7059456
Showing 1 changed file with 9 additions and 9 deletions.
18 changes: 9 additions & 9 deletions src/iembot/basicbot.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@
from twisted.internet.task import LoopingCall
from twisted.python import log
from twisted.python.logfile import DailyLogFile
from twisted.words.protocols.jabber import client, error, jid, xmlstream
from twisted.words.protocols.jabber import client, jid, xmlstream
from twisted.words.xish import domish, xpath
from twisted.words.xish.xmlstream import STREAM_END_EVENT

import iembot.util as botutil

Expand Down Expand Up @@ -79,6 +78,7 @@ def __init__(
self.ingestjid = None
self.conference = None
self.email_timestamps = []
self.keepalive_lc = None # Keepalive LoopingCall
fn = os.path.join(DATADIR, "startrek")
with open(fn, "r", encoding="utf-8") as fp:
self.fortunes = fp.read().split("\n%\n")
Expand Down Expand Up @@ -114,9 +114,10 @@ def authd(self, _xs=None):
self.load_chatrooms(True)
self.load_webhooks()

lc = LoopingCall(self.housekeeping)
lc.start(60)
self.xmlstream.addObserver(STREAM_END_EVENT, lambda _x: lc.stop)
# Start the keepalive loop
if self.keepalive_lc is None:
self.keepalive_lc = LoopingCall(self.housekeeping)
self.keepalive_lc.start(60)

def next_seqnum(self):
"""
Expand Down Expand Up @@ -237,9 +238,8 @@ def housekeeping(self):
"Logging out of Chat!", self, "IQ error limit reached..."
)
if self.xmlstream is not None:
# Unsure of the proper code that a client should generate
exc = error.StreamError("gone")
self.xmlstream.sendStreamError(exc)
# send a disconnect
self.xmlstream.sendFooter()
return
if self.xmlstream is None:
log.msg("xmlstream is None, not sending ping")
Expand All @@ -248,7 +248,7 @@ def housekeeping(self):
ping = domish.Element((None, "iq"))
ping["to"] = self.myjid.host
ping["type"] = "get"
pingid = f"{utcnow:%Y%m%d%H%M}"
pingid = f"{utcnow:%Y%m%d%H%M.%S}"
ping["id"] = pingid
ping.addChild(domish.Element(("urn:xmpp:ping", "ping")))
self.outstanding_pings.append(pingid)
Expand Down

0 comments on commit 7059456

Please sign in to comment.