summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorxengineering <mail2xengineering@protonmail.com>2021-01-30 15:53:15 +0100
committerxengineering <mail2xengineering@protonmail.com>2021-01-30 15:53:15 +0100
commit883e4bb795ed3f8501c74dd57bc8b0f6ea33f416 (patch)
tree4ec9afc099dd11f584f33b7cce6c76dffa0f8950
parent58daf50667396477021e4a4f57d8241e44ff268c (diff)
downloadxbot-883e4bb795ed3f8501c74dd57bc8b0f6ea33f416.tar
xbot-883e4bb795ed3f8501c74dd57bc8b0f6ea33f416.tar.zst
xbot-883e4bb795ed3f8501c74dd57bc8b0f6ea33f416.zip
Fix empty Profile Bug
-rw-r--r--Makefile7
-rw-r--r--src/xbot.c39
2 files changed, 36 insertions, 10 deletions
diff --git a/Makefile b/Makefile
index cd8b97d..ff0e394 100644
--- a/Makefile
+++ b/Makefile
@@ -21,10 +21,13 @@ build/%.o: src/%.c
$(CC) -c $< -o $@
install: all
- $(INSTALL_PROGRAM) $(TARGET_EXEC) $(bindir)/xbot
+ mkdir -p $(HOME)/.config/xbot/
+ touch $(HOME)/.config/xbot/profile.ini
+ chmod 600 $(HOME)/.config/xbot/profile.ini
+ sudo $(INSTALL_PROGRAM) $(TARGET_EXEC) $(bindir)/xbot
uninstall:
- rm -f $(bindir)/xbot
+ sudo rm -f $(bindir)/xbot
.PHONY: clean uninstall
clean:
diff --git a/src/xbot.c b/src/xbot.c
index 1eb6065..78bc9eb 100644
--- a/src/xbot.c
+++ b/src/xbot.c
@@ -20,6 +20,7 @@ struct xmpp_job_t
char *sender_jid;
char *sender_pwd;
xmpp_ctx_t *ctx;
+ uint8_t log_level;
};
@@ -39,6 +40,7 @@ int main(int argc, char **argv)
{
// create some pointers
struct xmpp_job_t job;
+ job.log_level = 0;
char *profile_path = NULL;
// parse CLI args, read profile and execute message sending
@@ -59,7 +61,7 @@ int main(int argc, char **argv)
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)
+ while((option=getopt(argc, argv, "m:j:p:d")) != -1)
{
switch(option)
{
@@ -75,6 +77,9 @@ void parse_cli_args(int argc, char **argv, struct xmpp_job_t *job, char **cfg_pa
*cfg_path = (char *) malloc((strlen(optarg) + 1)*sizeof(char));
strcpy(*cfg_path, optarg);
break;
+ case 'd':
+ job->log_level = 1;
+ break;
}
}
if(job->message == NULL || job->recipient_jid == NULL)
@@ -89,7 +94,10 @@ void parse_cli_args(int argc, char **argv, struct xmpp_job_t *job, char **cfg_pa
strcpy(*cfg_path, homedir);
strcat(*cfg_path, DEFAULT_PROFILE_PATH_APPENDIX);
}
- printf("message = '%s'\ntarget_jid = '%s'\nprofile_path = '%s'\n", job->message, job->recipient_jid, *cfg_path);
+ if (job->log_level)
+ {
+ printf("message = '%s'\ntarget_jid = '%s'\nprofile_path = '%s'\nlog_level = '%i'\n", job->message, job->recipient_jid, *cfg_path, job->log_level);
+ }
}
@@ -102,9 +110,17 @@ void read_xmpp_profile(char *cfg_path, struct xmpp_job_t *job)
exit(1);
}
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);
+ if (job->sender_jid == NULL || job->sender_pwd == NULL)
+ {
+ printf("Profile '%s' must have section 'xbot' and keys 'my_jabber_id' and 'password' configured!\n", cfg_path);
+ exit(1);
+ }
+ if (job->log_level)
+ {
+ printf("sender_jid = '%s'\n", job->sender_jid);
+ printf("sender_pwd = '%s'\n", job->sender_pwd);
+ }
}
@@ -117,7 +133,14 @@ void send_message(struct xmpp_job_t *job)
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
+ if (job->log_level)
+ {
+ log = xmpp_get_default_logger(XMPP_LEVEL_DEBUG); // create xmpp logger with high log level
+ }
+ else
+ {
+ log = xmpp_get_default_logger(XMPP_LEVEL_ERROR); // create xmpp logger with low log level
+ }
job->ctx = xmpp_ctx_new(NULL, log); // create xmpp context
// create xmpp connection, set own jabber id and password and connect
@@ -153,9 +176,9 @@ void conn_handler(
if(status == XMPP_CONN_CONNECT)
{
- fprintf(stderr, "DEBUG: connected\n");
+ if (job->log_level)fprintf(stderr, "DEBUG: connected\n");
secured = xmpp_conn_is_secured(conn);
- fprintf(stderr, "DEBUG: connection is %s.\n", secured ? "secured" : "NOT secured");
+ if (job->log_level)fprintf(stderr, "DEBUG: connection is %s.\n", secured ? "secured" : "NOT secured");
xmpp_stanza_t * stanza = xmpp_message_new(job->ctx, "chat", job->recipient_jid, "31757b032065e0c1a2f97253f23c2e2d1a1c8f40");
xmpp_stanza_set_from(stanza, job->sender_jid);
@@ -167,7 +190,7 @@ void conn_handler(
}
else
{
- fprintf(stderr, "DEBUG: disconnected\n");
+ if (job->log_level)fprintf(stderr, "DEBUG: disconnected\n");
xmpp_stop(job->ctx);
}
}