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/>.
35 int cstrcmp (char *one
, char *two
)
39 for(i
= 0; (unsigned) i
< strlen (one
); i
++) {
44 if ((two
[i
] == ' ' || two
[i
] == '\0') || two
[i
] == '\n')
50 static int getstring (char *str
)
52 if (len
> COMMANDS_CACHE_SIZE
-1) {
54 printf ("\nERROR -> commands cache buffer is full ! Clearing cache ..\n");
84 /* get input from keyboard */
87 int l
= getstring (cmd_cache
);
92 return commands_handler (cmd_cache
, l
);
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");
110 /* too long channel name */
111 if (config
.channel_len
>= 64)
114 config
.channel
= strdup (buffer
+6);
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
));
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
);
137 printf ("[***%s] %s\n", config
.nick
, buffer
+4);
143 } else if (!cstrcmp ("s", buffer
+1)) { /* change channel's window */
144 if (!config
.channel
) {
145 printf ("ERROR -> Please type /join #yourchannel first !\n");
149 free (config
.channel
);
151 config
.channel_len
= strlen (buffer
+3);
153 if (!config
.channel_len
) {
154 printf ("ERROR -> Please specify channel !\n");
158 config
.channel
= strdup (buffer
+3);
160 printf ("CLIENT -> Your window is switched to channel %s\n", config
.channel
);
162 } else if (!cstrcmp ("nick", buffer
+1)) { /* change nick name */
164 unsigned nick_len
= strlen (buffer
+3);
167 printf ("ERROR -> Please specify nick name !\n");
174 config
.nick
= strdup (buffer
+6);
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 */
184 /* we want send chat message to server */
185 char *msg
= (char *) malloc (sizeof (char) * (21+config
.channel_len
+len
));
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
);
204 printf ("[***%s] %s\n", config
.nick
, buffer
);
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");
218 } else if (!cstrcmp ("quit", buffer
+1)) { /* exit client */
222 printf ("CLIENT -> %s : Unknown command\n", buffer
+1);
226 /* we want send chat message to server */
227 char *msg
= (char *) malloc (sizeof (char) * (12+config
.channel_len
+len
));
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
);
241 printf ("[%s]: %s\n", config
.nick
, buffer
);
251 cmd_cache
= (char *) malloc (sizeof (char) * COMMANDS_CACHE_SIZE
);
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");