aboutsummaryrefslogtreecommitdiff
path: root/src/espik_net.c
blob: d56f070652c65ca1aa3f1839663ec0ca068aba46 (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
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>

#include "espik_global.h"
#include "espik_net.h"
#include "espik_common_handler.h"
#include "espik_error.h"
#include "espik_debug.h"
#include "espik_irc.h"
#include "espik.h"

#include <Ecore.h>
#include <Ecore_Con.h>

typedef int	(*Handler_Func) (void* data, int ev_type, void* ev);

static int	ret_int;			/* For debug */
char		connected = 0;

Ecore_Con_Server*	con_sock;

Ecore_Event_Handler*	ret;

void	espik_con_init (t_info serv_info)
{
	int nb_launch;
	espik_enter();

	nb_launch = ecore_con_init ();

	com_active = 0;

	/*
	espik_debug_printf ("nb_launch = %d", nb_launch);
	*/

	con_sock = ecore_con_server_connect (ECORE_CON_REMOTE_SYSTEM,
					     serv_info.server->host,
					     (int)serv_info.server->port, NULL);
	if (!con_sock)
	{
		espik_debug_print ("ecore_con_server_connect failed "
				   "(ECORE_CON_REMOTE_SYSTEM)");
		exit (-1);
	}
	espik_free (con_sock);

	if (! (ret = ecore_event_handler_add (ECORE_CON_EVENT_SERVER_DATA,
					(Handler_Func) server_data, NULL)))
	{
		espik_debug_print ("ecore_event_handler_add "
				   "(ECORE_CON_EVENT_SERVER_DATA)");
		exit (-1);
	}
	espik_free (ret);
	
	if (! (ret = ecore_event_handler_add (ECORE_CON_EVENT_SERVER_ADD,
					(Handler_Func) espik_con_up, NULL)))
	{
		espik_debug_print ("ecore_event_handler_add "
				   "(ECORE_CON_EVENT_SERVER_ADD)");
		exit (-1);
	}
	espik_free (ret);
	
	if (! (ret = ecore_event_handler_add (ECORE_CON_EVENT_SERVER_DEL,
					(Handler_Func) espik_con_shutdown, NULL)))
	{
		espik_debug_print ("ecore_event_handler_add "
				   "(ECORE_CON_EVENT_SERVER_DEL)");
		exit (-1);
	}
	espik_free (ret);
	
	espik_leave();
}

void	send_login()
{
	espik_enter();
//	espik_debug_print ("<<<<<<<< DUMMMMY CODE >>>>>>>");

	char *tmp, *tmp2;

	tmp = malloc (	strlen("NICK ")
			+ strlen (a_infos.client->nickname));
	tmp2 = malloc (	strlen("USER   :")
			+ strlen(a_infos.client->nickname)*2
			+ strlen (a_infos.client->realname)
			+ strlen (a_infos.server->host));

	sprintf (tmp, "NICK %s", a_infos.client->nickname);
	sprintf (tmp2, "USER %s %s %s: %s", a_infos.client->nickname,
		 a_infos.client->nickname, a_infos.server->host,
		 a_infos.client->realname);
	
	espik_debug_print("tmp: %s", tmp);
	espik_raw_send (tmp);
	espik_debug_print("tmp2: %s", tmp2);
	espik_raw_send (tmp2);
	espik_raw_send ("JOIN #e.fr");
	
	com_active = 1;
	
//	espik_debug_print ("<<<<<<<< DUMMMMY CODE >>>>>>>");

	free (tmp);
	free (tmp2);

	connected++;
	espik_leave();
}

void	espik_con_up()
{
	espik_enter();
	printf ("Connected\n");
	espik_leave();
}

void	espik_con_shutdown ()
{
	espik_enter();

	if (!connected)
		espik_debug_print ("Unable to connect to server");
	
	espik_debug_print ("ecore_con_server_del: %p",
			   ecore_con_server_del (con_sock));

	ecore_con_shutdown ();

	/*
	ecore_event_handler_del (handler_list.data);
	ecore_event_handler_del (handler_list.up);
	ecore_event_handler_del (handler_list.down);
	*/

	ecore_main_loop_quit ();
	espik_free (server_dns);

	espik_leave();
}

void	espik_raw_send (const char *msg)
{
	buf_t	buf;
	espik_enter();

	buf = make_buffer (strlen(msg) + 1);	/* <MESSAGE>\r\n\0" */

	snprintf (buf.buf, buf.len, "%s\r\n", msg);
	/* Override trailing \0 */
	espik_debug_print("strlen(msg): %d", strlen(msg));
	espik_debug_print("buf.len: %d", buf.len);	
	buf.buf[buf.len-1] = '\n';
//	buf.buf[buf.len] = '\0';

	espik_debug_print ("\n\nmsg(%d): %s\n\nout(%d-%d): %s\n", strlen(msg), msg, strlen(buf.buf), buf.len, buf.buf);
	/*
	printf ("out[len-3]: 0x%X\n", out[len - 3]);
	printf ("out[len-2]: 0x%X\n", out[len - 2]);
	printf ("out[len-1]: 0x%X\n", out[len - 1]);
	*/

	int i;
	for (i = 0 ; buf.buf[i] != 0 ; i++)
	{
		espik_debug_print ("buf.buf[%d]: '%d'", i, buf.buf[i]);
	}
	

	ret_int = ecore_con_server_send (con_sock, buf.buf, buf.len);

	espik_debug_print ("ret_int: %d", ret_int);
	free (buf.buf);
//	free (msg);
	espik_leave();
}