const char* connection_get_fulljid(void) { const char *jid = xmpp_conn_get_bound_jid(conn.xmpp_conn); if (jid) { return jid; } else { return xmpp_conn_get_jid(conn.xmpp_conn); } }
static void _connection_handler(xmpp_conn_t *const xmpp_conn, const xmpp_conn_event_t status, const int error, xmpp_stream_error_t *const stream_error, void *const userdata) { switch (status) { // login success case XMPP_CONN_CONNECT: log_debug("Connection handler: XMPP_CONN_CONNECT"); conn.conn_status = JABBER_CONNECTED; Jid *my_jid = jid_create(xmpp_conn_get_jid(conn.xmpp_conn)); conn.domain = strdup(my_jid->domainpart); jid_destroy(my_jid); conn.features_by_jid = g_hash_table_new_full(g_str_hash, g_str_equal, free, (GDestroyNotify)g_hash_table_destroy); g_hash_table_insert(conn.features_by_jid, strdup(conn.domain), g_hash_table_new_full(g_str_hash, g_str_equal, free, NULL)); session_login_success(connection_is_secured()); break; // disconnected case XMPP_CONN_DISCONNECT: log_debug("Connection handler: XMPP_CONN_DISCONNECT"); // lost connection for unknown reason if (conn.conn_status == JABBER_CONNECTED) { log_debug("Connection handler: Lost connection for unknown reason"); session_lost_connection(); // login attempt failed } else if (conn.conn_status != JABBER_DISCONNECTING) { log_debug("Connection handler: Login failed"); session_login_failed(); } // close stream response from server after disconnect is handled conn.conn_status = JABBER_DISCONNECTED; break; // connection failed case XMPP_CONN_FAIL: log_debug("Connection handler: XMPP_CONN_FAIL"); break; // unknown state default: log_error("Connection handler: Unknown status"); break; } }
static void _unavailable_handler(xmpp_stanza_t *const stanza) { inp_nonblocking(TRUE); xmpp_conn_t *conn = connection_get_conn(); const char *jid = xmpp_conn_get_jid(conn); const char *from = xmpp_stanza_get_from(stanza); if (!from) { log_warning("Unavailable presence received with no from attribute"); } log_debug("Unavailable presence handler fired for %s", from); Jid *my_jid = jid_create(jid); Jid *from_jid = jid_create(from); if (my_jid == NULL || from_jid == NULL) { jid_destroy(my_jid); jid_destroy(from_jid); return; } if (strcmp(my_jid->barejid, from_jid->barejid) !=0) { char *status_str = stanza_get_status(stanza, NULL); if (from_jid->resourcepart) { sv_ev_contact_offline(from_jid->barejid, from_jid->resourcepart, status_str); // hack for servers that do not send full jid with unavailable presence } else { sv_ev_contact_offline(from_jid->barejid, "__prof_default", status_str); } free(status_str); } else { if (from_jid->resourcepart) { connection_remove_available_resource(from_jid->resourcepart); } } jid_destroy(my_jid); jid_destroy(from_jid); }
static int _unavailable_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata) { const char *jid = xmpp_conn_get_jid(conn); char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); log_debug("Unavailable presence handler fired for %s", from); Jid *my_jid = jid_create(jid); Jid *from_jid = jid_create(from); if (my_jid == NULL || from_jid == NULL) { jid_destroy(my_jid); jid_destroy(from_jid); return 1; } char *status_str = stanza_get_status(stanza, NULL); if (strcmp(my_jid->barejid, from_jid->barejid) !=0) { if (from_jid->resourcepart != NULL) { handle_contact_offline(from_jid->barejid, from_jid->resourcepart, status_str); // hack for servers that do not send full jid with unavailable presence } else { handle_contact_offline(from_jid->barejid, "__prof_default", status_str); } } else { if (from_jid->resourcepart != NULL) { connection_remove_available_resource(from_jid->resourcepart); } } free(status_str); jid_destroy(my_jid); jid_destroy(from_jid); return 1; }
const char* connection_get_fulljid(void) { return xmpp_conn_get_jid(conn.xmpp_conn); }
static const char * _jabber_get_fulljid(void) { return xmpp_conn_get_jid(jabber_conn.conn); }
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; } // handler still fires for muc presence if (stanza_is_muc_presence(stanza)) { return 1; } char *from = xmpp_stanza_get_attribute(stanza, STANZA_ATTR_FROM); if (from) { log_info("Available presence handler fired for: %s", from); } else { log_info("Available presence handler fired"); } // exit when no from attribute if (!from) { log_warning("No from attribute found."); return 1; } // own jid is invalid const char *my_jid_str = xmpp_conn_get_jid(conn); Jid *my_jid = jid_create(my_jid_str); if (!my_jid) { if (my_jid_str) { log_error("Could not parse account JID: %s", my_jid_str); } else { log_error("Could not parse account JID: NULL"); } return 1; } // contact jid invalud Jid *from_jid = jid_create(from); if (!from_jid) { log_warning("Could not parse contact JID: %s", from); jid_destroy(my_jid); return 1; } // presence properties char *show_str = stanza_get_show(stanza, "online"); char *status_str = stanza_get_status(stanza, NULL); // presence last activity int idle_seconds = stanza_get_idle_time(stanza); GDateTime *last_activity = NULL; if (idle_seconds > 0) { GDateTime *now = g_date_time_new_now_local(); last_activity = g_date_time_add_seconds(now, 0 - idle_seconds); g_date_time_unref(now); } // priority int priority = 0; xmpp_stanza_t *priority_stanza = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_PRIORITY); if (priority_stanza != NULL) { char *priority_str = xmpp_stanza_get_text(priority_stanza); if (priority_str != NULL) { priority = atoi(priority_str); } free(priority_str); } // send disco info for capabilities, if not cached if ((g_strcmp0(my_jid->fulljid, from_jid->fulljid) != 0) && (stanza_contains_caps(stanza))) { log_info("Presence contains capabilities."); _handle_caps(stanza); } // create Resource Resource *resource = NULL; resource_presence_t presence = resource_presence_from_string(show_str); if (from_jid->resourcepart == NULL) { // hack for servers that do not send full jid resource = resource_new("__prof_default", presence, status_str, priority); } else { resource = resource_new(from_jid->resourcepart, presence, status_str, priority); } free(status_str); free(show_str); // check for self presence if (g_strcmp0(my_jid->barejid, from_jid->barejid) == 0) { connection_add_available_resource(resource); // contact presence } else { handle_contact_online(from_jid->barejid, resource, last_activity); } if (last_activity != NULL) { g_date_time_unref(last_activity); } jid_destroy(my_jid); jid_destroy(from_jid); 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; } // handler still fires for muc presence if (stanza_is_muc_presence(stanza)) { 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); if (my_jid == NULL || from_jid == NULL) { jid_destroy(my_jid); jid_destroy(from_jid); return 1; } char *show_str = stanza_get_show(stanza, "online"); char *status_str = stanza_get_status(stanza, NULL); int idle_seconds = stanza_get_idle_time(stanza); GDateTime *last_activity = NULL; char *caps_key = NULL; if (stanza_contains_caps(stanza)) { caps_key = _get_caps_key(stanza); } if (idle_seconds > 0) { GDateTime *now = g_date_time_new_now_local(); last_activity = g_date_time_add_seconds(now, 0 - idle_seconds); g_date_time_unref(now); } // get priority int priority = 0; xmpp_stanza_t *priority_stanza = xmpp_stanza_get_child_by_name(stanza, STANZA_NAME_PRIORITY); if (priority_stanza != NULL) { char *priority_str = xmpp_stanza_get_text(priority_stanza); if (priority_str != NULL) { priority = atoi(priority_str); } free(priority_str); } resource_presence_t presence = resource_presence_from_string(show_str); Resource *resource = NULL; // hack for servers that do not send fulljid with initial presence if (from_jid->resourcepart == NULL) { resource = resource_new("__prof_default", presence, status_str, priority, caps_key); } else { resource = resource_new(from_jid->resourcepart, presence, status_str, priority, caps_key); } // self presence if (strcmp(my_jid->barejid, from_jid->barejid) == 0) { connection_add_available_resource(resource); // contact presence } else { handle_contact_online(from_jid->barejid, resource, last_activity); } free(caps_key); free(status_str); free(show_str); jid_destroy(my_jid); jid_destroy(from_jid); if (last_activity != NULL) { g_date_time_unref(last_activity); } return 1; }
const char *xmpp_ua_get_jid(xmpp_ua_t *ua) { return xmpp_conn_get_jid(ua->conn); }
void xmpp_ua_get_unique_string(xmpp_ua_t *ua, char buf[128]) { unsigned int now = time(0); sprintf(buf, "%s_%u_%d", xmpp_conn_get_jid(ua->conn), now, xmpp_ua_get_unique_id()); }
static void _available_handler(xmpp_stanza_t *const stanza) { inp_nonblocking(TRUE); // handler still fires if error if (g_strcmp0(xmpp_stanza_get_type(stanza), STANZA_TYPE_ERROR) == 0) { return; } // 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; } // handler still fires for muc presence if (stanza_is_muc_presence(stanza)) { return; } int err = 0; XMPPPresence *xmpp_presence = stanza_parse_presence(stanza, &err); if (!xmpp_presence) { const char *from = NULL; switch(err) { case STANZA_PARSE_ERROR_NO_FROM: log_warning("Available presence handler fired with no from attribute."); break; case STANZA_PARSE_ERROR_INVALID_FROM: from = xmpp_stanza_get_from(stanza); log_warning("Available presence handler fired with invalid from attribute: %s", from); break; default: log_warning("Available presence handler fired, could not parse stanza."); break; } return; } else { char *jid = jid_fulljid_or_barejid(xmpp_presence->jid); log_debug("Presence available handler fired for: %s", jid); } xmpp_conn_t *conn = connection_get_conn(); const char *my_jid_str = xmpp_conn_get_jid(conn); Jid *my_jid = jid_create(my_jid_str); XMPPCaps *caps = stanza_parse_caps(stanza); if ((g_strcmp0(my_jid->fulljid, xmpp_presence->jid->fulljid) != 0) && caps) { log_info("Presence contains capabilities."); char *jid = jid_fulljid_or_barejid(xmpp_presence->jid); _handle_caps(jid, caps); } stanza_free_caps(caps); Resource *resource = stanza_resource_from_presence(xmpp_presence); if (g_strcmp0(xmpp_presence->jid->barejid, my_jid->barejid) == 0) { connection_add_available_resource(resource); } else { char *pgpsig = NULL; xmpp_stanza_t *x = xmpp_stanza_get_child_by_ns(stanza, STANZA_NS_SIGNED); if (x) { pgpsig = xmpp_stanza_get_text(x); } sv_ev_contact_online(xmpp_presence->jid->barejid, resource, xmpp_presence->last_activity, pgpsig); xmpp_ctx_t *ctx = connection_get_ctx(); xmpp_free(ctx, pgpsig); } jid_destroy(my_jid); stanza_free_presence(xmpp_presence); }