3 * Copyright (C) 2008 Tomas 'ZeXx86' Jedrzejek (zexx86@gmail.com)
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/>.
34 int cstrcmp (char *one
, char *two
)
38 for(i
= 0; (unsigned) i
< strlen (one
); i
++) {
43 if ((two
[i
] == ' ' || two
[i
] == '\0') || two
[i
] == '\n')
49 static int getstring (char *str
)
51 if (len
> COMMANDS_CACHE_SIZE
-1) {
52 printf ("\nERROR -> commands cache buffer is full ! Clearing cache ..\n");
80 /* get input from keyboard */
83 int l
= getstring (cmd_cache
);
88 return commands_handler (cmd_cache
, l
);
94 int commands_handler (char *buffer
, unsigned len
)
96 /* this is client command */
97 if (buffer
[0] == '/') {
98 if (!cstrcmp ("join", buffer
+1)) { /* join to channel ... ? */
99 config
.channel_len
= strlen (buffer
+6);
101 if (!config
.channel_len
) {
102 printf ("CLIENT -> Please specify channel !\n");
106 /* too long channel name */
107 if (config
.channel_len
>= 64)
110 config
.channel
= strdup (buffer
+6);
113 sprintf (str
, "JOIN %s\n", config
.channel
);
115 return net_send (str
, 6+config
.channel_len
);
116 } else if (!cstrcmp ("me", buffer
+1)) { /* send ACTION message */
117 /* we want send chat message to server */
118 char *msg
= (char *) malloc (sizeof (char) * (20+config
.channel_len
+len
));
123 memcpy (msg
, "PRIVMSG ", 8);
124 memcpy (msg
+8, config
.channel
, config
.channel_len
);
125 memcpy (msg
+8+config
.channel_len
, " :\1ACTION ", 10);
126 memcpy (msg
+18+config
.channel_len
, buffer
+4, len
-4);
127 msg
[14+config
.channel_len
+len
] = 1;
128 msg
[15+config
.channel_len
+len
] = '\n';
130 net_send (msg
, 16+config
.channel_len
+len
);
132 printf ("[***%s] %s\n", config
.nick
, buffer
+4);
137 } else if (!cstrcmp ("s", buffer
+1)) { /* change channel's window */
138 if (!config
.channel
) {
139 printf ("ERROR -> Please type /join #yourchannel first !\n");
143 free (config
.channel
);
145 config
.channel_len
= strlen (buffer
+3);
147 if (!config
.channel_len
) {
148 printf ("ERROR -> Please specify channel !\n");
152 config
.channel
= strdup (buffer
+3);
154 printf ("CLIENT -> Your window is switched to channel %s\n", config
.channel
);
156 } else if (!cstrcmp ("quit", buffer
+1)) { /* exit client */
160 printf ("CLIENT -> %s : Unknown command\n", buffer
+1);
164 /* we want send chat message to server */
165 char *msg
= (char *) malloc (sizeof (char) * (12+config
.channel_len
+len
));
170 memcpy (msg
, "PRIVMSG ", 8);
171 memcpy (msg
+8, config
.channel
, config
.channel_len
);
172 memcpy (msg
+8+config
.channel_len
, " :", 2);
173 memcpy (msg
+10+config
.channel_len
, buffer
, len
);
174 msg
[10+config
.channel_len
+len
] = '\n';
176 net_send (msg
, 11+config
.channel_len
+len
);
178 printf ("[%s]: %s\n", config
.nick
, buffer
);
187 cmd_cache
= (char *) malloc (sizeof (char) * COMMANDS_CACHE_SIZE
);
192 /* set socket "sock" to non-blocking */
193 int oldFlag
= fcntl (1, F_GETFL
, 0);
194 if (fcntl (1, F_SETFL
, oldFlag
| O_NONBLOCK
) == -1) {
195 printf ("Cant set socket to nonblocking mode\n");