summaryrefslogtreecommitdiff
path: root/src/xbot.c
blob: e9981e3a33a15051ceb3d88e8fa23ef043d97d03 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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);
	}
}