comparison libpurple/protocols/qq/keep_alive.c @ 15374:5fe8042783c1

Rename gtk/ and libgaim/ to pidgin/ and libpurple/
author Sean Egan <seanegan@gmail.com>
date Sat, 20 Jan 2007 02:32:10 +0000
parents
children b258250b72c9
comparison
equal deleted inserted replaced
15373:f79e0f4df793 15374:5fe8042783c1
1 /**
2 * @file keep_alive.c
3 *
4 * gaim
5 *
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.
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
34 #include "buddy_info.h"
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 {
67 qq_data *qd;
68 gint len;
69 gchar **segments;
70 guint8 *data;
71
72 g_return_if_fail(buf != NULL && buf_len != 0);
73
74 qd = (qq_data *) gc->proto_data;
75 len = buf_len;
76 data = g_newa(guint8, len);
77
78 if (qq_crypt(DECRYPT, buf, buf_len, qd->session_key, data, &len)) {
79 /* the last one is 60, don't know what it is */
80 if (NULL == (segments = split_data(data, len, "\x1f", 6)))
81 return;
82 /* segments[0] and segment[1] are all 0x30 ("0") */
83 qd->all_online = strtol(segments[2], NULL, 10);
84 if(0 == qd->all_online)
85 gaim_connection_error(gc, _("Keep alive error, seems connection lost!"));
86 g_free(qd->my_ip);
87 qd->my_ip = g_strdup(segments[3]);
88 qd->my_port = strtol(segments[4], NULL, 10);
89 g_strfreev(segments);
90 } else
91 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Error decrypt keep alive reply\n");
92
93 /* we refresh buddies's online status periodically */
94 /* qd->last_get_online is updated when setting get_buddies_online packet */
95 if ((time(NULL) - qd->last_get_online) >= QQ_UPDATE_ONLINE_INTERVAL)
96 qq_send_packet_get_buddies_online(gc, QQ_FRIENDS_ONLINE_POSITION_START);
97 }
98
99 /* refresh all buddies online/offline,
100 * after receiving reply for get_buddies_online packet */
101 void qq_refresh_all_buddy_status(GaimConnection *gc)
102 {
103 time_t now;
104 GList *list;
105 qq_data *qd;
106 qq_buddy *q_bud;
107
108 qd = (qq_data *) (gc->proto_data);
109 now = time(NULL);
110 list = qd->buddies;
111
112 while (list != NULL) {
113 q_bud = (qq_buddy *) list->data;
114 if (q_bud != NULL && now > q_bud->last_refresh + QQ_UPDATE_ONLINE_INTERVAL
115 && q_bud->status != QQ_BUDDY_ONLINE_INVISIBLE) {
116 q_bud->status = QQ_BUDDY_ONLINE_OFFLINE;
117 qq_update_buddy_contact(gc, q_bud);
118 }
119 list = list->next;
120 }
121 }
122
123 /*TODO: maybe this should be qq_update_buddy_status() ?*/
124 void qq_update_buddy_contact(GaimConnection *gc, qq_buddy *q_bud)
125 {
126 gchar *name;
127 GaimBuddy *bud;
128 gchar *status_id;
129
130 g_return_if_fail(q_bud != NULL);
131
132 name = uid_to_gaim_name(q_bud->uid);
133 bud = gaim_find_buddy(gc->account, name);
134 g_return_if_fail(bud != NULL);
135
136 if (bud != NULL) {
137 gaim_blist_server_alias_buddy(bud, q_bud->nickname); /* server */
138 q_bud->last_refresh = time(NULL);
139
140 /* gaim supports signon and idle time
141 * but it is not much use for QQ, I do not use them */
142 /* serv_got_update(gc, name, online, 0, q_bud->signon, q_bud->idle, bud->uc); */
143 status_id = "available";
144 switch(q_bud->status) {
145 case QQ_BUDDY_OFFLINE:
146 status_id = "offline";
147 break;
148 case QQ_BUDDY_ONLINE_NORMAL:
149 status_id = "available";
150 break;
151 case QQ_BUDDY_ONLINE_OFFLINE:
152 status_id = "offline";
153 break;
154 case QQ_BUDDY_ONLINE_AWAY:
155 status_id = "away";
156 break;
157 case QQ_BUDDY_ONLINE_INVISIBLE:
158 status_id = "invisible";
159 break;
160 default:
161 status_id = "invisible";
162 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "unknown status: %x\n", q_bud->status);
163 break;
164 }
165 gaim_debug(GAIM_DEBUG_INFO, "QQ", "set buddy %d to %s\n", q_bud->uid, status_id);
166 gaim_prpl_got_user_status(gc->account, name, status_id, NULL);
167 } else {
168 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "unknown buddy: %d\n", q_bud->uid);
169 }
170
171 gaim_debug(GAIM_DEBUG_INFO, "QQ", "qq_update_buddy_contact, client=%04x\n", q_bud->client_version);
172 g_free(name);
173 }