void hybrid_pref_set_boolean(const gchar *name, const gboolean value) { xmlnode *node; g_return_if_fail(hybrid_pref != NULL); g_return_if_fail(hybrid_pref->root != NULL); if (!(node = xmlnode_find(hybrid_pref->root, name))) { node = xmlnode_new_child(hybrid_pref->root, name); } if (xmlnode_has_prop(node, "type")) { xmlnode_set_prop(node, "type", "bool"); } else { xmlnode_new_prop(node, "type", "bool"); } if (value) { xmlnode_set_content(node, "1"); } else { xmlnode_set_content(node, "0"); } }
gint hybrid_logs_write(HybridLogs *log, const gchar *name, const gchar *msg, gboolean sendout) { xmlnode *time_node; xmlnode *name_node; xmlnode *cont_node; xmlnode *node; time_t now; const gchar *body; struct tm *local_time; gchar time_str[128]; g_return_val_if_fail(log != NULL, HYBRID_ERROR); g_return_val_if_fail(name != NULL, HYBRID_ERROR); now = time(NULL); local_time = localtime(&now); /* log file doesn't exist, we create one. */ if (!log->root) { body = "<root></root>"; log->root = xmlnode_root(body, strlen(body)); } node = xmlnode_new_child(log->root, "m"); if (sendout) { xmlnode_new_prop(node, "type", "o"); } else { xmlnode_new_prop(node, "type", "i"); } time_node = xmlnode_new_child(node, "t"); memset(time_str, 0, sizeof(time_str)); strftime(time_str, sizeof(time_str) - 1, "%H:%M:%S", local_time); xmlnode_set_content(time_node, time_str); name_node = xmlnode_new_child(node, "n"); xmlnode_set_content(name_node, name); cont_node = xmlnode_new_child(node, "c"); xmlnode_set_content(cont_node, msg); xmlnode_save_file(log->root, log->log_path); return HYBRID_OK; }
/** * Client binds a resource. */ static void xmpp_stream_bind(XmppStream *stream) { xmlnode *node; IqRequest *iq; g_return_if_fail(stream != NULL); iq = iq_request_create(stream, IQ_TYPE_SET); node = xmlnode_new_child(iq->node, "bind"); xmlnode_new_namespace(node, NULL, NS_XMPP_BIND); node = xmlnode_new_child(node, "resource"); xmlnode_set_content(node, "Hybrid."); iq_request_set_callback(iq, resource_bind_cb, NULL); if (iq_request_send(iq) != HYBRID_OK) { hybrid_account_error_reason( stream->account->account, "binds a resource error."); iq_request_destroy(iq); return; } iq_request_destroy(iq); }
void hybrid_pref_set_int(const gchar *name, gint value) { xmlnode *node; gchar *value_string; g_return_if_fail(hybrid_pref != NULL); g_return_if_fail(hybrid_pref->root != NULL); if (!(node = xmlnode_find(hybrid_pref->root, name))) { node = xmlnode_new_child(hybrid_pref->root, name); } if (xmlnode_has_prop(node, "type")) { xmlnode_set_prop(node, "type", "int"); } else { xmlnode_new_prop(node, "type", "int"); } value_string = g_strdup_printf("%d", value); xmlnode_set_content(node, value_string); g_free(value_string); }
gint xmpp_message_send(XmppStream *stream, const gchar *text, const gchar *to) { xmlnode *root; xmlnode *node; gchar *xml_string; g_return_val_if_fail(stream != NULL, HYBRID_ERROR); g_return_val_if_fail(text != NULL, HYBRID_ERROR); g_return_val_if_fail(to != NULL, HYBRID_ERROR); root = xmlnode_create("message"); xmlnode_new_prop(root, "from", stream->jid); xmlnode_new_prop(root, "to", to); xmlnode_new_prop(root, "type", "chat"); node = xmlnode_new_child(root, "body"); xmlnode_set_content(node, text); xml_string = xmlnode_to_string(root); xmlnode_free(root); hybrid_debug_info("xmpp", "send message to %s:\n%s", to, xml_string); if (hybrid_ssl_write(stream->ssl, xml_string, strlen(xml_string)) == -1) { hybrid_debug_error("xmpp", "send message to %s failed\n", to); g_free(xml_string); return HYBRID_ERROR; } g_free(xml_string); return HYBRID_OK; }
void hybrid_pref_set_string(const gchar *name, const gchar *value) { xmlnode *node; g_return_if_fail(hybrid_pref != NULL); g_return_if_fail(hybrid_pref->root != NULL); if (!(node = xmlnode_find(hybrid_pref->root, name))) { node = xmlnode_new_child(hybrid_pref->root, name); } if (xmlnode_has_prop(node, "type")) { xmlnode_set_prop(node, "type", "string"); } else { xmlnode_new_prop(node, "type", "string"); } xmlnode_set_content(node, value); }
/** * Start sasl authentication. */ static void xmpp_stream_startsasl(XmppStream *stream) { guchar *auth; gchar *auth_encoded; gchar *xml_string; gint username_len; gint password_len; xmlnode *node; g_return_if_fail(stream != NULL); hybrid_debug_info("xmpp", "start sasl authentication."); hybrid_account_set_connection_string(stream->account->account, "Start sasl authentication."); xmpp_stream_set_state(stream, XMPP_STATE_SASL_AUTHENTICATING); /* * construct the authentication string to be base64 encoded, * which is in form of '\0 + username + \0 + password ' */ username_len = strlen(stream->account->username); password_len = strlen(stream->account->password); auth = g_malloc0(username_len + password_len + 2); auth[0] = '\0'; memcpy(auth + 1, stream->account->username, username_len); auth[username_len + 1] = '\0'; memcpy(auth + 2 + username_len, stream->account->password, password_len); auth_encoded = hybrid_base64_encode(auth, username_len + password_len + 2); g_free(auth); /* construct the xml string, which is in form of: * * <auth xmlns="urn:ietf:params:xml:ns:xmpp-sasl" * mechanism="PLAIN">encoded sasl string</auth> */ node = xmlnode_create("auth"); xmlnode_new_namespace(node, NULL, NS_XMPP_SASL); xmlnode_new_namespace(node, "ga", "http://www.google.com/talk/protocol/auth"); xmlnode_new_prop(node, "ga:client-uses-full-bind-result", "true"); xmlnode_new_prop(node, "mechanism", "PLAIN"); xmlnode_set_content(node, auth_encoded); g_free(auth_encoded); xml_string = xmlnode_to_string(node); xmlnode_free(node); hybrid_debug_info("xmpp", "sasl send:\n%s", xml_string); if (hybrid_ssl_write(stream->ssl, xml_string, strlen(xml_string)) == -1) { hybrid_account_error_reason(stream->account->account, "SASL authentication error."); return; } g_free(xml_string); }
gint hybrid_logs_write(HybridLogs *log, const gchar *name, const gchar *msg, gboolean sendout) { xmlnode *head_node; xmlnode *body_node; xmlnode *time_node; xmlnode *font_node; xmlnode *name_node; xmlnode *cont_node; xmlnode *node; time_t now; const gchar *body; gchar *title; gchar *content; struct tm *local_time; gchar time_str[128]; g_return_val_if_fail(log != NULL, HYBRID_ERROR); g_return_val_if_fail(name != NULL, HYBRID_ERROR); now = time(NULL); local_time = localtime(&now); /* log file doesn't exist, we create one. */ if (!log->root) { body = "<html></html>"; title = g_strdup_printf(_("Conversation with %s (%s) at %d-%d-%d"), name, log->id, local_time->tm_year + 1900, local_time->tm_mon, local_time->tm_mday); log->root = xmlnode_root(body, strlen(body)); head_node = xmlnode_new_child(log->root, "head"); node = xmlnode_new_child(head_node, "meta"); xmlnode_new_prop(node, "http-equiv", "content-type"); xmlnode_new_prop(node, "content", "text/html; charset=UTF-8"); node = xmlnode_new_child(head_node, "title"); xmlnode_set_content(node, title); node = xmlnode_new_child(log->root, "body"); node = xmlnode_new_child(node, "h3"); xmlnode_set_content(node, title); g_free(title); } if (!(body_node = xmlnode_find(log->root, "body"))) { hybrid_debug_error("logs", "invalid log file."); g_free(title); return HYBRID_ERROR; } node = xmlnode_new_child(body_node, "span"); font_node = xmlnode_new_child(node, "font"); if (sendout) { xmlnode_new_prop(font_node, "color", "#16569E"); } else { xmlnode_new_prop(font_node, "color", "#A82F2F"); } time_node = xmlnode_new_child(font_node, "font"); xmlnode_new_prop(time_node, "size", "2"); memset(time_str, 0, sizeof(time_str)); strftime(time_str, sizeof(time_str) - 1, "(%H:%M:%S) ", local_time); xmlnode_set_content(time_node, time_str); content = g_strdup_printf("%s: ", name); name_node = xmlnode_new_child(font_node, "b"); xmlnode_set_content(name_node, content); g_free(content); cont_node = xmlnode_new_child(node, "font"); xmlnode_set_content(cont_node, msg); xmlnode_new_child(node, "br"); xmlnode_save_file(log->root, log->log_path); return HYBRID_OK; }