aboutsummaryrefslogtreecommitdiff
path: root/src/espik_irc.c
blob: 4da2139d42c3d7f41e1cf9923948487c2c1c3a7a (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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#include <stdlib.h>
#include <string.h>
#include <stdio.h>

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

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

char*	current_chan;

typedef struct
{
    char*	cmd_name;
    char*	(*format) (char *dest, char *msg);
} cmd_t;

typedef struct
{
	char*	buf;
	int		len;
} buf_t;

cmd_t	cmd_tab[] = {
	{ "JOIN",		irc_join	},
	{ "PART",		irc_part	},
	{ "PRIVMSG",	irc_privmsg	},
	{ "QUIT",		irc_quit	},
	{ "RAW",		irc_raw		},
	{ "NICK",		irc_nick	},
/*	{ "ACTION", irc_action		}, */
	{ 0, 0 }
};

char**	separate_commandline (char *msg)
{
	unsigned int	len;
	char**			two_words;

	/* Yes, do nothing, just get len ! */
	for (len = 0; msg[len] != ' '; len++);
		two_words = malloc (sizeof(char*) * 2);

	msg[len]	 = '\0';
	two_words[0] = msg;
	two_words[1] = msg + len + 1;

	return (two_words);
}

void	string_upper (char *str)
{
	unsigned int	len, i;

	len = strlen (str);

	for (i = 0; i < len; i++)
	{
		if (str[i] >= 0x61 && str[i] <= 0x7A)
			str[i] -= 0x20;
	}
#if _ESPIK_DEBUG_
	printf("string_upper: %s\n", str);
#endif
}

unsigned short	sendmsg_len (char* msg)
{
	return (strlen (msg) + 2);
}

static buf_t	make_buffer(int len)
{
	buf_t	buf;

	buf.buf = malloc (sizeof(char) * len);
	buf.len = len;

	return (buf);
}

char*	irc_privmsg (char* chan, char* msg)
{
	buf_t			out;

	out = make_buffer (strlen ("PRIVMSG") + 1	/* "PRIVMSG "		*/
			+ strlen (chan) + 2					/* ":<channel> "	*/
			+ strlen (msg) + 1);				/* "<msg>EOF"		*/

	snprintf (out.buf, out.len, "PRIVMSG %s :%s", chan, msg);

	return (out.buf);
}

char*	irc_join (char* chan __UNUSED__, char* msg)
{
	buf_t	out;
	
	/* COuld also by '&' and so one ...
	 * have to look on RFC, dev will be done with # only */
	if (msg[0] != '#')
	{
		printf ("%s is not a channel\n", msg);
		printf ("Usage: /JOIN #channel\n");
		return (0);
	}

	out = make_buffer (strlen ("JOIN") + 1		/* "JOIN "		*/
			+ strlen (msg) + 1);				/* "<msg>EOF"	*/

	snprintf (out.buf, out.len, "JOIN %s", msg);

	return (out.buf);
}

char*	irc_part (char* chan, char* msg)
{
	buf_t	out;

	if (msg[0] != '#')
	{
		out = make_buffer (strlen ("PART") + 1	/* "PART "		*/
				+ strlen (chan) + 2				/* ":<chan> "	*/
				+ strlen (msg) + 1);			/* "<msg>EOF"	*/

		snprintf (out.buf, out.len, "PART %s :%s", chan, msg);
	}
	else
	{
		char**	sep;

		sep = separate_commandline (msg);

		out = make_buffer (strlen ("PART") + 1	/* "PART "		*/
				+ strlen (sep[0]) + 2			/* ":<chan> "	*/
				+ strlen (sep[1]) + 1);			/* "<msg>EOF"	*/

		snprintf (out.buf, out.len, "PART %s :%s", sep[0], sep[1]);
	}

	return (out.buf);
}

char*	irc_quit (char* chan __UNUSED__, char* msg)
{
	buf_t	out;

	out = make_buffer (strlen ("QUIT") + 2		/* "QUIT :"		*/
			+ strlen (msg) + 1);				/* "<msg>EOF"	*/

	snprintf (out.buf, out.len, "QUIT :%s", msg);

#if _ESPIK_DEBUG_
	printf ("msg : %s\nout: %s\n", msg, out.buf);
#endif

	/*
		espik_raw_send (out);
		espik_con_shutdown ();

		irc_disconnect(sock);
	*/

	return (out.buf);
}

char*	irc_raw (char* chan __UNUSED__, char* msg)
{
	return (msg);
}

char*	irc_nick (char* chan __UNUSED__, char* nick)
{
	buf_t	out;

	out = make_buffer (strlen ("NICK") + 1		/* "NICK "		*/
			+ strlen (nick) + 1);				/* "<nick>EOF"	*/

	snprintf (out.buf, out.len, "NICK %s", nick);

#if _ESPIK_DEBUG_
	printf ("NICK CHANGED REQUEST: %s\n", nick);
#endif

	return (out.buf);
}

#if 0
Ecore_Con_Server*	irc_connect (char* host __UNUSED__, int port __UNUSED__)
{
    return (NULL);
}

void	irc_disconnect (Ecore_Con_Server *sock)
{
    printf ("> irc_disconnect\n");
    ecore_main_loop_quit ();
    printf ("< irc_disconnect\n");
}
#endif

char*	irc_send (char* msg)
{
	char*	out;

	out = NULL;

	current_chan = "#test";

	if (msg[0] == '/')
	{
		char**	cmd_and_message;
		cmd_t*	cmd;

		cmd_and_message = separate_commandline (msg + 1);

#if _ESPIK_DEBUG_
		printf ("irc_send: <separate_commandline\n");
#endif
		
		string_upper (cmd_and_message[0]);

		for (cmd = cmd_tab; cmd->cmd_name != 0; cmd++)
		{
			if ((strcmp (cmd_and_message[0], cmd->cmd_name)) == 0)
			{
				out = cmd->format (current_chan, cmd_and_message[1]);
				/* WHy do job after found the good to do ? */
				break;   
			}
		}
		if (!out)
		{
			printf ("%s is not a know command\n", cmd_and_message[0]);
			return (0);
		}
	}
	else
	{
		out = irc_privmsg (current_chan, msg);
	}

	if (!out)
		return (0);

	printf ("%s\n", out);

	return (out);
}