ConnectResult XMPPAccountHandler::connect() { UT_DEBUGMSG(("XMPPAccountHandler::connect()\n")); if (m_bLoggedIn) return CONNECT_ALREADY_CONNECTED; if (m_pConnection) return CONNECT_IN_PROGRESS; // try to request a frame here; note that this might return 0, for example on application startup XAP_Frame *pFrame = XAP_App::getApp()->getLastFocussedFrame(); const std::string server = getProperty("server"); const std::string username = getProperty("username"); const std::string port = getProperty("port"); // TODO: unused atm const std::string resource = getProperty("resource"); const std::string encryption = getProperty("encryption"); std::string jid = username + "@" + server; UT_DEBUGMSG(("Connecting to server: |%s|, username: |%s|, resource: |%s|\n", server.c_str(), username.c_str(), resource.c_str())); // NULL means perform SRV record lookup based on JID (Loudmouth 1.3.2+) m_pConnection = lm_connection_new(NULL); UT_return_val_if_fail(m_pConnection, CONNECT_INTERNAL_ERROR); lm_connection_set_jid(m_pConnection, jid.c_str()); // setup SSL if (lm_ssl_is_supported() && encryption == "true") { LmSSL* pSSL = lm_ssl_new(NULL, NULL, NULL, NULL); // TODO: free this lm_ssl_use_starttls(pSSL, TRUE, TRUE); lm_connection_set_ssl(m_pConnection, pSSL); lm_ssl_unref(pSSL); } GError* error = NULL; if (!lm_connection_open(m_pConnection, lm_connection_open_async_cb, this, NULL, &error)) { UT_DEBUGMSG(("Failed to open: %s\n", error ? error->message : "")); lm_connection_unref(m_pConnection); m_pConnection = NULL; if (pFrame) { // inform the user of the connection failure // TODO: this shouldn't be here, the caller should handle this UT_UTF8String msg; // TODO: make this localizable UT_UTF8String_sprintf(msg, "Error while connecting to %s: %s\n", server.c_str(), (error ? error->message : "")); pFrame->showMessageBox(msg.utf8_str(), XAP_Dialog_MessageBox::b_O, XAP_Dialog_MessageBox::a_OK); } return CONNECT_FAILED; } return CONNECT_IN_PROGRESS; }
void xmpp_init (void) { LmMessageHandler * handler; initialize_user_table(); connection = lm_connection_new (NULL); handler = lm_message_handler_new (xmpp_presence_callback, NULL, NULL); lm_connection_register_message_handler (connection, handler, LM_MESSAGE_TYPE_PRESENCE, 0); }
void xmpp_connect() { GError *err = NULL; const gchar *conf_server; LmSSL *ssl; int use_ssl, use_tls, port; const gchar *jid; wantconnection = 1; if (connection && lm_connection_is_open(connection)) { ui_print("connect: connection alredy opened, aborting\n"); return; } if (connection == NULL) connection = lm_connection_new(NULL); use_ssl = get_settings_int(USE_SSL); use_tls = get_settings_int(USE_TLS); jid = get_settings_str(JID); port = get_settings_int(PORT); if (use_ssl || use_tls) { if (!lm_ssl_is_supported()) { ui_print("Error: SSL not available\n"); } else if (use_ssl && use_tls) { ui_print("Error: You can't use ssl and tls at the same time"); } else { ssl = lm_ssl_new(NULL, ssl_cb, NULL, NULL); lm_ssl_use_starttls(ssl, !use_ssl, use_tls); lm_connection_set_ssl(connection, ssl); lm_ssl_unref(ssl); } } if (!port) { port = (use_ssl) ? LM_CONNECTION_DEFAULT_PORT_SSL : LM_CONNECTION_DEFAULT_PORT; } lm_connection_set_port(connection, port); lm_connection_set_jid(connection, jid); lm_connection_set_keep_alive_rate(connection, 30); lm_connection_set_disconnect_function(connection, connection_disconnect_cb, NULL, NULL); conf_server = get_settings_str(SERVER); if (!conf_server) { ui_print("ERROR: Insufficient configuration, " "connecting aborted (server not set)\n"); return; } else { if(!lm_connection_get_server(connection)) lm_connection_set_server(connection, conf_server); } ui_print("Opening connection to %s\n", conf_server); if(!lm_connection_open(connection, (LmResultFunction)connection_open_cb, NULL, NULL, &err)) { ui_print("Error opening connection: %s\n", err->message); g_error_free(err); } } /* xmpp_connect */
//doc Loudmouth registerAccount(server, username, password) Registers a new account at XMPP server. Returns <code>true</code> or <code>false</code>. IoObject *IoLoudmouth_registerAccount(IoLoudmouth *self, IoObject *locals, IoMessage *m) { char *server = IoMessage_locals_cStringArgAt_(m, locals, 0), *username = IoMessage_locals_cStringArgAt_(m, locals, 1), *password = IoMessage_locals_cStringArgAt_(m, locals, 2), *error_message = "Unknown error"; LmConnection *connection = lm_connection_new(server); LmMessage *xmpp_msg, *xmpp_reply; LmMessageNode *query, *node; int success = 0; if(!lm_connection_open_and_block(connection, NULL)) { error_message = "Could not open connection"; success = 0; } else { xmpp_msg = lm_message_new_with_sub_type(NULL, LM_MESSAGE_TYPE_IQ, LM_MESSAGE_SUB_TYPE_SET); query = lm_message_node_add_child(xmpp_msg->node, "query", NULL); lm_message_node_set_attributes(query, "xmlns", "jabber:iq:register", NULL); lm_message_node_add_child(query, "username", username); lm_message_node_add_child(query, "password", password); xmpp_reply = lm_connection_send_with_reply_and_block(connection, xmpp_msg, NULL); lm_connection_close(connection, NULL); lm_connection_unref(connection); if(!xmpp_reply) { success = 0; error_message = "No reply from server"; } else { switch(lm_message_get_sub_type(xmpp_reply)) { case LM_MESSAGE_SUB_TYPE_RESULT: success = 1; break; case LM_MESSAGE_SUB_TYPE_ERROR: default: success = 0; node = lm_message_node_find_child(xmpp_reply->node, "error"); error_message = (node == NULL) ? lm_message_node_get_value(node) : "Unknown error"; lm_message_node_unref(node); } } lm_message_unref(xmpp_reply); lm_message_unref(xmpp_msg); lm_message_node_unref(query); } free(server); free(username); free(password); IOASSERT(success, error_message); free(error_message); return IOBOOL(self, success); }
int main (int argc, char **argv) { LmConnection *conn; gchar *jid; LmSSL *ssl; Arguments arguments; GError *error = NULL; if (argc != 5) { print_usage (); return EXIT_FAILURE; } arguments.username = argv[1]; arguments.password = argv[2]; arguments.test_contact = argv[3]; arguments.test_message = argv[4]; /* Create a new mainloop to handle the default context */ main_loop = g_main_loop_new (NULL, FALSE); /* Create a new connection, don't set a specific host */ conn = lm_connection_new (NULL); ssl = lm_ssl_new (NULL, NULL, NULL, NULL); /* Require the use of STARTTLS */ lm_ssl_use_starttls (ssl, TRUE, TRUE); /* Use SSL for the connection */ lm_connection_set_ssl (conn, ssl); /* conn holds a ref now so we can safely remove the initial one */ lm_ssl_unref (ssl); jid = ensure_full_jid (argv[1]); /* Set the connection jid, this will implicitely lookup the host to * * connect to. */ lm_connection_set_jid (conn, jid); g_free (jid); if (!lm_connection_open (conn, open_cb, &arguments, NULL, &error)) { g_print ("Failed to open connection: %s\n", error->message); g_clear_error (&error); return EXIT_FAILURE; } /* Starting the mainloop with get Loudmouth to start process incoming * * events. This call will not return until g_main_loop_quit is called */ g_main_loop_run (main_loop); return EXIT_SUCCESS; }
int main (int argc, char **argv) { GMainLoop *main_loop; LmConnection *connection; LmMessageHandler *handler; gboolean result; UserInfo *info; LmProxy *proxy; guint proxy_port; if (argc < 6) { g_print ("Usage: %s <server> <username> <password> <proxyserver> <proxyport>\n", argv[0]); return 1; } connection = lm_connection_new (argv[1]); proxy = lm_proxy_new (LM_PROXY_TYPE_HTTP); lm_proxy_set_server (proxy, argv[4]); proxy_port = strtol (argv[5], (char **) NULL, 10); lm_proxy_set_port (proxy, proxy_port); lm_connection_set_proxy (connection, proxy); lm_proxy_unref (proxy); handler = lm_message_handler_new (handle_messages, NULL, NULL); lm_connection_register_message_handler (connection, handler, LM_MESSAGE_TYPE_MESSAGE, LM_HANDLER_PRIORITY_NORMAL); lm_message_handler_unref (handler); info = g_new0 (UserInfo, 1); info->name = g_strdup (argv[2]); info->passwd = g_strdup (argv[3]); result = lm_connection_open (connection, (LmResultFunction) connection_open_cb, info, NULL, NULL); if (!result) { g_print ("Opening connection failed: %d\n", result); } else { g_print ("Returned from the connection_open\n"); } main_loop = g_main_loop_new (NULL, FALSE); g_main_loop_run (main_loop); return 0; }
int main (int argc, char **argv) { GMainLoop *main_loop; LmConnection *connection; LmMessageHandler *handler; gboolean result; UserInfo *info; gchar *jid; if (argc < 6) { g_print ("Usage: %s <server> <username> <password> <connectserver> <connectport>\n", argv[0]); return 1; } connection = lm_connection_new (argv[4]); jid = g_strdup_printf ("%s@%s", argv[2], argv[1]); lm_connection_set_jid (connection, jid); g_free (jid); lm_connection_set_port (connection, strtol (argv[5], (char **) NULL, 10)); handler = lm_message_handler_new (handle_messages, NULL, NULL); lm_connection_register_message_handler (connection, handler, LM_MESSAGE_TYPE_MESSAGE, LM_HANDLER_PRIORITY_NORMAL); lm_message_handler_unref (handler); info = g_new0 (UserInfo, 1); info->name = g_strdup (argv[2]); info->passwd = g_strdup (argv[3]); result = lm_connection_open (connection, (LmResultFunction) connection_open_cb, info, NULL, NULL); if (!result) { g_print ("Opening connection failed: %d\n", result); } else { g_print ("Returned from the connection_open\n"); } main_loop = g_main_loop_new (NULL, FALSE); g_main_loop_run (main_loop); return 0; }
LM::Account::Account (boost::shared_ptr<Ekiga::PersonalDetails> details_, boost::shared_ptr<Dialect> dialect_, boost::shared_ptr<Cluster> cluster_, xmlNodePtr node_): details(details_), dialect(dialect_), cluster(cluster_), node(node_) { if (node == NULL) throw std::logic_error ("NULL node pointer received"); status = _("inactive"); xmlChar* xml_str = xmlGetProp (node, BAD_CAST "startup"); bool enable_on_startup = false; if (xml_str != NULL) { if (xmlStrEqual (xml_str, BAD_CAST "true")) { enable_on_startup = true; } else { enable_on_startup = false; } } xmlFree (xml_str); connection = lm_connection_new (NULL); LmMessageHandler* iq_lm_handler = lm_message_handler_new ((LmHandleMessageFunction)iq_handler_c, this, NULL); lm_connection_register_message_handler (connection, iq_lm_handler, LM_MESSAGE_TYPE_IQ, LM_HANDLER_PRIORITY_NORMAL); lm_message_handler_unref (iq_lm_handler); LmMessageHandler* presence_lm_handler = lm_message_handler_new ((LmHandleMessageFunction)presence_handler_c, this, NULL); lm_connection_register_message_handler (connection, presence_lm_handler, LM_MESSAGE_TYPE_PRESENCE, LM_HANDLER_PRIORITY_NORMAL); lm_message_handler_unref (presence_lm_handler); LmMessageHandler* message_lm_handler = lm_message_handler_new ((LmHandleMessageFunction)message_handler_c, this, NULL); lm_connection_register_message_handler (connection, message_lm_handler, LM_MESSAGE_TYPE_MESSAGE, LM_HANDLER_PRIORITY_NORMAL); lm_message_handler_unref (message_lm_handler); lm_connection_set_disconnect_function (connection, (LmDisconnectFunction)on_disconnected_c, this, NULL); if (enable_on_startup) { enable (); } }
static void start_registration(struct register_data *rd) { LmConnection *lmconn; GError *error = NULL; lmconn = lm_connection_new(NULL); if (rd->use_ssl) { if (!set_ssl(lmconn, &error, NULL, FALSE)) goto err; } else { if (!set_ssl(lmconn, &error, NULL, TRUE)) goto err; } if (settings_get_bool("xmpp_use_proxy") && !set_proxy(lmconn, &error)) goto err; if (rd->port <= 0) rd->port = rd->use_ssl ? LM_CONNECTION_DEFAULT_PORT_SSL : LM_CONNECTION_DEFAULT_PORT; lm_connection_set_server(lmconn, rd->address); lm_connection_set_port(lmconn, rd->port); lm_connection_set_jid(lmconn, NULL); rd->id = NULL; rd->lmconn = lmconn; rd->handler = NULL; register_data = g_slist_prepend(register_data, rd); lm_connection_set_disconnect_function(lmconn, register_lm_close_cb, rd, NULL); if (!lm_connection_open(lmconn, register_lm_open_cb, rd, NULL, &error)) { rd_cleanup(rd); signal_emit("xmpp register error", 3, rd->username, rd->domain, error != NULL ? error->message : NULL); if (error != NULL) g_error_free(error); } return; err: signal_emit("xmpp register error", 3, rd->username, rd->domain, error != NULL ? error->message : NULL); if (error != NULL) g_error_free(error); lm_connection_unref(lmconn); }
LM::Account::Account (boost::shared_ptr<Ekiga::PersonalDetails> details_, boost::shared_ptr<Dialect> dialect_, boost::shared_ptr<Cluster> cluster_, const std::string name, const std::string user, const std::string server, int port, const std::string resource, const std::string password, bool enable_on_startup): details(details_), dialect(dialect_), cluster(cluster_) { status = _("inactive"); node = xmlNewNode (NULL, BAD_CAST "entry"); xmlSetProp (node, BAD_CAST "name", BAD_CAST name.c_str ()); xmlSetProp (node, BAD_CAST "user", BAD_CAST user.c_str ()); xmlSetProp (node, BAD_CAST "server", BAD_CAST server.c_str ()); { std::stringstream sstream; sstream << port; xmlSetProp (node, BAD_CAST "port", BAD_CAST sstream.str ().c_str ()); } xmlSetProp (node, BAD_CAST "resource", BAD_CAST resource.c_str ()); xmlSetProp (node, BAD_CAST "password", BAD_CAST password.c_str ()); if (enable_on_startup) { xmlSetProp (node, BAD_CAST "startup", BAD_CAST "true"); } else { xmlSetProp (node, BAD_CAST "startup", BAD_CAST "false"); } connection = lm_connection_new (NULL); lm_connection_set_disconnect_function (connection, (LmDisconnectFunction)on_disconnected_c, this, NULL); if (enable_on_startup) { enable (); } }
void rebuild_connection(MP3tunesHarmony* harmony) { gchar* jid; /* if (harmony->connection != NULL) { lm_connection_unref(harmony->connection); harmony->connection = NULL; } if (harmony->harmony_download_message_handler != NULL) { lm_message_handler_unref(harmony->harmony_download_message_handler); harmony->harmony_download_message_handler = NULL; } */ harmony->connection = lm_connection_new(harmony->host); harmony->harmony_iq_message_handler = lm_message_handler_new(harmony_iq_callback, harmony, NULL); lm_connection_set_port(harmony->connection, harmony->port); jid = mp3tunes_harmony_get_jid(harmony); g_debug("Logging in with: %s", jid); lm_connection_set_jid(harmony->connection, jid); g_free(jid); lm_connection_register_message_handler(harmony->connection, harmony->harmony_iq_message_handler, LM_MESSAGE_TYPE_IQ, LM_HANDLER_PRIORITY_LAST); }
SERVER_REC * xmpp_server_init_connect(SERVER_CONNECT_REC *connrec) { XMPP_SERVER_REC *server; XMPP_SERVER_CONNECT_REC *conn = (XMPP_SERVER_CONNECT_REC *)connrec; char *recoded; if (conn->address == NULL || conn->address[0] == '\0') return NULL; if (conn->nick == NULL || conn->nick[0] == '\0') return NULL; g_return_val_if_fail(IS_XMPP_SERVER_CONNECT(conn), NULL); server = g_new0(XMPP_SERVER_REC, 1); server->chat_type = XMPP_PROTOCOL; server->user = xmpp_extract_user(conn->nick); server->domain = xmpp_have_domain(conn->nick) ? xmpp_extract_domain(conn->nick) : g_strdup(conn->address); server->jid = xmpp_have_domain(conn->nick) ? xmpp_strip_resource(conn->nick) : g_strconcat(server->user, "@", server->domain, (void *)NULL); server->resource = xmpp_extract_resource(conn->nick); if (server->resource == NULL) server->resource = g_strdup("irssi-xmpp"); server->priority = settings_get_int("xmpp_priority"); if (xmpp_priority_out_of_bound(server->priority)) server->priority = 0; server->ping_id = NULL; server->server_features = NULL; server->my_resources = NULL; server->roster = NULL; server->msg_handlers = NULL; server->channels_join = channels_join; server->isnickflag = isnickflag_func; server->ischannel = ischannel_func; server->get_nick_flags = get_nick_flags; server->send_message = send_message; server->connrec = (XMPP_SERVER_CONNECT_REC *)conn; server_connect_ref(connrec); /* don't use irssi's sockets */ server->connrec->no_connect = TRUE; server->connect_pid = -1; if (server->connrec->port <= 0) server->connrec->port = (server->connrec->use_ssl) ? LM_CONNECTION_DEFAULT_PORT_SSL : LM_CONNECTION_DEFAULT_PORT; if (conn->real_jid == NULL) conn->real_jid = conn->nick; else g_free(conn->nick); conn->nick = g_strdup(settings_get_bool("xmpp_set_nick_as_username") ? server->user : server->jid); /* init loudmouth connection structure */ server->lmconn = lm_connection_new(NULL); lm_connection_set_server(server->lmconn, server->connrec->address); lm_connection_set_port(server->lmconn, server->connrec->port); recoded = xmpp_recode_out(server->jid); lm_connection_set_jid(server->lmconn, recoded); g_free(recoded); lm_connection_set_keep_alive_rate(server->lmconn, 30); server->timeout_tag = 0; server_connect_init((SERVER_REC *)server); server->connect_tag = 1; return (SERVER_REC *)server; }