1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
|
/**
* @file group_internal.c
*
* purple
*
* Purple is the legal property of its developers, whose names are too numerous
* to list here. Please refer to the COPYRIGHT file distributed with this
* source distribution.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "internal.h"
#include "blist.h"
#include "debug.h"
#include "buddy_opt.h"
#include "group_internal.h"
#include "utils.h"
static qq_room_data *room_data_new(guint32 id, guint32 ext_id, const gchar *title)
{
qq_room_data *rmd;
purple_debug_info("QQ", "Created room data: %s, ext id %u, id %u\n",
title == NULL ? "(NULL)" : title,
ext_id, id);
rmd = g_new0(qq_room_data, 1);
rmd->my_role = QQ_ROOM_ROLE_NO;
rmd->id = id;
rmd->ext_id = ext_id;
rmd->type8 = 0x01; /* assume permanent Qun */
rmd->creator_uid = 10000; /* assume by QQ admin */
rmd->category = 0x01;
rmd->auth_type = 0x02; /* assume need auth */
rmd->title_utf8 = g_strdup(title == NULL ? "" : title);
rmd->desc_utf8 = g_strdup("");
rmd->notice_utf8 = g_strdup("");
rmd->members = NULL;
rmd->is_got_buddies = FALSE;
return rmd;
}
/* create a qq_room_data from hashtable */
static qq_room_data *room_data_new_by_hashtable(PurpleConnection *gc, GHashTable *data)
{
qq_room_data *rmd;
guint32 id, ext_id;
gchar *value;
value = g_hash_table_lookup(data, QQ_ROOM_KEY_INTERNAL_ID);
id = value ? strtoul(value, NULL, 10) : 0;
value = g_hash_table_lookup(data, QQ_ROOM_KEY_EXTERNAL_ID);
ext_id = value ? strtoul(value, NULL, 10) : 0;
value = g_hash_table_lookup(data, QQ_ROOM_KEY_TITLE_UTF8);
rmd = room_data_new(id, ext_id, value);
rmd->my_role = QQ_ROOM_ROLE_YES;
return rmd;
}
/* gracefully free all members in a room */
static void room_buddies_free(qq_room_data *rmd)
{
gint i;
GList *list;
qq_buddy_data *bd;
g_return_if_fail(rmd != NULL);
i = 0;
while (NULL != (list = rmd->members)) {
bd = (qq_buddy_data *) list->data;
i++;
rmd->members = g_list_remove(rmd->members, bd);
qq_buddy_data_free(bd);
}
rmd->members = NULL;
}
/* gracefully free the memory for one qq_room_data */
static void room_data_free(qq_room_data *rmd)
{
g_return_if_fail(rmd != NULL);
room_buddies_free(rmd);
g_free(rmd->title_utf8);
g_free(rmd->desc_utf8);
g_free(rmd->notice_utf8);
g_free(rmd);
}
void qq_room_update_chat_info(PurpleChat *chat, qq_room_data *rmd)
{
GHashTable *components;
if (rmd->title_utf8 != NULL && strlen(rmd->title_utf8) > 0) {
purple_blist_alias_chat(chat, rmd->title_utf8);
}
components = purple_chat_get_components(chat);
g_hash_table_replace(components,
g_strdup(QQ_ROOM_KEY_INTERNAL_ID),
g_strdup_printf("%u", rmd->id));
g_hash_table_replace(components,
g_strdup(QQ_ROOM_KEY_EXTERNAL_ID),
g_strdup_printf("%u", rmd->ext_id));
g_hash_table_replace(components,
g_strdup(QQ_ROOM_KEY_TITLE_UTF8), g_strdup(rmd->title_utf8));
}
static PurpleChat *chat_new(PurpleConnection *gc, qq_room_data *rmd)
{
GHashTable *components;
PurpleGroup *g;
PurpleChat *chat;
purple_debug_info("QQ", "Add new chat: id %u, ext id %u, title %s\n",
rmd->id, rmd->ext_id,
rmd->title_utf8 == NULL ? "(NULL)" : rmd->title_utf8);
components = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
g_hash_table_insert(components,
g_strdup(QQ_ROOM_KEY_INTERNAL_ID), g_strdup_printf("%u", rmd->id));
g_hash_table_insert(components, g_strdup(QQ_ROOM_KEY_EXTERNAL_ID),
g_strdup_printf("%u", rmd->ext_id));
g_hash_table_insert(components, g_strdup(QQ_ROOM_KEY_TITLE_UTF8), g_strdup(rmd->title_utf8));
chat = purple_chat_new(purple_connection_get_account(gc), rmd->title_utf8, components);
g = qq_group_find_or_new(PURPLE_GROUP_QQ_QUN);
purple_blist_add_chat(chat, g, NULL);
return chat;
}
PurpleChat *qq_room_find_or_new(PurpleConnection *gc, guint32 id, guint32 ext_id)
{
qq_data *qd;
qq_room_data *rmd;
PurpleChat *chat;
gchar *num_str;
g_return_val_if_fail (gc != NULL && gc->proto_data != NULL, NULL);
qd = (qq_data *) gc->proto_data;
g_return_val_if_fail(id != 0 && ext_id != 0, NULL);
purple_debug_info("QQ", "Find or add new room: id %u, ext id %u\n", id, ext_id);
rmd = qq_room_data_find(gc, id);
if (rmd == NULL) {
rmd = room_data_new(id, ext_id, NULL);
g_return_val_if_fail(rmd != NULL, NULL);
rmd->my_role = QQ_ROOM_ROLE_YES;
qd->groups = g_list_append(qd->groups, rmd);
}
num_str = g_strdup_printf("%u", ext_id);
chat = purple_blist_find_chat(purple_connection_get_account(gc), num_str);
g_free(num_str);
if (chat) {
return chat;
}
return chat_new(gc, rmd);
}
void qq_room_remove(PurpleConnection *gc, guint32 id)
{
qq_data *qd;
PurpleChat *chat;
qq_room_data *rmd;
gchar *num_str;
guint32 ext_id;
g_return_if_fail (gc != NULL && gc->proto_data != NULL);
qd = (qq_data *) gc->proto_data;
purple_debug_info("QQ", "Find and remove room data, id %u\n", id);
rmd = qq_room_data_find(gc, id);
g_return_if_fail (rmd != NULL);
ext_id = rmd->ext_id;
qd->groups = g_list_remove(qd->groups, rmd);
room_data_free(rmd);
purple_debug_info("QQ", "Find and remove chat, ext_id %u\n", ext_id);
num_str = g_strdup_printf("%u", ext_id);
chat = purple_blist_find_chat(purple_connection_get_account(gc), num_str);
g_free(num_str);
g_return_if_fail (chat != NULL);
purple_blist_remove_chat(chat);
}
/* find a qq_buddy_data by uid, called by im.c */
qq_buddy_data *qq_room_buddy_find(qq_room_data *rmd, guint32 uid)
{
GList *list;
qq_buddy_data *bd;
g_return_val_if_fail(rmd != NULL && uid > 0, NULL);
list = rmd->members;
while (list != NULL) {
bd = (qq_buddy_data *) list->data;
if (bd->uid == uid)
return bd;
else
list = list->next;
}
return NULL;
}
/* remove a qq_buddy_data by uid, called by qq_group_opt.c */
void qq_room_buddy_remove(qq_room_data *rmd, guint32 uid)
{
GList *list;
qq_buddy_data *bd;
g_return_if_fail(rmd != NULL && uid > 0);
list = rmd->members;
while (list != NULL) {
bd = (qq_buddy_data *) list->data;
if (bd->uid == uid) {
rmd->members = g_list_remove(rmd->members, bd);
return;
} else {
list = list->next;
}
}
}
qq_buddy_data *qq_room_buddy_find_or_new(PurpleConnection *gc, qq_room_data *rmd, guint32 member_uid)
{
qq_buddy_data *member, *bd;
PurpleBuddy *buddy;
g_return_val_if_fail(rmd != NULL && member_uid > 0, NULL);
member = qq_room_buddy_find(rmd, member_uid);
if (member == NULL) { /* first appear during my session */
member = g_new0(qq_buddy_data, 1);
member->uid = member_uid;
buddy = purple_find_buddy(purple_connection_get_account(gc), uid_to_purple_name(member_uid));
if (buddy != NULL) {
const gchar *alias = NULL;
bd = purple_buddy_get_protocol_data(buddy);
if (bd != NULL && bd->nickname != NULL)
member->nickname = g_strdup(bd->nickname);
else if ((alias = purple_buddy_get_alias(buddy)) != NULL)
member->nickname = g_strdup(alias);
}
rmd->members = g_list_append(rmd->members, member);
}
return member;
}
qq_room_data *qq_room_data_find(PurpleConnection *gc, guint32 room_id)
{
GList *list;
qq_room_data *rmd;
qq_data *qd;
qd = (qq_data *) gc->proto_data;
if (qd->groups == NULL || room_id <= 0)
return 0;
list = qd->groups;
while (list != NULL) {
rmd = (qq_room_data *) list->data;
if (rmd->id == room_id) {
return rmd;
}
list = list->next;
}
return NULL;
}
guint32 qq_room_get_next(PurpleConnection *gc, guint32 room_id)
{
GList *list;
qq_room_data *rmd;
qq_data *qd;
gboolean is_find = FALSE;
qd = (qq_data *) gc->proto_data;
if (qd->groups == NULL) {
return 0;
}
if (room_id <= 0) {
rmd = (qq_room_data *) qd->groups->data;
return rmd->id;
}
list = qd->groups;
while (list != NULL) {
rmd = (qq_room_data *) list->data;
list = list->next;
if (rmd->id == room_id) {
is_find = TRUE;
break;
}
}
g_return_val_if_fail(is_find, 0);
if (list == NULL) return 0; /* be the end */
rmd = (qq_room_data *) list->data;
g_return_val_if_fail(rmd != NULL, 0);
return rmd->id;
}
guint32 qq_room_get_next_conv(PurpleConnection *gc, guint32 room_id)
{
GList *list;
qq_room_data *rmd;
qq_data *qd;
gboolean is_find;
qd = (qq_data *) gc->proto_data;
list = qd->groups;
if (room_id > 0) {
/* search next room */
is_find = FALSE;
while (list != NULL) {
rmd = (qq_room_data *) list->data;
list = list->next;
if (rmd->id == room_id) {
is_find = TRUE;
break;
}
}
g_return_val_if_fail(is_find, 0);
}
while (list != NULL) {
rmd = (qq_room_data *) list->data;
g_return_val_if_fail(rmd != NULL, 0);
if (rmd->my_role == QQ_ROOM_ROLE_YES || rmd->my_role == QQ_ROOM_ROLE_ADMIN) {
if (NULL != purple_find_conversation_with_account(
PURPLE_CONV_TYPE_CHAT,rmd->title_utf8, purple_connection_get_account(gc))) {
/* In convseration*/
return rmd->id;
}
}
list = list->next;
}
return 0;
}
/* this should be called upon signin, even when we did not open group chat window */
void qq_room_data_initial(PurpleConnection *gc)
{
PurpleAccount *account;
PurpleChat *chat;
PurpleGroup *purple_group;
PurpleBlistNode *node;
qq_data *qd;
qq_room_data *rmd;
gint count;
account = purple_connection_get_account(gc);
qd = (qq_data *) gc->proto_data;
purple_debug_info("QQ", "Initial QQ Qun configurations\n");
purple_group = purple_find_group(PURPLE_GROUP_QQ_QUN);
if (purple_group == NULL) {
purple_debug_info("QQ", "We have no QQ Qun\n");
return;
}
count = 0;
for (node = purple_blist_node_get_first_child((PurpleBlistNode *)purple_group);
node != NULL;
node = purple_blist_node_get_sibling_next(node))
{
if ( !PURPLE_BLIST_NODE_IS_CHAT(node)) {
continue;
}
/* got one */
chat = (PurpleChat *) node;
if (account != purple_chat_get_account(chat)) /* not qq account*/
continue;
rmd = room_data_new_by_hashtable(gc, purple_chat_get_components(chat));
qd->groups = g_list_append(qd->groups, rmd);
count++;
}
purple_debug_info("QQ", "Load %d QQ Qun configurations\n", count);
}
void qq_room_data_free_all(PurpleConnection *gc)
{
qq_data *qd;
qq_room_data *rmd;
gint count;
g_return_if_fail (gc != NULL && gc->proto_data != NULL);
qd = (qq_data *) gc->proto_data;
count = 0;
while (qd->groups != NULL) {
rmd = (qq_room_data *) qd->groups->data;
qd->groups = g_list_remove(qd->groups, rmd);
room_data_free(rmd);
count++;
}
if (count > 0) {
purple_debug_info("QQ", "%d rooms are freed\n", count);
}
}
|