diff --git a/Greet/plugin.py b/Greet/plugin.py index 274b6e8..2635a97 100644 --- a/Greet/plugin.py +++ b/Greet/plugin.py @@ -20,17 +20,19 @@ class Greet(callbacks.Plugin): """ A plugin to greet users when they join a channel. Features flood protection and timeout between greetings. + Only greets users the first time they join during a session, + and resets when the greeting message changes. """ def __init__(self, irc): self.__parent = super(Greet, self) self.__parent.__init__(irc) - # Dictionary to store last greeting times per user - self.lastGreetTime = {} + # Dictionary to track which users have been greeted in this session + self.greetedUsers = {} - # Default cooldown between greetings for the same user (in seconds) - self.userCooldown = 300 # 5 minutes + # Dictionary to store the greeting message used for each user + self.userGreetingVersion = {} # Debug mode self.debugMode = False @@ -44,7 +46,8 @@ class Greet(callbacks.Plugin): # Log event for debugging if self.debugMode: - self._logDebug(irc, f"JOIN event detected: {nick} joined {channel}") + self._logDebug(irc, f"JOIN event detected: {nick} joined +{channel}") # Don't greet ourselves if nick == irc.nick: @@ -65,29 +68,44 @@ class Greet(callbacks.Plugin): self._logDebug(irc, f"No greeting message set for {channel}") return - # Check if this user was greeted recently + # Check if we've already greeted this user with this greeting userKey = f"{nick.lower()}@{channel}" - now = time.time() - if userKey in self.lastGreetTime: - lastTime = self.lastGreetTime[userKey] - if now - lastTime < self.userCooldown: + + if userKey in self.greetedUsers and channel in +self.greetedUsers[userKey]: + # If the greeting has changed since we last greeted this user, we + # should greet them again + lastGreetingUsed = self.userGreetingVersion.get(userKey, "") + if greeting == lastGreetingUsed: if self.debugMode: - self._logDebug(irc, f"User {nick} was greeted too recently") + self._logDebug(irc, f"User {nick} already greeted in this +session with current greeting") return + else: + if self.debugMode: + self._logDebug(irc, f"Greeting for {channel} has changed, +will greet {nick} again") # Get the timeout timeout = self.registryValue('timeout', channel) or 3 if self.debugMode: - self._logDebug(irc, f"Will greet in {channel} after {timeout}s: {greeting}") + self._logDebug(irc, f"Will greet in {channel} after {timeout}s: +{greeting}") - # Update last greet time for this user - self.lastGreetTime[userKey] = now + # Mark this user as greeted in this channel + if userKey not in self.greetedUsers: + self.greetedUsers[userKey] = set() + self.greetedUsers[userKey].add(channel) + + # Store the greeting version we're using for this user + self.userGreetingVersion[userKey] = greeting # Use a closure to capture the current values def sendGreeting(channelToGreet=channel, greetingMsg=greeting): if self.debugMode: - self._logDebug(irc, f"Sending greeting in {channelToGreet}: {greetingMsg}") + self._logDebug(irc, f"Sending greeting in {channelToGreet}: +{greetingMsg}") try: # Make sure the channel is still valid when we send the message if channelToGreet in irc.state.channels: @@ -104,13 +122,15 @@ class Greet(callbacks.Plugin): try: # Send to the owner via private message instead of to a channel for owner in self.registryValue('owners') or []: - irc.queueMsg(ircmsgs.privmsg(owner, f"[GREET-DEBUG] {message}")) + irc.queueMsg(ircmsgs.privmsg(owner, f"[GREET-DEBUG] +{message}")) return # Fallback: send to the first available channel for channel in list(irc.state.channels): try: - irc.queueMsg(ircmsgs.privmsg(channel, f"[GREET-DEBUG] {message}")) + irc.queueMsg(ircmsgs.privmsg(channel, f"[GREET-DEBUG] +{message}")) break except: continue @@ -165,6 +185,27 @@ class Greet(callbacks.Plugin): else: irc.replySuccess("Debug mode disabled") + @wrap(['channel']) + def resetgreethistory(self, irc, msg, args, channel): + """[] + + Resets the greeting history for the channel, causing all users to be +greeted again + on their next join (even if they were greeted before). + """ + # Clear the greeting history for this channel + for userKey in list(self.greetedUsers.keys()): + if channel in self.greetedUsers[userKey]: + self.greetedUsers[userKey].remove(channel) + # If this user has no more channels, remove the entry + # completely + if not self.greetedUsers[userKey]: + del self.greetedUsers[userKey] + if userKey in self.userGreetingVersion: + del self.userGreetingVersion[userKey] + + irc.replySuccess(f"Greeting history for {channel} has been reset.") + @wrap(['channel']) def greeton(self, irc, msg, args, channel): """[] @@ -174,9 +215,11 @@ class Greet(callbacks.Plugin): self.setRegistryValue('enabled', True, channel) greeting = self.registryValue('message', channel) if greeting: - irc.replySuccess(f"Greetings enabled for {channel}. Current greeting: \"{greeting}\"") + irc.replySuccess(f"Greetings enabled for {channel}. Current +greeting: \"{greeting}\"") else: - irc.error(f"Greetings enabled for {channel}, but no greeting message is set.") + irc.error(f"Greetings enabled for {channel}, but no greeting +message is set.") @wrap(['channel']) def greetoff(self, irc, msg, args, channel): @@ -195,7 +238,8 @@ class Greet(callbacks.Plugin): """ self.setRegistryValue('message', '', channel) self.setRegistryValue('enabled', False, channel) - irc.replySuccess(f"Greeting message cleared and disabled for {channel}.") + irc.replySuccess(f"Greeting message cleared and disabled for +{channel}.") @wrap(['channel']) def greetstatus(self, irc, msg, args, channel): @@ -207,12 +251,20 @@ class Greet(callbacks.Plugin): message = self.registryValue('message', channel) timeout = self.registryValue('timeout', channel) or 3 + # Count how many users have been greeted in this channel + greetedCount = sum(1 for userKey in self.greetedUsers if channel in +self.greetedUsers[userKey]) + if enabled and message: - irc.reply(f"Greetings are enabled for {channel}. Timeout: {timeout} seconds. Message: \"{message}\"") + irc.reply(f"Greetings are enabled for {channel}. Timeout: {timeout} +seconds. Message: \"{message}\"") + irc.reply(f"Users greeted in this session: {greetedCount}") elif enabled and not message: - irc.reply(f"Greetings are enabled for {channel}, but no message is set.") + irc.reply(f"Greetings are enabled for {channel}, but no message is +set.") else: - irc.reply(f"Greetings are disabled for {channel}. Message: \"{message or 'None'}\"") + irc.reply(f"Greetings are disabled for {channel}. Message: +\"{message or 'None'}\"") @wrap(['channel', 'positiveInt']) def greettimeout(self, irc, msg, args, channel, seconds): @@ -222,15 +274,18 @@ class Greet(callbacks.Plugin): Default is 3 seconds. """ self.setRegistryValue('timeout', seconds, channel) - irc.replySuccess(f"Greeting timeout for {channel} set to {seconds} seconds.") + irc.replySuccess(f"Greeting timeout for {channel} set to {seconds} +seconds.") @wrap(['channel', 'text']) def greet(self, irc, msg, args, channel, text): """[] Sets the greeting message for a channel. - The greeting will be sent when users join the channel, with flood -protection. + The greeting will be sent when users join the channel for the first +time + in a session. If the greeting message changes, users will be greeted +again. To manage greetings, use the other commands: greeton - Enables greetings for the channel @@ -241,13 +296,36 @@ protection. greettrigger - Simulates a join event with timeout (owner only) greetstatus - Shows current status and message greettimeout - Sets delay before greeting + resetgreethistory - Resets who has been greeted, greeting everyone +again Examples: greet #channel Welcome to our channel! """ + oldGreeting = self.registryValue('message', channel) self.setRegistryValue('message', text, channel) self.setRegistryValue('enabled', True, channel) - irc.replySuccess(f"Greeting message for {channel} set to: \"{text}\"") + + # If greeting message has changed, reset who has been greeted with this + # message + if oldGreeting != text: + # Mark that the greeting has changed by removing this channel from + # all users' greeted lists + for userKey in list(self.greetedUsers.keys()): + if channel in self.greetedUsers[userKey]: + self.greetedUsers[userKey].remove(channel) + # If this user has no more channels, remove the entry + # completely + if not self.greetedUsers[userKey]: + del self.greetedUsers[userKey] + if userKey in self.userGreetingVersion: + del self.userGreetingVersion[userKey] + + irc.replySuccess(f"Greeting message for {channel} set to: +\"{text}\" (greeting history reset)") + else: + irc.replySuccess(f"Greeting message for {channel} set to: +\"{text}\"") Class = Greet