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;
}
示例#2
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 */
示例#3
0
gboolean
set_ssl(LmConnection *lmconn, GError **error, gpointer user_data,
    gboolean use_starttls)
{
	LmSSL *ssl;

	if (!lm_ssl_is_supported() && error != NULL) {
		*error = g_new(GError, 1);
		(*error)->message =
		    g_strdup("SSL is not supported in this build");
		return FALSE;
	}
	ssl = lm_ssl_new(NULL, lm_ssl_cb, user_data, NULL);
	lm_connection_set_ssl(lmconn, ssl);
	if (use_starttls)
		lm_ssl_use_starttls(ssl, TRUE, TRUE);
	lm_ssl_unref(ssl);
	return TRUE;
}
示例#4
0
文件: IoLoudmouth.c 项目: ADTSH/io
//doc Loudmouth connect Connects to the server. Returns <code>self</code>.
IoObject *IoLoudmouth_connect(IoLoudmouth *self, IoObject *locals, IoMessage *m) {
//  Q: Should we io_free() these?
  IoSeq* username   = IoObject_getSlot_(self, IOSYMBOL("username"));
  IoSeq* password   = IoObject_getSlot_(self, IOSYMBOL("password"));
  IoSeq* resource   = IoObject_getSlot_(self, IOSYMBOL("resource"));
  IoSeq* host       = IoObject_getSlot_(self, IOSYMBOL("host"));
  IoNumber* port    = IoObject_getSlot_(self, IOSYMBOL("port"));
  IoObject* use_ssl = IoObject_getSlot_(self, IOSYMBOL("useSsl"));

  IOASSERT(ISSEQ(username), "Loudmouth: username should be a Sequence");
  IOASSERT(ISSEQ(password), "Loudmouth: password should be a Sequence");
  IOASSERT(ISSEQ(resource), "Loudmouth: resource should be a Sequence");
  IOASSERT(ISSEQ(host),     "Loudmouth: host should be a Sequence");
  IOASSERT(ISNUMBER(port),  "Loudmouth: port should be a Number");

  if(LMCONN(self) == NULL) {
    LmConnection *connection = lm_connection_new_with_context(CSTRING(host), main_context);
    IoObject_setDataPointer_(self, connection);

    lm_connection_set_jid(connection, CSTRING(IoObject_getSlot_(self, IOSYMBOL("jid"))));
    lm_connection_set_port(connection, CNUMBER(port));

    if(ISTRUE(use_ssl) && lm_ssl_is_supported()) {
      LmSSL *ssl = lm_ssl_new(NULL, onSslError, NULL, NULL);
      lm_connection_set_ssl(connection, ssl);
      lm_ssl_unref(ssl);
    }

    LmMessageHandler* handler = lm_message_handler_new(onXmppMessage, self, NULL);
    lm_connection_register_message_handler(
      connection, handler,
      LM_MESSAGE_TYPE_MESSAGE, LM_HANDLER_PRIORITY_NORMAL
    );
    lm_message_handler_unref(handler);

    lm_connection_set_disconnect_function(connection, onXmppDisconnect, NULL, NULL);
  }

  lm_connection_open(LMCONN(self), onXmppConnect, self, NULL, NULL);
  return self;
}
示例#5
0
文件: IoLoudmouth.c 项目: ADTSH/io
//doc Loudmouth isSslSupported
IoObject *IoLoudmouth_isSSLSupported(IoLoudmouth *self, IoObject *locals, IoMessage *m) {
  return IOBOOL(self, lm_ssl_is_supported());
}