Пример #1
0
void init_xmpp(void) {
  FILE *fd = NULL;
  char xmpp_password[11] = {'\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0', '\0'};
  xmpp_log_t *log;
  
  xmpp_initialize();
  
  fd = fopen("secrets_xmpp", "r");
  if (fd) {
    fgets(xmpp_password, 11, fd);
    fclose(fd);
    if (xmpp_password == NULL) {
      perror("Unable to read password for XMPP server.");
      exit(EXIT_FAILURE);
    }
  } else {
    perror("Unable to open secrets_xmpp file");
    exit(EXIT_FAILURE);
  }

  /* create a context */
  log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */
  ctx = xmpp_ctx_new(NULL, NULL);

  /* create a connection */
  conn = xmpp_conn_new(ctx);

  /* setup authentication information */
  xmpp_conn_set_jid(conn, "*****@*****.**");
  xmpp_conn_set_pass(conn, xmpp_password);

}
Пример #2
0
int main(int argc, char **argv)
{
    xmpp_ctx_t *ctx;
    xmpp_conn_t *conn;
    xmpp_log_t *log;
    char *jid, *pass;

    /* take a jid and password on the command line */
    if (argc != 3) {
        fprintf(stderr, "Usage: bot <jid> <pass>\n\n");
        return 1;
    }

    jid = argv[1];
    pass = argv[2];

    /* init library */
    xmpp_initialize();

    /* create a context */
    log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */
    ctx = xmpp_ctx_new(NULL, log);

    /* create a connection */
    conn = xmpp_conn_new(ctx);

    /*
     * also you can disable TLS support or force legacy SSL
     * connection without STARTTLS
     *
     * see xmpp_conn_set_flags() or examples/basic.c
     */

    /* setup authentication information */
    xmpp_conn_set_jid(conn, jid);
    xmpp_conn_set_pass(conn, pass);

    /* initiate connection */
    xmpp_connect_client(conn, NULL, 0, conn_handler, ctx);

    /* enter the event loop - 
       our connect handler will trigger an exit */
    xmpp_run(ctx);

    /* release our connection and context */
    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);

    /* final shutdown of the library */
    xmpp_shutdown();

    return 0;
}
Пример #3
0
int main(int argc, char **argv)
{
    xmpp_ctx_t *ctx;
    xmpp_conn_t *conn;
    xmpp_log_t *log;
    char *jid, *pass, *host;

    /* take a jid and password on the command line */
    if (argc < 3 || argc > 4) {
        fprintf(stderr, "Usage: basic <jid> <pass> [<host>]\n\n");
        return 1;
    }

    jid = argv[1];
    pass = argv[2];
    host = NULL;

    if (argc == 4)
        host = argv[3];

    /* init library */
    xmpp_initialize();

    /* create a context */
    log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */
    ctx = xmpp_ctx_new(NULL, log);

    /* create a connection */
    conn = xmpp_conn_new(ctx);

    /* setup authentication information */
    xmpp_conn_set_jid(conn, jid);
    xmpp_conn_set_pass(conn, pass);

    /* initiate connection */
    xmpp_connect_client(conn, host, 0, conn_handler, ctx);

    /* enter the event loop -
       our connect handler will trigger an exit */
    xmpp_run(ctx);

    /* release our connection and context */
    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);

    /* final shutdown of the library */
    xmpp_shutdown();

    return 0;
}
Пример #4
0
xmpp_conn_t*  XMPP_Init(char* jid, char* pass, char* host, xmpp_ctx_t  **pctx)
{
    xmpp_conn_t *conn;
    xmpp_log_t *log;

    printf("jid=[%s] pass=[%s] host=[%s]", jid, pass, host);
    xmpp_initialize();

    /* create a context */
    log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */
    *pctx = xmpp_ctx_new(NULL, log);

    /* create a connection */
    conn = xmpp_conn_new(*pctx);
    /* setup authentication information */
    xmpp_conn_set_jid(conn, jid);
    xmpp_conn_set_pass(conn, pass);

    /* initiate connection */
    xmpp_connect_client(conn, host, 0, conn_handler, *pctx);
    return conn;
}
Пример #5
0
int main(int argc, char *argv[])
{
	xmpp_ctx_t *ctx;
	xmpp_conn_t *conn;
	xmpp_log_t *log;
	const char *username;
	const char *password;

	username = bot_get_username();
	password = bot_get_password();

	xmpp_initialize();

	/* context */
	log = xmpp_get_default_logger(XMPP_LEVEL_INFO);
	ctx = xmpp_ctx_new(NULL, log);

	/* 创建连接 */
	conn = xmpp_conn_new(ctx);

	/* 设置认证信息 */
	xmpp_conn_set_jid(conn, username);
	xmpp_conn_set_pass(conn, password);

	/* 初始化连接 */
	xmpp_connect_client(conn, NULL, 0, bot_conn_handler, ctx);

	/* 进入主循环 */
	xmpp_run(ctx);

	/* 断开连接和释放资源 */
	xmpp_conn_release(conn);
	xmpp_ctx_free(ctx);

	xmpp_shutdown();
	
	return 0;
}
Пример #6
0
    int
main(int argc, char **argv)
{
    xmppipe_state_t *state = NULL;
    xmpp_log_t *log = NULL;
    char *jid = NULL;
    char *pass = NULL;
    char *addr = NULL;
    u_int16_t port = 0;
    int flags = 0;

    int ch = 0;

    if (setvbuf(stdout, NULL, _IOLBF, 0) < 0)
        err(EXIT_FAILURE, "setvbuf");

    state = xmppipe_calloc(1, sizeof(xmppipe_state_t));

    xmppipe_next_state(state, XMPPIPE_S_CONNECTING);
    state->bufsz = 2049;
    state->poll = 10;
    state->keepalive = 60 * 1000;
    state->keepalive_limit = 3;
    state->sm_request_interval = 1;
    state->sm_fc = 15;
    state->sm_unacked = 5;
    state->room = xmppipe_roomname("stdout");
    state->resource = xmppipe_strdup(XMPPIPE_RESOURCE);
    state->opt |= XMPPIPE_OPT_GROUPCHAT;

    jid = xmppipe_getenv("XMPPIPE_USERNAME");
    pass = xmppipe_getenv("XMPPIPE_PASSWORD");

    if (xmppipe_sandbox_init(state) < 0)
        err(EXIT_FAILURE, "sandbox failed");

    while ( (ch = getopt_long(argc, argv, "a:b:c:dDeF:hI:k:K:o:P:p:r:sS:u:U:vx",
                    long_options, NULL)) != -1) {
        switch (ch) {
            case 'u':
                /* username/jid */
                free(jid);
                jid = xmppipe_strdup(optarg);
                break;
            case 'p':
                /* password */
                free(pass);
                pass = xmppipe_strdup(optarg);
                break;
            case 'o':
                /* output/muc */
                free(state->room);
                state->room = xmppipe_strdup(optarg);
                break;
            case 'a': {
                    /* address:port */
                    char *p = NULL;
                    free(addr);
                    addr = xmppipe_strdup(optarg);
                    p = strchr(addr, ':');
                    if (p) {
                        *p++ = '\0';
                        port = xmppipe_strtonum(state, p, 0, 0xfffe);
                    }
                }
                break;
            case 'r':
                free(state->resource);
                state->resource = xmppipe_strdup(optarg);
                break;
            case 'S':
                free(state->subject);
                state->subject = xmppipe_strdup(optarg);
                break;
            case 'v':
                state->verbose++;
                break;
            case 'F':
                if (strcmp(optarg, "text") == 0)
                    state->format = XMPPIPE_FMT_TEXT;
                else if (strcmp(optarg, "csv") == 0)
                    state->format = XMPPIPE_FMT_CSV;
                else
                    usage(state);

                break;
            case 'x':
                state->encode = 1;
                break;

            case 'b':
                /* read buffer size */
                state->bufsz = xmppipe_strtonum(state, optarg, 3, 0xfffe);
                break;
            case 'c':
                /* XEP-0198: stream management flow control */
                state->sm_fc = xmppipe_strtonum(state, optarg, 0, 0xfffe);
                break;
            case 'I':
                /* XEP-0198: stream management request interval */
                state->sm_request_interval = xmppipe_strtonum(state, optarg, 0,
                        0xfffe);
                break;
            case 'k':
                /* XEP-0199: XMPP ping keepalives */
                state->sm_request_interval = xmppipe_strtonum(state, optarg, 0,
                        0xfffe) * 1000;
                break;
            case 'K':
                /* XEP-0199: number of keepalive without a reply */
                state->keepalive_limit = xmppipe_strtonum(state, optarg, 1,
                        0xfffe);
                break;
            case 'P':
                /* poll delay */
                state->poll = xmppipe_strtonum(state, optarg, 0, 0xfffe);
                break;
            case 'U':
                /* XEP-0198: stream management unacked requests */
                state->sm_unacked = xmppipe_strtonum(state, optarg, 0, 0xfffe);
                break;

            case 'd':
                state->opt |= XMPPIPE_OPT_DISCARD;
                break;
            case 'D':
                state->opt |= XMPPIPE_OPT_DISCARD;
                state->opt |= XMPPIPE_OPT_DISCARD_TO_STDOUT;
                break;
            case 'e':
                state->opt |= XMPPIPE_OPT_EOF;
                break;
            case 's':
                state->opt |= XMPPIPE_OPT_SIGPIPE;
                break;

            case OPT_NO_TLS_VERIFY:
                flags |= XMPP_CONN_FLAG_TRUST_TLS;
                break;
            case OPT_CHAT:
                state->opt &= ~XMPPIPE_OPT_GROUPCHAT;
                break;

            case OPT_CHAT_MARKER:
                state->opt |= XMPPIPE_OPT_CHAT_MARKER;
                break;

            case 'h':
            default:
                usage(state);
        }
    }

    argc -= optind;
    argv += optind;

    if (argc > 0) {
      free(state->room);
      state->room = xmppipe_strdup(argv[0]);
    }

    if (jid == NULL)
        usage(state);

    if (state->encode && BASE64_LENGTH(state->bufsz) + 1 > 0xffff)
        usage(state);

    state->server = xmppipe_servername(jid);

    if (strchr(state->room, '@')) {
        state->out = xmppipe_strdup(state->room);
        state->mucjid = xmppipe_mucjid(state->out, state->resource);
    }
    else if (!(state->opt & XMPPIPE_OPT_GROUPCHAT)) {
        state->out = xmppipe_strdup(jid);
    }

    if (xmppipe_fmt_init() < 0)
        errx(EXIT_FAILURE, "xmppipe_fmt_init");

    xmpp_initialize();

    log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);

    state->ctx = xmpp_ctx_new(NULL, (state->verbose > 1 ? log : NULL));
    if (state->ctx == NULL)
        errx(EXIT_FAILURE, "could not allocate context");

    state->conn = xmpp_conn_new(state->ctx);
    if (state->conn == NULL)
        errx(EXIT_FAILURE, "could not allocate connection");

    if (xmpp_conn_set_flags(state->conn, flags) < 0)
        errx(EXIT_FAILURE, "failed to set connection flags");

    xmpp_conn_set_jid(state->conn, jid);
    xmpp_conn_set_pass(state->conn, pass);

    if (xmpp_connect_client(state->conn, addr, port, handle_connection, state) < 0)
        errx(EXIT_FAILURE, "connection failed");

    if (xmppipe_connect_init(state) < 0)
        errx(EXIT_FAILURE, "XMPP handshake failed");

    if (state->verbose)
        (void)fprintf(stderr, "sandbox: stdin: %s\n", XMPPIPE_SANDBOX);

    if (xmppipe_sandbox_stdin(state) < 0)
        err(EXIT_FAILURE, "sandbox failed");

    if (xmppipe_stream_init(state) < 0)
        errx(EXIT_FAILURE, "enabling stream management failed");

    if ( (state->opt & XMPPIPE_OPT_GROUPCHAT) && xmppipe_muc_init(state) < 0)
        errx(EXIT_FAILURE, "failed to join MUC");

    if (xmppipe_presence_init(state) < 0)
        errx(EXIT_FAILURE, "publishing presence failed");

    if ( (state->opt & XMPPIPE_OPT_GROUPCHAT) && state->subject)
        xmppipe_muc_subject(state, state->subject);

    event_loop(state);

    xmppipe_stream_close(state);
    (void)xmpp_conn_release(state->conn);
    xmpp_ctx_free(state->ctx);
    xmpp_shutdown();

    return 0;
}
Пример #7
0
int main(int argc, char **argv)
{
    xmpp_ctx_t *ctx;
    xmpp_conn_t *conn;
    xmpp_log_t *log;
    char *jid, *pass, *host = NULL;
    long flags = 0;
    int i;

    /* take a jid and password on the command line */
    for (i = 1; i < argc; ++i) {
        if (strcmp(argv[i], "--disable-tls") == 0)
            flags |= XMPP_CONN_FLAG_DISABLE_TLS;
        else if (strcmp(argv[i], "--mandatory-tls") == 0)
            flags |= XMPP_CONN_FLAG_MANDATORY_TLS;
        else if (strcmp(argv[i], "--legacy-ssl") == 0)
            flags |= XMPP_CONN_FLAG_LEGACY_SSL;
        else
            break;
    }
    if ((argc - i) < 2 || (argc - i) > 3) {
        fprintf(stderr, "Usage: basic [options] <jid> <pass> [<host>]\n\n"
                        "Options:\n"
                        "  --disable-tls        Disable TLS.\n"
                        "  --mandatory-tls      Deny plaintext connection.\n"
                        "  --legacy-ssl         Use old style SSL.\n\n"
                        "Note: --disable-tls conflicts with --mandatory-tls or "
                              "--legacy-ssl\n");
        return 1;
    }

    jid = argv[i];
    pass = argv[i + 1];
    if (i + 2 < argc)
        host = argv[i + 2];

    /*
     * Note, this example doesn't handle errors. Applications should check
     * return values of non-void functions.
     */

    /* init library */
    xmpp_initialize();

    /* create a context */
    log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); /* pass NULL instead to silence output */
    ctx = xmpp_ctx_new(NULL, log);

    /* create a connection */
    conn = xmpp_conn_new(ctx);

    /* configure connection properties (optional) */
    xmpp_conn_set_flags(conn, flags);

    /* setup authentication information */
    xmpp_conn_set_jid(conn, jid);
    xmpp_conn_set_pass(conn, pass);

    /* initiate connection */
    xmpp_connect_client(conn, host, 0, NULL, conn_handler, ctx);

    /* enter the event loop -
       our connect handler will trigger an exit */
    xmpp_run(ctx);

    /* release our connection and context */
    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);

    /* final shutdown of the library */
    xmpp_shutdown();

    return 0;
}
Пример #8
0
int onreceive(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata);
void sendmsg(char* recipient, char* message);
//int handle_roster(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata);

int main(int argc, char** argv)
{
    /* Parse command-line options */
    OPTBEGIN {
        default:
            usage();
    } OPTEND;
    /* Parse config file if it exists */
    load_config();
    /* Start the connection */
    xmpp_initialize();
    xmpp_log_t*  log  = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
    Ctx  = xmpp_ctx_new(NULL, log);
    Conn = xmpp_conn_new(Ctx);
    /* setup authentication information and connect */
    xmpp_conn_set_flags(Conn, Flags);
    xmpp_conn_set_jid(Conn,  User);
    xmpp_conn_set_pass(Conn, Pass);
    xmpp_connect_client(Conn, Server, Port, onconnect, Ctx);
    /* enter the event loop our connect handler will trigger an exit */
    xmpp_run(Ctx);
    /* gracefully shut everything down */
    xmpp_conn_release(Conn);
    xmpp_ctx_free(Ctx);
    xmpp_shutdown();
    return 0;
}
Пример #9
0
int main(int argc, char **argv)
{
    safe_mem_init();


    xmpp_ctx_t *ctx;
    xmpp_conn_t *conn;
    xmpp_log_t *log;

    char *jid = NULL, *pass = NULL, *host = NULL;

    if (argc > 1) {
        jid = argv[1];
    }
    if (argc > 2) {
        pass = argv[2];
    }
    if (argc > 3) {
        host = argv[3];
    }

    jid = require_string("输入账号", 64, jid);
    pass = require_string("输入密码", 64, pass);
    host = require_string("输入主机", 128, host);

    xmpp_initialize();

    if (test_thread(argc, argv)) {
        printf("test thread ok.\n");
    } else {
        printf("test thread fail.\n");
    }


    log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
    ctx = xmpp_ctx_new(NULL, log);

    conn = xmpp_conn_new(ctx);

    xmpp_conn_set_jid(conn, jid);
    xmpp_conn_set_pass(conn, pass);

    //// 连接服务器
    xmpp_connect_client(conn, host, 0, conn_handler, ctx);

    int err = pthread_create(&xmpp_thread, NULL, xmpp_routine, ctx);
    if (err != 0) {
        printf("can't create xmpp thread: %s\n", strerror(err));
        return 1;
    }

    pthread_join(xmpp_thread, NULL);
    pthread_join(console_thread, NULL);

    xmpp_conn_release(conn);
    xmpp_ctx_free(ctx);
    xmpp_shutdown();

    safe_mem_free(jid);
    safe_mem_free(pass);
    if (host) {
        safe_mem_free(host);
    }

    safe_mem_check(memcb, NULL);
    _CrtDumpMemoryLeaks();

    printf("按任意按键关闭..");
    getchar();
    return 0;
};
Пример #10
0
int main(int argc, char *argv[])
{
    bool looping = true;
    int c, opt;
    xmpp_t *xmpp;
    xmppdata_t xdata;

    char *host = "localhost", *jid = "user1@localhost/res1", *pass = "******", *tojid = "user1@localhost/res1";
    int port = 5222;

    while ((opt = getopt(argc, argv, "s:p:w:j:t:h")) != -1) {
        switch (opt)
        {
            case 's':
                host = optarg;
                break;
            case 'p':
                port = atoi(optarg);
                break;
            case 'w':
                pass = optarg;
                break;
            case 'j':
                jid = optarg;
                break;
            case 't':
                tojid = optarg;
                break;
            case 'h':
            default:
                print_usage();
                return -1;
        }
    }

    xmpp_log_t *log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG);
    xmpp = xmpphelper_new(conn_handler, NULL, log, NULL);
    xmpphelper_connect(xmpp, host, port, jid, pass);
    xmppchat_handler_add(xmpphelper_get_conn(xmpp), chat_recv_handler, xmpp);
    xmpphelper_run(xmpp);

    while (looping) {
        c = getchar();
        switch (c)
        {
            case 'q':
                xmpphelper_stop(xmpp);
                looping = false;
                break;
            case 's':
                xdata.data = "hello world";
                xdata.tojid = tojid;
                xmppchat_send_message(xmpphelper_get_conn(xmpp), &xdata);
                break;
            case 'e':
            {
                char *data = "hello world base64!!";
                char *encdata;
                xmpp_b64encode(data, strlen(data), &encdata);
                xdata.data = encdata;
                xdata.tojid = tojid;
                xmppchat_send_message(xmpphelper_get_conn(xmpp), &xdata);
                xmpp_b64free(encdata);
                break;
            }
            case 'r':
                xdata.data = "reply message ";
                xdata.tojid = g_rejid;
                xmppchat_send_message(xmpphelper_get_conn(xmpp), &xdata);
                break;
            default:
                break;
        }
    }
    xmpphelper_join(xmpp);
    xmppchat_handler_del(xmpphelper_get_conn(xmpp), chat_recv_handler);

    xmpphelper_release(xmpp);
    return 0;
}