about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorJames Booth <boothj5@gmail.com>2012-11-13 14:40:24 -0800
committerJames Booth <boothj5@gmail.com>2012-11-13 14:40:24 -0800
commitfaa5f8871f76565c1f50f5e67353320debbf741e (patch)
treed49b2bde4003dbee7381d5a1ecb44c761bf1a833 /src
parent8ecbe0c590832200134e54fe55da872917278f64 (diff)
parent52c4c3d953008cc89af230651568f2081e132c9b (diff)
downloadprofani-tty-faa5f8871f76565c1f50f5e67353320debbf741e.tar.gz
Merge pull request #81 from pasis/priority
introduce priority support
Diffstat (limited to 'src')
-rw-r--r--src/command.c74
-rw-r--r--src/jabber.c73
-rw-r--r--src/jabber.h3
-rw-r--r--src/preferences.c13
-rw-r--r--src/preferences.h2
-rw-r--r--src/stanza.c2
-rw-r--r--src/stanza.h1
-rw-r--r--src/windows.c2
8 files changed, 156 insertions, 14 deletions
diff --git a/src/command.c b/src/command.c
index c2668bef..ae59e7b3 100644
--- a/src/command.c
+++ b/src/command.c
@@ -22,6 +22,8 @@
 
 #include <stdlib.h>
 #include <string.h>
+#include <limits.h>
+#include <errno.h>
 
 #include <glib.h>
 
@@ -79,6 +81,8 @@ static void _notify_autocomplete(char *input, int *size);
 static void _parameter_autocomplete(char *input, int *size, char *command,
     autocomplete_func func);
 
+static int _strtoi(char *str, int *saveptr, int min, int max);
+
 // command prototypes
 static gboolean _cmd_quit(const char * const inp, struct cmd_help_t help);
 static gboolean _cmd_help(const char * const inp, struct cmd_help_t help);
@@ -95,6 +99,7 @@ static gboolean _cmd_join(const char * const inp, struct cmd_help_t help);
 static gboolean _cmd_set_beep(const char * const inp, struct cmd_help_t help);
 static gboolean _cmd_set_notify(const char * const inp, struct cmd_help_t help);
 static gboolean _cmd_set_log(const char * const inp, struct cmd_help_t help);
+static gboolean _cmd_set_priority(const char * const inp, struct cmd_help_t help);
 static gboolean _cmd_set_intype(const char * const inp, struct cmd_help_t help);
 static gboolean _cmd_set_flash(const char * const inp, struct cmd_help_t help);
 static gboolean _cmd_set_showsplash(const char * const inp, struct cmd_help_t help);
@@ -433,6 +438,16 @@ static struct cmd_t setting_commands[] =
           "",
           "Config file section : [log]",
           "Config file value :   maxsize=bytes",
+          NULL } } },
+
+    { "/priority",
+        _cmd_set_priority,
+        { "/priority <value>", "Set priority for connection.",
+        { "/priority <value>",
+          "--------------------",
+          "value : Number between -128 and 127. Default value is 0.",
+          "",
+          "Config file section : [jabber]",
           NULL } } }
 };
 
@@ -1432,13 +1447,43 @@ _cmd_set_log(const char * const inp, struct cmd_help_t help)
     value = strtok(NULL, " ");
     if (subcmd == NULL || value == NULL) {
         cons_show("Usage: %s", help.usage);
-    } else {
-        if (strcmp(subcmd, "maxsize") == 0) {
-            intval = atoi(value);
+        return TRUE;
+    }
+
+    if (strcmp(subcmd, "maxsize") == 0) {
+        if (_strtoi(value, &intval, PREFS_MIN_LOG_SIZE, INT_MAX) == 0) {
             prefs_set_max_log_size(intval);
             cons_show("Log maxinum size set to %d bytes", intval);
         }
-        /* TODO: make 'level' subcommand for debug level */
+    }
+    /* TODO: make 'level' subcommand for debug level */
+
+    return TRUE;
+}
+
+static gboolean
+_cmd_set_priority(const char * const inp, struct cmd_help_t help)
+{
+    char *value;
+    char inp_cpy[strlen(inp) + 1];
+    int intval;
+
+    strcpy(inp_cpy, inp);
+    strtok(inp_cpy, " ");
+    value = strtok(NULL, " ");
+    if (value == NULL) {
+        cons_show("Usage: %s", help.usage);
+        return TRUE;
+    }
+
+    if (_strtoi(value, &intval, -128, 127) == 0) {
+        char *status = jabber_get_status();
+        prefs_set_priority((int)intval);
+        // update presence with new priority
+        jabber_update_presence(jabber_get_presence(), status);
+        if (status != NULL)
+            free(status);
+        cons_show("Priority set to %d.", intval);
     }
     return TRUE;
 }
@@ -1705,3 +1750,24 @@ _notify_autocomplete(char *input, int *size)
         }
     }
 }
+
+static int
+_strtoi(char *str, int *saveptr, int min, int max)
+{
+    char *ptr;
+    int val;
+
+    errno = 0;
+    val = (int)strtol(str, &ptr, 0);
+    if (*str == '\0' || *ptr != '\0') {
+        cons_show("Illegal character. Must be a number.");
+        return -1;
+    } else if (errno == ERANGE || val < min || val > max) {
+        cons_show("Value out of range. Must be in %d..%d.", min, max);
+        return -1;
+    }
+
+    *saveptr = val;
+
+    return 0;
+}
diff --git a/src/jabber.c b/src/jabber.c
index 44e92da4..f4f53732 100644
--- a/src/jabber.c
+++ b/src/jabber.c
@@ -43,7 +43,9 @@ static struct _jabber_conn_t {
     xmpp_conn_t *conn;
     jabber_conn_status_t conn_status;
     jabber_presence_t presence;
+    char *status;
     int tls_disabled;
+    int priority;
 } jabber_conn;
 
 static log_level_t _get_log_level(xmpp_log_level_t xmpp_level);
@@ -78,6 +80,7 @@ jabber_init(const int disable_tls)
     log_info("Initialising XMPP");
     jabber_conn.conn_status = JABBER_STARTED;
     jabber_conn.presence = PRESENCE_OFFLINE;
+    jabber_conn.status = NULL;
     jabber_conn.tls_disabled = disable_tls;
 }
 
@@ -86,6 +89,9 @@ jabber_restart(void)
 {
     jabber_conn.conn_status = JABBER_STARTED;
     jabber_conn.presence = PRESENCE_OFFLINE;
+    if (jabber_conn.status != NULL)
+        free(jabber_conn.status);
+    jabber_conn.status = NULL;
 }
 
 jabber_conn_status_t
@@ -274,9 +280,20 @@ jabber_leave_chat_room(const char * const room_jid)
 void
 jabber_update_presence(jabber_presence_t status, const char * const msg)
 {
+    int pri;
+    char *show;
+
+    // don't send presence when disconnected
+    if (jabber_conn.conn_status != JABBER_CONNECTED)
+        return;
+
+    pri = prefs_get_priority();
+    if (pri < -128 || pri > 127)
+        pri = 0;
+
     jabber_conn.presence = status;
+    jabber_conn.priority = pri;
 
-    char *show = NULL;
     switch(status)
     {
         case PRESENCE_AWAY:
@@ -291,12 +308,29 @@ jabber_update_presence(jabber_presence_t status, const char * const msg)
         case PRESENCE_XA:
             show = STANZA_TEXT_XA;
             break;
-        default:
-            show = STANZA_TEXT_ONLINE;
+        default: // PRESENCE_ONLINE
+            show = NULL;
             break;
     }
 
+    if (jabber_conn.status != NULL)
+        free(jabber_conn.status);
+    if (msg != NULL)
+        jabber_conn.status = strdup(msg);
+
     xmpp_stanza_t *presence = stanza_create_presence(jabber_conn.ctx, show, msg);
+    if (pri != 0) {
+        xmpp_stanza_t *priority, *value;
+        char pri_str[10];
+
+        snprintf(pri_str, sizeof(pri_str), "%d", pri);
+        priority = xmpp_stanza_new(jabber_conn.ctx);
+        value = xmpp_stanza_new(jabber_conn.ctx);
+        xmpp_stanza_set_name(priority, STANZA_NAME_PRIORITY);
+        xmpp_stanza_set_text(value, pri_str);
+        xmpp_stanza_add_child(priority, value);
+        xmpp_stanza_add_child(presence, priority);
+    }
     xmpp_send(jabber_conn.conn, presence);
     xmpp_stanza_release(presence);
 }
@@ -313,6 +347,27 @@ jabber_get_jid(void)
     return xmpp_conn_get_jid(jabber_conn.conn);
 }
 
+int
+jabber_get_priority(void)
+{
+    return jabber_conn.priority;
+}
+
+jabber_presence_t
+jabber_get_presence(void)
+{
+    return jabber_conn.presence;
+}
+
+char *
+jabber_get_status(void)
+{
+    if (jabber_conn.status == NULL)
+        return NULL;
+    else
+        return strdup(jabber_conn.status);
+}
+
 void
 jabber_free_resources(void)
 {
@@ -582,7 +637,6 @@ static int
 _roster_handler(xmpp_conn_t * const conn,
     xmpp_stanza_t * const stanza, void * const userdata)
 {
-    xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
     xmpp_stanza_t *query, *item;
     char *type = xmpp_stanza_get_type(stanza);
 
@@ -604,11 +658,12 @@ _roster_handler(xmpp_conn_t * const conn,
 
             item = xmpp_stanza_get_next(item);
         }
-        xmpp_stanza_t* pres;
-        pres = xmpp_stanza_new(ctx);
-        xmpp_stanza_set_name(pres, STANZA_NAME_PRESENCE);
-        xmpp_send(conn, pres);
-        xmpp_stanza_release(pres);
+
+        /* TODO: Save somehow last presence show and use it for initial
+         *       presence rather than PRESENCE_ONLINE. It will be helpful 
+         *       when I set dnd status and reconnect for some reason */
+        // send initial presence
+        jabber_update_presence(PRESENCE_ONLINE, NULL);
     }
 
     return 1;
diff --git a/src/jabber.h b/src/jabber.h
index 5dea2a06..3b59552c 100644
--- a/src/jabber.h
+++ b/src/jabber.h
@@ -63,6 +63,9 @@ void jabber_send_gone(const char * const recipient);
 void jabber_update_presence(jabber_presence_t status, const char * const msg);
 const char * jabber_get_jid(void);
 jabber_conn_status_t jabber_get_connection_status(void);
+int jabber_get_priority(void);
+jabber_presence_t jabber_get_presence(void);
+char * jabber_get_status(void);
 void jabber_free_resources(void);
 void jabber_restart(void);
 
diff --git a/src/preferences.c b/src/preferences.c
index fc9fe180..e5e9653e 100644
--- a/src/preferences.c
+++ b/src/preferences.c
@@ -327,6 +327,19 @@ prefs_set_max_log_size(gint value)
     _save_prefs();
 }
 
+gint
+prefs_get_priority(void)
+{
+    return g_key_file_get_integer(prefs, "jabber", "priority", NULL);
+}
+
+void
+prefs_set_priority(gint value)
+{
+    g_key_file_set_integer(prefs, "jabber", "priority", value);
+    _save_prefs();
+}
+
 gboolean
 prefs_get_vercheck(void)
 {
diff --git a/src/preferences.h b/src/preferences.h
index dc09810a..d045f517 100644
--- a/src/preferences.h
+++ b/src/preferences.h
@@ -72,6 +72,8 @@ void prefs_set_notify_remind(gint period);
 gint prefs_get_notify_remind(void);
 void prefs_set_max_log_size(gint value);
 gint prefs_get_max_log_size(void);
+void prefs_set_priority(gint value);
+gint prefs_get_priority(void);
 
 void prefs_add_login(const char *jid);
 
diff --git a/src/stanza.c b/src/stanza.c
index bef10751..788aea88 100644
--- a/src/stanza.c
+++ b/src/stanza.c
@@ -122,7 +122,7 @@ stanza_create_presence(xmpp_ctx_t *ctx, const char * const show,
     xmpp_stanza_t *presence = xmpp_stanza_new(ctx);
     xmpp_stanza_set_name(presence, STANZA_NAME_PRESENCE);
 
-    if (strcmp(show, STANZA_TEXT_ONLINE) != 0) {
+    if (show != NULL) {
         xmpp_stanza_t *show_stanza = xmpp_stanza_new(ctx);
         xmpp_stanza_set_name(show_stanza, STANZA_NAME_SHOW);
         xmpp_stanza_t *text = xmpp_stanza_new(ctx);
diff --git a/src/stanza.h b/src/stanza.h
index 706307fa..442741fd 100644
--- a/src/stanza.h
+++ b/src/stanza.h
@@ -34,6 +34,7 @@
 #define STANZA_NAME_MESSAGE "message"
 #define STANZA_NAME_BODY "body"
 #define STANZA_NAME_PRESENCE "presence"
+#define STANZA_NAME_PRIORITY "priority"
 #define STANZA_NAME_X "x"
 #define STANZA_NAME_SHOW "show"
 #define STANZA_NAME_STATUS "status"
diff --git a/src/windows.c b/src/windows.c
index fe14a358..4401a126 100644
--- a/src/windows.c
+++ b/src/windows.c
@@ -914,6 +914,8 @@ cons_prefs(void)
         cons_show("Reminder notification period : %d seconds", remind_period);
     }
 
+    cons_show("Priority                     : %d", prefs_get_priority());
+
     cons_show("");
 
     if (_curr_prof_win == 0)