Fixed buffer overflow in mserver; fixed type of checkinfo () 2nd parameter; memory
[ZeXOS.git] / apps / irc / commands.c
blobf968f247736c7eb8f9872a69b661369ec972e38c
1 /*
2 * ZeX/OS
3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
4 * Copyright (C) 2009 Tomas 'ZeXx86' Jedrzejek (zexx86@zexos.org)
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include <time.h>
22 #include <stdio.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <stdlib.h>
26 #include <unistd.h>
27 #include "commands.h"
28 #include "config.h"
29 #include "net.h"
31 char *cmd_cache;
33 static unsigned len;
35 int cstrcmp (char *one, char *two)
37 int i;
39 for(i = 0; (unsigned) i < strlen (one); i ++) {
40 if (one[i] != two[i])
41 return -1;
44 if ((two[i] == ' ' || two[i] == '\0') || two[i] == '\n')
45 return 0;
47 return -2;
50 static int getstring (char *str)
52 if (len > COMMANDS_CACHE_SIZE-1) {
53 setcolor (4, 0);
54 printf ("\nERROR -> commands cache buffer is full ! Clearing cache ..\n");
55 setcolor (15, 0);
57 len = 0;
58 return 0;
61 char c = getchar ();
63 if (c != -1) {
64 if (c == '\n') {
65 unsigned l = len;
66 len = 0;
68 return l;
70 if (c == '\b') {
71 if (len > 0)
72 len --;
74 return 0;
77 str[len] = c;
78 len ++;
81 return 0;
84 /* get input from keyboard */
85 int commands_get ()
87 int l = getstring (cmd_cache);
89 if (l > 0) {
90 cmd_cache[l] = '\0';
92 return commands_handler (cmd_cache, l);
95 return 1;
98 int commands_handler (char *buffer, unsigned len)
100 /* this is client command */
101 if (buffer[0] == '/') {
102 if (!cstrcmp ("join", buffer+1)) { /* join to channel ... ? */
103 config.channel_len = strlen (buffer+6);
105 if (!config.channel_len) {
106 printf ("CLIENT -> Please specify channel !\n");
107 return 1;
110 /* too long channel name */
111 if (config.channel_len >= 64)
112 return 1;
114 config.channel = strdup (buffer+6);
116 char str[64];
117 sprintf (str, "JOIN %s\n", config.channel);
119 return net_send (str, 6+config.channel_len);
120 } else if (!cstrcmp ("me", buffer+1)) { /* send ACTION message */
121 /* we want send chat message to server */
122 char *msg = (char *) malloc (sizeof (char) * (20+config.channel_len+len));
124 if (!msg)
125 return 0;
127 memcpy (msg, "PRIVMSG ", 8);
128 memcpy (msg+8, config.channel, config.channel_len);
129 memcpy (msg+8+config.channel_len, " :\1ACTION ", 10);
130 memcpy (msg+18+config.channel_len, buffer+4, len-4);
131 msg[14+config.channel_len+len] = 1;
132 msg[15+config.channel_len+len] = '\n';
134 net_send (msg, 16+config.channel_len+len);
136 setcolor (6, 0);
137 printf ("[***%s] %s\n", config.nick, buffer+4);
138 setcolor (15, 0);
140 free (msg);
142 return 1;
143 } else if (!cstrcmp ("s", buffer+1)) { /* change channel's window */
144 if (!config.channel) {
145 printf ("ERROR -> Please type /join #yourchannel first !\n");
146 return 1;
149 free (config.channel);
151 config.channel_len = strlen (buffer+3);
153 if (!config.channel_len) {
154 printf ("ERROR -> Please specify channel !\n");
155 return 1;
158 config.channel = strdup (buffer+3);
160 printf ("CLIENT -> Your window is switched to channel %s\n", config.channel);
161 return 1;
162 } else if (!cstrcmp ("nick", buffer+1)) { /* change nick name */
164 unsigned nick_len = strlen (buffer+3);
166 if (!nick_len) {
167 printf ("ERROR -> Please specify nick name !\n");
168 return 1;
171 if (config.nick)
172 free (config.nick);
174 config.nick = strdup (buffer+6);
176 char str[64];
177 sprintf (str, "NICK %s\n", config.nick);
179 printf ("CLIENT -> Your nick name was changed to %s\n", config.nick);
181 return net_send (str, 6+nick_len);
182 } else if (!cstrcmp ("os", buffer+1)) { /* send ACTION message */
183 len = 73;
184 /* we want send chat message to server */
185 char *msg = (char *) malloc (sizeof (char) * (21+config.channel_len+len));
187 if (!msg)
188 return 0;
190 memcpy (buffer, "is running on ZeX/OS - operating system created by ZeXx86 - www.zexos.org", 73);
192 memcpy (msg, "PRIVMSG ", 8);
193 memcpy (msg+8, config.channel, config.channel_len);
194 memcpy (msg+8+config.channel_len, " : ACTION ", 10);
195 memcpy (msg+18+config.channel_len, buffer, len);
196 msg[10+config.channel_len] = 1;
197 msg[18+config.channel_len+len] = 1;
198 msg[19+config.channel_len+len] = '\n';
199 msg[20+config.channel_len+len] = '\0';
201 net_send (msg, 20+config.channel_len+len);
203 setcolor (6, 0);
204 printf ("[***%s] %s\n", config.nick, buffer);
205 setcolor (15, 0);
207 free (msg);
209 return 1;
210 } else if (!cstrcmp ("help", buffer+1)) { /* exit client */
211 printf ("Command\t\t\tDescription\n"
212 "/join <channel>\t\tJoin to specified channel\n"
213 "/me <message>\t\tSpeak with 3rd person\n"
214 "/s <channel>\t\tSwitch channel where you talk\n"
215 "/nick <nick>\t\tChange nick name\n"
216 "/quit\t\t\tQuit a client\n");
217 return 1;
218 } else if (!cstrcmp ("quit", buffer+1)) { /* exit client */
219 return 0;
222 printf ("CLIENT -> %s : Unknown command\n", buffer+1);
223 return 1;
226 /* we want send chat message to server */
227 char *msg = (char *) malloc (sizeof (char) * (12+config.channel_len+len));
229 if (!msg)
230 return 0;
232 memcpy (msg, "PRIVMSG ", 8);
233 memcpy (msg+8, config.channel, config.channel_len);
234 memcpy (msg+8+config.channel_len, " :", 2);
235 memcpy (msg+10+config.channel_len, buffer, len);
236 msg[10+config.channel_len+len] = '\n';
238 net_send (msg, 11+config.channel_len+len);
240 setcolor (7, 0);
241 printf ("[%s]: %s\n", config.nick, buffer);
242 setcolor (15, 0);
244 free (msg);
246 return 1;
249 int commands_init ()
251 cmd_cache = (char *) malloc (sizeof (char) * COMMANDS_CACHE_SIZE);
253 if (!cmd_cache)
254 return 0;
256 /* set socket "sock" to non-blocking */
257 int oldFlag = fcntl (1, F_GETFL, 0);
258 if (fcntl (1, F_SETFL, oldFlag | O_NONBLOCK) == -1) {
259 printf ("Cant set socket to nonblocking mode\n");
260 return 0;
263 len = 0;
265 return 1;