From 2f3e887c83ea172f0f4195378f149659d6379974 Mon Sep 17 00:00:00 2001 From: polymath Date: Sun, 6 Oct 2024 21:10:51 +0200 Subject: [PATCH 1/2] Adding welcome functionality to the users who join a public room ! --- kaikout/__main__.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/kaikout/__main__.py b/kaikout/__main__.py index 9534e5e..5d258c9 100644 --- a/kaikout/__main__.py +++ b/kaikout/__main__.py @@ -11,6 +11,7 @@ from kaikout.utilities import Config from kaikout.xmpp.client import XmppClient from getpass import getpass from argparse import ArgumentParser +from erdhe import integrate_with_kaikout # Add this import import logging # import os # import slixmpp @@ -56,5 +57,11 @@ def main(): port = account_xmpp['client']['port'] if 'port' in account_xmpp['client'] else None XmppClient(jid, password, hostname, port, alias) + # Create the XMPP client + xmpp_client = XmppClient(jid, password, hostname, port, alias) + + # Integrate the welcome whisper feature + welcomer = integrate_with_kaikout(xmpp_client) + if __name__ == '__main__': - main() + main() \ No newline at end of file -- 2.45.2 From 706188827a771003979dc82773418610502c6ca2 Mon Sep 17 00:00:00 2001 From: polymath Date: Sun, 6 Oct 2024 21:11:53 +0200 Subject: [PATCH 2/2] erdhe Adds the function of welcoming users with a message when joined ! --- kaikout/erdhe.py | 55 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 kaikout/erdhe.py diff --git a/kaikout/erdhe.py b/kaikout/erdhe.py new file mode 100644 index 0000000..fac8028 --- /dev/null +++ b/kaikout/erdhe.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python3 + +from slixmpp import ClientXMPP +from slixmpp.exceptions import IqError, IqTimeout +import logging + +class WelcomeWhisperer: + """ + A feature class that sends welcome whispers to users joining an XMPP room. + """ + def __init__(self, xmpp_client): + self.xmpp = xmpp_client + self.rooms = {} + + # Register event handlers + self.xmpp.add_event_handler("groupchat_presence", self.handle_groupchat_presence) + + def handle_groupchat_presence(self, presence): + """ + Handle presence stanzas from chat rooms. + """ + if presence['type'] == 'available': + room = presence['from'].bare + nick = presence['from'].resource + + # Check if this is a new user joining (not already in our room roster) + if room in self.rooms and nick not in self.rooms[room]: + self.send_welcome_whisper(room, nick) + + # Update our room roster + if room not in self.rooms: + self.rooms[room] = set() + self.rooms[room].add(nick) + + def send_welcome_whisper(self, room, nick): + """ + Send a welcome whisper to a user who just joined the room. + """ + message = f"Welcome to the room {nick}, have a good time in the channel!" + try: + self.xmpp.send_message( + mto=room, + mbody=f"/w {nick} {message}", + mtype='groupchat' + ) + logging.info(f"Sent welcome whisper to {nick} in {room}") + except Exception as e: + logging.error(f"Failed to send welcome whisper: {e}") + +def integrate_with_kaikout(xmpp_client): + """ + Integrate the WelcomeWhisperer feature with an existing Kaikout XmppClient instance. + """ + welcomer = WelcomeWhisperer(xmpp_client) + return welcomer \ No newline at end of file -- 2.45.2