From 73dcb4a3b31bc5a0496a65b88074785ecaf3344a Mon Sep 17 00:00:00 2001
From: xengineering <mail2xengineering@protonmail.com>
Date: Fri, 29 Jan 2021 16:29:47 +0100
Subject: Add existing Files

---
 .gitignore          |   2 +
 Makefile            |  22 ++++++++
 README.md           |  14 ++++-
 example_profile.ini |   3 ++
 src/xbot.c          | 151 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 191 insertions(+), 1 deletion(-)
 create mode 100644 .gitignore
 create mode 100644 Makefile
 create mode 100644 example_profile.ini
 create mode 100644 src/xbot.c

diff --git a/.gitignore b/.gitignore
new file mode 100644
index 0000000..8291684
--- /dev/null
+++ b/.gitignore
@@ -0,0 +1,2 @@
+profile.ini
+build
diff --git a/Makefile b/Makefile
new file mode 100644
index 0000000..f1f8622
--- /dev/null
+++ b/Makefile
@@ -0,0 +1,22 @@
+
+
+TARGET_EXEC := build/xbot
+
+SRCS := $(wildcard src/*.c)
+OBJS := $(patsubst src/%.c, build/%.o, $(SRCS))
+LDFLAGS := -lstrophe -liniparser
+
+all: $(TARGET_EXEC)
+
+$(TARGET_EXEC): build/xbot.o
+	mkdir -p build
+	$(CC) $(LDFLAGS) -o $@ $<
+
+build/%.o: src/%.c
+	mkdir -p build
+	$(CC) -c $< -o $@
+
+.PHONY: clean
+clean:
+	rm -r build
+
diff --git a/README.md b/README.md
index 5ddaf7d..ed604ab 100644
--- a/README.md
+++ b/README.md
@@ -1,3 +1,15 @@
+
 # xbot
 
-A Software to create simpel Chat Bots for the XMPP/Jabber Protocol.
\ No newline at end of file
+A Software to create simpel Chat Bots for the XMPP/Jabber Protocol.
+
+## Current State
+
+This software is not ready to use. The first milestone is a simple terminal application which sends a message on a command like this:
+
+```
+xbot -m "Hello from your XMPP bot" -j "recipient@example.com" -p /path/to/my/xmpp/profile.ini
+```
+
+Have a look at ```example_profile.ini``` to see how your own JID and password have to be formalized.
+
diff --git a/example_profile.ini b/example_profile.ini
new file mode 100644
index 0000000..d6ba4f2
--- /dev/null
+++ b/example_profile.ini
@@ -0,0 +1,3 @@
+[xbot]
+my_jabber_id=user@example.com
+password=password123&%
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);
+	}
+}
+
-- 
cgit v1.2.3-70-g09d2