purpleline_chats.cpp
8.36 KB
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
#include "purpleline.hpp"
std::map<ChatType, std::string> PurpleLine::chat_type_to_string {
{ ChatType::GROUP, "group" },
{ ChatType::ROOM, "room" },
};
ChatType PurpleLine::get_chat_type(const char *type_ptr) {
if (!type_ptr)
return ChatType::ANY; // Invalid
std::string type_str(type_ptr);
if (type_str == chat_type_to_string[ChatType::GROUP])
return ChatType::GROUP;
else if (type_str == chat_type_to_string[ChatType::ROOM])
return ChatType::ROOM;
else if (type_str == chat_type_to_string[ChatType::GROUP_INVITE])
return ChatType::GROUP_INVITE;
return ChatType::ANY; // Invalid
}
std::string PurpleLine::get_room_display_name(line::Room &room) {
std::vector<line::Contact *> rcontacts;
for (line::Contact &rc: room.contacts) {
if (contacts.count(rc.mid) == 1)
rcontacts.push_back(&contacts[rc.mid]);
}
if (rcontacts.size() == 0)
return "Empty chat";
std::stringstream ss;
ss << "Chat with ";
switch (rcontacts.size()) {
case 1:
ss << rcontacts[0]->displayName;
break;
case 2:
ss << rcontacts[0]->displayName << " and " << rcontacts[1]->displayName;
break;
default:
ss << rcontacts[0]->displayName << " and " << (rcontacts.size() - 1) << " other people";
break;
}
return ss.str();
}
void PurpleLine::handle_group_invite(
line::Group &group,
line::Contact &invitee,
line::Contact &inviter)
{
blist_update_buddy(invitee, true);
if (invitee.mid == profile.mid) {
// Current user was invited - show popup
GHashTable *components = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
g_hash_table_insert(components, g_strdup("type"),
g_strdup(chat_type_to_string[ChatType::GROUP_INVITE].c_str()));
g_hash_table_insert(components, g_strdup("id"), g_strdup(group.id.c_str()));
// Invites on initial sync do not have inviter data
std::string who = (inviter.__isset.mid)
? inviter.displayName
: std::string("A member");
serv_got_chat_invite(
conn,
group.name.c_str(),
who.c_str(),
nullptr,
components);
} else {
// Another user was invited - if a chat is open, add the user
PurpleConversation *conv = purple_find_conversation_with_account(
PURPLE_CONV_TYPE_CHAT,
group.id.c_str(),
acct);
if (conv) {
std::string msg = "Invited by " + inviter.displayName;
purple_conv_chat_add_user(
PURPLE_CONV_CHAT(conv),
invitee.mid.c_str(),
msg.c_str(),
PURPLE_CBFLAGS_AWAY,
TRUE);
}
}
}
void PurpleLine::set_chat_participants(PurpleConvChat *chat, line::Group &group) {
purple_conv_chat_clear_users(chat);
GList *users = NULL, *flags = NULL;
for (line::Contact &c: group.members) {
line::Contact &contact = get_up_to_date_contact(c);
blist_update_buddy(contact, true);
int cbflags = 0;
if (contact.mid == group.creator.mid)
cbflags |= PURPLE_CBFLAGS_FOUNDER;
users = g_list_prepend(users, (gpointer)contact.mid.c_str());
flags = g_list_prepend(flags, GINT_TO_POINTER(cbflags));
}
for (line::Contact &c: group.invitee) {
line::Contact &contact = get_up_to_date_contact(c);
blist_update_buddy(contact, true);
users = g_list_prepend(users, (gpointer)contact.mid.c_str());
flags = g_list_prepend(flags, GINT_TO_POINTER(PURPLE_CBFLAGS_AWAY));
}
purple_conv_chat_add_users(chat, users, NULL, flags, FALSE);
g_list_free(users);
g_list_free(flags);
}
void PurpleLine::set_chat_participants(PurpleConvChat *chat, line::Room &room) {
purple_conv_chat_clear_users(chat);
GList *users = NULL, *flags = NULL;
for (line::Contact &rc: room.contacts) {
// Room contacts don't have full contact information.
if (contacts.count(rc.mid) == 0)
blist_update_buddy(rc.mid, true);
else
blist_update_buddy(contacts[rc.mid], true);
users = g_list_prepend(users, (gpointer)rc.mid.c_str());
flags = g_list_prepend(flags, GINT_TO_POINTER(0));
}
// Room contact lists don't contain self, so add for consistency
users = g_list_prepend(users, (gpointer)profile.mid.c_str());
flags = g_list_prepend(flags, GINT_TO_POINTER(0));
purple_conv_chat_add_users(chat, users, NULL, flags, FALSE);
g_list_free(users);
g_list_free(flags);
}
char *PurpleLine::get_chat_name(GHashTable *components) {
return g_strdup((char *)g_hash_table_lookup(components, (gconstpointer)"id"));
}
GList *PurpleLine::chat_info() {
struct proto_chat_entry *entry = g_new0(struct proto_chat_entry, 1);
entry->label = "Group _Name";
entry->identifier = "name",
entry->required = TRUE;
return g_list_append(nullptr, entry);
}
void PurpleLine::join_chat(GHashTable *components) {
char *id_ptr = (char *)g_hash_table_lookup(components, "id");
if (!id_ptr) {
purple_debug_warning("line", "Tried to join a chat with no id.\n");
return;
}
std::string id(id_ptr);
ChatType type = get_chat_type((char *)g_hash_table_lookup(components, "type"));
if (type == ChatType::ANY) {
purple_debug_warning("line", "Tried to join a chat with weird type.\n");
return;
}
if (type == ChatType::GROUP_INVITE) {
c_out->send_acceptGroupInvitation(0, id);
c_out->send([this, id]{
try {
c_out->recv_acceptGroupInvitation();
} catch (line::TalkException &err) {
purple_notify_warning(
(void *)conn,
"Failed to join LINE group",
"Your invitation was probably cancelled.",
err.reason.c_str());
return;
}
c_out->send_getGroup(id);
c_out->send([this]{
line::Group group;
c_out->recv_getGroup(group);
if (!group.__isset.id) {
purple_debug_warning("line", "Couldn't get group: %s\n", group.id.c_str());
return;
}
join_chat_success(ChatType::GROUP, group.id);
});
});
return;
}
join_chat_success(type, id);
}
void PurpleLine::join_chat_success(ChatType type, std::string id) {
// Assume chat is on buddy list for now.
PurpleConversation *conv = serv_got_joined_chat(
conn,
next_purple_id++,
id.c_str());
if (type == ChatType::GROUP) {
line::Group &group = groups[id];
set_chat_participants(PURPLE_CONV_CHAT(conv), group);
} else if (type == ChatType::ROOM) {
line::Room &room = rooms[id];
set_chat_participants(PURPLE_CONV_CHAT(conv), room);
}
}
void PurpleLine::reject_chat(GHashTable *components) {
char *id_ptr = (char *)g_hash_table_lookup(components, "id");
if (!id_ptr) {
purple_debug_warning("line", "Tried to reject an invitation with no id.\n");
return;
}
std::string id(id_ptr);
c_out->send_rejectGroupInvitation(0, id);
c_out->send([this]() {
try {
c_out->recv_rejectGroupInvitation();
} catch (line::TalkException &err) {
notify_error(err.reason);
}
});
}
void PurpleLine::chat_leave(int id) {
PurpleConversation *conv = purple_find_chat(conn, id);
if (!conv)
return;
PurpleConvChat *chat = PURPLE_CONV_CHAT(conv);
for (GList *buddies = purple_conv_chat_get_users(chat);
buddies;
buddies = g_list_next(buddies))
{
PurpleConvChatBuddy *buddy = (PurpleConvChatBuddy *)buddies->data;
blist_remove_buddy(purple_conv_chat_cb_get_name(buddy), true, chat);
}
}
int PurpleLine::chat_send(int id, const char *message, PurpleMessageFlags flags) {
PurpleConversation *conv = purple_find_chat(conn, id);
if (!conv) {
purple_debug_warning("line", "Tried to send to a nonexistent chat.\n");
return 0;
}
return send_message(purple_conversation_get_name(conv), message);
}
PurpleChat *PurpleLine::find_blist_chat(const char *name) {
return blist_find_chat(name, ChatType::ANY);
}