Fixed ZDE build - missing header file
[ZeXOS.git] / apps / im / src / proto.c
blob6313e743e0bb149335cd8d35befccac9b51dfb80
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 "platform.h"
21 #include "config.h"
22 #include "user.h"
23 #include "net.h"
25 #include <fcntl.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <stdlib.h>
31 "REGISTER <name> <password> -- registruje noveho uzivatela\n"
32 "LOGIN <name> <password> -- prihasuje uzivatela\n"
33 "LOGOUT -- odhasuje uzivatela\n"
34 "LIST -- vypise zoznam kontaktov\n"
35 "ADD <name> -- prida uzivatela do zoznamu kontaktov\n"
36 "DEL <name> -- odoberie uzivatela zo zoznamu kontaktov\n"
37 "STATUS <new_status> -- zmeni status uzivatelovy\n"
38 "MSG <name> <message> -- odosle spravu uzivatelovy\n";
41 struct proto_cmd_t {
42 char *name;
43 unsigned char len;
46 static struct proto_cmd_t proto_cmd[] = {
47 { "REGISTER", 8 },
48 { "LOGIN", 5 },
49 { "LOGOUT", 6 },
50 { "LIST", 4 },
51 { "ADD", 3 },
52 { "DEL", 3 },
53 { "STATUS", 6 },
54 { "MSG", 3 },
55 { "CHANGE", 6 },
56 { "MY:", 3 },
57 { "ERR", 3 },
58 { "OK", 2 },
59 { (char *) NULL, (unsigned char) NULL }
62 int proto_login (char *nick, char *password)
64 unsigned nick_len = strlen (nick);
65 unsigned password_len = strlen (password);
67 char *str = (char *) malloc (sizeof (char) * (proto_cmd[1].len + nick_len + password_len + 4));
69 if (!str)
70 return 0;
72 sprintf (str, "%s %s %s\n", proto_cmd[1].name, nick, password);
74 net_send (str, proto_cmd[1].len + nick_len + password_len + 3);
76 free (str);
78 return 1;
81 int proto_msg (char *target, char *msg, unsigned len)
83 unsigned target_len = strlen (target);
85 char *str = (char *) malloc (sizeof (char) * (proto_cmd[7].len + target_len + len + 4));
87 if (!str)
88 return 0;
90 sprintf (str, "%s %s %s\n", proto_cmd[7].name, target, msg);
92 net_send (str, proto_cmd[7].len + target_len + len + 3);
94 free (str);
96 return 1;
99 int proto_add (char *target)
101 unsigned target_len = strlen (target);
103 char *str = (char *) malloc (sizeof (char) * (proto_cmd[4].len + target_len + 3));
105 if (!str)
106 return 0;
108 sprintf (str, "%s %s\n", proto_cmd[4].name, target);
110 net_send (str, proto_cmd[4].len + target_len + 2);
112 free (str);
114 return 1;
117 int proto_del (char *target)
119 unsigned target_len = strlen (target);
121 char *str = (char *) malloc (sizeof (char) * (proto_cmd[5].len + target_len + 3));
123 if (!str)
124 return 0;
126 sprintf (str, "%s %s\n", proto_cmd[5].name, target);
128 net_send (str, proto_cmd[5].len + target_len + 2);
130 free (str);
132 return 1;
135 int proto_list ()
137 char *str = (char *) malloc (sizeof (char) * (proto_cmd[3].len + 2));
139 if (!str)
140 return 0;
142 sprintf (str, "%s\n", proto_cmd[3].name);
144 net_send (str, proto_cmd[3].len + 1);
146 free (str);
148 return 1;
151 int proto_status (char *msg, unsigned len)
153 char *str = (char *) malloc (sizeof (char) * (proto_cmd[6].len + len + 3));
155 if (!str)
156 return 0;
158 sprintf (str, "%s %s\n", proto_cmd[6].name, msg);
160 net_send (str, proto_cmd[6].len + len + 2);
162 free (str);
164 return 1;
167 int proto_register (char *nick, char *password)
169 unsigned nick_len = strlen (nick);
170 unsigned password_len = strlen (password);
172 if (!nick_len || !password_len)
173 return 0;
175 char *str = (char *) malloc (sizeof (char) * (proto_cmd[0].len + nick_len + password_len + 4));
177 if (!str)
178 return 0;
180 sprintf (str, "%s %s %s\n", proto_cmd[0].name, nick, password);
182 net_send (str, proto_cmd[0].len + nick_len + password_len + 3);
184 free (str);
186 return 1;
189 int proto_handler (char *str, unsigned len)
191 if (!strncmp (str, proto_cmd[8].name, proto_cmd[8].len))
192 return user_statusincoming (str+4, len-4);
194 if (!strncmp (str, proto_cmd[7].name, proto_cmd[7].len))
195 return user_msgincoming (str+4, len-4);
197 if (!strncmp (str, proto_cmd[9].name, proto_cmd[9].len))
198 return user_list_get (str+4, len-4);
200 if (!strncmp (str, proto_cmd[10].name, proto_cmd[10].len))
201 return user_error (str+4, len-4);
203 if (!strncmp (str, proto_cmd[11].name, proto_cmd[11].len))
204 return user_info (str+3, len-3);
206 return 1;
209 /** INIT PROTOCOL function */
210 int init_proto ()
212 /* nick and password are not filled in config */
213 if (!cfg->autolog)
214 return 1;
216 if (!proto_login (cfg->nick, cfg->password))
217 return 0;
219 if (!proto_list ())
220 return 0;
222 return 1;