1 #include "system.h"
2 #include "lj.h"
3 #include "hash.h"
4 #include "net.h"
5 #include "util.h"
6
7 #include <stdlib.h>
8 #include <string.h>
9
10 char *create_getfriends_request(const lj_user * user, const lj_friendinfo * logininfo);
11
12 /*****************************************************************************
13 * This function generates the body of the HTTP request, we make the headers in
14 * lj_friend. This is mostly done so that we can just use strlen() to calculate
15 * the Content Length.
16 */
17
18 int
lj_getfriends(const lj_server * const serverinfo,lj_user * user,lj_friendinfo * friendinfo)19 lj_getfriends(const lj_server * const serverinfo, lj_user *user, lj_friendinfo *friendinfo)
20 {
21 char *friend_text;
22 int i, ret = 0;
23 hashtable *lj_hash = NULL;
24 char *value = NULL;
25
26 lj_setauth(serverinfo, user);
27 /* First we send our request */
28 create_hash(&lj_hash);
29 friend_text = create_getfriends_request(user, friendinfo);
30 lj_send_request(serverinfo, user, friend_text, lj_hash);
31 free(friend_text);
32
33 /* were we a success? */
34 friendinfo->success = 1;
35 get(lj_hash, &value, "success");
36 if (!value || strcmp(value, "OK"))
37 {
38 /* we failed */
39 friendinfo->success = 0;
40 ret = 1;
41 }
42 free(value);
43
44 /* next we look for an errmsg */
45 get(lj_hash, &friendinfo->errmsg, "errmsg");
46
47 /* now we snag number of friends */
48 geti(lj_hash, &friendinfo->count, "friend_count");
49 geti(lj_hash, &friendinfo->focount, "friendof_count");
50
51 friendinfo->friends =
52 lj_malloc((friendinfo->count) * sizeof(lj_friendninfo));
53 for (i = 0; i < friendinfo->count; i++)
54 {
55 lj_friendninfo *info = &friendinfo->friends[i];
56 getp(lj_hash, &info->user, "friend_%d_user", i + 1);
57 getp(lj_hash, &info->name, "friend_%d_name", i + 1);
58 getp(lj_hash, &info->birthday, "friend_%d_birthday", i + 1);
59 getp(lj_hash, &info->type, "friend_%d_type", i + 1);
60 getp(lj_hash, &info->status, "friend_%d_status", i + 1);
61 }
62 friendinfo->friendsof =
63 lj_malloc((friendinfo->focount) * sizeof(lj_friendninfo));
64 for (i = 0; i < friendinfo->focount; i++)
65 {
66 lj_friendninfo *info = &friendinfo->friendsof[i];
67 getp(lj_hash, &info->user, "friendof_%d_user", i + 1);
68 getp(lj_hash, &info->name, "friendof_%d_name", i + 1);
69 }
70
71 lj_debug(1, "success: [%s]\n", friendinfo->success ? "OK" : "FAIL");
72 if (friendinfo->errmsg)
73 lj_debug(1, "errmsg: [%s]\n", friendinfo->errmsg);
74
75 /* Then we close the connection and return our success / failure code */
76 delete_hash(lj_hash);
77 return ret;
78 }
79
80 char *
create_getfriends_request(const lj_user * const user,const lj_friendinfo * const friendinfo)81 create_getfriends_request(const lj_user * const user, const lj_friendinfo * const friendinfo)
82 {
83 char *ret;
84
85 asprintf(&ret,
86 "mode=getfriends&%s"
87 "&includefriendof=1"
88 "&includebdays=1",
89 user->auth);
90
91 (void)friendinfo;
92 return ret;
93 }
94