1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-12-04 14:46:46 -05:00

introduce priority support

add new command /priority <int>
improve jabber_update_presence()
use jabber_update_presence() for sending initial presence
save priority and status string to jabber_conn structure
This commit is contained in:
Dmitry Podgorny 2012-11-13 12:51:28 +02:00
parent b14aab4ade
commit a114fe88b8
8 changed files with 118 additions and 11 deletions

View File

@ -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 <value>", "Set priority for connection.",
{ "/priority <value>",
"--------------------",
"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)
{

View File

@ -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;

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);
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);

View File

@ -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)
{

View File

@ -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);

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_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);

View File

@ -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"

View File

@ -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)