Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / apps / irc / proto.c
blobc2f4313913062c3aaeaaaac26d8e87444d520586
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
5 * This program is free software: you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation, either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include <time.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include "commands.h"
25 #include "config.h"
26 #include "proto.h"
27 #include "net.h"
29 char *parser;
30 char *parser_tmp;
31 char *pong_msg;
32 unsigned parser_len;
34 #ifdef LINUX
35 int setcolor (unsigned char fg, unsigned char bg)
37 return 1;
39 #endif
41 int proto_command (char *buffer, unsigned len)
43 buffer[len] = '\0';
45 if (buffer[0] == ':') {
46 /* server message */
48 if (!strncmp (config.server, buffer+1, config.server_len)) {
49 char *str = strchr (buffer+2+config.server_len, ':');
51 /* servername 353 - user list */
52 setcolor (2, 0);
53 printf ("SERVER -> %s\n", str ? str+1 : "!MESSAGE PARSE ERROR!\n");
54 setcolor (15, 0);
55 return 1;
58 char *str = strchr (buffer+2, '!');
60 /* cut of nick */
61 if (str)
62 str[0] = '\0';
64 char *str2 = strchr (str+1, ' ');
66 if (!str2) {
67 printf ("ERROR -> unknown command from server\n");
68 return 1;
71 /* chat message */
72 if (!cstrcmp ("PRIVMSG", str2+1)) {
73 char *msg = strchr (str2+10, ':');
75 if (!msg)
76 return 1;
78 msg --;
79 /* cut of channel name */
80 msg[0] = '\0';
82 msg += 2;
84 //if (!strncmp (str2+9, config.channel, config.channel_len)) {
85 if (strncmp (msg+1, "ACTION", 6)) {
86 setcolor (14, 0);
87 printf ("> %s: %s\n", buffer+1, msg);
88 } else {
89 setcolor (10, 0);
90 printf ("> ***%s %s\n", buffer+1, msg+8);
92 /*} else {
93 if (strncmp (msg+1, "ACTION", 6)) {
94 setcolor (11, 0);
95 printf ("> (%s) %s: %s\n", str2+9, buffer+1, msg);
96 } else {
97 setcolor (3, 0);
98 printf ("> (%s) ***%s %s\n", str2+9, buffer+1, msg+8);
102 setcolor (15, 0);
103 return 1;
104 } else if (!cstrcmp ("QUIT", str2+1)) {
105 setcolor (4, 0);
106 printf ("User %s was disconnected\n", buffer+1);
107 setcolor (15, 0);
108 return 1;
111 return 1;
115 if (!strncmp (buffer, "PING", 4))
116 return net_send (pong_msg, 10+(3*config.server_len));
118 setcolor (7, 0);
119 printf (">> %s\n", buffer);
120 setcolor (15, 0);
122 return 1;
125 int proto_parser ()
127 unsigned i = 0;
129 while (i < parser_len) {
130 if (parser[i] == '\n') {
131 proto_command (parser, i);
133 i ++;
135 //memcpy (parser+parser_len-len+i, buffer, len);
136 if (i == parser_len) {
137 parser_len = 0;
138 parser = parser_tmp;
139 //printf ("parser_len == i - NULL\n");
140 //memset (parser, 0, PROTO_BUFSIZE_PARSER);
141 break;
142 } else {
143 //printf ("parser_len: %d / %d\n", i, parser_len);
145 parser += i;
146 parser_len -= i;
148 proto_parser ();
149 break;
153 i ++;
156 return 1;
159 int proto_handler (char *buffer, unsigned len)
161 if (parser_len > (PROTO_BUFSIZE_PARSER-len)) {
162 printf ("ERROR -> receive buffer is full, this cause loss of last packets\n");
164 return 0;
167 memcpy (parser+parser_len, buffer, len);
168 parser_len += len;
170 //printf ("proto_handler () - %d\n", len);
172 return 1;
175 int init_proto ()
177 parser = (char *) malloc (sizeof (char) * PROTO_BUFSIZE_PARSER);
179 if (!parser)
180 return 0;
182 parser_tmp = parser;
184 parser_len = 0;
186 char str[64];
187 sprintf (str, "USER %s blah blah :zexos irc client\nNICK %s\n", config.nick, config.nick);
189 net_send (str, strlen (str));
191 pong_msg = (char *) malloc (sizeof (char) * (config.server_len*3 + 10));
193 if (!pong_msg)
194 return 0;
196 pong_msg[0] = ':';
197 memcpy (pong_msg+1, config.server, config.server_len);
198 memcpy (pong_msg+1+config.server_len, " PONG ", 6);
199 memcpy (pong_msg+7+config.server_len, config.server, config.server_len);
200 memcpy (pong_msg+7+(2*config.server_len), " :", 2);
201 memcpy (pong_msg+9+(2*config.server_len), config.server, config.server_len);
202 pong_msg[9+(3*config.server_len)] = '\n';
204 return 1;