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 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
|
|
50
|
|
51 qd = (qq_data *) gc->proto_data;
|
|
52 raw_data = g_newa(guint8, 4);
|
|
53 cursor = raw_data;
|
|
54
|
|
55 /* In fact, we can send whatever we like to server
|
|
56 * with this command, server return the same result including
|
|
57 * the amount of online QQ users, my ip and port */
|
|
58 create_packet_dw(raw_data, &cursor, qd->uid);
|
|
59
|
|
60 qq_send_cmd(gc, QQ_CMD_KEEP_ALIVE, TRUE, 0, TRUE, raw_data, 4);
|
|
61 }
|
|
62
|
|
63 /* parse the return of keep-alive packet, it includes some system information */
|
|
64 void qq_process_keep_alive_reply(guint8 *buf, gint buf_len, GaimConnection *gc) {
|
|
65 qq_data *qd;
|
|
66 gint len;
|
|
67 gchar **segments;
|
|
68 guint8 *data;
|
|
69
|
|
70 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
|
|
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 g_return_if_fail(gc != NULL && gc->proto_data != NULL);
|
|
108
|
|
109 qd = (qq_data *) (gc->proto_data);
|
|
110 now = time(NULL);
|
|
111 list = qd->buddies;
|
|
112 g_return_if_fail(qd != NULL);
|
|
113
|
|
114 while (list != NULL) {
|
|
115 q_bud = (qq_buddy *) list->data;
|
|
116 if (q_bud != NULL && now > q_bud->last_refresh + QQ_UPDATE_ONLINE_INTERVAL
|
|
117 && q_bud->status != QQ_BUDDY_ONLINE_INVISIBLE) {
|
|
118 q_bud->status = QQ_BUDDY_ONLINE_OFFLINE;
|
|
119 qq_update_buddy_contact(gc, q_bud);
|
|
120 }
|
|
121 list = list->next;
|
|
122 }
|
|
123 }
|
|
124
|
|
125 void qq_update_buddy_contact(GaimConnection *gc, qq_buddy *q_bud)
|
|
126 {
|
|
127 gchar *name;
|
|
128 GaimBuddy *bud;
|
|
129 gchar *status_id;
|
|
130
|
|
131 g_return_if_fail(gc != NULL && q_bud != NULL);
|
|
132
|
|
133 name = uid_to_gaim_name(q_bud->uid);
|
|
134 bud = gaim_find_buddy(gc->account, name);
|
|
135 g_return_if_fail(bud != NULL);
|
|
136
|
|
137 if (bud != NULL) {
|
|
138 gaim_blist_server_alias_buddy(bud, q_bud->nickname); /* server */
|
|
139 q_bud->last_refresh = time(NULL);
|
|
140
|
|
141 /* gaim supports signon and idle time
|
|
142 * but it is not much use for QQ, I do not use them */
|
|
143 /* serv_got_update(gc, name, online, 0, q_bud->signon, q_bud->idle, bud->uc); */
|
|
144 status_id = "available";
|
|
145 switch(q_bud->status) {
|
|
146 case QQ_BUDDY_OFFLINE:
|
|
147 status_id = "offline";
|
|
148 break;
|
|
149 case QQ_BUDDY_ONLINE_NORMAL:
|
|
150 status_id = "available";
|
|
151 break;
|
|
152 case QQ_BUDDY_ONLINE_OFFLINE:
|
|
153 status_id = "offline";
|
|
154 break;
|
|
155 case QQ_BUDDY_ONLINE_AWAY:
|
|
156 status_id = "away";
|
|
157 break;
|
|
158 case QQ_BUDDY_ONLINE_INVISIBLE:
|
|
159 status_id = "invisible";
|
|
160 break;
|
|
161 default:
|
|
162 status_id = "invisible";
|
14318
|
163 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "unknown status: %x\n", q_bud->status);
|
14192
|
164 break;
|
|
165 }
|
|
166 gaim_debug(GAIM_DEBUG_INFO, "QQ", "set buddy %d to %s\n", q_bud->uid, status_id);
|
|
167 gaim_prpl_got_user_status(gc->account, name, status_id, NULL);
|
|
168 } else {
|
14318
|
169 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "unknown buddy: %d\n", q_bud->uid);
|
14192
|
170 }
|
|
171
|
|
172 gaim_debug(GAIM_DEBUG_INFO, "QQ", "qq_update_buddy_contact, client=%04x\n", q_bud->client_version);
|
|
173 g_free(name);
|
|
174 }
|