mirror of
https://github.com/profanity-im/profanity.git
synced 2024-09-29 19:56:07 -04:00
Added usage of handlers to message and presence modules
This commit is contained in:
parent
8c9f916246
commit
ebc87b8b82
@ -269,7 +269,8 @@ connection_free_resources(void)
|
|||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
connection_error_handler(xmpp_stanza_t * const stanza)
|
connection_error_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
|
||||||
|
void * const userdata)
|
||||||
{
|
{
|
||||||
gchar *err_msg = NULL;
|
gchar *err_msg = NULL;
|
||||||
gchar *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
gchar *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
||||||
|
@ -28,7 +28,8 @@
|
|||||||
void connection_free_resources(void);
|
void connection_free_resources(void);
|
||||||
xmpp_conn_t *connection_get_conn(void);
|
xmpp_conn_t *connection_get_conn(void);
|
||||||
xmpp_ctx_t *connection_get_ctx(void);
|
xmpp_ctx_t *connection_get_ctx(void);
|
||||||
int connection_error_handler(xmpp_stanza_t * const stanza);
|
int connection_error_handler(xmpp_conn_t * const conn,
|
||||||
|
xmpp_stanza_t * const stanza, void * const userdata);
|
||||||
void connection_set_priority(int priority);
|
void connection_set_priority(int priority);
|
||||||
void connection_set_presence_message(const char * const message);
|
void connection_set_presence_message(const char * const message);
|
||||||
|
|
||||||
|
@ -37,17 +37,20 @@
|
|||||||
|
|
||||||
#define HANDLE(ns, type, func) xmpp_handler_add(conn, func, ns, STANZA_NAME_MESSAGE, type, ctx)
|
#define HANDLE(ns, type, func) xmpp_handler_add(conn, func, ns, STANZA_NAME_MESSAGE, type, ctx)
|
||||||
|
|
||||||
static int _message_handler(xmpp_conn_t * const conn,
|
static int _groupchat_message_handler(xmpp_conn_t * const conn,
|
||||||
|
xmpp_stanza_t * const stanza, void * const userdata);
|
||||||
|
static int _chat_message_handler(xmpp_conn_t * const conn,
|
||||||
xmpp_stanza_t * const stanza, void * const userdata);
|
xmpp_stanza_t * const stanza, void * const userdata);
|
||||||
static int _groupchat_message_handler(xmpp_stanza_t * const stanza);
|
|
||||||
static int _chat_message_handler(xmpp_stanza_t * const stanza);
|
|
||||||
|
|
||||||
void
|
void
|
||||||
message_add_handlers(void)
|
message_add_handlers(void)
|
||||||
{
|
{
|
||||||
xmpp_conn_t * const conn = connection_get_conn();
|
xmpp_conn_t * const conn = connection_get_conn();
|
||||||
xmpp_ctx_t * const ctx = connection_get_ctx();
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
||||||
HANDLE(NULL, NULL, _message_handler);
|
|
||||||
|
HANDLE(NULL, STANZA_TYPE_ERROR, connection_error_handler);
|
||||||
|
HANDLE(NULL, STANZA_TYPE_GROUPCHAT, _groupchat_message_handler);
|
||||||
|
HANDLE(NULL, STANZA_TYPE_CHAT, _chat_message_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -140,28 +143,8 @@ message_send_gone(const char * const recipient)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_message_handler(xmpp_conn_t * const conn,
|
_groupchat_message_handler(xmpp_conn_t * const conn,
|
||||||
xmpp_stanza_t * const stanza, void * const userdata)
|
xmpp_stanza_t * const stanza, void * const userdata)
|
||||||
{
|
|
||||||
gchar *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE);
|
|
||||||
|
|
||||||
if (type == NULL) {
|
|
||||||
log_error("Message stanza received with no type attribute");
|
|
||||||
return 1;
|
|
||||||
} else if (strcmp(type, STANZA_TYPE_ERROR) == 0) {
|
|
||||||
return connection_error_handler(stanza);
|
|
||||||
} else if (strcmp(type, STANZA_TYPE_GROUPCHAT) == 0) {
|
|
||||||
return _groupchat_message_handler(stanza);
|
|
||||||
} else if (strcmp(type, STANZA_TYPE_CHAT) == 0) {
|
|
||||||
return _chat_message_handler(stanza);
|
|
||||||
} else {
|
|
||||||
log_error("Message stanza received with unknown type: %s", type);
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static int
|
|
||||||
_groupchat_message_handler(xmpp_stanza_t * const stanza)
|
|
||||||
{
|
{
|
||||||
char *message = NULL;
|
char *message = NULL;
|
||||||
char *room_jid = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
char *room_jid = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
||||||
@ -227,7 +210,8 @@ _groupchat_message_handler(xmpp_stanza_t * const stanza)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_chat_message_handler(xmpp_stanza_t * const stanza)
|
_chat_message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
|
||||||
|
void * const userdata)
|
||||||
{
|
{
|
||||||
gchar *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
gchar *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
||||||
Jid *jid = jid_create(from);
|
Jid *jid = jid_create(from);
|
||||||
|
@ -39,11 +39,19 @@ static GHashTable *sub_requests;
|
|||||||
|
|
||||||
#define HANDLE(ns, type, func) xmpp_handler_add(conn, func, ns, STANZA_NAME_PRESENCE, type, ctx)
|
#define HANDLE(ns, type, func) xmpp_handler_add(conn, func, ns, STANZA_NAME_PRESENCE, type, ctx)
|
||||||
|
|
||||||
static int _presence_handler(xmpp_conn_t * const conn,
|
static int _unavailable_handler(xmpp_conn_t * const conn,
|
||||||
|
xmpp_stanza_t * const stanza, void * const userdata);
|
||||||
|
static int _subscribe_handler(xmpp_conn_t * const conn,
|
||||||
|
xmpp_stanza_t * const stanza, void * const userdata);
|
||||||
|
static int _subscribed_handler(xmpp_conn_t * const conn,
|
||||||
|
xmpp_stanza_t * const stanza, void * const userdata);
|
||||||
|
static int _unsubscribed_handler(xmpp_conn_t * const conn,
|
||||||
|
xmpp_stanza_t * const stanza, void * const userdata);
|
||||||
|
static int _available_handler(xmpp_conn_t * const conn,
|
||||||
xmpp_stanza_t * const stanza, void * const userdata);
|
xmpp_stanza_t * const stanza, void * const userdata);
|
||||||
static char* _handle_presence_caps(xmpp_stanza_t * const stanza);
|
static char* _handle_presence_caps(xmpp_stanza_t * const stanza);
|
||||||
static int _room_presence_handler(const char * const jid,
|
static int _room_presence_handler(xmpp_conn_t * const conn,
|
||||||
xmpp_stanza_t * const stanza);
|
xmpp_stanza_t * const stanza, void * const userdata);
|
||||||
|
|
||||||
void
|
void
|
||||||
presence_init(void)
|
presence_init(void)
|
||||||
@ -56,7 +64,14 @@ presence_add_handlers(void)
|
|||||||
{
|
{
|
||||||
xmpp_conn_t * const conn = connection_get_conn();
|
xmpp_conn_t * const conn = connection_get_conn();
|
||||||
xmpp_ctx_t * const ctx = connection_get_ctx();
|
xmpp_ctx_t * const ctx = connection_get_ctx();
|
||||||
HANDLE(NULL, NULL, _presence_handler);
|
|
||||||
|
HANDLE(NULL, STANZA_TYPE_ERROR, connection_error_handler);
|
||||||
|
HANDLE(STANZA_NS_MUC_USER, NULL, _room_presence_handler);
|
||||||
|
HANDLE(NULL, STANZA_TYPE_UNAVAILABLE, _unavailable_handler);
|
||||||
|
HANDLE(NULL, STANZA_TYPE_SUBSCRIBE, _subscribe_handler);
|
||||||
|
HANDLE(NULL, STANZA_TYPE_SUBSCRIBED, _subscribed_handler);
|
||||||
|
HANDLE(NULL, STANZA_TYPE_UNSUBSCRIBED, _unsubscribed_handler);
|
||||||
|
HANDLE(NULL, NULL, _available_handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
void
|
void
|
||||||
@ -215,32 +230,108 @@ presence_leave_chat_room(const char * const room_jid)
|
|||||||
xmpp_stanza_release(presence);
|
xmpp_stanza_release(presence);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
_unsubscribed_handler(xmpp_conn_t * const conn,
|
||||||
|
xmpp_stanza_t * const stanza, void * const userdata)
|
||||||
|
{
|
||||||
|
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
||||||
|
Jid *from_jid = jid_create(from);
|
||||||
|
log_debug("unsubscribed presence handler fired for %s", from);
|
||||||
|
|
||||||
|
prof_handle_subscription(from_jid->barejid, PRESENCE_UNSUBSCRIBED);
|
||||||
|
g_hash_table_remove(sub_requests, from_jid->barejid);
|
||||||
|
|
||||||
|
jid_destroy(from_jid);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_presence_handler(xmpp_conn_t * const conn,
|
_subscribed_handler(xmpp_conn_t * const conn,
|
||||||
|
xmpp_stanza_t * const stanza, void * const userdata)
|
||||||
|
{
|
||||||
|
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
||||||
|
Jid *from_jid = jid_create(from);
|
||||||
|
log_debug("subscribed presence handler fired for %s", from);
|
||||||
|
|
||||||
|
prof_handle_subscription(from_jid->barejid, PRESENCE_SUBSCRIBED);
|
||||||
|
g_hash_table_remove(sub_requests, from_jid->barejid);
|
||||||
|
|
||||||
|
jid_destroy(from_jid);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
_subscribe_handler(xmpp_conn_t * const conn,
|
||||||
|
xmpp_stanza_t * const stanza, void * const userdata)
|
||||||
|
{
|
||||||
|
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
||||||
|
Jid *from_jid = jid_create(from);
|
||||||
|
log_debug("subscribe presence handler fired for %s", from);
|
||||||
|
|
||||||
|
prof_handle_subscription(from_jid->barejid, PRESENCE_SUBSCRIBE);
|
||||||
|
g_hash_table_insert(sub_requests, strdup(from_jid->barejid), strdup(from_jid->barejid));
|
||||||
|
|
||||||
|
jid_destroy(from_jid);
|
||||||
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
_unavailable_handler(xmpp_conn_t * const conn,
|
||||||
xmpp_stanza_t * const stanza, void * const userdata)
|
xmpp_stanza_t * const stanza, void * const userdata)
|
||||||
{
|
{
|
||||||
const char *jid = xmpp_conn_get_jid(conn);
|
const char *jid = xmpp_conn_get_jid(conn);
|
||||||
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
||||||
char *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE);
|
log_debug("unavailable presence handler fired for %s", from);
|
||||||
|
|
||||||
Jid *my_jid = jid_create(jid);
|
Jid *my_jid = jid_create(jid);
|
||||||
Jid *from_jid = jid_create(from);
|
Jid *from_jid = jid_create(from);
|
||||||
|
|
||||||
if ((type != NULL) && (strcmp(type, STANZA_TYPE_ERROR) == 0)) {
|
char *status_str;
|
||||||
return connection_error_handler(stanza);
|
xmpp_stanza_t *status = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_STATUS);
|
||||||
|
if (status != NULL)
|
||||||
|
status_str = xmpp_stanza_get_text(status);
|
||||||
|
else
|
||||||
|
status_str = NULL;
|
||||||
|
|
||||||
|
if (strcmp(my_jid->barejid, from_jid->barejid) !=0) {
|
||||||
|
prof_handle_contact_offline(from_jid->barejid, "offline", status_str);
|
||||||
}
|
}
|
||||||
|
|
||||||
// handle chat room presence
|
jid_destroy(my_jid);
|
||||||
if (muc_room_is_active(from_jid)) {
|
jid_destroy(from_jid);
|
||||||
return _room_presence_handler(from_jid->str, stanza);
|
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
static int
|
||||||
|
_available_handler(xmpp_conn_t * const conn,
|
||||||
|
xmpp_stanza_t * const stanza, void * const userdata)
|
||||||
|
{
|
||||||
|
// handler still fires if error
|
||||||
|
if (g_strcmp0(xmpp_stanza_get_type(stanza), STANZA_TYPE_ERROR) == 0) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
// handler still fires if other types
|
||||||
|
if ((g_strcmp0(xmpp_stanza_get_type(stanza), STANZA_TYPE_UNAVAILABLE) == 0) ||
|
||||||
|
(g_strcmp0(xmpp_stanza_get_type(stanza), STANZA_TYPE_SUBSCRIBE) == 0) ||
|
||||||
|
(g_strcmp0(xmpp_stanza_get_type(stanza), STANZA_TYPE_SUBSCRIBED) == 0) ||
|
||||||
|
(g_strcmp0(xmpp_stanza_get_type(stanza), STANZA_TYPE_UNSUBSCRIBED) == 0)) {
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
const char *jid = xmpp_conn_get_jid(conn);
|
||||||
|
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
||||||
|
log_debug("available presence handler fired for %s", from);
|
||||||
|
|
||||||
|
Jid *my_jid = jid_create(jid);
|
||||||
|
Jid *from_jid = jid_create(from);
|
||||||
|
|
||||||
// handle regular presence
|
|
||||||
} else {
|
|
||||||
log_debug("Regular presence received from %s", from);
|
|
||||||
char *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE);
|
|
||||||
char *show_str, *status_str;
|
char *show_str, *status_str;
|
||||||
|
char *caps_key = _handle_presence_caps(stanza);
|
||||||
int idle_seconds = stanza_get_idle_time(stanza);
|
int idle_seconds = stanza_get_idle_time(stanza);
|
||||||
GDateTime *last_activity = NULL;
|
GDateTime *last_activity = NULL;
|
||||||
|
|
||||||
@ -250,7 +341,11 @@ _presence_handler(xmpp_conn_t * const conn,
|
|||||||
g_date_time_unref(now);
|
g_date_time_unref(now);
|
||||||
}
|
}
|
||||||
|
|
||||||
char *caps_key = _handle_presence_caps(stanza);
|
xmpp_stanza_t *show = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_SHOW);
|
||||||
|
if (show != NULL)
|
||||||
|
show_str = xmpp_stanza_get_text(show);
|
||||||
|
else
|
||||||
|
show_str = "online";
|
||||||
|
|
||||||
xmpp_stanza_t *status = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_STATUS);
|
xmpp_stanza_t *status = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_STATUS);
|
||||||
if (status != NULL)
|
if (status != NULL)
|
||||||
@ -258,43 +353,21 @@ _presence_handler(xmpp_conn_t * const conn,
|
|||||||
else
|
else
|
||||||
status_str = NULL;
|
status_str = NULL;
|
||||||
|
|
||||||
if (type == NULL) { // available
|
|
||||||
xmpp_stanza_t *show = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_SHOW);
|
|
||||||
if (show != NULL)
|
|
||||||
show_str = xmpp_stanza_get_text(show);
|
|
||||||
else
|
|
||||||
show_str = "online";
|
|
||||||
|
|
||||||
if (strcmp(my_jid->barejid, from_jid->barejid) !=0) {
|
if (strcmp(my_jid->barejid, from_jid->barejid) !=0) {
|
||||||
prof_handle_contact_online(from_jid->barejid, show_str, status_str, last_activity, caps_key);
|
prof_handle_contact_online(from_jid->barejid, show_str, status_str, last_activity, caps_key);
|
||||||
}
|
}
|
||||||
} else if (strcmp(type, STANZA_TYPE_UNAVAILABLE) == 0) {
|
|
||||||
if (strcmp(my_jid->barejid, from_jid->barejid) !=0) {
|
jid_destroy(my_jid);
|
||||||
prof_handle_contact_offline(from_jid->barejid, "offline", status_str);
|
jid_destroy(from_jid);
|
||||||
}
|
|
||||||
|
|
||||||
if (last_activity != NULL) {
|
if (last_activity != NULL) {
|
||||||
g_date_time_unref(last_activity);
|
g_date_time_unref(last_activity);
|
||||||
}
|
}
|
||||||
|
|
||||||
// subscriptions
|
|
||||||
} else if (strcmp(type, STANZA_TYPE_SUBSCRIBE) == 0) {
|
|
||||||
prof_handle_subscription(from_jid->barejid, PRESENCE_SUBSCRIBE);
|
|
||||||
g_hash_table_insert(sub_requests, strdup(from_jid->barejid), strdup(from_jid->barejid));
|
|
||||||
} else if (strcmp(type, STANZA_TYPE_SUBSCRIBED) == 0) {
|
|
||||||
prof_handle_subscription(from_jid->barejid, PRESENCE_SUBSCRIBED);
|
|
||||||
g_hash_table_remove(sub_requests, from_jid->barejid);
|
|
||||||
} else if (strcmp(type, STANZA_TYPE_UNSUBSCRIBED) == 0) {
|
|
||||||
prof_handle_subscription(from_jid->barejid, PRESENCE_UNSUBSCRIBED);
|
|
||||||
g_hash_table_remove(sub_requests, from_jid->barejid);
|
|
||||||
} else { /* unknown type */
|
|
||||||
log_debug("Received presence with unknown type '%s'", type);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
static char *
|
static char *
|
||||||
_handle_presence_caps(xmpp_stanza_t * const stanza)
|
_handle_presence_caps(xmpp_stanza_t * const stanza)
|
||||||
{
|
{
|
||||||
@ -387,16 +460,22 @@ _handle_presence_caps(xmpp_stanza_t * const stanza)
|
|||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
_room_presence_handler(const char * const jid, xmpp_stanza_t * const stanza)
|
_room_presence_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza,
|
||||||
|
void * const userdata)
|
||||||
{
|
{
|
||||||
char *room = NULL;
|
// handler still fires if error
|
||||||
char *nick = NULL;
|
if (g_strcmp0(xmpp_stanza_get_type(stanza), STANZA_TYPE_ERROR) == 0) {
|
||||||
|
|
||||||
if (!parse_room_jid(jid, &room, &nick)) {
|
|
||||||
log_error("Could not parse room jid: %s", room);
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *jid = xmpp_conn_get_jid(conn);
|
||||||
|
char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM);
|
||||||
|
Jid *my_jid = jid_create(jid);
|
||||||
|
Jid *from_jid = jid_create(from);
|
||||||
|
|
||||||
|
char *room = from_jid->barejid;
|
||||||
|
char *nick = from_jid->resourcepart;
|
||||||
|
|
||||||
// handle self presence
|
// handle self presence
|
||||||
if (stanza_is_muc_self_presence(stanza, jabber_get_jid())) {
|
if (stanza_is_muc_self_presence(stanza, jabber_get_jid())) {
|
||||||
char *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE);
|
char *type = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_TYPE);
|
||||||
@ -428,7 +507,7 @@ _room_presence_handler(const char * const jid, xmpp_stanza_t * const stanza)
|
|||||||
char *show_str, *status_str;
|
char *show_str, *status_str;
|
||||||
char *caps_key = _handle_presence_caps(stanza);
|
char *caps_key = _handle_presence_caps(stanza);
|
||||||
|
|
||||||
log_debug("Room presence received from %s", jid);
|
log_debug("Room presence received from %s", from_jid->fulljid);
|
||||||
|
|
||||||
xmpp_stanza_t *status = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_STATUS);
|
xmpp_stanza_t *status = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_STATUS);
|
||||||
if (status != NULL) {
|
if (status != NULL) {
|
||||||
@ -472,8 +551,8 @@ _room_presence_handler(const char * const jid, xmpp_stanza_t * const stanza)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
free(room);
|
jid_destroy(my_jid);
|
||||||
free(nick);
|
jid_destroy(from_jid);
|
||||||
|
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user