diff options
author | Alexander Lantsev <aunsane@gmail.com> | 2014-03-07 19:41:15 +0000 |
---|---|---|
committer | Alexander Lantsev <aunsane@gmail.com> | 2014-03-07 19:41:15 +0000 |
commit | 3997c1912c9ef57697452f357b02b10b8f5bc6a7 (patch) | |
tree | 06003caf27213aa4bd5ecaf36992b03037c7b1b3 /plugins/Dropbox/src/dropbox_commands.cpp | |
parent | 370996a4a7ced75fe363bc1c3eb34f77f5d7aee1 (diff) |
Dropbox:
- code cleaning and reorganization
- added "/help" command
- more informative authorization request
- version bumped
git-svn-id: http://svn.miranda-ng.org/main/trunk@8456 1316c22d-e87f-b044-9b9b-93d7a3e3ba9c
Diffstat (limited to 'plugins/Dropbox/src/dropbox_commands.cpp')
-rw-r--r-- | plugins/Dropbox/src/dropbox_commands.cpp | 123 |
1 files changed, 77 insertions, 46 deletions
diff --git a/plugins/Dropbox/src/dropbox_commands.cpp b/plugins/Dropbox/src/dropbox_commands.cpp index 10b20657d0..ff819ddc2b 100644 --- a/plugins/Dropbox/src/dropbox_commands.cpp +++ b/plugins/Dropbox/src/dropbox_commands.cpp @@ -1,8 +1,21 @@ #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)
{
- MessageParam *param = (MessageParam*)arg;
+ CommandParam *param = (CommandParam*)arg;
char *name = (char*)param->data;
@@ -10,14 +23,14 @@ void CDropbox::CommandContent(void *arg) if (name)
url.AppendFormat("/%s", name);
- HttpRequest *request = new HttpRequest(INSTANCE->hNetlibUser, REQUEST_GET, url);
+ 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)
+ if (response && response->resultCode == HTTP_STATUS_OK)
{
CMStringA message;
@@ -25,7 +38,7 @@ void CDropbox::CommandContent(void *arg) if (root)
{
JSONNODE *node = json_get(root, "is_dir");
- bool isDir = json_as_bool(node);
+ bool isDir = json_as_bool(node) > 0;
if (!isDir)
message.AppendFormat("\"%s\" %s", name, Translate("is file"));
else
@@ -47,7 +60,7 @@ void CDropbox::CommandContent(void *arg) }
ProtoBroadcastAck(MODULE, param->hContact, ACKTYPE_MESSAGE, ACKRESULT_SUCCESS, param->hProcess, 0);
- CallContactService(INSTANCE->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)message.GetBuffer());
+ CallContactService(param->instance->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)message.GetBuffer());
return;
}
@@ -58,73 +71,91 @@ void CDropbox::CommandContent(void *arg) void CDropbox::CommandShare(void *arg)
{
- MessageParam *param = (MessageParam*)arg;
+ CommandParam *param = (CommandParam*)arg;
char *name = (char*)param->data;
-
- CMStringA url = DROPBOX_API_URL "/shares/" DROPBOX_API_ROOT;
if (name)
- url.AppendFormat("/%s", name);
-
- HttpRequest *request = new HttpRequest(INSTANCE->hNetlibUser, REQUEST_POST, url);
- request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret"));
+ {
+ CMStringA url = DROPBOX_API_URL "/shares/" DROPBOX_API_ROOT;
+ if (name)
+ url.AppendFormat("/%s", name);
- mir_ptr<NETLIBHTTPREQUEST> response(request->Send());
+ HttpRequest *request = new HttpRequest(param->instance->hNetlibUser, REQUEST_POST, url);
+ request->AddBearerAuthHeader(db_get_sa(NULL, MODULE, "TokenSecret"));
- delete request;
+ mir_ptr<NETLIBHTTPREQUEST> response(request->Send());
- if (response && response->resultCode == HTTP_STATUS::OK)
- {
- CMStringA link;
+ delete request;
- JSONNODE *root = json_parse(response->pData);
- if (root)
+ if (response && response->resultCode == HTTP_STATUS_OK)
{
- 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(INSTANCE->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)link.GetBuffer());
+ CMStringA link;
- return;
+ JSONNODE *root = json_parse(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)
{
- MessageParam *param = (MessageParam*)arg;
+ CommandParam *param = (CommandParam*)arg;
char *name = (char*)param->data;
+ if (name)
+ {
+ CMStringA pparam = CMStringA("root=" DROPBOX_API_ROOT "&path=") + name;
- CMStringA pparam = CMStringA("root=" DROPBOX_API_ROOT "&path=") + name;
-
- HttpRequest *request = new HttpRequest(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();
+ 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());
+ mir_ptr<NETLIBHTTPREQUEST> response(request->Send());
- delete request;
+ delete request;
- if (response && response->resultCode == HTTP_STATUS::OK)
- {
- JSONNODE *root = json_parse(response->pData);
- if (root)
+ if (response && response->resultCode == HTTP_STATUS_OK)
{
- JSONNODE *node = json_get(root, "is_deleted");
- bool isDeleted = json_as_bool(node);
- 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(INSTANCE->GetDefaultContact(), PSR_MESSAGE, 0, (LPARAM)message.GetBuffer());
-
- return;
+ JSONNODE *root = json_parse(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);
}
\ No newline at end of file |