Implemented idle condition

Time in seconds since the last activity by the user on any channel or query
window, including joins, parts, messages, and actions.  Notifications will
only be sent if the elapsed time is greater than this value.  A value of
0 (zero) will disable this condition.
This commit is contained in:
John Reese 2011-01-18 15:24:50 -05:00
parent c9cac29c0e
commit c85cb39c12
2 changed files with 83 additions and 4 deletions

View File

@ -90,6 +90,12 @@ Configuration
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.
* `idle = 0`
Time in seconds since the last activity by the user on any channel or query window,
including joins, parts, messages, and actions. Notifications will only be sent if the
elapsed time is greater than this value. A value of 0 (zero) will disable this condition.
* `last_active = 180`
Time in seconds since the last message sent by the user on that channel or query window.

View File

@ -40,12 +40,15 @@ class CNotifoMod : public CModule
// User agent to use
CString user_agent;
// Time last notification was sent
// Time last notification was sent for a given context
map <CString, unsigned int> last_notification_time;
// Time of last activity by user
// Time of last activity by user for a given context
map <CString, unsigned int> last_active_time;
// Time of last activity by user in any context
unsigned int idle_time;
// User object
CUser *user;
@ -59,6 +62,7 @@ class CNotifoMod : public CModule
app = "ZNC";
crlf = "\r\n";
idle_time = time(NULL);
notifo_auth = "";
notifo_host = "api.notifo.com";
notifo_url = "/v1/send_notification";
@ -74,6 +78,7 @@ class CNotifoMod : public CModule
// Notification conditions
defaults["away_only"] = "no";
defaults["client_count_less_than"] = "0";
defaults["idle"] = "0";
defaults["last_active"] = "180";
defaults["last_notification"] = "300";
defaults["nick_blacklist"] = "";
@ -206,6 +211,19 @@ class CNotifoMod : public CModule
return false;
}
/**
* Check if the idle condition is met.
*
* @return True if idle is zero or elapsed time is greater than idle
*/
bool idle()
{
unsigned int value = options["idle"].ToUInt();
unsigned int now = time(NULL);
return value == 0
|| idle_time + value < now;
}
/**
* Check if the last_active condition is met.
*
@ -274,6 +292,7 @@ class CNotifoMod : public CModule
return away_only()
&& client_count_less_than()
&& highlight(message)
&& idle()
&& last_active(context)
&& last_notification(context)
&& nick_blacklist(nick)
@ -290,6 +309,7 @@ class CNotifoMod : public CModule
{
CString context = nick.GetNick();
return away_only()
&& idle()
&& last_active(context)
&& last_notification(context)
&& nick_blacklist(nick)
@ -416,7 +436,7 @@ class CNotifoMod : public CModule
*/
EModRet OnUserMsg(CString& target, CString& message)
{
last_active_time[target] = time(NULL);
last_active_time[target] = idle_time = time(NULL);
return CONTINUE;
}
@ -428,7 +448,54 @@ class CNotifoMod : public CModule
*/
EModRet OnUserAction(CString& target, CString& message)
{
last_active_time[target] = time(NULL);
last_active_time[target] = idle_time = time(NULL);
return CONTINUE;
}
/**
* Handle the user joining a channel.
*
* @param channel Channel name
* @param key Channel key
*/
EModRet OnUserJoin(CString& channel, CString& key)
{
idle_time = time(NULL);
return CONTINUE;
}
/**
* Handle the user parting a channel.
*
* @param channel Channel name
* @param message Part message
*/
EModRet OnUserPart(CString& channel, CString& message)
{
idle_time = time(NULL);
return CONTINUE;
}
/**
* Handle the user setting the channel topic.
*
* @param channel Channel name
* @param topic Topic message
*/
EModRet OnUserTopic(CString& channel, CString& topic)
{
idle_time = time(NULL);
return CONTINUE;
}
/**
* Handle the user requesting the channel topic.
*
* @param channel Channel name
*/
EModRet OnUserTopicRequest(CString& channel)
{
idle_time = time(NULL);
return CONTINUE;
}
@ -553,6 +620,12 @@ class CNotifoMod : public CModule
table.SetCell("Condition", "client_count");
table.SetCell("Status", CString(client_count()));
unsigned int ago = time(NULL) - idle_time;
table.AddRow();
table.SetCell("Condition", "idle");
table.SetCell("Status", CString(ago) + " seconds");
PutModule(table);
}
// SEND command