forked from majn/telegram-purple
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtgp-blist.c
144 lines (125 loc) · 5.14 KB
/
tgp-blist.c
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
/* This file is part of telegram-purple
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
Copyright Matthias Jentsch 2015
*/
#include "tgp-blist.h"
#include "tgp-structs.h"
#include "tgp-2prpl.h"
#include <glib.h>
#include <blist.h>
#include <assert.h>
const char *tgp_blist_peer_get_purple_name (struct tgl_state *TLS, tgl_peer_id_t id) {
const char *name = g_hash_table_lookup (tg_get_data (TLS)->id_to_purple_name, GINT_TO_POINTER(tgl_get_peer_id (id)));
if (! name) {
g_warn_if_reached();
return NULL;
}
return name;
}
void tgp_blist_peer_add_purple_name (struct tgl_state *TLS, tgl_peer_id_t id, const char *purple_name) {
g_hash_table_replace (tg_get_data (TLS)->id_to_purple_name, GINT_TO_POINTER(tgl_get_peer_id (id)), g_strdup (purple_name));
}
tgl_peer_t *tgp_blist_peer_find (struct tgl_state *TLS, const char *purple_name) {
// buddies will keep the name they had when they were first added to the user list. The print_name
// of the peer may have changed since then, therefore the ID stored in the buddy is used to fetch
// the user name.
PurpleBuddy *buddy = purple_find_buddy (tg_get_acc (TLS), purple_name);
if (! buddy) {
// foreign users are not in the buddy list by default, therefore the name used by libpurple and the
// print name is always identical
return tgl_peer_get_by_name (TLS, purple_name);
}
if (! tgp_blist_buddy_has_id (buddy)) {
return NULL;
}
return tgl_peer_get (TLS, tgp_blist_buddy_get_id (buddy));
}
PurpleBuddy *tgp_blist_buddy_new (struct tgl_state *TLS, tgl_peer_t *user) {
PurpleBuddy *buddy = purple_buddy_new (tg_get_acc (TLS), tgp_blist_peer_get_purple_name (TLS, user->id), NULL);
tgp_blist_buddy_set_id (buddy, user->id);
return buddy;
}
PurpleBuddy *tgp_blist_buddy_migrate (struct tgl_state *TLS, PurpleBuddy *buddy, struct tgl_user *user) {
purple_blist_remove_buddy (buddy);
buddy = purple_buddy_new (tg_get_acc (TLS), user->print_name, NULL);
tgp_blist_buddy_set_id (buddy, user->id);
purple_blist_add_buddy (buddy, NULL, tgp_blist_group_init ("Telegram"), NULL);
return buddy;
}
void tgp_blist_buddy_set_id (PurpleBuddy *buddy, tgl_peer_id_t id) {
int uid = tgl_get_peer_id (id),
type = tgl_get_peer_type (id);
assert (type == TGL_PEER_ENCR_CHAT || type == TGL_PEER_USER);
purple_blist_node_set_int (&buddy->node, TGP_BUDDY_KEY_PEER_ID, uid);
purple_blist_node_set_int (&buddy->node, TGP_BUDDY_KEY_PEER_TYPE, type);
}
int tgp_blist_buddy_has_id (PurpleBuddy *buddy) {
return purple_blist_node_get_int (&buddy->node, TGP_BUDDY_KEY_PEER_ID) != 0;
}
tgl_peer_id_t tgp_blist_buddy_get_id (PurpleBuddy *buddy) {
int id = purple_blist_node_get_int (&buddy->node, TGP_BUDDY_KEY_PEER_ID),
type = purple_blist_node_get_int (&buddy->node, TGP_BUDDY_KEY_PEER_TYPE);
if (type == TGL_PEER_USER) {
return TGL_MK_USER (id);
} else if (type == TGL_PEER_ENCR_CHAT) {
return TGL_MK_ENCR_CHAT (id);
} else {
assert (FALSE);
}
}
tgl_peer_t *tgp_blist_buddy_get_peer (PurpleBuddy *buddy) {
if (! tgp_blist_buddy_has_id (buddy)) {
return NULL;
}
return tgl_peer_get (pbn_get_conn (&buddy->node)->TLS, tgp_blist_buddy_get_id (buddy));
}
PurpleBuddy *tgp_blist_buddy_find (struct tgl_state *TLS, tgl_peer_id_t user) {
PurpleBlistNode *node = purple_blist_get_root ();
while (node) {
if (PURPLE_BLIST_NODE_IS_BUDDY(node)) {
PurpleBuddy *buddy = PURPLE_BUDDY(node);
if (purple_buddy_get_account (buddy) == tg_get_acc (TLS)) {
if (purple_blist_node_get_int (node, TGP_BUDDY_KEY_PEER_ID) == tgl_get_peer_id (user)) {
assert (tgl_get_peer_type (user) == purple_blist_node_get_int (node, TGP_BUDDY_KEY_PEER_TYPE));
return buddy;
}
}
}
node = purple_blist_node_next (node, FALSE);
}
return NULL;
}
PurpleChat *tgp_blist_chat_find (struct tgl_state *TLS, tgl_peer_id_t user) {
PurpleBlistNode *node = purple_blist_get_root ();
while (node) {
if (PURPLE_BLIST_NODE_IS_CHAT(node)) {
PurpleChat *chat = PURPLE_CHAT(node);
if (purple_chat_get_account (chat) == tg_get_acc (TLS)) {
const char *id = g_hash_table_lookup (purple_chat_get_components (chat), "id");
if (id && *id && atoi (id) == tgl_get_peer_id (user)) {
return chat;
}
}
}
node = purple_blist_node_next (node, FALSE);
}
return NULL;
}
PurpleGroup *tgp_blist_group_init (const char *name) {
PurpleGroup *grp = purple_find_group (name);
if (grp == NULL) {
grp = purple_group_new (name);
purple_blist_add_group (grp, NULL);
}
return grp;
}