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 */
|
15138
|
45 #define QQ_UPDATE_LEVELS_INTERVAL 600 /* in sec */
|
14192
|
46
|
|
47 /* send keep-alive packet to QQ server (it is a heart-beat) */
|
|
48 void qq_send_packet_keep_alive(GaimConnection *gc)
|
|
49 {
|
|
50 qq_data *qd;
|
|
51 guint8 *raw_data, *cursor;
|
|
52
|
|
53 qd = (qq_data *) gc->proto_data;
|
|
54 raw_data = g_newa(guint8, 4);
|
|
55 cursor = raw_data;
|
|
56
|
|
57 /* In fact, we can send whatever we like to server
|
|
58 * with this command, server return the same result including
|
|
59 * the amount of online QQ users, my ip and port */
|
|
60 create_packet_dw(raw_data, &cursor, qd->uid);
|
|
61
|
|
62 qq_send_cmd(gc, QQ_CMD_KEEP_ALIVE, TRUE, 0, TRUE, raw_data, 4);
|
|
63 }
|
|
64
|
|
65 /* parse the return of keep-alive packet, it includes some system information */
|
15138
|
66 void qq_process_keep_alive_reply(guint8 *buf, gint buf_len, GaimConnection *gc)
|
|
67 {
|
14192
|
68 qq_data *qd;
|
|
69 gint len;
|
|
70 gchar **segments;
|
|
71 guint8 *data;
|
|
72
|
|
73 g_return_if_fail(buf != NULL && buf_len != 0);
|
|
74
|
|
75 qd = (qq_data *) gc->proto_data;
|
|
76 len = buf_len;
|
|
77 data = g_newa(guint8, len);
|
|
78
|
|
79 if (qq_crypt(DECRYPT, buf, buf_len, qd->session_key, data, &len)) {
|
|
80 /* the last one is 60, don't know what it is */
|
|
81 if (NULL == (segments = split_data(data, len, "\x1f", 6)))
|
|
82 return;
|
|
83 /* segments[0] and segment[1] are all 0x30 ("0") */
|
|
84 qd->all_online = strtol(segments[2], NULL, 10);
|
|
85 if(0 == qd->all_online)
|
|
86 gaim_connection_error(gc, _("Keep alive error, seems connection lost!"));
|
|
87 g_free(qd->my_ip);
|
|
88 qd->my_ip = g_strdup(segments[3]);
|
|
89 qd->my_port = strtol(segments[4], NULL, 10);
|
|
90 g_strfreev(segments);
|
|
91 } else
|
|
92 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Error decrypt keep alive reply\n");
|
|
93
|
|
94 /* we refresh buddies's online status periodically */
|
15138
|
95 /* qd->last_get_online is updated when setting get_buddies_online packet */
|
14192
|
96 if ((time(NULL) - qd->last_get_online) >= QQ_UPDATE_ONLINE_INTERVAL)
|
|
97 qq_send_packet_get_buddies_online(gc, QQ_FRIENDS_ONLINE_POSITION_START);
|
15138
|
98 if ((time(NULL) - qd->last_get_levels) >= QQ_UPDATE_LEVELS_INTERVAL)
|
|
99 qq_send_packet_get_buddies_levels(gc);
|
14192
|
100 }
|
|
101
|
|
102 /* refresh all buddies online/offline,
|
|
103 * after receiving reply for get_buddies_online packet */
|
|
104 void qq_refresh_all_buddy_status(GaimConnection *gc)
|
|
105 {
|
|
106 time_t now;
|
|
107 GList *list;
|
|
108 qq_data *qd;
|
|
109 qq_buddy *q_bud;
|
|
110
|
|
111 qd = (qq_data *) (gc->proto_data);
|
|
112 now = time(NULL);
|
|
113 list = qd->buddies;
|
|
114
|
|
115 while (list != NULL) {
|
|
116 q_bud = (qq_buddy *) list->data;
|
|
117 if (q_bud != NULL && now > q_bud->last_refresh + QQ_UPDATE_ONLINE_INTERVAL
|
|
118 && q_bud->status != QQ_BUDDY_ONLINE_INVISIBLE) {
|
|
119 q_bud->status = QQ_BUDDY_ONLINE_OFFLINE;
|
|
120 qq_update_buddy_contact(gc, q_bud);
|
|
121 }
|
|
122 list = list->next;
|
|
123 }
|
|
124 }
|
|
125
|
15138
|
126 /*TODO: maybe this should be qq_update_buddy_status() ?*/
|
14192
|
127 void qq_update_buddy_contact(GaimConnection *gc, qq_buddy *q_bud)
|
|
128 {
|
|
129 gchar *name;
|
|
130 GaimBuddy *bud;
|
|
131 gchar *status_id;
|
|
132
|
14629
|
133 g_return_if_fail(q_bud != NULL);
|
14192
|
134
|
|
135 name = uid_to_gaim_name(q_bud->uid);
|
|
136 bud = gaim_find_buddy(gc->account, name);
|
|
137 g_return_if_fail(bud != NULL);
|
|
138
|
|
139 if (bud != NULL) {
|
|
140 gaim_blist_server_alias_buddy(bud, q_bud->nickname); /* server */
|
|
141 q_bud->last_refresh = time(NULL);
|
|
142
|
|
143 /* gaim supports signon and idle time
|
|
144 * but it is not much use for QQ, I do not use them */
|
|
145 /* serv_got_update(gc, name, online, 0, q_bud->signon, q_bud->idle, bud->uc); */
|
|
146 status_id = "available";
|
|
147 switch(q_bud->status) {
|
|
148 case QQ_BUDDY_OFFLINE:
|
|
149 status_id = "offline";
|
|
150 break;
|
|
151 case QQ_BUDDY_ONLINE_NORMAL:
|
|
152 status_id = "available";
|
|
153 break;
|
|
154 case QQ_BUDDY_ONLINE_OFFLINE:
|
|
155 status_id = "offline";
|
|
156 break;
|
|
157 case QQ_BUDDY_ONLINE_AWAY:
|
|
158 status_id = "away";
|
|
159 break;
|
|
160 case QQ_BUDDY_ONLINE_INVISIBLE:
|
|
161 status_id = "invisible";
|
|
162 break;
|
|
163 default:
|
|
164 status_id = "invisible";
|
14318
|
165 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "unknown status: %x\n", q_bud->status);
|
14192
|
166 break;
|
|
167 }
|
|
168 gaim_debug(GAIM_DEBUG_INFO, "QQ", "set buddy %d to %s\n", q_bud->uid, status_id);
|
|
169 gaim_prpl_got_user_status(gc->account, name, status_id, NULL);
|
|
170 } else {
|
14318
|
171 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "unknown buddy: %d\n", q_bud->uid);
|
14192
|
172 }
|
|
173
|
|
174 gaim_debug(GAIM_DEBUG_INFO, "QQ", "qq_update_buddy_contact, client=%04x\n", q_bud->client_version);
|
|
175 g_free(name);
|
|
176 }
|