purpleline_cmds.cpp
4.86 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
#include "purpleline.hpp"
void PurpleLine::register_commands() {
purple_cmd_register(
"sticker",
"w",
PURPLE_CMD_P_PRPL,
(PurpleCmdFlag)(PURPLE_CMD_FLAG_PRPL_ONLY | PURPLE_CMD_FLAG_IM | PURPLE_CMD_FLAG_CHAT),
LINE_PRPL_ID,
WRAPPER(PurpleLine::cmd_sticker),
"Sends a sticker. The argument should be of the format VER/PKGID/ID.",
nullptr);
purple_cmd_register(
"history",
"w",
PURPLE_CMD_P_PRPL,
(PurpleCmdFlag)(PURPLE_CMD_FLAG_PRPL_ONLY | PURPLE_CMD_FLAG_IM | PURPLE_CMD_FLAG_CHAT
| PURPLE_CMD_FLAG_ALLOW_WRONG_ARGS),
LINE_PRPL_ID,
WRAPPER(PurpleLine::cmd_history),
"Shows more chat history. Optional argument specifies number of messages to show.",
nullptr);
purple_cmd_register(
"open",
"w",
PURPLE_CMD_P_PRPL,
(PurpleCmdFlag)(PURPLE_CMD_FLAG_PRPL_ONLY | PURPLE_CMD_FLAG_IM | PURPLE_CMD_FLAG_CHAT),
LINE_PRPL_ID,
WRAPPER(PurpleLine::cmd_open),
"Opens an attachment (image, audio) by number.",
nullptr);
}
PurpleCmdRet PurpleLine::cmd_sticker(PurpleConversation *conv,
const gchar *, gchar **args, gchar **error, void *)
{
static const char *sticker_fields[] = { "STKVER", "STKPKGID", "STKID" };
line::Message msg;
std::stringstream ss(args[0]);
std::string item;
int part = 0;
while (std::getline(ss, item, '/')) {
if (part == 3) {
*error = g_strdup("Invalid sticker.");
return PURPLE_CMD_RET_FAILED;
}
msg.contentMetadata[sticker_fields[part]] = item;
part++;
}
if (part != 3) {
*error = g_strdup("Invalid sticker.");
return PURPLE_CMD_RET_FAILED;
}
msg.contentType = line::ContentType::STICKER;
msg.to = purple_conversation_get_name(conv);
write_message(msg, false);
send_message(msg);
return PURPLE_CMD_RET_OK;
}
PurpleCmdRet PurpleLine::cmd_history(PurpleConversation *conv,
const gchar *, gchar **args, gchar **error, void *)
{
int count = 10;
if (args[0]) {
try {
count = std::stoi(args[0]);
} catch (...) {
*error = g_strdup("Invalid message count.");
return PURPLE_CMD_RET_FAILED;
}
}
fetch_conversation_history(conv, count, true);
return PURPLE_CMD_RET_OK;
}
PurpleCmdRet PurpleLine::cmd_open(PurpleConversation *conv,
const gchar *, gchar **args, gchar **error, void *)
{
static std::map<line::ContentType::type, std::string> attachment_extensions = {
{ line::ContentType::IMAGE, ".jpg" },
{ line::ContentType::VIDEO, ".mp4" },
{ line::ContentType::AUDIO, ".mp3" },
};
std::string token(args[0]);
Attachment *att = conv_attachment_get(conv, token);
if (!att) {
*error = g_strdup("No such attachment.");
return PURPLE_CMD_RET_FAILED;
}
if (att->path != "" && g_file_test(att->path.c_str(), G_FILE_TEST_EXISTS)) {
purple_notify_uri(conn, att->path.c_str());
return PURPLE_CMD_RET_OK;
}
// Ensure there's nothing funny about the id as we're going to use it as a path element
try {
std::stoll(att->id);
} catch (...) {
*error = g_strdup("Failed to download attachment.");
return PURPLE_CMD_RET_FAILED;
}
std::string ext = ".jpg";
if (attachment_extensions.count(att->type))
ext = attachment_extensions[att->type];
std::string dir = get_tmp_dir(true);
gchar *path_p = g_build_filename(
dir.c_str(),
(att->id + ext).c_str(),
nullptr);
std::string path(path_p);
g_free(path_p);
purple_conversation_write(
conv,
"",
"Downloading attachment...",
(PurpleMessageFlags)PURPLE_MESSAGE_SYSTEM,
time(NULL));
std::string url = std::string(LINE_OS_URL) + "os/m/"+ att->id;
PurpleConversationType ctype = purple_conversation_get_type(conv);
std::string cname = std::string(purple_conversation_get_name(conv));
http.request(url, HTTPFlag::AUTH | HTTPFlag::LARGE,
[this, path, token, ctype, cname]
(int status, const guchar *data, gsize len)
{
if (status == 200 && data && len > 0) {
g_file_set_contents(path.c_str(), (const char *)data, len, nullptr);
temp_files.push_back(path);
PurpleConversation *conv = purple_find_conversation_with_account(
ctype, cname.c_str(), acct);
if (conv) {
Attachment *att = conv_attachment_get(conv, token);
if (att)
att->path = path;
}
purple_notify_uri(conn, path.c_str());
} else {
notify_error("Failed to download attachment.");
}
});
return PURPLE_CMD_RET_OK;
}