How do I make a Python plug-in for Hexchat thread-safe?

289 views Asked by At

I tried to write a simple Python plug-in for the Hexchat IRC client. However, it seems it is not thread-safe and causes that Hexchat crashes.

__module_name__ = "Demo"
__module_version__ = "1.0"
__module_description__ = "Demo plug-in"

import hexchat
from threading import Thread
from time import sleep

def sleep10seconds():
    # In the real script, I open here an HTTP URL on the web server that is
    # running on the IRC server host to un-ban the IP address.    
    hexchat.prnt("before sleep")
    sleep(10)
    hexchat.prnt("sleep end")
    # Here I reconnect to the IRC server. If the un-ban was
    # successful, everything should be fine now. If it failed,
    # a new "Disconnect" print event happens, and the whole process
    # (disconnected_cb -> sleep10seconds) happens again.
    hexchat.command("server " + hexchat.get_info("host"))
    return

# This function is called when a "disconnected" print event was found
def disconnected_cb(word, word_eol, userdata):
    # The sleep() needs to run in a separate thread. Otherwise, the UI
    # freezes for the time of the sleep()
    myThread = Thread(target=sleep10seconds)
    myThread.start()
    return hexchat.EAT_NONE

hexchat.command("discon")
hexchat.command("server " + hexchat.get_info("host"))
hexchat.hook_print("Disconnected", disconnected_cb)

I read some articles about thread safety, but I'm not a developer. I couldn't figure out what I need to change. Can you please help me? Thanks.

EDIT: Some additonal information about what I try to accomplish: The plug-in will be a temporary workaround for an old and very weird installation I have here. In a branch office, we have a fabrication machine. A long time ago, someone wanted to monitor this machine. The company used IRC for internal communication and he decided to set up a machine with Red Hat Linux 8 (yes RHL, not RHEL) for monitoring. He wrote a software that retrieves errors from the fabrication machine and post them to a channel of an IRC server running on the same host.

The branch office is connected to our office via VPN, but the branch office has only a slow internet connection and it's was not very stable recently. This causes that the connection to the remote IRC server sometimes gets lost several times in a short period of time and, after some reconnects, the IRC server temporarily bans the client IP address. Consequently, these clients can't monitor the status. The developer also created a CGI script to un-ban the IP of the client that opens the URL to the CGI script.

The whole IRC host and the other software that runs on this host is mostly a blackbox that nobody wants to touch. For this reason, a Hexchat plug-in seems to be a good client-side workaround until the fabrication machine and the whole setup will be replaced (hopefully) before the end of next year.

When the IRC server bans and disconnects an IP address, the plug-in should open the CGI script address via HTTP to un-ban it. The CGI script only returns "done" as soon as you open the URL, but the un-ban process is not finished at this time. Additionally, it often fails (~50%). That's why the plug-in should wait 10 seconds to be sure that the background process is done and then reconnect to the server. If the ban is still active, for example because the background process failed on the server, the client will be disconnected again, another "disconnected" event happens, and the whole process of the plug-in will start over.

Sorry for the long explanation, but this old setup I found here is too weird and it required some more words to explain why I want to write such a Hexchat plug-in. However, the plug-in will simplify some people's life until we have a new fabrication machine with a professional monitoring solution one day. :-)

0

There are 0 answers