1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-11-03 19:37:16 -05:00

Implemented /autoaway mode idle

This commit is contained in:
James Booth 2012-12-01 17:46:25 +00:00
parent f7d0bcba4c
commit fae848ea64
6 changed files with 46 additions and 16 deletions

View File

@ -546,7 +546,7 @@ static struct cmd_t setting_commands[] =
" away - Sends an away presence.", " away - Sends an away presence.",
" off - Disabled (default).", " off - Disabled (default).",
"time : Number of minutes before the presence change is sent, the default is 15.", "time : Number of minutes before the presence change is sent, the default is 15.",
"message : Optional message to send with the presence change.", "message : Optional message to send with the presence change, use no value to clear.",
"check : on|off, when enabled, checks for activity and sends online presence, default is 'on'.", "check : on|off, when enabled, checks for activity and sends online presence, default is 'on'.",
"", "",
"Example: /autoaway mode idle", "Example: /autoaway mode idle",
@ -1765,8 +1765,13 @@ _cmd_set_autoaway(gchar **args, struct cmd_help_t help)
} }
if (strcmp(setting, "message") == 0) { if (strcmp(setting, "message") == 0) {
if (strcmp(value, "off") == 0) {
prefs_set_autoaway_message(NULL);
cons_show("Auto away message cleared.");
} else {
prefs_set_autoaway_message(value); prefs_set_autoaway_message(value);
cons_show("Auto away message set to: \"%s\".", value); cons_show("Auto away message set to: \"%s\".", value);
}
return TRUE; return TRUE;
} }
@ -1789,7 +1794,7 @@ _cmd_set_priority(gchar **args, struct cmd_help_t help)
char *status = jabber_get_status(); char *status = jabber_get_status();
prefs_set_priority((int)intval); prefs_set_priority((int)intval);
// update presence with new priority // update presence with new priority
jabber_update_presence(jabber_get_presence(), status); jabber_update_presence(jabber_get_presence(), status, 0);
if (status != NULL) if (status != NULL)
free(status); free(status);
cons_show("Priority set to %d.", intval); cons_show("Priority set to %d.", intval);
@ -1899,7 +1904,7 @@ _update_presence(const jabber_presence_t presence,
if (conn_status != JABBER_CONNECTED) { if (conn_status != JABBER_CONNECTED) {
cons_show("You are not currently connected."); cons_show("You are not currently connected.");
} else { } else {
jabber_update_presence(presence, msg); jabber_update_presence(presence, msg, 0);
title_bar_set_status(presence); title_bar_set_status(presence);
if (msg != NULL) { if (msg != NULL) {
cons_show("Status set to %s, \"%s\"", show, msg); cons_show("Status set to %s, \"%s\"", show, msg);

View File

@ -324,7 +324,8 @@ jabber_leave_chat_room(const char * const room_jid)
} }
void void
jabber_update_presence(jabber_presence_t status, const char * const msg) jabber_update_presence(jabber_presence_t status, const char * const msg,
int idle)
{ {
int pri; int pri;
char *show; char *show;
@ -379,6 +380,17 @@ jabber_update_presence(jabber_presence_t status, const char * const msg)
xmpp_stanza_add_child(priority, value); xmpp_stanza_add_child(priority, value);
xmpp_stanza_add_child(presence, priority); xmpp_stanza_add_child(presence, priority);
} }
if (idle > 0) {
xmpp_stanza_t *query = xmpp_stanza_new(jabber_conn.ctx);
xmpp_stanza_set_name(query, STANZA_NAME_QUERY);
xmpp_stanza_set_ns(query, STANZA_NS_LASTACTIVITY);
char idle_str[10];
snprintf(idle_str, sizeof(idle_str), "%d", idle);
xmpp_stanza_set_attribute(query, STANZA_ATTR_SECONDS, idle_str);
xmpp_stanza_add_child(presence, query);
}
xmpp_send(jabber_conn.conn, presence); xmpp_send(jabber_conn.conn, presence);
// send presence for each room // send presence for each room
@ -891,7 +903,7 @@ _roster_handler(xmpp_conn_t * const conn,
* presence rather than PRESENCE_ONLINE. It will be helpful * presence rather than PRESENCE_ONLINE. It will be helpful
* when I set dnd status and reconnect for some reason */ * when I set dnd status and reconnect for some reason */
// send initial presence // send initial presence
jabber_update_presence(PRESENCE_ONLINE, NULL); jabber_update_presence(PRESENCE_ONLINE, NULL, 0);
} }
return 1; return 1;

View File

@ -62,7 +62,8 @@ void jabber_send_inactive(const char * const recipient);
void jabber_send_composing(const char * const recipient); void jabber_send_composing(const char * const recipient);
void jabber_send_paused(const char * const recipient); void jabber_send_paused(const char * const recipient);
void jabber_send_gone(const char * const recipient); void jabber_send_gone(const char * const recipient);
void jabber_update_presence(jabber_presence_t status, const char * const msg); void jabber_update_presence(jabber_presence_t status, const char * const msg,
int idle);
const char * jabber_get_jid(void); const char * jabber_get_jid(void);
jabber_conn_status_t jabber_get_connection_status(void); jabber_conn_status_t jabber_get_connection_status(void);
int jabber_get_priority(void); int jabber_get_priority(void);

View File

@ -389,7 +389,11 @@ prefs_get_autoaway_message(void)
void void
prefs_set_autoaway_message(gchar *value) prefs_set_autoaway_message(gchar *value)
{ {
if (value == NULL) {
g_key_file_remove_key(prefs, "autoaway", "message", NULL);
} else {
g_key_file_set_string(prefs, "autoaway", "message", value); g_key_file_set_string(prefs, "autoaway", "message", value);
}
_save_prefs(); _save_prefs();
} }

View File

@ -452,8 +452,10 @@ _handle_idle_time()
if (!idle) { if (!idle) {
if (idle_ms >= prefs_time) { if (idle_ms >= prefs_time) {
idle = TRUE; idle = TRUE;
// handle away mode
if (strcmp(prefs_get_autoaway_mode(), "away") == 0) { if (strcmp(prefs_get_autoaway_mode(), "away") == 0) {
jabber_update_presence(PRESENCE_AWAY, prefs_get_autoaway_message()); jabber_update_presence(PRESENCE_AWAY, prefs_get_autoaway_message(), 0);
if (prefs_get_autoaway_message() != NULL) { if (prefs_get_autoaway_message() != NULL) {
cons_show("Idle for %d minutes, status set to away, \"%s\".", cons_show("Idle for %d minutes, status set to away, \"%s\".",
prefs_get_autoaway_time(), prefs_get_autoaway_message()); prefs_get_autoaway_time(), prefs_get_autoaway_message());
@ -465,24 +467,28 @@ _handle_idle_time()
title_bar_set_status(PRESENCE_AWAY); title_bar_set_status(PRESENCE_AWAY);
win_current_page_off(); win_current_page_off();
} }
// handle idle mode
} else if (strcmp(prefs_get_autoaway_mode(), "idle") == 0) { } else if (strcmp(prefs_get_autoaway_mode(), "idle") == 0) {
cons_show("IDLE"); jabber_update_presence(PRESENCE_ONLINE,
win_current_page_off(); prefs_get_autoaway_message(), idle_ms / 1000);
} }
} }
} else { } else {
if (idle_ms < prefs_time) { if (idle_ms < prefs_time) {
idle = FALSE; idle = FALSE;
// handle check
if (prefs_get_autoaway_check()) { if (prefs_get_autoaway_check()) {
if (strcmp(prefs_get_autoaway_mode(), "away") == 0) { if (strcmp(prefs_get_autoaway_mode(), "away") == 0) {
jabber_update_presence(PRESENCE_ONLINE, NULL); jabber_update_presence(PRESENCE_ONLINE, NULL, 0);
cons_show("Auto presence online."); cons_show("No longer idle, status set to online.");
title_bar_set_status(PRESENCE_ONLINE); title_bar_set_status(PRESENCE_ONLINE);
win_current_page_off(); win_current_page_off();
} else if (strcmp(prefs_get_autoaway_mode(), "idle") == 0) { } else if (strcmp(prefs_get_autoaway_mode(), "idle") == 0) {
cons_show("BACK"); jabber_update_presence(PRESENCE_ONLINE, NULL, 0);
win_current_page_off(); title_bar_set_status(PRESENCE_ONLINE);
} }
} }
} }

View File

@ -71,6 +71,7 @@
#define STANZA_ATTR_NICK "nick" #define STANZA_ATTR_NICK "nick"
#define STANZA_ATTR_ASK "ask" #define STANZA_ATTR_ASK "ask"
#define STANZA_ATTR_ID "id" #define STANZA_ATTR_ID "id"
#define STANZA_ATTR_SECONDS "seconds"
#define STANZA_TEXT_AWAY "away" #define STANZA_TEXT_AWAY "away"
#define STANZA_TEXT_DND "dnd" #define STANZA_TEXT_DND "dnd"
@ -82,6 +83,7 @@
#define STANZA_NS_MUC "http://jabber.org/protocol/muc" #define STANZA_NS_MUC "http://jabber.org/protocol/muc"
#define STANZA_NS_MUC_USER "http://jabber.org/protocol/muc#user" #define STANZA_NS_MUC_USER "http://jabber.org/protocol/muc#user"
#define STANZA_NS_PING "urn:xmpp:ping" #define STANZA_NS_PING "urn:xmpp:ping"
#define STANZA_NS_LASTACTIVITY "jabber:iq:last"
xmpp_stanza_t* stanza_create_chat_state(xmpp_ctx_t *ctx, xmpp_stanza_t* stanza_create_chat_state(xmpp_ctx_t *ctx,
const char * const recipient, const char * const state); const char * const recipient, const char * const state);