summaryrefslogtreecommitdiff
path: root/src/xbot.c
blob: eaca1dac1e598ed6fee624ed0667f0eefa0a666f (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
152
153
154
155
156
157
158
159
160
161
162
// 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 parse_cli_args(int argc, char **argv, char **msg, char **jid, char **cfg);
void read_xmpp_profile(char *path, char **jid, char **pwd);
void send_message(char *msg, char *recipient, char *sender, char *pwd);
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 *my_jid = NULL;
	char *my_pwd = NULL;

	parse_cli_args(argc, argv, &message, &target_jid, &profile_path);
	read_xmpp_profile(profile_path, &my_jid, &my_pwd);
	send_message(message, target_jid, my_jid, my_pwd);

	// free memory
	if(message != NULL)free(message);
	if(target_jid != NULL)free(target_jid);
	if(profile_path != NULL)free(profile_path);
	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 send_message(char *msg, char *recipient, char *sender, char *pwd)
{
	// setup some pointers and variables
	xmpp_ctx_t *ctx;
	xmpp_conn_t *conn;
	xmpp_log_t *log;
	long flags = 0;

	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
	ctx = xmpp_ctx_new(NULL, log);  // create xmpp context

	// create xmpp connection, set own jabber id and password and connect
	conn = xmpp_conn_new(ctx);
	xmpp_conn_set_flags(conn, flags);
	xmpp_conn_set_jid(conn, sender);
	xmpp_conn_set_pass(conn, pwd);
	xmpp_connect_client(conn, NULL, 0, conn_handler, ctx);

	xmpp_run(ctx);  // event loop - conn_handler will trigger exit

	xmpp_conn_release(conn);

	// cleanup
	xmpp_ctx_free(ctx);
	xmpp_shutdown();
}


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
)
{
	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");

		// time to play some stanzas

		xmpp_disconnect(conn);
	}
	else
	{
		fprintf(stderr, "DEBUG: disconnected\n");
		xmpp_stop(ctx);
	}
}