summaryrefslogtreecommitdiffstats
path: root/friendfinder/receiver.c
blob: c9a030f2f5401f4e42c576bdc70439218ef9cf2a (plain) (blame)
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <libircclient/libircclient.h>

#include "openssl/blowfish.h"
#include "draw_user.h"

static irc_session_t *session;
irc_callbacks_t callbacks;
int counter = 0;
char sender_name[100];

int ret;

BF_KEY key;

int init_connection_receiver(char* server_ip, char* user)
{
	printf("RECEIVER: initialising connection...\n");	
	
	session = irc_create_session(&callbacks);
	int con = irc_connect(session, server_ip, 6666, NULL, user, "receiver", "receiver");
	if (con != 0)
	{
		printf("RECEIVER: connection error-code: %i \n", con);

		return 0;
	}

	if (irc_is_connected(session) == 1)
	{
		printf("RECEIVER: connected...\n");
		
		return 1;
	}
}

void disconnect_receiver()
{
	irc_disconnect(session);
	irc_destroy_session(session);
	printf("RECEIVER: disconnected...\n");
}

void dump_data(char* lat, char* lon, char* nick)
{
	struct position *pos = (struct position*) malloc(sizeof(struct position));
	pos->lat = lat;
	pos->lon = lon;
	pos->nick = nick;

	draw_user(pos);
}

void get_position(irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{

	char *decrypted_lat = (char*) malloc(sizeof(params[1]));
	char *decrypted_lon = (char*) malloc(sizeof(params[1]));

	decrypted_lat = NULL;
	decrypted_lon = NULL;

	irc_target_get_nick(origin, sender_name, sizeof(sender_name));
	
	if (sender_name != NULL && strcmp(sender_name, "_s") == 1)
	{

		//first message is the ret param, which is needed for message decryption
		if (counter == 0)
		{
			//BF_set_key(&key, 5, "abcde");	
		}

	        //dump_event(session, event, origin, params, count);
		if (counter % 2 == 0)
		{
			BF_ecb_encrypt(params[1], decrypted_lat, &key, BF_DECRYPT);		
	    		printf("RECEIVER: lat %s, origin: %s \n", decrypted_lat, sender_name);
		}
		
		if (counter % 2 != 0)
		{
			BF_ecb_encrypt(params[1], decrypted_lon, &key, BF_DECRYPT);
			printf("RECEIVER: lon %s , origin: %s \n", decrypted_lon, sender_name);
			printf("====\n");
		}
		
		if(decrypted_lon != NULL && decrypted_lat != NULL)
		{
			dump_data(decrypted_lat, decrypted_lon, sender_name);
			decrypted_lon = NULL;
			decrypted_lat = NULL;
		}	
	//	if (strcmp(params[1], "128") != 0)
		{ 
			irc_cmd_msg(session, "#test", sender_name);
			counter++;
		}
	//	else
		{
	//		counter++;
		}
	}

}

void on_connect_receiver(irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{
	printf("RECEIVER: on_connect_receiver \n");
	BF_set_key(&key, 5, "abcde");	
	
	irc_cmd_join(session, "#test", NULL);	
	irc_cmd_msg(session, "#test", "connected"); 		
}

void receiver_main(void *user)
{
	memset(&callbacks, 0, sizeof(callbacks));
	
	callbacks.event_channel = get_position;
	callbacks.event_connect = on_connect_receiver;
	
	char *username = (char*) user;
	//TODO pointer zeigt immer auf gleiche variable....damit wird immer gleicher from wert genutzt und _r _s angehängt....fixen
	strcat(username, "_r");
	
	init_connection_receiver("127.0.0.1", username);

	irc_run(session);
	printf("done\n");
}