14192
|
1 /**
|
15025
|
2 * @file keep_alive.c
|
14192
|
3 *
|
15025
|
4 * gaim
|
14192
|
5 *
|
15025
|
6 * Gaim is the legal property of its developers, whose names are too numerous
|
|
7 * to list here. Please refer to the COPYRIGHT file distributed with this
|
|
8 * source distribution.
|
14192
|
9 *
|
|
10 * This program is free software; you can redistribute it and/or modify
|
|
11 * it under the terms of the GNU General Public License as published by
|
|
12 * the Free Software Foundation; either version 2 of the License, or
|
|
13 * (at your option) any later version.
|
|
14 *
|
|
15 * This program is distributed in the hope that it will be useful,
|
|
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
18 * GNU General Public License for more details.
|
|
19 *
|
|
20 * You should have received a copy of the GNU General Public License
|
|
21 * along with this program; if not, write to the Free Software
|
|
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
23 *
|
|
24 *
|
|
25 * OICQ encryption algorithm
|
|
26 * Convert from ASM code provided by PerlOICQ
|
|
27 *
|
|
28 * Puzzlebird, Nov-Dec 2002
|
|
29 */
|
|
30
|
|
31 #include "debug.h"
|
|
32 #include "server.h"
|
|
33
|
15071
|
34 #include "buddy_info.h"
|
14192
|
35 #include "buddy_list.h"
|
|
36 #include "buddy_status.h"
|
|
37 #include "crypt.h"
|
|
38 #include "header_info.h"
|
|
39 #include "keep_alive.h"
|
|
40 #include "packet_parse.h"
|
|
41 #include "send_core.h"
|
|
42 #include "utils.h"
|
|
43
|
|
44 #define QQ_UPDATE_ONLINE_INTERVAL 300 /* in sec */
|
|
45
|
|
46 /* send keep-alive packet to QQ server (it is a heart-beat) */
|
|
47 void qq_send_packet_keep_alive(GaimConnection *gc)
|
|
48 {
|
|
49 qq_data *qd;
|
|
50 guint8 *raw_data, *cursor;
|
|
51
|
|
52 qd = (qq_data *) gc->proto_data;
|
|
53 raw_data = g_newa(guint8, 4);
|
|
54 cursor = raw_data;
|
|
55
|
|
56 /* In fact, we can send whatever we like to server
|
|
57 * with this command, server return the same result including
|
|
58 * the amount of online QQ users, my ip and port */
|
|
59 create_packet_dw(raw_data, &cursor, qd->uid);
|
|
60
|
|
61 qq_send_cmd(gc, QQ_CMD_KEEP_ALIVE, TRUE, 0, TRUE, raw_data, 4);
|
|
62 }
|
|
63
|
|
64 /* parse the return of keep-alive packet, it includes some system information */
|
|
65 void qq_process_keep_alive_reply(guint8 *buf, gint buf_len, GaimConnection *gc) {
|
|
66 qq_data *qd;
|
|
67 gint len;
|
|
68 gchar **segments;
|
|
69 guint8 *data;
|
|
70
|
|
71 g_return_if_fail(buf != NULL && buf_len != 0);
|
|
72
|
|
73 qd = (qq_data *) gc->proto_data;
|
|
74 len = buf_len;
|
|
75 data = g_newa(guint8, len);
|
|
76
|
|
77 if (qq_crypt(DECRYPT, buf, buf_len, qd->session_key, data, &len)) {
|
|
78 /* the last one is 60, don't know what it is */
|
|
79 if (NULL == (segments = split_data(data, len, "\x1f", 6)))
|
|
80 return;
|
|
81 /* segments[0] and segment[1] are all 0x30 ("0") */
|
|
82 qd->all_online = strtol(segments[2], NULL, 10);
|
|
83 if(0 == qd->all_online)
|
|
84 gaim_connection_error(gc, _("Keep alive error, seems connection lost!"));
|
|
85 g_free(qd->my_ip);
|
|
86 qd->my_ip = g_strdup(segments[3]);
|
|
87 qd->my_port = strtol(segments[4], NULL, 10);
|
|
88 g_strfreev(segments);
|
|
89 } else
|
|
90 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Error decrypt keep alive reply\n");
|
|
91
|
|
92 /* we refresh buddies's online status periodically */
|
|
93 /* qd->lasat_get_online is updated when setting get_buddies_online packet */
|
|
94 if ((time(NULL) - qd->last_get_online) >= QQ_UPDATE_ONLINE_INTERVAL)
|
|
95 qq_send_packet_get_buddies_online(gc, QQ_FRIENDS_ONLINE_POSITION_START);
|
|
96 }
|
|
97
|
|
98 /* refresh all buddies online/offline,
|
|
99 * after receiving reply for get_buddies_online packet */
|
|
100 void qq_refresh_all_buddy_status(GaimConnection *gc)
|
|
101 {
|
|
102 time_t now;
|
|
103 GList *list;
|
|
104 qq_data *qd;
|
|
105 qq_buddy *q_bud;
|
|
106
|
|
107 qd = (qq_data *) (gc->proto_data);
|
|
108 now = time(NULL);
|
|
109 list = qd->buddies;
|
|
110
|
|
111 while (list != NULL) {
|
|
112 q_bud = (qq_buddy *) list->data;
|
|
113 if (q_bud != NULL && now > q_bud->last_refresh + QQ_UPDATE_ONLINE_INTERVAL
|
|
114 && q_bud->status != QQ_BUDDY_ONLINE_INVISIBLE) {
|
|
115 q_bud->status = QQ_BUDDY_ONLINE_OFFLINE;
|
|
116 qq_update_buddy_contact(gc, q_bud);
|
|
117 }
|
|
118 list = list->next;
|
|
119 }
|
|
120 }
|
|
121
|
15071
|
122 static void _qq_update_buddy_icon(GaimAccount *account, const gchar *name, gint face)
|
|
123 {
|
|
124 GaimBuddyIcon *icon = gaim_buddy_icons_find(account, name);
|
|
125 gchar *icon_num_str = face_to_icon_str(face);
|
|
126 gchar *icon_path = g_strconcat(QQBUDDYICONDIR, G_DIR_SEPARATOR_S,
|
|
127 QQ_ICON_PREFIX, icon_num_str, QQ_ICON_SUFFIX, NULL);
|
|
128 const gchar *old_path = gaim_buddy_icon_get_path(icon);
|
|
129 if (icon == NULL || old_path == NULL
|
|
130 || g_ascii_strcasecmp(icon_path, old_path) != 0)
|
|
131 qq_set_buddy_icon_for_user(account, name, icon_path);
|
|
132 g_free(icon_num_str);
|
|
133 g_free(icon_path);
|
|
134 }
|
|
135
|
14192
|
136 void qq_update_buddy_contact(GaimConnection *gc, qq_buddy *q_bud)
|
|
137 {
|
|
138 gchar *name;
|
|
139 GaimBuddy *bud;
|
|
140 gchar *status_id;
|
|
141
|
14629
|
142 g_return_if_fail(q_bud != NULL);
|
14192
|
143
|
|
144 name = uid_to_gaim_name(q_bud->uid);
|
|
145 bud = gaim_find_buddy(gc->account, name);
|
|
146 g_return_if_fail(bud != NULL);
|
|
147
|
|
148 if (bud != NULL) {
|
|
149 gaim_blist_server_alias_buddy(bud, q_bud->nickname); /* server */
|
|
150 q_bud->last_refresh = time(NULL);
|
|
151
|
|
152 /* gaim supports signon and idle time
|
|
153 * but it is not much use for QQ, I do not use them */
|
|
154 /* serv_got_update(gc, name, online, 0, q_bud->signon, q_bud->idle, bud->uc); */
|
|
155 status_id = "available";
|
|
156 switch(q_bud->status) {
|
|
157 case QQ_BUDDY_OFFLINE:
|
|
158 status_id = "offline";
|
|
159 break;
|
|
160 case QQ_BUDDY_ONLINE_NORMAL:
|
|
161 status_id = "available";
|
|
162 break;
|
|
163 case QQ_BUDDY_ONLINE_OFFLINE:
|
|
164 status_id = "offline";
|
|
165 break;
|
|
166 case QQ_BUDDY_ONLINE_AWAY:
|
|
167 status_id = "away";
|
|
168 break;
|
|
169 case QQ_BUDDY_ONLINE_INVISIBLE:
|
|
170 status_id = "invisible";
|
|
171 break;
|
|
172 default:
|
|
173 status_id = "invisible";
|
14318
|
174 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "unknown status: %x\n", q_bud->status);
|
14192
|
175 break;
|
|
176 }
|
|
177 gaim_debug(GAIM_DEBUG_INFO, "QQ", "set buddy %d to %s\n", q_bud->uid, status_id);
|
|
178 gaim_prpl_got_user_status(gc->account, name, status_id, NULL);
|
15071
|
179 _qq_update_buddy_icon(gc->account, name, q_bud->face);
|
14192
|
180 } else {
|
14318
|
181 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "unknown buddy: %d\n", q_bud->uid);
|
14192
|
182 }
|
|
183
|
|
184 gaim_debug(GAIM_DEBUG_INFO, "QQ", "qq_update_buddy_contact, client=%04x\n", q_bud->client_version);
|
|
185 g_free(name);
|
|
186 }
|