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/>.
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
));
40 user
->id
= strdup (id
);
43 user
->next
= &user_list
;
44 user
->prev
= user_list
.prev
;
45 user
->prev
->next
= user
;
46 user
->next
->prev
= user
;
51 static int strnchrn (char *str
, unsigned len
)
68 int user_list_get (char *data
, unsigned len
)
73 unsigned i
= strnchrn (data
+l
, len
-l
);
78 if (!strcmp (data
+l
, "THEY:"))
81 printf ("user_list_add: %s\n", data
+l
);
82 if (!user_list_add (data
+l
))
91 user_t
*user_list_find (char *target
, unsigned len
)
95 for (c
= user_list
.next
; c
!= &user_list
; c
= c
->next
) {
96 if (!strncmp (c
->id
, target
, len
))
103 int user_list_del (char *id
)
105 user_t
*user
= user_list_find (id
, strlen (id
));
110 user
->next
->prev
= user
->prev
;
111 user
->prev
->next
= user
->next
;
121 int user_list_show ()
123 printf ("Contact list:\n");
127 for (c
= user_list
.next
; c
!= &user_list
; c
= c
->next
) {
128 if (c
== user_list
.next
)
129 printf ("%s", c
->id
);
131 printf (", %s", c
->id
);
139 int user_cmd_talkto (char *target
, unsigned len
)
141 user_t
*c
= user_list_find (target
, len
);
151 static int strnchrr (char *str
, unsigned len
)
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
);
177 int nick_len
= strlen (str
);
182 user_t
*user
= user_list_find (str
, nick_len
);
185 printf ("WARNING -> Incoming status (%s) ID (%s) not agree with contact list items\n", str
+nick_len
+1, str
);
190 if (!strncmp (user
->id
, cfg
->nick
, strlen (cfg
->nick
)))
191 printf ("* Your status is changed to: %s\n", str
+nick_len
+1);
193 printf ("* User %s change status to: %s\n", user
->id
, str
+nick_len
+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
);
206 int nick_len
= strlen (str
);
212 str
[nick_len
] = '\0';
215 user_t
*user
= user_list_find (str
, nick_len
);
218 printf ("WARNING -> Unauthorized message from ID (%s): %s\n", str
, str
+nick_len
+2);
226 printf ("Autoswitched to user %s\n> %s: %s\n", user
->id
, user
->id
, str
+nick_len
+2);
229 printf ("> %s\n", str
+nick_len
+2);
231 printf ("> %s: %s\n", user
->id
, str
+nick_len
+2);
240 printf ("ERROR -> Please login first, use /h for help\n");
248 int user_msgparse (char *str
, unsigned len
)
253 return proto_msg (talkto
->id
, str
, len
);
255 printf ("WARNING -> No user is selected for conversation\nPlease type: /t <user@server>\n");
266 if (str
[1] == 'm') { /* message */
267 if (!user_islogged ())
270 int l
= strnchrr (str
+3, len
-3);
273 printf ("WARNING -> wrong message syntax\nSYNTAX: /m <nick@server> <message>\n");
278 int nick_len
= strlen (str
+3);
283 user_t
*user
= user_list_find (str
+3, nick_len
);
286 printf ("WARNING -> this ID not agree with contact list items\n");
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 ())
296 return user_list_show ();
297 } else if (str
[1] == 't') { /* select user to talk */
298 if (!user_islogged ())
301 if (user_cmd_talkto (str
+3, len
-3))
302 printf ("Selected user: %s\n", str
+3);
304 printf ("WARNING -> this ID (%s) not agree with contact list items\n", str
+3);
307 } else if (str
[1] == 's') { /* change your status */
308 if (!user_islogged ())
311 if (proto_status (str
+3, len
-3))
312 printf ("* Status is: %s\n", str
+3);
315 } else if (str
[1] == 'a') { /* add new user to contact list */
316 if (!user_islogged ())
319 if (!proto_add (str
+3))
322 if (!user_list_add (str
+3))
325 printf ("+ ID %s added to contact list\n", str
+3);
328 } else if (str
[1] == 'd') { /* delete user from contact list */
329 if (!user_islogged ())
332 if (!user_list_del (str
+3))
335 if (!proto_del (str
+3))
338 printf ("- ID %s deleted from contact list\n", str
+3);
341 } else if (str
[1] == 'r') { /* register new account on server */
342 int l
= strnchrr (str
+3, len
-3);
345 printf ("WARNING -> wrong register syntax\nSYNTAX: /r <nick> <password>\n");
350 int nick_len
= strlen (str
+3);
355 if (!proto_register (str
+3, str
+4+nick_len
))
358 printf ("! Registration request for ID (%s) was send\n", str
+3);
361 } else if (str
[1] == 'l') { /* login to your account */
362 /*if (cfg->autolog) {
363 printf ("ERROR -> You are already logged in\n");
368 int l
= strnchrr (str
+3, len
-3);
371 printf ("WARNING -> wrong register syntax\nSYNTAX: /l <nick> <password>\n");
376 int nick_len
= strlen (str
+3);
381 if (!proto_login (str
+3, str
+4+nick_len
))
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");
401 printf ("WARNING -> command '%s' is invalid, try /h for help\n", str
);
406 int user_error (char *str
, unsigned len
)
408 printf ("ERROR -> %s\n", str
);
413 int user_info (char *str
, unsigned len
)
415 if (!strncmp (str
, "msg send", len
))
418 printf ("INFO -> %s\n", str
);
425 user_list
.next
= &user_list
;
426 user_list
.prev
= &user_list
;