summaryrefslogtreecommitdiffstats
path: root/friendfinder/sender.c
diff options
context:
space:
mode:
Diffstat (limited to 'friendfinder/sender.c')
-rw-r--r--friendfinder/sender.c168
1 files changed, 168 insertions, 0 deletions
diff --git a/friendfinder/sender.c b/friendfinder/sender.c
new file mode 100644
index 0000000..cef8aeb
--- /dev/null
+++ b/friendfinder/sender.c
@@ -0,0 +1,168 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <stdint.h>
+
+#include "libircclient.h"
+#include "openssl/blowfish.h"
+
+static irc_session_t *session;
+irc_callbacks_t callbacks;
+int msg_count = 0;
+int ack_count = 0;
+int resend = 0;
+char* nick;
+
+int first_send = 0;
+int run_session = 0;
+
+BF_KEY key;
+
+int init_connection(char* server_ip, char* user)
+{
+ printf("initialising connection...\n");
+
+ session = irc_create_session(&callbacks);
+ int con = irc_connect(session, server_ip, 6667, NULL, user, user, user);
+
+ if (irc_is_connected(session) == 1)
+ {
+ printf("connected... \n");
+ return 0;
+
+ }
+
+ if (con != 0)
+ {
+ printf("connection error-code: %i \n", con);
+ return 1;
+ }
+}
+
+void disconnect()
+{
+ printf("disconnected...\n");
+ irc_disconnect(session);
+ irc_destroy_session(session);
+}
+
+//create random position between two y coordinates. bb_y_left < bb_y_right, bb_y_left = 0!
+int random_position_y(int bb_y_right)
+{
+ return rand() % bb_y_right;
+}
+
+//create random position between two x coordinates. bb_x_lower < bb_x_upper, bb_x_lower = 0!
+int random_position_x(int bb_x_upper)
+{
+ return rand() % bb_x_upper;
+}
+
+void send_position(irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
+{
+
+ unsigned char crypted_lat[64];
+ unsigned char crypted_lon[64];
+
+ if (first_send == 0)
+ {
+ BF_set_key(&key, 5, "abcde");
+
+ irc_cmd_join(session, "#test", NULL);
+ }
+
+ for(int i = 0; i < 2; i++)
+ {
+ int lat = random_position_x(1000);
+ printf("lat position %i \n", lat);
+ int lon = random_position_y(1000);
+ printf("lon position %i \n", lon);
+
+ unsigned char lat_char[8];
+ sprintf(lat_char, "%i", lat);
+
+ unsigned char lon_char[8];
+ sprintf(lon_char, "%i", lon);
+
+ BF_ecb_encrypt(lat_char, crypted_lat, &key, BF_ENCRYPT);
+ BF_ecb_encrypt(lon_char, crypted_lon, &key, BF_ENCRYPT);
+
+ BF_ecb_encrypt(crypted_lat, lat_char, &key, BF_DECRYPT);
+ printf("self-test: %s \n", lat_char);
+
+ int _x = irc_cmd_msg(session, "#test", crypted_lat);
+ int _y = irc_cmd_msg(session, "#test", crypted_lon);
+
+ //increase counter variable, which counts number of send positions!
+ msg_count = msg_count + 2;
+
+ if (_x == 0 && _y == 0)
+ {
+ printf("send succesfull %i %i \n", lat, lon);
+ }
+
+ if (_y != 0)
+ {
+ printf("error code due sending: %i \n", _y);
+ }
+
+ if (_x != 0)
+ {
+ printf("error code due sending: %i \n", _x);
+ }
+
+ first_send = 1;
+ }
+
+ if(resend == 1)
+ {
+ get_aknowledge(session, event, origin, params, count);
+ resend = 0;
+ ack_count = 0;
+ }
+}
+
+void get_aknowledge(irc_session_t * session, const char * event, const char * origin, const char ** params, unsigned int count)
+{
+ if(strcmp(params[1], nick) == 0)
+ {
+ //use count variable, to count number of aknowledged positions! second number has to be twice as big, as the number of
+ //for-runs in send_position
+ if (ack_count <= 3 && msg_count > 0)
+ {
+ ack_count++;
+ msg_count--;
+ }
+
+ if (ack_count == 4 && msg_count == 0)
+ {
+ resend = 1;
+ send_position(session, event, origin, params, count);
+ }
+ }
+}
+
+void sender_main(char *nick)
+{
+ memset(&callbacks, 0, sizeof(callbacks));
+
+ callbacks.event_connect = send_position;
+ callbacks.event_channel = get_aknowledge;
+
+
+ init_connection("127.0.0.1", nick);
+}
+
+void sender_run_irc()
+{
+ if (run_session == 0)
+ {
+ irc_run(session);
+ run_session = 1;
+ }
+}
+
+void sender_stop_irc()
+{
+ irc_cmd_quit(session, "paused");
+}