Cleanup in elf.c with .bss section clean; adm command mounts cdrom instead of floppy...
[ZeXOS.git] / apps / im / src / user.c
bloba843df7f072cea54a1b1f1731f018ec6898cb93d
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 "config.h"
21 #include "proto.h"
22 #include "user.h"
24 #include <stdio.h>
25 #include <string.h>
26 #include <stdlib.h>
28 static user_t user_list;
29 static user_t *talkto;
32 int user_list_add (char *id)
34 /* alloc and init context */
35 user_t *user = (user_t *) malloc (sizeof (user_t));
37 if (!user)
38 return 0;
40 user->id = strdup (id);
42 /* add into list */
43 user->next = &user_list;
44 user->prev = user_list.prev;
45 user->prev->next = user;
46 user->next->prev = user;
48 return 1;
51 static int strnchrn (char *str, unsigned len)
53 unsigned l = 0;
55 while (l < len) {
56 if (str[l] == '\n') {
57 str[l] = '\0';
59 return l;
62 l ++;
65 return 0;
68 int user_list_get (char *data, unsigned len)
70 unsigned l = 0;
72 while (l < len) {
73 unsigned i = strnchrn (data+l, len-l);
75 if (!i)
76 break;
78 if (!strcmp (data+l, "THEY:"))
79 break;
81 printf ("user_list_add: %s\n", data+l);
82 if (!user_list_add (data+l))
83 return 0;
85 l += (i + 1);
88 return 1;
91 user_t *user_list_find (char *target, unsigned len)
93 user_t *c;
95 for (c = user_list.next; c != &user_list; c = c->next) {
96 if (!strncmp (c->id, target, len))
97 return c;
100 return 0;
103 int user_list_del (char *id)
105 user_t *user = user_list_find (id, strlen (id));
107 if (!user)
108 return 0;
110 user->next->prev = user->prev;
111 user->prev->next = user->next;
113 free (user->id);
114 user->len = 0;
116 free (user);
118 return 1;
121 int user_list_show ()
123 printf ("Contact list:\n");
125 user_t *c;
127 for (c = user_list.next; c != &user_list; c = c->next) {
128 if (c == user_list.next)
129 printf ("%s", c->id);
130 else
131 printf (", %s", c->id);
134 printf ("\n");
136 return 1;
139 int user_cmd_talkto (char *target, unsigned len)
141 user_t *c = user_list_find (target, len);
143 if (!c)
144 return 0;
146 talkto = c;
148 return 1;
151 static int strnchrr (char *str, unsigned len)
153 unsigned l = 0;
155 while (l < len) {
156 if (str[l] == ' ') {
157 str[l] = '\0';
159 return l;
162 l ++;
165 return 0;
168 /* Some user announce us about new status */
169 int user_statusincoming (char *str, unsigned len)
171 printf ("status: %s\n", str);
172 int l = strnchrr (str, len);
174 if (!l)
175 return 0;
177 int nick_len = strlen (str);
179 if (!nick_len)
180 return 0;
182 user_t *user = user_list_find (str, nick_len);
184 if (!user) {
185 printf ("WARNING -> Incoming status (%s) ID (%s) not agree with contact list items\n", str+nick_len+1, str);
187 return 0;
190 if (!strncmp (user->id, cfg->nick, strlen (cfg->nick)))
191 printf ("* Your status is changed to: %s\n", str+nick_len+1);
192 else
193 printf ("* User %s change status to: %s\n", user->id, str+nick_len+1);
195 return 1;
198 /* Some user send to us new message, let's print it to screen */
199 int user_msgincoming (char *str, unsigned len)
201 int l = strnchrr (str, len);
203 if (!l)
204 return 0;
206 int nick_len = strlen (str);
208 if (!nick_len)
209 return 0;
210 else {
211 nick_len --;
212 str[nick_len] = '\0';
215 user_t *user = user_list_find (str, nick_len);
217 if (!user) {
218 printf ("WARNING -> Unauthorized message from ID (%s): %s\n", str, str+nick_len+2);
220 return 0;
223 if (!talkto) {
224 talkto = user;
226 printf ("Autoswitched to user %s\n> %s: %s\n", user->id, user->id, str+nick_len+2);
227 } else {
228 if (talkto == user)
229 printf ("> %s\n", str+nick_len+2);
230 else
231 printf ("> %s: %s\n", user->id, str+nick_len+2);
234 return 1;
237 int user_islogged ()
239 if (!cfg->autolog) {
240 printf ("ERROR -> Please login first, use /h for help\n");
242 return 0;
245 return 1;
248 int user_msgparse (char *str, unsigned len)
250 /* direct message */
251 if (str[0] != '/') {
252 if (talkto)
253 return proto_msg (talkto->id, str, len);
254 else {
255 printf ("WARNING -> No user is selected for conversation\nPlease type: /t <user@server>\n");
257 return 0;
260 return 1;
263 /* user commands */
266 if (str[1] == 'm') { /* message */
267 if (!user_islogged ())
268 return 0;
270 int l = strnchrr (str+3, len-3);
272 if (!l) {
273 printf ("WARNING -> wrong message syntax\nSYNTAX: /m <nick@server> <message>\n");
275 return 0;
278 int nick_len = strlen (str+3);
280 if (!nick_len)
281 return 0;
283 user_t *user = user_list_find (str+3, nick_len);
285 if (!user) {
286 printf ("WARNING -> this ID not agree with contact list items\n");
288 return 0;
291 return proto_msg (user->id, str+4+nick_len, len-4-nick_len);
292 } else if (str[1] == 'c') { /* contact list */
293 if (!user_islogged ())
294 return 0;
296 return user_list_show ();
297 } else if (str[1] == 't') { /* select user to talk */
298 if (!user_islogged ())
299 return 0;
301 if (user_cmd_talkto (str+3, len-3))
302 printf ("Selected user: %s\n", str+3);
303 else
304 printf ("WARNING -> this ID (%s) not agree with contact list items\n", str+3);
306 return 1;
307 } else if (str[1] == 's') { /* change your status */
308 if (!user_islogged ())
309 return 0;
311 if (proto_status (str+3, len-3))
312 printf ("* Status is: %s\n", str+3);
314 return 1;
315 } else if (str[1] == 'a') { /* add new user to contact list */
316 if (!user_islogged ())
317 return 0;
319 if (!proto_add (str+3))
320 return 0;
322 if (!user_list_add (str+3))
323 return 0;
325 printf ("+ ID %s added to contact list\n", str+3);
327 return 1;
328 } else if (str[1] == 'd') { /* delete user from contact list */
329 if (!user_islogged ())
330 return 0;
332 if (!user_list_del (str+3))
333 return 0;
335 if (!proto_del (str+3))
336 return 0;
338 printf ("- ID %s deleted from contact list\n", str+3);
340 return 1;
341 } else if (str[1] == 'r') { /* register new account on server */
342 int l = strnchrr (str+3, len-3);
344 if (!l) {
345 printf ("WARNING -> wrong register syntax\nSYNTAX: /r <nick> <password>\n");
347 return 0;
350 int nick_len = strlen (str+3);
352 if (!nick_len)
353 return 0;
355 if (!proto_register (str+3, str+4+nick_len))
356 return 0;
358 printf ("! Registration request for ID (%s) was send\n", str+3);
360 return 1;
361 } else if (str[1] == 'l') { /* login to your account */
362 /*if (cfg->autolog) {
363 printf ("ERROR -> You are already logged in\n");
365 return 0;
368 int l = strnchrr (str+3, len-3);
370 if (!l) {
371 printf ("WARNING -> wrong register syntax\nSYNTAX: /l <nick> <password>\n");
373 return 0;
376 int nick_len = strlen (str+3);
378 if (!nick_len)
379 return 0;
381 if (!proto_login (str+3, str+4+nick_len))
382 return 0;
384 cfg->autolog = 1;
386 return proto_list ();;
387 } else if (str[1] == 'h') { /* show help */
388 printf ("Command list:\n/m <nick@server> <message>\twrite message to your buddy\n"
389 "/c\t\t\t\tdisplay contact list\n"
390 "/t <nick@server>\t\tselect user for conversation window\n"
391 "/s <status>\t\t\tchange your status\n"
392 "/a <nick@server>\t\tadd new user to contact list\n"
393 "/d <nick@server>\t\tdelete user from contact list\n"
394 "/r <nick> <password>\t\tregister new account on server\n"
395 "/l <nick> <password>\t\tlogin to your account\n"
396 "/h\t\t\t\tshow this help\n");
398 return 1;
401 printf ("WARNING -> command '%s' is invalid, try /h for help\n", str);
403 return 1;
406 int user_error (char *str, unsigned len)
408 printf ("ERROR -> %s\n", str);
410 return 1;
413 int user_info (char *str, unsigned len)
415 if (!strncmp (str, "msg send", len))
416 return 0;
418 printf ("INFO -> %s\n", str);
420 return 1;
423 int init_user ()
425 user_list.next = &user_list;
426 user_list.prev = &user_list;
428 talkto = 0;
430 return 1;