libpurple/protocols/gg/gg.c

changeset 27388
e72ccf802d25
parent 27371
6f8830d73092
child 27626
62fbae50be08
equal deleted inserted replaced
27387:83a8de0ad8a4 27388:e72ccf802d25
5 * 5 *
6 * Copyright (C) 2005 Bartosz Oler <bartosz@bzimage.us> 6 * Copyright (C) 2005 Bartosz Oler <bartosz@bzimage.us>
7 * 7 *
8 * Some parts of the code are adapted or taken from the previous implementation 8 * Some parts of the code are adapted or taken from the previous implementation
9 * of this plugin written by Arkadiusz Miskiewicz <misiek@pld.org.pl> 9 * of this plugin written by Arkadiusz Miskiewicz <misiek@pld.org.pl>
10 * Some parts Copyright (C) 2009 Krzysztof Klinikowski <grommasher@gmail.com>
10 * 11 *
11 * Thanks to Google's Summer of Code Program. 12 * Thanks to Google's Summer of Code Program.
12 * 13 *
13 * This program is free software; you can redistribute it and/or modify 14 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by 15 * it under the terms of the GNU General Public License as published by
34 #include "blist.h" 35 #include "blist.h"
35 #include "accountopt.h" 36 #include "accountopt.h"
36 #include "debug.h" 37 #include "debug.h"
37 #include "util.h" 38 #include "util.h"
38 #include "request.h" 39 #include "request.h"
40 #include "xmlnode.h"
39 41
40 #include <libgadu.h> 42 #include <libgadu.h>
41 43
42 #include "gg.h" 44 #include "gg.h"
43 #include "confer.h" 45 #include "confer.h"
856 858
857 /* Prototypes */ 859 /* Prototypes */
858 static void ggp_set_status(PurpleAccount *account, PurpleStatus *status); 860 static void ggp_set_status(PurpleAccount *account, PurpleStatus *status);
859 static int ggp_to_gg_status(PurpleStatus *status, char **msg); 861 static int ggp_to_gg_status(PurpleStatus *status, char **msg);
860 862
863 struct gg_fetch_avatar_data
864 {
865 PurpleConnection *gc;
866 gchar *uin;
867 gchar *avatar_url;
868 };
869
870
871 static void gg_fetch_avatar_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,
872 const gchar *data, size_t len, const gchar *error_message) {
873 struct gg_fetch_avatar_data *d = user_data;
874 PurpleAccount *account;
875 PurpleBuddy *buddy;
876 gpointer buddy_icon_data;
877
878 /* FIXME: This shouldn't be necessary */
879 if (!PURPLE_CONNECTION_IS_VALID(d->gc)) {
880 g_free(d->uin);
881 g_free(d->avatar_url);
882 g_free(d);
883 g_return_if_reached();
884 }
885
886 account = purple_connection_get_account(d->gc);
887 buddy = purple_find_buddy(account, d->uin);
888
889 if (buddy == NULL)
890 goto out;
891
892 buddy_icon_data = g_memdup(data, len);
893
894 purple_buddy_icons_set_for_user(account, purple_buddy_get_name(buddy),
895 buddy_icon_data, len, d->avatar_url);
896 purple_debug_info("gg", "UIN: %s should have avatar now\n", d->uin);
897
898 out:
899 g_free(d->uin);
900 g_free(d->avatar_url);
901 g_free(d);
902 }
903
904 static void gg_get_avatar_url_cb(PurpleUtilFetchUrlData *url_data, gpointer user_data,
905 const gchar *url_text, size_t len, const gchar *error_message) {
906 struct gg_fetch_avatar_data *data;
907 PurpleConnection *gc = user_data;
908 PurpleAccount *account;
909 PurpleBuddy *buddy;
910 const char *uin;
911 const char *is_blank;
912 const char *checksum;
913
914 gchar *bigavatar = NULL;
915 xmlnode *xml = NULL;
916 xmlnode *xmlnode_users;
917 xmlnode *xmlnode_user;
918 xmlnode *xmlnode_avatars;
919 xmlnode *xmlnode_avatar;
920 xmlnode *xmlnode_bigavatar;
921
922 g_return_if_fail(PURPLE_CONNECTION_IS_VALID(gc));
923 account = purple_connection_get_account(gc);
924
925 if (error_message != NULL)
926 purple_debug_error("gg", "gg_get_avatars_cb error: %s\n", error_message);
927 else if (len > 0 && url_text && *url_text) {
928 xml = xmlnode_from_str(url_text, -1);
929 if (xml == NULL)
930 goto out;
931
932 xmlnode_users = xmlnode_get_child(xml, "users");
933 if (xmlnode_users == NULL)
934 goto out;
935
936 xmlnode_user = xmlnode_get_child(xmlnode_users, "user");
937 if (xmlnode_user == NULL)
938 goto out;
939
940 uin = xmlnode_get_attrib(xmlnode_user, "uin");
941
942 xmlnode_avatars = xmlnode_get_child(xmlnode_user, "avatars");
943 if (xmlnode_avatars == NULL)
944 goto out;
945
946 xmlnode_avatar = xmlnode_get_child(xmlnode_avatars, "avatar");
947 if (xmlnode_avatar == NULL)
948 goto out;
949
950 xmlnode_bigavatar = xmlnode_get_child(xmlnode_avatar, "bigAvatar");
951 if (xmlnode_bigavatar == NULL)
952 goto out;
953
954 is_blank = xmlnode_get_attrib(xmlnode_avatar, "blank");
955 bigavatar = xmlnode_get_data(xmlnode_bigavatar);
956
957 purple_debug_info("gg", "gg_get_avatar_url_cb: UIN %s, IS_BLANK %s, "
958 "URL %s\n",
959 uin ? uin : "(null)", is_blank ? is_blank : "(null)",
960 bigavatar ? bigavatar : "(null)");
961
962 if (uin != NULL && bigavatar != NULL) {
963 buddy = purple_find_buddy(account, uin);
964 if (buddy == NULL)
965 goto out;
966
967 checksum = purple_buddy_icons_get_checksum_for_user(buddy);
968
969 if (purple_strequal(is_blank, "1")) {
970 purple_buddy_icons_set_for_user(account,
971 purple_buddy_get_name(buddy), NULL, 0, NULL);
972 } else if (!purple_strequal(checksum, bigavatar)) {
973 data = g_new0(struct gg_fetch_avatar_data, 1);
974 data->gc = gc;
975 data->uin = g_strdup(uin);
976 data->avatar_url = g_strdup(bigavatar);
977
978 url_data = purple_util_fetch_url_request_len_with_account(account,
979 bigavatar, TRUE, "Mozilla/4.0 (compatible; MSIE 5.0)",
980 FALSE, NULL, FALSE, -1, gg_fetch_avatar_cb, data);
981 }
982 }
983 }
984
985 out:
986 if (xml)
987 xmlnode_free(xml);
988 g_free(bigavatar);
989 }
861 990
862 /** 991 /**
863 * Handle change of the status of the buddy. 992 * Handle change of the status of the buddy.
864 * 993 *
865 * @param gc PurpleConnection 994 * @param gc PurpleConnection
871 int status, const char *descr) 1000 int status, const char *descr)
872 { 1001 {
873 gchar *from; 1002 gchar *from;
874 const char *st; 1003 const char *st;
875 gchar *msg; 1004 gchar *msg;
876 1005 gchar *avatarurl;
877 from = g_strdup_printf("%ld", (unsigned long int)uin); 1006 PurpleUtilFetchUrlData *url_data;
1007
1008 from = g_strdup_printf("%u", uin);
1009 avatarurl = g_strdup_printf("http://api.gadu-gadu.pl/avatars/%s/0.xml", from);
1010
1011 url_data = purple_util_fetch_url_request_len_with_account(
1012 purple_connection_get_account(gc), avatarurl, TRUE,
1013 "Mozilla/4.0 (compatible; MSIE 5.5)", FALSE, NULL, FALSE, -1,
1014 gg_get_avatar_url_cb, gc);
1015
1016 g_free(avatarurl);
1017
878 switch (status) { 1018 switch (status) {
879 case GG_STATUS_NOT_AVAIL: 1019 case GG_STATUS_NOT_AVAIL:
880 case GG_STATUS_NOT_AVAIL_DESCR: 1020 case GG_STATUS_NOT_AVAIL_DESCR:
881 st = "offline"; 1021 st = "offline";
882 break; 1022 break;

mercurial