Implemented last_notification condition

Time in seconds since the last notification sent from that channel or query
window. Notifications will only be sent if the elapsed time is greater than
this value. A value of 0 (zero) will disable this condition.

Note that this condition keeps track of the last notification sent from each
channel and query window separately, so a recent PM from Joe will not affect a
notification sent from channel #foo.
This commit is contained in:
John Reese 2011-01-14 10:49:08 -05:00
parent 786e0b4928
commit 798a50f3a8
2 changed files with 43 additions and 7 deletions

View File

@ -89,6 +89,16 @@ Configuration
Notifications will only be sent if the number of connected IRC clients is less than this Notifications will only be sent if the number of connected IRC clients is less than this
value. A value of 0 (zero) will disable this condition. value. A value of 0 (zero) will disable this condition.
* `last_notification = 300`
Time in seconds since the last notification sent from that channel or query window.
Notifications will only be sent if the elapsed time is greater than this value. A value
of 0 (zero) will disable this condition.
Note that this condition keeps track of the last notification sent from each channel and
query window separately, so a recent PM from Joe will not affect a notification sent
from channel #foo.
### Notifications ### Notifications

View File

@ -14,6 +14,7 @@
#include "Chan.h" #include "Chan.h"
#include "User.h" #include "User.h"
#include "Modules.h" #include "Modules.h"
#include "time.h"
#if (!defined(VERSION_MAJOR) || !defined(VERSION_MINOR) || (VERSION_MAJOR == 0 && VERSION_MINOR < 72)) #if (!defined(VERSION_MAJOR) || !defined(VERSION_MINOR) || (VERSION_MAJOR == 0 && VERSION_MINOR < 72))
#error This module needs ZNC 0.072 or newer. #error This module needs ZNC 0.072 or newer.
@ -39,6 +40,9 @@ class CNotifoMod : public CModule
// User agent to use // User agent to use
CString user_agent; CString user_agent;
// Time last notification was sent
map <CString, unsigned int> last_notification_time;
// User object // User object
CUser *user; CUser *user;
@ -67,6 +71,7 @@ class CNotifoMod : public CModule
// Notification conditions // Notification conditions
defaults["away_only"] = "no"; defaults["away_only"] = "no";
defaults["client_count_less_than"] = "0"; defaults["client_count_less_than"] = "0";
defaults["last_notification"] = "300";
// Notification settings // Notification settings
defaults["message_length"] = "100"; defaults["message_length"] = "100";
@ -104,9 +109,13 @@ class CNotifoMod : public CModule
* *
* @param message Message to be sent to the user * @param message Message to be sent to the user
* @param title Message title to use * @param title Message title to use
* @param context Channel or nick context
*/ */
void send_message(const CString& message, const CString& title="New Message") void send_message(const CString& message, const CString& title="New Message", const CString& context="")
{ {
// Set the last notification time
last_notification_time[context] = time(NULL);
// Shorten message if needed // Shorten message if needed
unsigned int message_length = options["message_length"].ToUInt(); unsigned int message_length = options["message_length"].ToUInt();
CString short_message = message; CString short_message = message;
@ -192,6 +201,21 @@ class CNotifoMod : public CModule
return false; return false;
} }
/**
* Check if the last_notification condition is met.
*
* @param context Channel or nick context
* @return True if last_notification is zero or elapsed time is greater than last_nofication
*/
bool last_notification(const CString& context)
{
unsigned int value = options["last_notification"].ToUInt();
unsigned int now = time(NULL);
return value == 0
|| last_notification_time.count(context) < 1
|| last_notification_time[context] + value < now;
}
/** /**
* Determine when to notify the user of a channel message. * Determine when to notify the user of a channel message.
* *
@ -204,7 +228,8 @@ class CNotifoMod : public CModule
{ {
return away_only() return away_only()
&& client_count_less_than() && client_count_less_than()
&& highlight(message); && highlight(message)
&& last_notification(channel.GetName());
} }
/** /**
@ -215,7 +240,8 @@ class CNotifoMod : public CModule
*/ */
bool notify_pm(const CNick& nick) bool notify_pm(const CNick& nick)
{ {
return away_only(); return away_only()
&& last_notification(nick.GetNick());
} }
protected: protected:
@ -262,7 +288,7 @@ class CNotifoMod : public CModule
msg += ": <" + nick.GetNick(); msg += ": <" + nick.GetNick();
msg += "> " + message; msg += "> " + message;
send_message(msg, title); send_message(msg, title, channel.GetName());
} }
return CONTINUE; return CONTINUE;
@ -284,7 +310,7 @@ class CNotifoMod : public CModule
msg += ": " + nick.GetNick(); msg += ": " + nick.GetNick();
msg += " " + message; msg += " " + message;
send_message(msg, title); send_message(msg, title, channel.GetName());
} }
return CONTINUE; return CONTINUE;
@ -304,7 +330,7 @@ class CNotifoMod : public CModule
CString msg = "From " + nick.GetNick(); CString msg = "From " + nick.GetNick();
msg += ": " + message; msg += ": " + message;
send_message(msg, title); send_message(msg, title, nick.GetNick());
} }
return CONTINUE; return CONTINUE;
@ -324,7 +350,7 @@ class CNotifoMod : public CModule
CString msg = "* " + nick.GetNick(); CString msg = "* " + nick.GetNick();
msg += " " + message; msg += " " + message;
send_message(msg, title); send_message(msg, title, nick.GetNick());
} }
return CONTINUE; return CONTINUE;