summaryrefslogblamecommitdiffstats
path: root/friendfinder/msg_sender.c
blob: ac5c4e29ca9422c214a409e7aebfa465ce1ff7f9 (plain) (tree)
1
2
3
4
5
6
7
8
9







                             
                
                      
                      



                              
                      
                
              
                  
              

                
                          


           

                                                
                                                           
        
                                                 

                                                 
                                                                                


                                           
                                                      





                         
                                                                        



                         







                                                
                                       

                              


                                                                                    





                                                  
        
                                                   
                                                                                                                         
                                                                                                                                      
 

                                                                 
                                      
        
 
                                
         

                                              

         
                               
         
                          
                
                                              
                 

                                                  
                                                                                        
 
                                                                         

                                                             
                                                           
                                 
                                                                
                                         
                                                                                               

                                         
                                                        
                         
                                                                                 
                                
                                                                                                       
 




                                                                                                    

                            


                                                           
                 
         
 



                                                           


                      
                     



                                         
                                         


                                         

                                          
        
                                          
                                  
                                                 

 
                           
 
                                           
         



                                                                                            
                                                              

        
 
 

                                                                 
                      
                                                                        

                                                            
                 
                                                                              

                                                                                     
                                                        

                                                                            
                                                                                                   



                                                                                                    

                                                  
                 

                                    
                                                                      

                                                                              
                                                                   

                                                                                    

                                            
                                  
                                         
 



                                                           
 
                                       

                            



                    


                                                                                                                           
                                                                                   

                          
                                                
                                                                
 

                                                         


                        



                                                                                                                           
                                                         
                                                                 

                                                                      
 








                                                        

                                                                                      
                                                 


                                             

                 

 
                                   



                                                 

                                             
                                                                        


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