summaryrefslogtreecommitdiffstats
path: root/friendfinder/msg_sender.c
blob: ac5c4e29ca9422c214a409e7aebfa465ce1ff7f9 (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
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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

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

#include "msg_sender.h"
#include "gui.h"
#include "read_file.h"
#include "cryptossl.h"

static irc_session_t *session;
irc_callbacks_t callbacks;

char sender_name[100];
char* nick_from;
char* send_to;
char* msg_to_send;
char msg[300];

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");
	
//	msg = (char*) malloc(sizeof(char) * 300);

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

struct msg_part* prepare_msg(char *msg)
{
	int len = strlen(msg);
	int len_used = 0;

	struct msg_part *msg_p = (struct msg_part*) malloc(sizeof(struct msg_part));
	
	if (len % 6 != 0)
		msg_p->part_count = len / 6;

	if (len % 6 == 0)
		msg_p->part_count = (len / 6) - 1;
	
	int inc_part_count = msg_p->part_count + 1;
	msg_p->msg_parts = (char**) malloc(sizeof(char*) * inc_part_count + 1); // richtig: (char**) malloc(sizeof(....))
	msg_p->msg_parts = (char(*)[inc_part_count]) malloc(sizeof(char*) * inc_part_count); // richtig: (char**) malloc(sizeof(....))

	printf("%i \n", inc_part_count);
	char *message = (char*) malloc(sizeof(char) * (len + 1));
	memcpy(message, msg, len + 1);
	

	if (len <= 6 && len > 0)
	{
		msg_p->msg_parts[0] = message;
		return msg_p;
	}
	
	if (len > 6 && len > 0)
	{
		int i = 0;
		
		while (i <= msg_p->part_count)
		{
			if (i < msg_p->part_count)
			{
				msg_p->msg_parts[i] = (char*) malloc(sizeof(char) * 10);

				strncpy(msg_p->msg_parts[i], message, 6);
				//msg_p->msg_parts[6] = '\0';

				for (int j = 0; j < 6; j++)
				{
					if (len - len_used >= 6)
					{
						*(message + j) = *(message + len_used + j + 6);
					}
				}
				len_used = len_used + 6;
			}
			else if (i == msg_p->part_count && (len - len_used) != 0)
			{	
				msg_p->msg_parts[i] = (char*) malloc(sizeof(char) * (len - len_used)); 

				if ((len - len_used) < 6)
					strncpy(msg_p->msg_parts[i], message, (len - len_used) + 1);

				if ((len - len_used) == 6)
					strncpy(msg_p->msg_parts[i], message, (len - len_used) + 1);
			}
			i++;
			
			if (i == 6)
				msg_p->msg_parts[6] = '\0';
		}
	}

	for (int i = 0; i <= msg_p->part_count; i++)
	{
		printf("%i %s \n", i, msg_p->msg_parts[i]);
	}
	
	free(message);

	return msg_p;
}

void repair_msg(char* inc_msg, char *msg)
{
	printf("repair_msg: %s \n", msg);
	printf("%i \n", strlen(msg));
	printf("inc_msg: %s\n", inc_msg);
	printf("%i \n", strlen(inc_msg));
	if (strcmp(inc_msg, "//c//") != 0)
		strcat(msg, inc_msg);
	
	if (strcmp(inc_msg, "//c//") == 0)
		show_message(msg);
		printf("repair_msg: %s \n", msg);
}

void set_txt_msg(char *msg)
{
	if (msg != NULL && session != NULL)
	{
		struct msg_part *msg_p = (struct msg_part*) malloc(sizeof(struct msg_part));
		msg_p = prepare_msg(msg);
		send_message(session, msg_p);
	}
	else printf("MSG_SENDER: session not initialized \n");

	
}

void send_message(irc_session_t *session, struct msg_part *msg_p)
{
	int blubb = 0;
	if (msg_p->msg_parts != NULL && strlen(msg_p->msg_parts[0]) > 0)
	{
		for (int i = 0; i <= msg_p->part_count; i++)
		{
			char *crypted_msg = (char*) malloc(sizeof(char) * 20);
			char *crypted_msg_base64 = (char*) malloc(sizeof(char) * 50);

			if (msg_p->msg_parts[i] != NULL)
			{	
				blubb = blubb + sizeof(msg_p->msg_parts[i]);
				BF_ecb_encrypt(msg_p->msg_parts[i], crypted_msg, &key, BF_ENCRYPT);
				to_base64(crypted_msg, strlen(crypted_msg), crypted_msg_base64, 50);
				irc_cmd_msg(session, "#msg", crypted_msg_base64);

				free(crypted_msg_base64);
				free(crypted_msg);
			}
		}
		char* eof = "//c//";

		char *crypted_eof = (char*) malloc(sizeof(char) * 20);
		char *crypted_eof_base64 =  (char*) malloc(sizeof(char) * 50);

		BF_ecb_encrypt(eof, crypted_eof, &key, BF_ENCRYPT);
		to_base64(crypted_eof, strlen(crypted_eof), crypted_eof_base64, 50);
		irc_cmd_msg(session, "#msg", crypted_eof_base64);
		blubb = blubb + sizeof(eof);
		printf("%i \n", blubb);
		free(crypted_eof);
		free(crypted_eof_base64);

		for (int i = 0; i < msg_p->part_count; i++)
		{
			free(msg_p->msg_parts[i]);
		}

		free(msg_p->msg_parts);
		free(msg_p);

	}
	else return;
}


void on_connect(irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{
	struct key_data *keyd = (struct key_data*) malloc(sizeof(struct key_data));
	
	keyd = read_key();
	irc_cmd_join(session, "#msg", NULL);	
//	irc_cmd_msg(session, "#msg", "connected"); 		

	BF_set_key(&key, keyd->key_length , keyd->key);	
	//BF_set_key(&key, sizeof(keyd->key), keyd->key);

	free(keyd->key);
	free(keyd);
}

void on_channel(irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
{
	char *inc_msg = (char*) malloc(sizeof(char) * 9);
	char *inc_msg_base64 = (char*) malloc(sizeof(char) * 50);

	irc_target_get_nick(origin, sender_name, sizeof(sender_name));

	if (nick_from = sender_name)
	{ 
		if (strcmp(params[1], "connected") == 0)
		{
			return;
		}	
		
		else
		{
			from_base64(params[1], strlen(params[1]), inc_msg_base64, 50);
			BF_ecb_encrypt(inc_msg_base64, inc_msg, &key, BF_DECRYPT);
			repair_msg(inc_msg, msg);

			free(inc_msg_base64);
			free(inc_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);
	}
}