From a114fe88b80010dd3c4f29319bd67e7daa533cb1 Mon Sep 17 00:00:00 2001 From: Dmitry Podgorny Date: Tue, 13 Nov 2012 12:51:28 +0200 Subject: [PATCH] introduce priority support add new command /priority improve jabber_update_presence() use jabber_update_presence() for sending initial presence save priority and status string to jabber_conn structure --- src/command.c | 36 ++++++++++++++++++++++++ src/jabber.c | 70 ++++++++++++++++++++++++++++++++++++++++------- src/jabber.h | 3 ++ src/preferences.c | 13 +++++++++ src/preferences.h | 2 ++ src/stanza.c | 2 +- src/stanza.h | 1 + src/windows.c | 2 ++ 8 files changed, 118 insertions(+), 11 deletions(-) diff --git a/src/command.c b/src/command.c index a723f6f8..bf7ae5be 100644 --- a/src/command.c +++ b/src/command.c @@ -95,6 +95,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); @@ -423,6 +424,16 @@ static struct cmd_t setting_commands[] = "", "Config file section : [log]", "Config file value : maxsize=bytes", + NULL } } }, + + { "/priority", + _cmd_set_priority, + { "/priority ", "Set priority for connection.", + { "/priority ", + "--------------------", + "value : Number between -128 and 127. Default value is 0.", + "", + "Config file section : [jabber]", NULL } } } }; @@ -1404,6 +1415,31 @@ _cmd_set_log(const char * const inp, struct cmd_help_t help) 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); + } else { + char *status = jabber_get_status(); + intval = atoi(value); + prefs_set_priority(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; +} + static gboolean _cmd_vercheck(const char * const inp, struct cmd_help_t help) { diff --git a/src/jabber.c b/src/jabber.c index bb691a42..1af0bcdb 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,15 @@ jabber_leave_chat_room(const char * const room_jid) void jabber_update_presence(jabber_presence_t status, const char * const msg) { - jabber_conn.presence = status; + int pri = prefs_get_priority(); + char *show; + + 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 +303,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[5]; + + sprintf(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 +342,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) { @@ -571,7 +621,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); @@ -593,11 +642,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 a0536a7f..6fd8e17d 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)