app irc was completely rewrited from base, should be stable now, support multichannel...
[ZeXOS.git] / apps / irc / commands.c
blob511bd37f2b845b7095718c24413a1964b13a6833
1 /*
2 * ZeX/OS
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/>.
20 #include <time.h>
21 #include <stdio.h>
22 #include <fcntl.h>
23 #include <string.h>
24 #include <stdlib.h>
25 #include <unistd.h>
26 #include "commands.h"
27 #include "config.h"
28 #include "net.h"
30 char *cmd_cache;
32 static unsigned len;
34 int cstrcmp (char *one, char *two)
36 int i;
38 for(i = 0; (unsigned) i < strlen (one); i ++) {
39 if (one[i] != two[i])
40 return -1;
43 if ((two[i] == ' ' || two[i] == '\0') || two[i] == '\n')
44 return 0;
46 return -2;
49 static int getstring (char *str)
51 if (len > COMMANDS_CACHE_SIZE-1) {
52 printf ("\nERROR -> commands cache buffer is full ! Clearing cache ..\n");
53 len = 0;
54 return 0;
57 char c = getchar ();
59 if (c != -1) {
60 if (c == '\n') {
61 unsigned l = len;
62 len = 0;
64 return l;
66 if (c == '\b') {
67 if (len > 0)
68 len --;
70 return 0;
73 str[len] = c;
74 len ++;
77 return 0;
80 /* get input from keyboard */
81 int commands_get ()
83 int l = getstring (cmd_cache);
85 if (l > 0) {
86 cmd_cache[l] = '\0';
88 return commands_handler (cmd_cache, l);
91 return 1;
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");
103 return 1;
106 /* too long channel name */
107 if (config.channel_len >= 64)
108 return 1;
110 config.channel = strdup (buffer+6);
112 char str[64];
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));
120 if (!msg)
121 return 0;
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);
134 free (msg);
136 return 1;
137 } else if (!cstrcmp ("s", buffer+1)) { /* change channel's window */
138 if (!config.channel) {
139 printf ("ERROR -> Please type /join #yourchannel first !\n");
140 return 1;
143 free (config.channel);
145 config.channel_len = strlen (buffer+3);
147 if (!config.channel_len) {
148 printf ("ERROR -> Please specify channel !\n");
149 return 1;
152 config.channel = strdup (buffer+3);
154 printf ("CLIENT -> Your window is switched to channel %s\n", config.channel);
155 return 1;
156 } else if (!cstrcmp ("quit", buffer+1)) { /* exit client */
157 return 0;
160 printf ("CLIENT -> %s : Unknown command\n", buffer+1);
161 return 1;
164 /* we want send chat message to server */
165 char *msg = (char *) malloc (sizeof (char) * (12+config.channel_len+len));
167 if (!msg)
168 return 0;
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);
180 free (msg);
182 return 1;
185 int commands_init ()
187 cmd_cache = (char *) malloc (sizeof (char) * COMMANDS_CACHE_SIZE);
189 if (!cmd_cache)
190 return 0;
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");
196 return 0;
199 len = 0;
201 return 1;