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

Merge pull request #81 from pasis/priority

introduce priority support
This commit is contained in:
James Booth 2012-11-13 14:40:24 -08:00
commit faa5f8871f
8 changed files with 157 additions and 15 deletions

View File

@ -22,6 +22,8 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <limits.h>
#include <errno.h>
#include <glib.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, static void _parameter_autocomplete(char *input, int *size, char *command,
autocomplete_func func); autocomplete_func func);
static int _strtoi(char *str, int *saveptr, int min, int max);
// command prototypes // command prototypes
static gboolean _cmd_quit(const char * const inp, struct cmd_help_t help); 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); 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_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_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_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_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_flash(const char * const inp, struct cmd_help_t help);
static gboolean _cmd_set_showsplash(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 section : [log]",
"Config file value : maxsize=bytes", "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 } } } NULL } } }
}; };
@ -1432,13 +1447,43 @@ _cmd_set_log(const char * const inp, struct cmd_help_t help)
value = strtok(NULL, " "); value = strtok(NULL, " ");
if (subcmd == NULL || value == NULL) { if (subcmd == NULL || value == NULL) {
cons_show("Usage: %s", help.usage); cons_show("Usage: %s", help.usage);
} else { return TRUE;
if (strcmp(subcmd, "maxsize") == 0) { }
intval = atoi(value);
if (strcmp(subcmd, "maxsize") == 0) {
if (_strtoi(value, &intval, PREFS_MIN_LOG_SIZE, INT_MAX) == 0) {
prefs_set_max_log_size(intval); prefs_set_max_log_size(intval);
cons_show("Log maxinum size set to %d bytes", 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; 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;
}

View File

@ -43,7 +43,9 @@ static struct _jabber_conn_t {
xmpp_conn_t *conn; xmpp_conn_t *conn;
jabber_conn_status_t conn_status; jabber_conn_status_t conn_status;
jabber_presence_t presence; jabber_presence_t presence;
char *status;
int tls_disabled; int tls_disabled;
int priority;
} jabber_conn; } jabber_conn;
static log_level_t _get_log_level(xmpp_log_level_t xmpp_level); 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"); log_info("Initialising XMPP");
jabber_conn.conn_status = JABBER_STARTED; jabber_conn.conn_status = JABBER_STARTED;
jabber_conn.presence = PRESENCE_OFFLINE; jabber_conn.presence = PRESENCE_OFFLINE;
jabber_conn.status = NULL;
jabber_conn.tls_disabled = disable_tls; jabber_conn.tls_disabled = disable_tls;
} }
@ -86,6 +89,9 @@ jabber_restart(void)
{ {
jabber_conn.conn_status = JABBER_STARTED; jabber_conn.conn_status = JABBER_STARTED;
jabber_conn.presence = PRESENCE_OFFLINE; jabber_conn.presence = PRESENCE_OFFLINE;
if (jabber_conn.status != NULL)
free(jabber_conn.status);
jabber_conn.status = NULL;
} }
jabber_conn_status_t jabber_conn_status_t
@ -274,9 +280,20 @@ 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)
{ {
jabber_conn.presence = status; 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) switch(status)
{ {
case PRESENCE_AWAY: case PRESENCE_AWAY:
@ -291,12 +308,29 @@ jabber_update_presence(jabber_presence_t status, const char * const msg)
case PRESENCE_XA: case PRESENCE_XA:
show = STANZA_TEXT_XA; show = STANZA_TEXT_XA;
break; break;
default: default: // PRESENCE_ONLINE
show = STANZA_TEXT_ONLINE; show = NULL;
break; 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); 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_send(jabber_conn.conn, presence);
xmpp_stanza_release(presence); xmpp_stanza_release(presence);
} }
@ -313,6 +347,27 @@ jabber_get_jid(void)
return xmpp_conn_get_jid(jabber_conn.conn); 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 void
jabber_free_resources(void) jabber_free_resources(void)
{ {
@ -582,7 +637,6 @@ static int
_roster_handler(xmpp_conn_t * const conn, _roster_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata) xmpp_stanza_t * const stanza, void * const userdata)
{ {
xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata;
xmpp_stanza_t *query, *item; xmpp_stanza_t *query, *item;
char *type = xmpp_stanza_get_type(stanza); char *type = xmpp_stanza_get_type(stanza);
@ -604,11 +658,12 @@ _roster_handler(xmpp_conn_t * const conn,
item = xmpp_stanza_get_next(item); item = xmpp_stanza_get_next(item);
} }
xmpp_stanza_t* pres;
pres = xmpp_stanza_new(ctx); /* TODO: Save somehow last presence show and use it for initial
xmpp_stanza_set_name(pres, STANZA_NAME_PRESENCE); * presence rather than PRESENCE_ONLINE. It will be helpful
xmpp_send(conn, pres); * when I set dnd status and reconnect for some reason */
xmpp_stanza_release(pres); // send initial presence
jabber_update_presence(PRESENCE_ONLINE, NULL);
} }
return 1; return 1;

View File

@ -63,6 +63,9 @@ 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);
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);
jabber_presence_t jabber_get_presence(void);
char * jabber_get_status(void);
void jabber_free_resources(void); void jabber_free_resources(void);
void jabber_restart(void); void jabber_restart(void);

View File

@ -327,6 +327,19 @@ prefs_set_max_log_size(gint value)
_save_prefs(); _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 gboolean
prefs_get_vercheck(void) prefs_get_vercheck(void)
{ {

View File

@ -72,6 +72,8 @@ void prefs_set_notify_remind(gint period);
gint prefs_get_notify_remind(void); gint prefs_get_notify_remind(void);
void prefs_set_max_log_size(gint value); void prefs_set_max_log_size(gint value);
gint prefs_get_max_log_size(void); 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); void prefs_add_login(const char *jid);

View File

@ -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_t *presence = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(presence, STANZA_NAME_PRESENCE); 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_t *show_stanza = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(show_stanza, STANZA_NAME_SHOW); xmpp_stanza_set_name(show_stanza, STANZA_NAME_SHOW);
xmpp_stanza_t *text = xmpp_stanza_new(ctx); xmpp_stanza_t *text = xmpp_stanza_new(ctx);

View File

@ -34,6 +34,7 @@
#define STANZA_NAME_MESSAGE "message" #define STANZA_NAME_MESSAGE "message"
#define STANZA_NAME_BODY "body" #define STANZA_NAME_BODY "body"
#define STANZA_NAME_PRESENCE "presence" #define STANZA_NAME_PRESENCE "presence"
#define STANZA_NAME_PRIORITY "priority"
#define STANZA_NAME_X "x" #define STANZA_NAME_X "x"
#define STANZA_NAME_SHOW "show" #define STANZA_NAME_SHOW "show"
#define STANZA_NAME_STATUS "status" #define STANZA_NAME_STATUS "status"

View File

@ -914,6 +914,8 @@ cons_prefs(void)
cons_show("Reminder notification period : %d seconds", remind_period); cons_show("Reminder notification period : %d seconds", remind_period);
} }
cons_show("Priority : %d", prefs_get_priority());
cons_show(""); cons_show("");
if (_curr_prof_win == 0) if (_curr_prof_win == 0)