diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/xbot.c | 151 |
1 files changed, 151 insertions, 0 deletions
diff --git a/src/xbot.c b/src/xbot.c new file mode 100644 index 0000000..e9981e3 --- /dev/null +++ b/src/xbot.c @@ -0,0 +1,151 @@ +// vim: tabstop=4 shiftwidth=4 noexpandtab + + +#include <stdio.h> +#include <stdint.h> +#include <stdlib.h> +#include <unistd.h> // important for getopt +#include <string.h> +#include <iniparser.h> +#include <strophe.h> + + +#define DEFAULT_PROFILE_PATH "~/.config/xbot/profile.ini" + + +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 *profile_content = NULL; + char *my_jid = NULL; + char *my_pwd = NULL; + + // parse shell arguments + int option; + while((option=getopt(argc, argv, "m:j:p:")) != -1) + { + switch(option) + { + case 'm': + message = (char *) malloc((strlen(optarg) + 1)*sizeof(char)); + strcpy(message, optarg); + break; + case 'j': + target_jid = (char *) malloc((strlen(optarg) + 1)*sizeof(char)); + strcpy(target_jid, optarg); + break; + case 'p': + profile_path = (char *) malloc((strlen(optarg) + 1)*sizeof(char)); + strcpy(profile_path, optarg); + break; + } + } + if(message == NULL || target_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(profile_path == NULL) + { + profile_path = malloc((strlen(DEFAULT_PROFILE_PATH) + 1)*sizeof(char)); + strcpy(profile_path, DEFAULT_PROFILE_PATH); + } + printf("message = '%s'\ntarget_jid = '%s'\nprofile_path = '%s'\n", message, target_jid, profile_path); + + // read profile.ini + /*FILE *file = fopen(profile_path, "rb"); + if(file == NULL) + { + printf("Error opening file\n"); + exit(1); + } + fseek(file, 0, SEEK_END); + uint32_t file_length = ftell(file); + profile_content = (char *) malloc((file_length + 1) * sizeof(char)); + fseek(file, 0, SEEK_SET); + fread(profile_content, sizeof(char), (file_length + 1), file); + profile_content[file_length] = '\0'; + fclose(file); + printf("File content is:\n'%s'\n", profile_content);*/ + + // parse profile.ini + dictionary *profile = iniparser_load(profile_path); + if(profile == NULL) + { + printf("Could not parse %s\n", profile_path); + exit(1); + } + my_jid = (char *) iniparser_getstring(profile, "xbot:my_jabber_id", NULL); + printf("my_jid = '%s'\n", my_jid); + my_pwd = (char *) iniparser_getstring(profile, "xbot:password", NULL); + printf("my_pwd = '%s'\n", my_pwd); + + // send message + /*xmpp_ctx_t *ctx; + xmpp_conn_t *conn; + xmpp_log_t *log; + xmpp_initialize(); + log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); + ctx = xmpp_ctx_new(NULL, log); + conn = xmpp_conn_new(ctx); + xmpp_conn_set_jid(conn, my_jid); + xmpp_conn_set_pass(conn, my_pwd); + xmpp_connect_client(conn, NULL, 0, conn_handler, ctx); + xmpp_run(ctx); + xmpp_conn_release(conn); + xmpp_ctx_free(ctx); + xmpp_shutdown();*/ + + // free memory + if(message != NULL)free(message); + if(target_jid != NULL)free(target_jid); + if(profile_path != NULL)free(profile_path); + if(profile_content != NULL)free(profile_content); + if(my_jid != NULL)free(my_jid); + if(my_pwd != NULL)free(my_pwd); + return 0; +} + + +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 +) +{ + // TODO: Implement this stuff! + 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"); + xmpp_disconnect(conn); + } + else + { + fprintf(stderr, "DEBUG: disconnected\n"); + xmpp_stop(ctx); + } +} + |