summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <mail2xengineering@protonmail.com>2021-01-29 16:57:53 +0100
committerxengineering <mail2xengineering@protonmail.com>2021-01-29 16:57:53 +0100
commitb2faae2e049a235a8339303dc270756463d503f9 (patch)
treeedfa381c41863cc0cfe5297855a13c6f2403a8e7
parent73dcb4a3b31bc5a0496a65b88074785ecaf3344a (diff)
downloadxbot-b2faae2e049a235a8339303dc270756463d503f9.tar
xbot-b2faae2e049a235a8339303dc270756463d503f9.tar.zst
xbot-b2faae2e049a235a8339303dc270756463d503f9.zip
Refactor Code
-rw-r--r--src/xbot.c117
1 files changed, 55 insertions, 62 deletions
diff --git a/src/xbot.c b/src/xbot.c
index e9981e3..ec16d6b 100644
--- a/src/xbot.c
+++ b/src/xbot.c
@@ -13,6 +13,8 @@
#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 conn_handler(
xmpp_conn_t *const conn,
const xmpp_conn_event_t status,
@@ -22,76 +24,18 @@ void conn_handler(
);
-int main(int argc, char ** argv)
+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);
+ parse_cli_args(argc, argv, &message, &target_jid, &profile_path); // get information from user
+ read_xmpp_profile(profile_path, &my_jid, &my_pwd);
// send message
/*xmpp_ctx_t *ctx;
@@ -113,13 +57,62 @@ int main(int argc, char ** argv)
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 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 conn_handler(
xmpp_conn_t *const conn,
const xmpp_conn_event_t status,