Exemplo n.º 1
0
/*
 * Соеденение с сервером и аутентификация
 */
LmConnection* jid_connect(const struct _jid *jid, GMainContext *context) {
  LmConnection  *new_connect;
  GError        *error = NULL;
  
  // Проверяем context
  if(!context)
    context = g_main_context_default();

  // Создаём соединение и пытаемся соедениться
  new_connect = lm_connection_new_with_context(jid->server, 
                                          context);
  if(!lm_connection_open_and_block(new_connect, &error)) {
    g_print ("Couldn't open connection to '%s':\n%s\n",
            jid->server, error->message);
    return NULL;
  }
  // Авторизируемся
  if(!lm_connection_authenticate_and_block(new_connect, jid->username,
                               jid->password, jid->resource, &error)) {
    g_print("Couldn't authenticate with '%s' '%s':\n%s\n",
             jid->username, jid->password, error->message);
    return NULL;
  }

  printf("Established connect with %s\n", jid->server);
  return new_connect;
}
Exemplo n.º 2
0
Arquivo: xmpp.c Projeto: grouzen/xinb
gboolean xmpp_connect(Xinb *x)
{
    gchar *server = g_hash_table_lookup(x->config, "server");
    gchar *username = g_hash_table_lookup(x->config, "username");
    gchar *password = g_hash_table_lookup(x->config, "password");
    gchar *resource = g_hash_table_lookup(x->config, "resource");
    
    x->conn = lm_connection_new_with_context(server, x->context);
    x->state = LM_CONNECTION_STATE_OPENING;
    
    if(!lm_connection_open_and_block(x->conn, &(x->gerror))) {
        log_record(x, LOGS_ERR, "Couldn't open new connection to '%s': %s",
                   server, x->gerror->message);
        g_clear_error(&(x->gerror));
        return FALSE;
    }
    x->state = LM_CONNECTION_STATE_OPEN;

    if(!lm_connection_authenticate_and_block(x->conn,  username, password,
                                             resource, &(x->gerror))) {
        log_record(x, LOGS_ERR, "Couldn't authenticate with '%s', '%s': %s",
                   username, password, x->gerror->message);
        g_clear_error(&(x->gerror));
        return FALSE;
    }
    x->state = LM_CONNECTION_STATE_AUTHENTICATED;
    
    log_record(x, LOGS_INFO, "'%s@%s/%s' has been connected",
               username, server, resource);
    
    return TRUE;
}
Exemplo n.º 3
0
//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;
}
Exemplo n.º 4
0
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);
}
Exemplo n.º 5
0
gboolean jid_reg(struct _jid *jid, GMainContext *context) {
  LmConnection  *reg_connect;
  LmMessage     *m, *reply;
  LmMessageNode *query, *node;
  GError        *error = NULL;
  
  // Проверяем context
  if(!context)
    context = g_main_context_default();
  
  // Подключаемся
  reg_connect = lm_connection_new_with_context(jid->server, context);
  // Настраиваем ssl
/*  if(jid->use_ssl) {
    LmSSL *ssl;

    // Проверяем поддержку ssl
    if(!lm_ssl_is_supported()) {
      g_print("Your Loudmouth doesn't support SSL. Reinstall loudmouth.\n");
      jid->use_ssl = FALSE;
    }

    g_print("Configure ssl\n");
    ssl = lm_ssl_new(NULL, LM_SSL_RESPONSE_CONTINUE, NULL, NULL);
    lm_connection_set_ssl(reg_connect, ssl);
    lm_ssl_unref(ssl);

    lm_connection_set_port(reg_connect, LM_CONNECTION_DEFAULT_PORT_SSL);

  }
*/
  // Проверяем коннект
  if(!lm_connection_open_and_block(reg_connect, &error)) {
    fprintf(stderr, "Failed to open connection: %s\n", error->message);
    return FALSE;
  }

  // Определяем сообщение
  m = lm_message_new_with_sub_type(NULL, LM_MESSAGE_TYPE_IQ,
                                    LM_MESSAGE_SUB_TYPE_SET);

  // Составляем запрос
  query = lm_message_node_add_child(m->node, "query", NULL);
  lm_message_node_set_attributes(query, "xmlns", "jabber:iq:register", NULL);
  lm_message_node_add_child(query, "username", jid->username);
  lm_message_node_add_child(query, "password", jid->password);

  // Отпревляем сообщение и ждём ответ
  reply = lm_connection_send_with_reply_and_block(reg_connect, m, &error);

  if(!reply) {
    fprintf(stderr, "Failed to send registration request on server \"%s\":\n %s\n",
             jid->server, error->message);
    return FALSE;
  } 
  
  //Закрываем соединение
  lm_connection_close(reg_connect, NULL);
  lm_connection_unref(reg_connect);    
  
  // Проверяем ответ
  switch(lm_message_get_sub_type(reply)) {
    case LM_MESSAGE_SUB_TYPE_RESULT:
      g_print("Succeeded in register account '%s@%s'\n",
                                   jid->username, jid->server);
      break;

    case LM_MESSAGE_SUB_TYPE_ERROR:
    default:
      g_print("Failed to register account '%s@%s' due to: ",
                        jid->username, jid->server);

      node = lm_message_node_find_child(reply->node, "error");
      if(node)
        g_print("%s\n", lm_message_node_get_value (node));
      else
        g_print("Unknown error\n");

      return FALSE;
      break;
  } 
  
  return TRUE;
}