summaryrefslogtreecommitdiffstats
path: root/friendfinder/msg_sender.c
blob: d525a3dc9d803e431e6e598c123183a34a6b6818 (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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

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

#include "msg_sender.h"
#include "gui.h"

static irc_session_t *session;
irc_callbacks_t callbacks;

char* nick_from;
char* send_to;
char* msg_to_send;

irc_dcc_t dccid;
irc_dcc_t buddy_dccid = 1;

BF_KEY key;


int init_connection(char* server_ip, char* user)
{
	printf("MSG_SENDER: initialising connection...\n");

	session = irc_create_session(&callbacks);
	int con = irc_connect(session, server_ip, 6666, NULL, user, user, user);
	
	if (irc_is_connected(session) == 1)
	{
		printf("MSG_SENDER: connected... \n");
		return 0;
	
	}

	if (con != 0)
	{
		printf("MSG_SENDER: connection error-code: %i \n", con);
		return 1;
	}
}

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

void set_txt_msg(char *msg)
{
	if (msg != NULL && session != NULL)
	{
		send_message(session, msg);
	}
	else printf("MSG_SENDER: NULL value \n");
	
}

void send_message(irc_session_t *session, char *msg)
{
	if (msg != NULL)
	{
		char *crypted_msg = (char*) malloc(sizeof(msg));

		BF_ecb_encrypt(msg, crypted_msg, &key, BF_ENCRYPT);	

		int i = irc_cmd_msg(session, "#msg", crypted_msg);
	
		if (i == 0)
		{
			printf("MSG_SENDER: message '%s' sent successful \n", msg);
		}
	}
	else return;
}


void on_connect(irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{
	irc_cmd_join(session, "#msg", NULL);	
	irc_cmd_msg(session, "#msg", "connected"); 		

	BF_set_key(&key, 5, "abcde");	
}

void on_channel(irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{
	//char msg[4096];
	char *msg = (char*) malloc(sizeof(params[1]));
	
	BF_ecb_encrypt(params[1], msg, &key, BF_DECRYPT);	
	printf("%s:   %s \n", params[0], msg);

	show_message(msg);
}

void msg_main_loop(void *nicknames)
{
	memset(&callbacks, 0, sizeof(callbacks));

	callbacks.event_connect = on_connect;
	callbacks.event_channel = on_channel;

	struct nick *nicks = (struct nick*) malloc(sizeof(struct nick));
	nicks = (struct nick*) nicknames;
	nick_from = nicks->from;
	send_to = nicks->to;

 	if (init_connection("127.0.0.1", nick_from) == 0)
	{	
		printf("MSG_SENDER: connection succesfull\n");

		irc_run(session);
	}
}