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

Moved functions to xmpp_presence

This commit is contained in:
James Booth 2013-01-28 22:55:26 +00:00
parent 7255ef5e1c
commit bfe1d76e5f
6 changed files with 193 additions and 158 deletions

View File

@ -1835,7 +1835,7 @@ _cmd_join(gchar **args, struct cmd_help_t help)
Jid *room_jid = jid_create_from_bare_and_resource(room, nick); Jid *room_jid = jid_create_from_bare_and_resource(room, nick);
if (!muc_room_is_active(room_jid)) { if (!muc_room_is_active(room_jid)) {
jabber_join(room_jid); presence_join_room(room_jid);
} }
win_join_chat(room_jid); win_join_chat(room_jid);
@ -1858,7 +1858,7 @@ _cmd_nick(gchar **args, struct cmd_help_t help)
char *room = win_current_get_recipient(); char *room = win_current_get_recipient();
char *nick = args[0]; char *nick = args[0];
jabber_change_room_nick(room, nick); presence_change_room_nick(room, nick);
return TRUE; return TRUE;
} }
@ -1927,7 +1927,7 @@ _cmd_close(gchar **args, struct cmd_help_t help)
if (conn_status == JABBER_CONNECTED) { if (conn_status == JABBER_CONNECTED) {
if (win_current_is_groupchat()) { if (win_current_is_groupchat()) {
char *room_jid = win_current_get_recipient(); char *room_jid = win_current_get_recipient();
jabber_leave_chat_room(room_jid); presence_leave_chat_room(room_jid);
} else if (win_current_is_chat() || win_current_is_private()) { } else if (win_current_is_chat() || win_current_is_private()) {
if (prefs_get_states()) { if (prefs_get_states()) {
@ -2180,7 +2180,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, 0); presence_update(jabber_get_presence(), status, 0);
cons_show("Priority set to %d.", intval); cons_show("Priority set to %d.", intval);
} }
@ -2302,7 +2302,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, 0); presence_update(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

@ -469,7 +469,7 @@ _handle_idle_time()
// handle away mode // 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(), 0); presence_update(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());
@ -484,7 +484,7 @@ _handle_idle_time()
// handle idle mode // handle idle mode
} else if (strcmp(prefs_get_autoaway_mode(), "idle") == 0) { } else if (strcmp(prefs_get_autoaway_mode(), "idle") == 0) {
jabber_update_presence(PRESENCE_ONLINE, presence_update(PRESENCE_ONLINE,
prefs_get_autoaway_message(), idle_ms / 1000); prefs_get_autoaway_message(), idle_ms / 1000);
} }
} }
@ -496,12 +496,12 @@ _handle_idle_time()
// handle check // 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, 0); presence_update(PRESENCE_ONLINE, NULL, 0);
cons_show("No longer idle, status set to 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) {
jabber_update_presence(PRESENCE_ONLINE, NULL, 0); presence_update(PRESENCE_ONLINE, NULL, 0);
title_bar_set_status(PRESENCE_ONLINE); title_bar_set_status(PRESENCE_ONLINE);
} }
} }

View File

@ -146,17 +146,12 @@ jabber_conn_status_t jabber_connect_with_account(ProfAccount *account,
const char * const passwd); const char * const passwd);
void jabber_disconnect(void); void jabber_disconnect(void);
void jabber_process_events(void); void jabber_process_events(void);
void jabber_join(Jid *jid);
void jabber_change_room_nick(const char * const room, const char * const nick);
void jabber_leave_chat_room(const char * const room_jid);
void jabber_send(const char * const msg, const char * const recipient); void jabber_send(const char * const msg, const char * const recipient);
void jabber_send_groupchat(const char * const msg, const char * const recipient); void jabber_send_groupchat(const char * const msg, const char * const recipient);
void jabber_send_inactive(const char * const recipient); 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,
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);
@ -168,6 +163,10 @@ void jabber_set_autoping(int seconds);
xmpp_conn_t *jabber_get_conn(void); xmpp_conn_t *jabber_get_conn(void);
xmpp_ctx_t *jabber_get_ctx(void); xmpp_ctx_t *jabber_get_ctx(void);
int error_handler(xmpp_stanza_t * const stanza); int error_handler(xmpp_stanza_t * const stanza);
void jabber_conn_set_presence(jabber_presence_t presence);
void jabber_conn_set_priority(int priority);
void jabber_conn_set_status(const char * const message);
char* jabber_get_account_name(void);
// iq functions // iq functions
void iq_add_handlers(xmpp_conn_t * const conn, xmpp_ctx_t * const ctx); void iq_add_handlers(xmpp_conn_t * const conn, xmpp_ctx_t * const ctx);
@ -179,6 +178,11 @@ GList* presence_get_subscription_requests(void);
void presence_free_sub_requests(void); void presence_free_sub_requests(void);
int presence_handler(xmpp_conn_t * const conn, int presence_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata); xmpp_stanza_t * const stanza, void * const userdata);
void presence_join_room(Jid *jid);
void presence_change_room_nick(const char * const room, const char * const nick);
void presence_leave_chat_room(const char * const room_jid);
void presence_update(jabber_presence_t status, const char * const msg,
int idle);
// caps functions // caps functions
void caps_init(void); void caps_init(void);

View File

@ -318,149 +318,6 @@ jabber_send_gone(const char * const recipient)
chat_session_set_sent(recipient); chat_session_set_sent(recipient);
} }
void
jabber_join(Jid *jid)
{
xmpp_stanza_t *presence = stanza_create_room_join_presence(jabber_conn.ctx,
jid->fulljid);
xmpp_send(jabber_conn.conn, presence);
xmpp_stanza_release(presence);
muc_join_room(jid->barejid, jid->resourcepart);
}
void
jabber_change_room_nick(const char * const room, const char * const nick)
{
char *full_room_jid = create_fulljid(room, nick);
xmpp_stanza_t *presence = stanza_create_room_newnick_presence(jabber_conn.ctx,
full_room_jid);
xmpp_send(jabber_conn.conn, presence);
xmpp_stanza_release(presence);
free(full_room_jid);
}
void
jabber_leave_chat_room(const char * const room_jid)
{
char *nick = muc_get_room_nick(room_jid);
xmpp_stanza_t *presence = stanza_create_room_leave_presence(jabber_conn.ctx,
room_jid, nick);
xmpp_send(jabber_conn.conn, presence);
xmpp_stanza_release(presence);
}
void
jabber_update_presence(jabber_presence_t status, const char * const msg,
int idle)
{
int pri;
char *show, *last;
// don't send presence when disconnected
if (jabber_conn.conn_status != JABBER_CONNECTED)
return;
pri = prefs_get_priority();
if (pri < JABBER_PRIORITY_MIN || pri > JABBER_PRIORITY_MAX)
pri = 0;
jabber_conn.presence = status;
jabber_conn.priority = pri;
switch(status)
{
case PRESENCE_AWAY:
show = STANZA_TEXT_AWAY;
last = STANZA_TEXT_AWAY;
break;
case PRESENCE_DND:
show = STANZA_TEXT_DND;
last = STANZA_TEXT_DND;
break;
case PRESENCE_CHAT:
show = STANZA_TEXT_CHAT;
last = STANZA_TEXT_CHAT;
break;
case PRESENCE_XA:
show = STANZA_TEXT_XA;
last = STANZA_TEXT_XA;
break;
default: // PRESENCE_ONLINE
show = NULL;
last = STANZA_TEXT_ONLINE;
break;
}
if (jabber_conn.status != NULL) {
free(jabber_conn.status);
jabber_conn.status = NULL;
}
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);
}
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);
}
// add caps
xmpp_stanza_t *caps = xmpp_stanza_new(jabber_conn.ctx);
xmpp_stanza_set_name(caps, STANZA_NAME_C);
xmpp_stanza_set_ns(caps, STANZA_NS_CAPS);
xmpp_stanza_t *query = caps_create_query_response_stanza(jabber_conn.ctx);
char *sha1 = caps_create_sha1_str(query);
xmpp_stanza_set_attribute(caps, STANZA_ATTR_HASH, "sha-1");
xmpp_stanza_set_attribute(caps, STANZA_ATTR_NODE, "http://www.profanity.im");
xmpp_stanza_set_attribute(caps, STANZA_ATTR_VER, sha1);
xmpp_stanza_add_child(presence, caps);
xmpp_send(jabber_conn.conn, presence);
// send presence for each room
GList *rooms = muc_get_active_room_list();
while (rooms != NULL) {
char *room = rooms->data;
char *nick = muc_get_room_nick(room);
char *full_room_jid = create_fulljid(room, nick);
xmpp_stanza_set_attribute(presence, STANZA_ATTR_TO, full_room_jid);
xmpp_send(jabber_conn.conn, presence);
rooms = g_list_next(rooms);
}
g_list_free(rooms);
xmpp_stanza_release(presence);
FREE_SET_NULL(sha1);
// set last presence for account
accounts_set_last_presence(saved_account.name, last);
}
void void
jabber_set_autoping(int seconds) jabber_set_autoping(int seconds)
{ {
@ -518,6 +375,33 @@ jabber_get_status(void)
return jabber_conn.status; return jabber_conn.status;
} }
char *
jabber_get_account_name(void)
{
return saved_account.name;
}
void
jabber_conn_set_presence(jabber_presence_t presence)
{
jabber_conn.presence = presence;
}
void
jabber_conn_set_status(const char * const message)
{
FREE_SET_NULL(jabber_conn.status);
if (message != NULL) {
jabber_conn.status = strdup(message);
}
}
void
jabber_conn_set_priority(int priority)
{
jabber_conn.priority = priority;
}
void void
jabber_free_resources(void) jabber_free_resources(void)
{ {

View File

@ -141,7 +141,7 @@ _iq_handle_roster_result(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
* 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, 0); presence_update(PRESENCE_ONLINE, NULL, 0);
} }
return 1; return 1;

View File

@ -25,8 +25,10 @@
#include <glib.h> #include <glib.h>
#include "common.h"
#include "log.h" #include "log.h"
#include "muc.h" #include "muc.h"
#include "preferences.h"
#include "profanity.h" #include "profanity.h"
#include "xmpp.h" #include "xmpp.h"
@ -87,6 +89,151 @@ presence_free_sub_requests(void)
g_hash_table_remove_all(sub_requests); g_hash_table_remove_all(sub_requests);
} }
void
presence_join_room(Jid *jid)
{
xmpp_ctx_t *ctx = jabber_get_ctx();
xmpp_conn_t *conn = jabber_get_conn();
xmpp_stanza_t *presence = stanza_create_room_join_presence(ctx, jid->fulljid);
xmpp_send(conn, presence);
xmpp_stanza_release(presence);
muc_join_room(jid->barejid, jid->resourcepart);
}
void
presence_change_room_nick(const char * const room, const char * const nick)
{
xmpp_ctx_t *ctx = jabber_get_ctx();
xmpp_conn_t *conn = jabber_get_conn();
char *full_room_jid = create_fulljid(room, nick);
xmpp_stanza_t *presence = stanza_create_room_newnick_presence(ctx, full_room_jid);
xmpp_send(conn, presence);
xmpp_stanza_release(presence);
free(full_room_jid);
}
void
presence_leave_chat_room(const char * const room_jid)
{
xmpp_ctx_t *ctx = jabber_get_ctx();
xmpp_conn_t *conn = jabber_get_conn();
char *nick = muc_get_room_nick(room_jid);
xmpp_stanza_t *presence = stanza_create_room_leave_presence(ctx, room_jid,
nick);
xmpp_send(conn, presence);
xmpp_stanza_release(presence);
}
void
presence_update(jabber_presence_t status, const char * const msg,
int idle)
{
xmpp_ctx_t *ctx = jabber_get_ctx();
xmpp_conn_t *conn = jabber_get_conn();
int pri;
char *show, *last;
// don't send presence when disconnected
if (jabber_get_connection_status() != JABBER_CONNECTED)
return;
pri = prefs_get_priority();
if (pri < JABBER_PRIORITY_MIN || pri > JABBER_PRIORITY_MAX)
pri = 0;
jabber_conn_set_presence(status);
jabber_conn_set_priority(pri);
switch(status)
{
case PRESENCE_AWAY:
show = STANZA_TEXT_AWAY;
last = STANZA_TEXT_AWAY;
break;
case PRESENCE_DND:
show = STANZA_TEXT_DND;
last = STANZA_TEXT_DND;
break;
case PRESENCE_CHAT:
show = STANZA_TEXT_CHAT;
last = STANZA_TEXT_CHAT;
break;
case PRESENCE_XA:
show = STANZA_TEXT_XA;
last = STANZA_TEXT_XA;
break;
default: // PRESENCE_ONLINE
show = NULL;
last = STANZA_TEXT_ONLINE;
break;
}
jabber_conn_set_status(msg);
xmpp_stanza_t *presence = stanza_create_presence(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(ctx);
value = xmpp_stanza_new(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);
}
if (idle > 0) {
xmpp_stanza_t *query = xmpp_stanza_new(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);
}
// add caps
xmpp_stanza_t *caps = xmpp_stanza_new(ctx);
xmpp_stanza_set_name(caps, STANZA_NAME_C);
xmpp_stanza_set_ns(caps, STANZA_NS_CAPS);
xmpp_stanza_t *query = caps_create_query_response_stanza(ctx);
char *sha1 = caps_create_sha1_str(query);
xmpp_stanza_set_attribute(caps, STANZA_ATTR_HASH, "sha-1");
xmpp_stanza_set_attribute(caps, STANZA_ATTR_NODE, "http://www.profanity.im");
xmpp_stanza_set_attribute(caps, STANZA_ATTR_VER, sha1);
xmpp_stanza_add_child(presence, caps);
xmpp_send(conn, presence);
// send presence for each room
GList *rooms = muc_get_active_room_list();
while (rooms != NULL) {
char *room = rooms->data;
char *nick = muc_get_room_nick(room);
char *full_room_jid = create_fulljid(room, nick);
xmpp_stanza_set_attribute(presence, STANZA_ATTR_TO, full_room_jid);
xmpp_send(conn, presence);
rooms = g_list_next(rooms);
}
g_list_free(rooms);
xmpp_stanza_release(presence);
FREE_SET_NULL(sha1);
// set last presence for account
accounts_set_last_presence(jabber_get_account_name(), last);
}
int int
presence_handler(xmpp_conn_t * const conn, presence_handler(xmpp_conn_t * const conn,
xmpp_stanza_t * const stanza, void * const userdata) xmpp_stanza_t * const stanza, void * const userdata)