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/>.
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";
46 static struct proto_cmd_t proto_cmd
[] = {
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));
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);
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));
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);
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));
108 sprintf (str
, "%s %s\n", proto_cmd
[4].name
, target
);
110 net_send (str
, proto_cmd
[4].len
+ target_len
+ 2);
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));
126 sprintf (str
, "%s %s\n", proto_cmd
[5].name
, target
);
128 net_send (str
, proto_cmd
[5].len
+ target_len
+ 2);
137 char *str
= (char *) malloc (sizeof (char) * (proto_cmd
[3].len
+ 2));
142 sprintf (str
, "%s\n", proto_cmd
[3].name
);
144 net_send (str
, proto_cmd
[3].len
+ 1);
151 int proto_status (char *msg
, unsigned len
)
153 char *str
= (char *) malloc (sizeof (char) * (proto_cmd
[6].len
+ len
+ 3));
158 sprintf (str
, "%s %s\n", proto_cmd
[6].name
, msg
);
160 net_send (str
, proto_cmd
[6].len
+ len
+ 2);
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
)
175 char *str
= (char *) malloc (sizeof (char) * (proto_cmd
[0].len
+ nick_len
+ password_len
+ 4));
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);
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);
209 /** INIT PROTOCOL function */
212 /* nick and password are not filled in config */
216 if (!proto_login (cfg
->nick
, cfg
->password
))