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
|
#include "common.h"
void CDropbox::CommandHelp(void *arg)
{
CommandParam *param = (CommandParam*)arg;
CMStringA help = Translate("Dropbox supports the following commands:"); help += "\n";
help += "\"/content [dir]\" - "; help += Translate("shows all files in folder \"dir\" (\"dir\" can be omitted for root folder)"); help += "\n";
help += "\"/share <path>\" - "; help += Translate("returns download link for file or folder with specified path (\"path\" is relative from root folder)"); help += "\n";
help += "\"/delete <path>\" - "; help += Translate("deletes file or folder with specified path (\"path\" is relative from root folder)");
ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hProcess, 0);
CallContactService(param->instance->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)help.GetBuffer());
}
void CDropbox::CommandContent(void *arg)
{
CommandParam *param = (CommandParam*)arg;
char *name = (char*)param->data;
CMStringA url = DROPBOX_API_URL "/metadata/" DROPBOX_API_ROOT;
if (name)
url.AppendFormat("/%s", ptrA(mir_utf8encode(name)));
HttpRequest *request = new HttpRequest(param->instance->hNetlibUser, REQUEST_GET, url);
request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret"));
mir_ptr<NETLIBHTTPREQUEST> response(request->Send());
delete request;
if (response && response->resultCode == HTTP_STATUS_OK)
{
CMStringA message;
JSONROOT root(response->pData);
if (root)
{
JSONNODE *node = json_get(root, "is_dir");
bool isDir = json_as_bool(node) > 0;
if (!isDir)
message.AppendFormat("\"%s\" %s", name, Translate("is file"));
else
{
JSONNODE *content = json_as_array(json_get(root, "contents"));
for (int i = 0;; i++)
{
JSONNODE *item = json_at(content, i);
if (item == NULL)
{
if (i == 0)
message.AppendFormat("\"%s\" %s", name, Translate("is empty"));
break;
}
ptrA subName(mir_u2a(json_as_string(json_get(item, "path"))));
message.AppendFormat("%s\n", (subName[0] == '/') ? &subName[1] : subName);
}
}
ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hProcess, 0);
CallContactService(param->instance->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)message.GetBuffer());
return;
}
}
ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, param->hProcess, 0);
}
void CDropbox::CommandShare(void *arg)
{
CommandParam *param = (CommandParam*)arg;
char *name = (char*)param->data;
if (name)
{
CMStringA url = DROPBOX_API_URL "/shares/" DROPBOX_API_ROOT;
if (name)
url.AppendFormat("/%s", ptrA(mir_utf8encode(name)));
HttpRequest *request = new HttpRequest(param->instance->hNetlibUser, REQUEST_POST, url);
request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret"));
mir_ptr<NETLIBHTTPREQUEST> response(request->Send());
delete request;
if (response && response->resultCode == HTTP_STATUS_OK)
{
CMStringA link;
JSONROOT root(response->pData);
if (root)
{
JSONNODE *node = json_get(root, "url");
link = mir_u2a(json_as_string(node));
ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hProcess, 0);
CallContactService(param->instance->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)link.GetBuffer());
return;
}
}
}
else
{
CMStringA error; error.AppendFormat(Translate("\"%s\" command has invalid parameter.\nUse \"/help\" for more info."), "/share");
ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hProcess, 0);
CallContactService(param->instance->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)error.GetBuffer());
return;
}
ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, param->hProcess, 0);
}
void CDropbox::CommandDelete(void *arg)
{
CommandParam *param = (CommandParam*)arg;
char *name = (char*)param->data;
if (name)
{
CMStringA pparam = CMStringA("root=" DROPBOX_API_ROOT "&path=") + ptrA(mir_utf8encode(name));
HttpRequest *request = new HttpRequest(param->instance->hNetlibUser, REQUEST_POST, DROPBOX_API_URL "/fileops/delete");
request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret"));
request->AddHeader("Content-Type", "application/x-www-form-urlencoded");
request->pData = mir_strdup(pparam);
request->dataLength = pparam.GetLength();
mir_ptr<NETLIBHTTPREQUEST> response(request->Send());
delete request;
if (response && response->resultCode == HTTP_STATUS_OK)
{
JSONROOT root(response->pData);
if (root)
{
JSONNODE *node = json_get(root, "is_deleted");
bool isDeleted = json_as_bool(node) > 0;
CMStringA message;
message.AppendFormat("%s %s", name, !isDeleted ? Translate("is not deleted") : Translate("is deleted"));
ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hProcess, 0);
CallContactService(param->instance->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)message.GetBuffer());
return;
}
}
}
else
{
CMStringA error; error.AppendFormat(Translate("\"%s\" command has invalid parameter.\nUse \"/help\" for more info."), "/delete");
ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hProcess, 0);
CallContactService(param->instance->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)error.GetBuffer());
}
ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_FAILED, param->hProcess, 0);
}
|