diff options
author | xengineering <mail2xengineering@protonmail.com> | 2021-01-30 14:08:31 +0100 |
---|---|---|
committer | xengineering <mail2xengineering@protonmail.com> | 2021-01-30 14:08:31 +0100 |
commit | 67d373164190404fe0995bdadb81715cb32fe15b (patch) | |
tree | a98ca28c142bf9aa1e7879ea81cf029e35484297 | |
parent | 7897ac41444b7be10e9881ea30ed67fe14929cef (diff) | |
download | xbot-67d373164190404fe0995bdadb81715cb32fe15b.tar xbot-67d373164190404fe0995bdadb81715cb32fe15b.tar.zst xbot-67d373164190404fe0995bdadb81715cb32fe15b.zip |
Reach MVP
-rw-r--r-- | src/xbot.c | 104 |
1 files changed, 57 insertions, 47 deletions
@@ -13,9 +13,19 @@ #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); +struct xmpp_job_t +{ + char *message; + char *recipient_jid; + char *sender_jid; + char *sender_pwd; + xmpp_ctx_t *ctx; +}; + + +void parse_cli_args(int argc, char **argv, struct xmpp_job_t *job, char **cfg_path); +void read_xmpp_profile(char *cfg_path, struct xmpp_job_t *job); +void send_message(struct xmpp_job_t *job); void conn_handler( xmpp_conn_t *const conn, const xmpp_conn_event_t status, @@ -27,29 +37,26 @@ void conn_handler( int main(int argc, char **argv) { - - // create string pointers - char *message = NULL; - char *target_jid = NULL; + // create some pointers + struct xmpp_job_t job; 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); + // parse CLI args, read profile and execute message sending + parse_cli_args(argc, argv, &job, &profile_path); + read_xmpp_profile(profile_path, &job); + send_message(&job); // free memory - if(message != NULL)free(message); - if(target_jid != NULL)free(target_jid); + if(job.message != NULL)free(job.message); + if(job.recipient_jid != NULL)free(job.recipient_jid); + if(job.sender_jid != NULL)free(job.sender_jid); + if(job.sender_pwd != NULL)free(job.sender_pwd); 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) +void parse_cli_args(int argc, char **argv, struct xmpp_job_t *job, char **cfg_path) { int option; while((option=getopt(argc, argv, "m:j:p:")) != -1) @@ -57,52 +64,51 @@ void parse_cli_args(int argc, char **argv, char **msg, char **jid, char **cfg) switch(option) { case 'm': - *msg = (char *) malloc((strlen(optarg) + 1)*sizeof(char)); - strcpy(*msg, optarg); + job->message = (char *) malloc((strlen(optarg) + 1)*sizeof(char)); + strcpy(job->message, optarg); break; case 'j': - *jid = (char *) malloc((strlen(optarg) + 1)*sizeof(char)); - strcpy(*jid, optarg); + job->recipient_jid = (char *) malloc((strlen(optarg) + 1)*sizeof(char)); + strcpy(job->recipient_jid, optarg); break; case 'p': - *cfg = (char *) malloc((strlen(optarg) + 1)*sizeof(char)); - strcpy(*cfg, optarg); + *cfg_path = (char *) malloc((strlen(optarg) + 1)*sizeof(char)); + strcpy(*cfg_path, optarg); break; } } - if(*msg == NULL || *jid == NULL) + if(job->message == NULL || job->recipient_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) + if(*cfg_path == NULL) { - *cfg = malloc((strlen(DEFAULT_PROFILE_PATH) + 1)*sizeof(char)); - strcpy(*cfg, DEFAULT_PROFILE_PATH); + *cfg_path = malloc((strlen(DEFAULT_PROFILE_PATH) + 1)*sizeof(char)); + strcpy(*cfg_path, DEFAULT_PROFILE_PATH); } - printf("message = '%s'\ntarget_jid = '%s'\nprofile_path = '%s'\n", *msg, *jid, *cfg); + printf("message = '%s'\ntarget_jid = '%s'\nprofile_path = '%s'\n", job->message, job->recipient_jid, *cfg_path); } -void read_xmpp_profile(char *path, char **jid, char **pwd) +void read_xmpp_profile(char *cfg_path, struct xmpp_job_t *job) { - dictionary *profile = iniparser_load(path); + dictionary *profile = iniparser_load(cfg_path); if(profile == NULL) { - printf("Could not parse %s\n", path); + printf("Could not parse %s\n", cfg_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); + job->sender_jid = (char *) iniparser_getstring(profile, "xbot:my_jabber_id", NULL); + printf("sender_jid = '%s'\n", job->sender_jid); + job->sender_pwd = (char *) iniparser_getstring(profile, "xbot:password", NULL); + printf("sender_pwd = '%s'\n", job->sender_pwd); } -void send_message(char *msg, char *recipient, char *sender, char *pwd) +void send_message(struct xmpp_job_t *job) { // setup some pointers and variables - xmpp_ctx_t *ctx; xmpp_conn_t *conn; xmpp_log_t *log; long flags = 0; @@ -110,21 +116,21 @@ void send_message(char *msg, char *recipient, char *sender, char *pwd) 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 + job->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); + conn = xmpp_conn_new(job->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_conn_set_jid(conn, job->sender_jid); + xmpp_conn_set_pass(conn, job->sender_pwd); + xmpp_connect_client(conn, NULL, 0, conn_handler, job); - xmpp_run(ctx); // event loop - conn_handler will trigger exit + xmpp_run(job->ctx); // event loop - conn_handler will trigger exit xmpp_conn_release(conn); // cleanup - xmpp_ctx_free(ctx); + xmpp_ctx_free(job->ctx); xmpp_shutdown(); } @@ -137,7 +143,7 @@ void conn_handler( void *const userdata ) { - xmpp_ctx_t *ctx = (xmpp_ctx_t *)userdata; + struct xmpp_job_t *job = (struct xmpp_job_t *)userdata; int secured; (void)error; @@ -149,14 +155,18 @@ void conn_handler( secured = xmpp_conn_is_secured(conn); fprintf(stderr, "DEBUG: connection is %s.\n", secured ? "secured" : "NOT secured"); - // time to play some stanzas + xmpp_stanza_t * stanza = xmpp_message_new(job->ctx, "chat", job->recipient_jid, "31757b032065e0c1a2f97253f23c2e2d1a1c8f40"); + xmpp_stanza_set_from(stanza, job->sender_jid); + xmpp_message_set_body(stanza, job->message); + xmpp_send(conn, stanza); + xmpp_stanza_release(stanza); xmpp_disconnect(conn); } else { fprintf(stderr, "DEBUG: disconnected\n"); - xmpp_stop(ctx); + xmpp_stop(job->ctx); } } |