コード例 #1
0
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
ファイル: xmpp_conn.c プロジェクト: tadzik/gtkabber
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
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;
}
コード例 #4
0
ファイル: test-http-proxy.c プロジェクト: alban/loudmouth
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;
}
コード例 #5
0
ファイル: test-tunnel.c プロジェクト: GNUFreetalk/loudmouth
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;
}
コード例 #6
0
ファイル: registration.c プロジェクト: ailin-nemui/irssi-xmpp
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);
}
コード例 #7
0
ファイル: xmpp-servers.c プロジェクト: cdidier/irssi-xmpp
void
xmpp_server_connect(XMPP_SERVER_REC *server)
{
	GError *error;
	const char *err_msg;

	if (!IS_XMPP_SERVER(server))
		return;
	error = NULL;
	err_msg = NULL;
	if (server->connrec->use_ssl) {
		if (!set_ssl(server->lmconn, &error, server, FALSE)) {
			err_msg = "Cannot init ssl";
			goto err;
		}
	} else
		set_ssl(server->lmconn, &error, server, TRUE);
	if (settings_get_bool("xmpp_use_proxy")
	    && !set_proxy(server->lmconn, &error)) {
		err_msg = "Cannot set proxy";
		goto err;
	}
	lm_connection_set_disconnect_function(server->lmconn,
	    lm_close_cb, server, NULL);
	lookup_servers = g_slist_append(lookup_servers, server);
	signal_emit("server looking", 1, server);
	server->timeout_tag = g_timeout_add(
	    settings_get_time("server_connect_timeout"),
	    (GSourceFunc)check_connection_timeout, server);
	if (!lm_connection_open(server->lmconn,  lm_open_cb, server,
	    NULL, &error)) {
		err_msg = "Connection failed";
		goto err;
	}
	return;

err:
	server->connection_lost = TRUE;
	if (error != NULL) {
		server_connect_failed(SERVER(server), error->message);
		g_error_free(error);
	} else
		server_connect_failed(SERVER(server), err_msg);
}
コード例 #8
0
ファイル: harmony.c プロジェクト: KDE/amarok
gboolean open_connection(MP3tunesHarmony *harmony) {
    GError *err = NULL;

    rebuild_connection(harmony);

    if (!lm_connection_is_open(harmony->connection)) {
        lm_connection_open(harmony->connection, 
                           (LmResultFunction) open_connection_callback,
                           harmony,
                           NULL,
                           &err);

        if (err != NULL) {
            error_emit(harmony, MP3TUNES_HARMONY_ERROR_MISC, "Opening the connection failed", err);
            return FALSE;
        }
    }

    return TRUE;
}
コード例 #9
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;
}
コード例 #10
0
void
LM::Account::enable ()
{
  GError *error = NULL;
  xmlChar* server =  NULL;
  unsigned port = LM_CONNECTION_DEFAULT_PORT;
  LmSSL* ssl = NULL;

  server = xmlGetProp (node, BAD_CAST "server");
  {
    xmlChar* port_str = xmlGetProp (node, BAD_CAST "port");

    port = atoi ((const char*)port_str);
    xmlFree (port_str);
  }

  {
    gchar* jid = NULL;
    xmlChar* user = NULL;
    xmlChar* resource = NULL;
    user = xmlGetProp (node, BAD_CAST "user");
    resource = xmlGetProp (node, BAD_CAST "resource");
    jid = g_strdup_printf ("%s@%s/%s", user, server, resource);
    lm_connection_set_jid (connection, jid);
    g_free (jid);
    xmlFree (user);
    xmlFree (resource);
  }

  /* FIXME: this is an ugly workaround */
  if (g_strcmp0 ("gmail.com", (const char*)server) == 0)
    lm_connection_set_server (connection, "xmpp.l.google.com");
  else
    lm_connection_set_server (connection, (const char*)server);

  lm_connection_set_port (connection, port);

  ssl = lm_ssl_new (NULL, NULL, NULL, NULL);
  lm_ssl_use_starttls (ssl, TRUE, TRUE);
  lm_connection_set_ssl(connection, ssl);
  lm_ssl_unref (ssl);

  if ( !lm_connection_open (connection,
			    (LmResultFunction)on_connection_opened_c,
			    this, NULL, &error)) {

    gchar* message = NULL;

    message = g_strdup_printf (_("error connecting (%s)"), error->message);
    status = message;
    g_free (message);
    g_error_free (error);
  } else {

    status = _("connecting");
  }

  xmlFree (server);

  xmlSetProp (node, BAD_CAST "startup", BAD_CAST "true");
  trigger_saving ();

  updated ();
}
コード例 #11
0
ファイル: lm-send-async.c プロジェクト: GNUFreetalk/loudmouth
int
main (int argc, char **argv)
{
    GMainContext   *main_context;
    GOptionContext *context;
    LmConnection   *connection;

    context = g_option_context_new ("- test send message asynchronously");
    g_option_context_add_main_entries (context, entries, NULL);
    g_option_context_parse (context, &argc, &argv, NULL);
    g_option_context_free (context);
    
    if (!username || !password || !recipient) {
        g_printerr ("For usage, try %s --help\n", argv[0]);
        return EXIT_FAILURE;
    }

    if (username && strchr (username, '@') == NULL) {
        g_printerr ("LmSendAsync: Username must have an '@' included\n");
        return EXIT_FAILURE;
    }

    main_context = g_main_context_new ();
    connection = lm_connection_new_with_context (server, main_context);
    lm_connection_set_port (connection, port);
    lm_connection_set_jid (connection, username);

    if (fingerprint) {
        LmSSL *ssl;
        char  *p;
        int    i;
        
        if (port == LM_CONNECTION_DEFAULT_PORT) {
            lm_connection_set_port (connection,
                                    LM_CONNECTION_DEFAULT_PORT_SSL);
        }

        for (i = 0, p = fingerprint; *p && *(p+1); i++, p += 3) {
            expected_fingerprint[i] = (unsigned char) g_ascii_strtoull (p, NULL, 16);
        }
    
        ssl = lm_ssl_new (expected_fingerprint,
                          (LmSSLFunction) ssl_cb,
                          NULL, NULL);
    
        lm_ssl_use_starttls (ssl, TRUE, FALSE);

        lm_connection_set_ssl (connection, ssl);
        lm_ssl_unref (ssl);
    }

    if (!lm_connection_open (connection, 
                             (LmResultFunction) connection_open_cb,
                             NULL, NULL, NULL)) {
        g_printerr ("LmSendAsync: Could not open a connection\n");
        return EXIT_FAILURE;
    }

    main_loop = g_main_loop_new (main_context, FALSE);
    g_main_loop_run (main_loop);

    return (test_success ? EXIT_SUCCESS : EXIT_FAILURE);
}