mirror of
https://github.com/profanity-im/profanity.git
synced 2025-02-02 15:08:15 -05:00
Added boilerplate code to handle chat states
This commit is contained in:
parent
5ce977284b
commit
3c82fb28c4
@ -28,8 +28,8 @@
|
|||||||
#include "chat_session.h"
|
#include "chat_session.h"
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
#define INACTIVE_TIMOUT 10.0
|
#define INACTIVE_TIMOUT 120.0
|
||||||
#define GONE_TIMOUT 20.0
|
#define GONE_TIMOUT 600.0
|
||||||
|
|
||||||
static ChatSession _chat_session_new(const char * const recipient,
|
static ChatSession _chat_session_new(const char * const recipient,
|
||||||
gboolean recipient_supports);
|
gboolean recipient_supports);
|
||||||
@ -181,7 +181,7 @@ chat_session_gone(const char * const recipient)
|
|||||||
}
|
}
|
||||||
|
|
||||||
gboolean
|
gboolean
|
||||||
chat_session_recipient_supports(const char * const recipient)
|
chat_session_get_recipient_supports(const char * const recipient)
|
||||||
{
|
{
|
||||||
ChatSession session = g_hash_table_lookup(sessions, recipient);
|
ChatSession session = g_hash_table_lookup(sessions, recipient);
|
||||||
|
|
||||||
@ -193,6 +193,19 @@ chat_session_recipient_supports(const char * const recipient)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
chat_session_set_recipient_supports(const char * const recipient,
|
||||||
|
gboolean recipient_supports)
|
||||||
|
{
|
||||||
|
ChatSession session = g_hash_table_lookup(sessions, recipient);
|
||||||
|
|
||||||
|
if (session == NULL) {
|
||||||
|
log_error("No chat session found for %s.", recipient);
|
||||||
|
} else {
|
||||||
|
session->recipient_supports = recipient_supports;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static ChatSession
|
static ChatSession
|
||||||
_chat_session_new(const char * const recipient, gboolean recipient_supports)
|
_chat_session_new(const char * const recipient, gboolean recipient_supports)
|
||||||
{
|
{
|
||||||
|
@ -33,7 +33,9 @@ void chat_session_start(const char * const recipient,
|
|||||||
gboolean recipient_supports);
|
gboolean recipient_supports);
|
||||||
gboolean chat_session_exists(const char * const recipient);
|
gboolean chat_session_exists(const char * const recipient);
|
||||||
void chat_session_end(const char * const recipient);
|
void chat_session_end(const char * const recipient);
|
||||||
gboolean chat_session_recipient_supports(const char * const recipient);
|
gboolean chat_session_get_recipient_supports(const char * const recipient);
|
||||||
|
void chat_session_set_recipient_supports(const char * const recipient,
|
||||||
|
gboolean recipient_supports);
|
||||||
|
|
||||||
void chat_session_set_active(const char * const recipient);
|
void chat_session_set_active(const char * const recipient);
|
||||||
void chat_session_no_activity(const char * const recipient);
|
void chat_session_no_activity(const char * const recipient);
|
||||||
|
69
src/jabber.c
69
src/jabber.c
@ -158,7 +158,8 @@ jabber_send(const char * const msg, const char * const recipient)
|
|||||||
text = xmpp_stanza_new(jabber_conn.ctx);
|
text = xmpp_stanza_new(jabber_conn.ctx);
|
||||||
xmpp_stanza_set_text(text, coded_msg3);
|
xmpp_stanza_set_text(text, coded_msg3);
|
||||||
|
|
||||||
if (chat_session_recipient_supports(recipient)) {
|
// always send <active/> with messages when recipient supports chat states
|
||||||
|
if (chat_session_get_recipient_supports(recipient)) {
|
||||||
active = xmpp_stanza_new(jabber_conn.ctx);
|
active = xmpp_stanza_new(jabber_conn.ctx);
|
||||||
xmpp_stanza_set_name(active, "active");
|
xmpp_stanza_set_name(active, "active");
|
||||||
xmpp_stanza_set_ns(active, "http://jabber.org/protocol/chatstates");
|
xmpp_stanza_set_ns(active, "http://jabber.org/protocol/chatstates");
|
||||||
@ -352,26 +353,62 @@ _message_handler(xmpp_conn_t * const conn,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, "body");
|
|
||||||
|
|
||||||
// if no message, check for chatstates
|
char from_cpy[strlen(from) + 1];
|
||||||
if (body == NULL) {
|
strcpy(from_cpy, from);
|
||||||
|
char *short_from = strtok(from_cpy, "/");
|
||||||
|
|
||||||
if (prefs_get_notify_typing() || prefs_get_intype()) {
|
//determine chatstate support of recipient
|
||||||
if (xmpp_stanza_get_child_by_name(stanza, "active") != NULL) {
|
gboolean recipient_supports = FALSE;
|
||||||
// active
|
|
||||||
} else if (xmpp_stanza_get_child_by_name(stanza, "composing") != NULL) {
|
|
||||||
// composing
|
|
||||||
prof_handle_typing(from);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
if ((xmpp_stanza_get_child_by_name(stanza, "active") != NULL) ||
|
||||||
|
(xmpp_stanza_get_child_by_name(stanza, "composing") != NULL) ||
|
||||||
|
(xmpp_stanza_get_child_by_name(stanza, "paused") != NULL) ||
|
||||||
|
(xmpp_stanza_get_child_by_name(stanza, "gone") != NULL) ||
|
||||||
|
(xmpp_stanza_get_child_by_name(stanza, "inactive") != NULL)) {
|
||||||
|
recipient_supports = TRUE;
|
||||||
}
|
}
|
||||||
|
|
||||||
// message body recieved
|
// create of update session
|
||||||
char *message = xmpp_stanza_get_text(body);
|
if (!chat_session_exists(short_from)) {
|
||||||
prof_handle_incoming_message(from, message);
|
chat_session_start(short_from, recipient_supports);
|
||||||
|
} else {
|
||||||
|
chat_session_set_recipient_supports(short_from, recipient_supports);
|
||||||
|
}
|
||||||
|
|
||||||
|
// deal with chat states
|
||||||
|
if (recipient_supports) {
|
||||||
|
|
||||||
|
// handle <composing/>
|
||||||
|
if (xmpp_stanza_get_child_by_name(stanza, "composing") != NULL) {
|
||||||
|
if (prefs_get_notify_typing() || prefs_get_intype()) {
|
||||||
|
prof_handle_typing(short_from);
|
||||||
|
}
|
||||||
|
|
||||||
|
// handle <paused/>
|
||||||
|
} else if (xmpp_stanza_get_child_by_name(stanza, "paused") != NULL) {
|
||||||
|
// do something
|
||||||
|
|
||||||
|
// handle <inactive/>
|
||||||
|
} else if (xmpp_stanza_get_child_by_name(stanza, "inactive") != NULL) {
|
||||||
|
// do something
|
||||||
|
|
||||||
|
// handle <gone/>
|
||||||
|
} else if (xmpp_stanza_get_child_by_name(stanza, "gone") != NULL) {
|
||||||
|
// do something
|
||||||
|
|
||||||
|
// handle <active/>
|
||||||
|
} else {
|
||||||
|
// do something
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// check for and deal with message
|
||||||
|
xmpp_stanza_t *body = xmpp_stanza_get_child_by_name(stanza, "body");
|
||||||
|
if (body != NULL) {
|
||||||
|
char *message = xmpp_stanza_get_text(body);
|
||||||
|
prof_handle_incoming_message(short_from, message);
|
||||||
|
}
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user