summaryrefslogtreecommitdiffstats
path: root/friendfinder/msg_sender.c
blob: 4980ad1e3322ea90a5205137f7b479ec1bb6b2f0 (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
#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"

static irc_session_t *session;
irc_callbacks_t callbacks;

char sender_name[100];
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)
	{
		prepare_msg(msg);
		printf("done \n");
		//send_message(session, msg);
	}
	else printf("MSG_SENDER: NULL value \n");
}

//bekommt noch char** als return value
void prepare_msg(char *msg)
{
	int len = strlen(msg);
	int part_count = (strlen(msg) / 7); //division mit rest, wenn ich mich nicht irre => wird auf nächst kleinere zahl abgerundet! passt!
	printf("part_count %i \n", part_count);
	char *message = (char*) malloc(strlen(msg)*2);
	message = msg;
	
	if (len <= 7)
	{
		char *msg_parts[1];
		msg_parts[0] = msg;
		return msg_parts;
	}
	
	if (len > 7)
	{
		char **msg_parts = (char(*)[part_count]) malloc(sizeof(char) * part_count);
		int i = 0;
		
		while (i <= part_count)
		{
			printf("while ( %i < %i ) \n", i, part_count);
			if (i < part_count)
			{	
				printf("if ( %i < %i ) \n", i, part_count);
				strncpy(msg_parts[i], message, 7);
			 	message = message + 7; //FIXME
				printf("msg_parts[%i]: %s \n",i, msg_parts[i]);
			}
			else if (i == part_count)
			{	
				printf("if ( %i == %i )", i, part_count);
				strncpy(msg_parts[i], message, strlen(msg));
				printf("msg_parts[%i]: %s \n",i, msg_parts[i]);
			}
			i++;
		}
		//return msg;
	}
}

void repair_msg(char* inc_msg, char *msg)
{
	int len = strlen(msg);
	
	if (len == 7)
	{
		strcat(inc_msg, msg);
	}
	if (len < 7)
	{
		strcat(inc_msg, msg);
		strcat(inc_msg, '\0');
	}
}

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)
{
	char *_key = read_key();
	printf("%s \n", _key);	
	irc_cmd_join(session, "#msg", NULL);	
	irc_cmd_msg(session, "#msg", "connected"); 		

	BF_set_key(&key, strlen(_key) - 1, _key);	
}

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

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

	if (nick_from = sender_name)
	{ 
		if (strcmp(params[1], "connected") == 0)
		{
			return;
		}	
		
		else
		{
			BF_ecb_encrypt(params[1], msg, &key, BF_DECRYPT);	
			printf("%s:   %s \n", params[0], msg);

			show_message(msg);
		}
	}
	free(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);
	}
}