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