// vim: tabstop=4 shiftwidth=4 noexpandtab #include #include #include #include // important for getopt #include #include #include #define DEFAULT_PROFILE_PATH "~/.config/xbot/profile.ini" void parse_cli_args(int argc, char **argv, char **msg, char **jid, char **cfg); void read_xmpp_profile(char *path, char **jid, char **pwd); void send_message(char *msg, char *recipient, char *sender, char *pwd); void conn_handler( xmpp_conn_t *const conn, const xmpp_conn_event_t status, const int error, xmpp_stream_error_t *const stream_error, void *const userdata ); int main(int argc, char **argv) { // create string pointers char *message = NULL; char *target_jid = NULL; char *profile_path = NULL; char *my_jid = NULL; char *my_pwd = NULL; parse_cli_args(argc, argv, &message, &target_jid, &profile_path); read_xmpp_profile(profile_path, &my_jid, &my_pwd); send_message(message, target_jid, my_jid, my_pwd); // free memory if(message != NULL)free(message); if(target_jid != NULL)free(target_jid); if(profile_path != NULL)free(profile_path); if(my_jid != NULL)free(my_jid); if(my_pwd != NULL)free(my_pwd); return 0; } void parse_cli_args(int argc, char **argv, char **msg, char **jid, char **cfg) { int option; while((option=getopt(argc, argv, "m:j:p:")) != -1) { switch(option) { case 'm': *msg = (char *) malloc((strlen(optarg) + 1)*sizeof(char)); strcpy(*msg, optarg); break; case 'j': *jid = (char *) malloc((strlen(optarg) + 1)*sizeof(char)); strcpy(*jid, optarg); break; case 'p': *cfg = (char *) malloc((strlen(optarg) + 1)*sizeof(char)); strcpy(*cfg, optarg); break; } } if(*msg == NULL || *jid == NULL) { printf("You have to provide a message with the '-m' option and a target jid with the '-j' option!\n"); exit(1); } if(*cfg == NULL) { *cfg = malloc((strlen(DEFAULT_PROFILE_PATH) + 1)*sizeof(char)); strcpy(*cfg, DEFAULT_PROFILE_PATH); } printf("message = '%s'\ntarget_jid = '%s'\nprofile_path = '%s'\n", *msg, *jid, *cfg); } void read_xmpp_profile(char *path, char **jid, char **pwd) { dictionary *profile = iniparser_load(path); if(profile == NULL) { printf("Could not parse %s\n", path); exit(1); } *jid = (char *) iniparser_getstring(profile, "xbot:my_jabber_id", NULL); printf("my_jid = '%s'\n", *jid); *pwd = (char *) iniparser_getstring(profile, "xbot:password", NULL); printf("my_pwd = '%s'\n", *pwd); } void send_message(char *msg, char *recipient, char *sender, char *pwd) { // setup some pointers and variables xmpp_ctx_t *ctx; xmpp_conn_t *conn; xmpp_log_t *log; long flags = 0; flags |= XMPP_CONN_FLAG_MANDATORY_TLS; // force transport encryption xmpp_initialize(); // init libstrophe (xmpp library) log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); // create xmpp logger ctx = xmpp_ctx_new(NULL, log); // create xmpp context // create xmpp connection, set own jabber id and password and connect conn = xmpp_conn_new(ctx); xmpp_conn_set_flags(conn, flags); xmpp_conn_set_jid(conn, sender); xmpp_conn_set_pass(conn, pwd); xmpp_connect_client(conn, NULL, 0, conn_handler, ctx); xmpp_run(ctx); // event loop - conn_handler will trigger exit xmpp_conn_release(conn); // cleanup xmpp_ctx_free(ctx); xmpp_shutdown(); } void conn_handler( xmpp_conn_t *const conn, const xmpp_conn_event_t status, const int error, xmpp_stream_error_t *const stream_error, void *const userdata ) { xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; int secured; (void)error; (void)stream_error; if(status == XMPP_CONN_CONNECT) { fprintf(stderr, "DEBUG: connected\n"); secured = xmpp_conn_is_secured(conn); fprintf(stderr, "DEBUG: connection is %s.\n", secured ? "secured" : "NOT secured"); // time to play some stanzas xmpp_disconnect(conn); } else { fprintf(stderr, "DEBUG: disconnected\n"); xmpp_stop(ctx); } }