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

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

#include <wchar.h>

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

#if 0
static char* convert_utf_unicode(char* putf)
{
	int len;
	wchar_t* punicode;
	espik_enter();

	len = strlen(putf)+ 1;

	punicode = (wchar_t *)malloc(len * sizeof(wchar_t));

	if (punicode == NULL)
		return NULL;

#ifdef UNICODE
	strcpy(punicode,putf);
#else
	if (mbstowcs(punicode,putf,len) < 0)
	{
		free(punicode);
		return NULL;
	}
#endif
	
	espik_leave();
	return punicode;
}
	

static char* convert_unicode_utf(const wchar_t *punicode)
{
	int	len;
	char*	putf;
	espik_enter();

	len = wcslen(punicode) + 1;

	putf = (char *)malloc(len * sizeof(wchar_t));

	if (!putf)
		return NULL;
#ifdef UNICODE
	strcpy(putf, punicode);
#else
	if (wcstombs(putf,punicode,len) < 0)
	{
		free (putf);
		return NULL;
	}
#endif
	espik_leave();
	return putf;
}
#endif


int	server_data (void* data __UNUSED__, int ev_type __UNUSED__,
		     Ecore_Con_Event_Server_Data* ev)
{
	unsigned int	i;
	char*		msg;
	espik_enter();
	static int	count_num_recv = 0;

	msg = strdup((char*)ev->data);
	//msg = convert_unicode_utf((const wchar_t*)ev->data);
	//msg = convert_utf_unicode((char*)ev->data);

	if (!msg)
		return 0;

	count_num_recv++;
	
	espik_debug_print ("%s", msg);

	for (i=0 ; i < strlen(msg) ; i++)
	{
		if (msg[i] == ' ')
			msg[i] = '\0';
	}

	if (count_num_recv >= 1 && !com_active)
	{
		server_dns = strdup (msg+1);
		send_login();
	}

	if (! (strcmp (msg, "PING")))
	{
		espik_raw_send ("PONG guybrush.melee");
	}

	memset (msg, 0, strlen(msg));
	free (msg);

	espik_leave();
	return (0);
}

int	kb_get (void* data __UNUSED__, Ecore_Fd_Handler* fd_handler)
{
	char*	kb;
	char*	tmp;
	int	count;
	espik_enter();

	kb = malloc (200);

	do
	{
		memset (kb, 0, 200);
		count = read (ecore_main_fd_handler_fd_get (fd_handler), kb,
			      198);

		if (count > 1)
		{
			del_backslash (kb);
			tmp = irc_send (kb);

			if (tmp)
			{
				espik_raw_send (tmp);
				free (tmp);
			}
		}
	}
	while (count >= 198);

	if (count == -1)
		perror ("read");

	if (count == 0)
	{
		tmp = irc_quit (NULL, "^D pressed");
		espik_debug_print ("tmp: %s", tmp);
		espik_raw_send (tmp);
		espik_debug_print ("AFTER espik_raw_send");
		//espik_raw_send (tmp);

		/*
		irc_disconnect();
		*/
		espik_con_shutdown ();

		free (tmp);
		espik_leave();
		return (0);
	}

	espik_leave();
	return (1);
}