首页 | 数据结构 | 文件列表 | 数据字段 | 全局定义

keep_alive.c

浏览该文件的文档。
00001 
00029 // START OF FILE
00030 /*****************************************************************************/
00031 #include "debug.h"              // gaim_debug
00032 #include "server.h"             // serv_got_update
00033 
00034 #include "utils.h"              // uid_to_gaim_name
00035 #include "packet_parse.h"       // create_packet
00036 #include "buddy_list.h"         // qq_send_packet_get_buddies_online
00037 #include "buddy_status.h"       // QQ_BUDDY_ONLINE_NORMAL
00038 #include "crypt.h"              // qq_crypt
00039 #include "header_info.h"        // cmd alias
00040 #include "keep_alive.h"
00041 #include "send_core.h"          // qq_send_cmd
00042 
00043 #define QQ_UPDATE_ONLINE_INTERVAL   300 // in sec
00044 
00045 /*****************************************************************************/
00046 // send keep-alive packet to QQ server (it is a heart-beat)
00047 void qq_send_packet_keep_alive(GaimConnection * gc)
00048 {
00049         qq_data *qd;
00050         guint8 *raw_data, *cursor;
00051 
00052         g_return_if_fail(gc != NULL && gc->proto_data != NULL);
00053 
00054         qd = (qq_data *) gc->proto_data;
00055         raw_data = g_newa(guint8, 4);
00056         cursor = raw_data;
00057 
00058         // In fact, we can send whatever we like to server
00059         // with this command, server return the same result including
00060         // the amount of online QQ users, my ip and port
00061         create_packet_dw(raw_data, &cursor, qd->uid);
00062 
00063         qq_send_cmd(gc, QQ_CMD_KEEP_ALIVE, TRUE, 0, TRUE, raw_data, 4);
00064 
00065 }                               // qq_send_packet_keep_alive
00066 
00067 /*****************************************************************************/
00068 // parse the return of keep-alive packet, it includes some system information
00069 void qq_process_keep_alive_reply(guint8 * buf, gint buf_len, GaimConnection * gc) {
00070         qq_data *qd;
00071         gint len;
00072         gchar *data, **segments;        // the returns are gchar, no need guint8
00073 
00074         g_return_if_fail(gc != NULL && gc->proto_data != NULL);
00075         g_return_if_fail(buf != NULL && buf_len != 0);
00076 
00077         qd = (qq_data *) gc->proto_data;
00078         len = buf_len;
00079         data = g_newa(guint8, len);
00080 
00081         if (qq_crypt(DECRYPT, buf, buf_len, qd->session_key, data, &len)) {
00082                 if (NULL == (segments = split_data(data, len, "\x1f", 6)))
00083                         return;
00084                 // segments[0] and segment[1] are all 0x30 ("0")
00085                 qd->all_online = strtol(segments[2], NULL, 10);
00086                 g_free(qd->my_ip);
00087                 qd->my_ip = g_strdup(segments[3]);
00088                 qd->my_port = strtol(segments[4], NULL, 10);
00089                 g_strfreev(segments);
00090         } else
00091                 gaim_debug(GAIM_DEBUG_ERROR, "QQ", "Error decrypt keep alive reply\n");
00092 
00093         // we refresh buddies's online status periodically 
00094         // qd->lasat_get_online is updated when setting get_buddies_online packet
00095         if ((time(NULL) - qd->last_get_online) >= QQ_UPDATE_ONLINE_INTERVAL)
00096                 qq_send_packet_get_buddies_online(gc, QQ_FRIENDS_ONLINE_POSITION_START);
00097 
00098 }                               // qq_process_keep_alive_reply
00099 
00100 /*****************************************************************************/
00101 // refresh all buddies online/offline,
00102 // after receiving reply for get_buddies_online packet 
00103 void qq_refresh_all_buddy_status(GaimConnection * gc)
00104 {
00105         time_t now;
00106         GList *list;
00107         qq_data *qd;
00108         qq_buddy *q_bud;
00109 
00110         g_return_if_fail(gc != NULL && gc->proto_data != NULL);
00111 
00112         qd = (qq_data *) (gc->proto_data);
00113         now = time(NULL);
00114         list = qd->buddies;
00115         g_return_if_fail(qd != NULL);
00116 
00117         while (list != NULL) {
00118                 q_bud = (qq_buddy *) list->data;
00119                 if (q_bud != NULL && now > q_bud->last_refresh + QQ_UPDATE_ONLINE_INTERVAL) {
00120                         q_bud->status = QQ_BUDDY_ONLINE_OFFLINE;
00121                         qq_update_buddy_contact(gc, q_bud);
00122                 }
00123                 list = list->next;
00124         }                       // while
00125 }                               // qq_refresh_all_buddy_status
00126 
00127 /*****************************************************************************/
00128 void qq_update_buddy_contact(GaimConnection * gc, qq_buddy * q_bud)
00129 {
00130         gchar *name;
00131         gboolean online;
00132         GaimBuddy *bud;
00133         g_return_if_fail(gc != NULL && q_bud != NULL);
00134 
00135         online = is_online(q_bud->status);
00136         name = uid_to_gaim_name(q_bud->uid);
00137         bud = gaim_find_buddy(gc->account, name);
00138         if (bud == NULL) return;
00139 
00140         if (bud != NULL) {
00141                 gaim_blist_alias_buddy(bud, q_bud->nickname);
00142                 q_bud->last_refresh = time(NULL);
00143 
00144                 // gaim support signon and idle time
00145                 // but it is not much useful for QQ, I do not use them it
00146                 serv_got_update(gc, name, online, 0, q_bud->signon, q_bud->idle, bud->uc);
00147         }                       // if bud
00148 
00149         g_free(name);
00150 }                               // qq_update_buddy_contact
00151 
00152 /*****************************************************************************/
00153 // END OF FILE

Generated at Mon May 8 15:41:24 2006 for OpenQ by  doxygen 1.4.4