summaryrefslogtreecommitdiff
path: root/plugins/SendScreenshotPlus/src/CSendHost_uploadpie.cpp
blob: f88bca48bba2379dc663b2b78ed3b225fd2ccbee (plain)
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
/*
            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
                    Version 2, December 2004

 Copyright (C) 2014 Miranda NG project (http://miranda-ng.org)

 Everyone is permitted to copy and distribute verbatim or modified
 copies of this license document, and changing it is allowed as long
 as the name is changed.

            DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
   TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION

  0. You just DO WHAT THE FUCK YOU WANT TO.
*/
#include "stdafx.h"

CSendHost_UploadPie::CSendHost_UploadPie(HWND Owner, MCONTACT hContact, bool bAsync, int expire)
	: m_expire(expire), CSend(Owner, hContact, bAsync)
{
	m_EnableItem = SS_DLG_DESCRIPTION | SS_DLG_AUTOSEND | SS_DLG_DELETEAFTERSSEND;
	m_pszSendTyp = LPGENW("Image upload");
}

CSendHost_UploadPie::~CSendHost_UploadPie()
{
}

/////////////////////////////////////////////////////////////////////////////////////////

static const char kHostURL[] = "https://uploadpie.com/";

int CSendHost_UploadPie::Send()
{
	if (!g_hNetlibUser) { /// check Netlib
		Error(SS_ERR_INIT, m_pszSendTyp);
		Exit(ACKRESULT_FAILED);
		return !m_bAsync;
	}
	memset(&m_nlhr, 0, sizeof(m_nlhr));
	char* tmp; tmp = mir_u2a(m_pszFile);
	HTTPFormData frm[] = {
		{ "MAX_FILE_SIZE", HTTPFORM_INT(3145728) },// ??
		{ "upload", HTTPFORM_INT(1) },// ??
		{ "uploadedfile", HTTPFORM_FILE(tmp) },
		{ "expire", HTTPFORM_INT(m_expire) },// 30m
		//{ "expire", HTTPFORM_INT(2) },// 1h
		//{ "expire", HTTPFORM_INT(3) },// 6h
		//{ "expire", HTTPFORM_INT(4) },// 1d
		//{ "expire", HTTPFORM_INT(5) },// 1w
		//{ "x", HTTPFORM_INT(130) },// relative X coordinate of "BAKE FILE" button (unused?)
		//{ "y", HTTPFORM_INT(17) },// relative Y coordinate of "BAKE FILE" button (unused?)
	};
	int error = HTTPFormCreate(&m_nlhr, REQUEST_POST, kHostURL, frm, sizeof(frm) / sizeof(HTTPFormData));
	mir_free(tmp);
	if (error)
		return !m_bAsync;
	/// start upload thread
	if (m_bAsync) {
		mir_forkthread(&CSendHost_UploadPie::SendThread, this);
		return 0;
	}
	SendThread(this);
	return 1;
}

void CSendHost_UploadPie::SendThread(void* obj)
{
	CSendHost_UploadPie* self = (CSendHost_UploadPie*)obj;
	/// send DATA and wait for m_nlreply
	NETLIBHTTPREQUEST* reply = Netlib_HttpTransaction(g_hNetlibUser, &self->m_nlhr);
	self->HTTPFormDestroy(&self->m_nlhr);
	if (reply) {
		if (reply->resultCode >= 200 && reply->resultCode < 300 && reply->dataLength) {
			reply->pData[reply->dataLength - 1] = '\0';/// make sure its null terminated
			char* url = reply->pData;
			do {
				char* pos;
				if ((url = strstr(url, kHostURL))) {
					for (pos = url + _countof(kHostURL)-1; (*pos >= '0'&&*pos <= '9') || (*pos >= 'a'&&*pos <= 'z') || (*pos >= 'A'&&*pos <= 'Z') || *pos == '_' || *pos == '-' || *pos == '"' || *pos == '\''; ++pos) {
						if (*pos == '"' || *pos == '\'') break;
					}
					if (url + _countof(kHostURL)-1 != pos && (*pos == '"' || *pos == '\'')) {
						*pos = '\0';
						break;
					}
					++url;
				}
			} while (url);
			
			if (url) {
				mir_free(self->m_URL), self->m_URL = mir_strdup(url);
				Netlib_FreeHttpRequest(reply);
				self->svcSendMsgExit(url); return;
			}
			else {/// check error mess from server
				const char* err = GetHTMLContent(reply->pData, "<p id=\"error\"", "</p>");
				wchar_t* werr;
				if (err) werr = mir_a2u(err);
				else werr = mir_a2u(reply->pData);
				self->Error(L"%s", werr);
				mir_free(werr);
			}
		}
		else self->Error(SS_ERR_RESPONSE, self->m_pszSendTyp, reply->resultCode);

		Netlib_FreeHttpRequest(reply);
	}
	else self->Error(SS_ERR_NORESPONSE, self->m_pszSendTyp, self->m_nlhr.resultCode);

	self->Exit(ACKRESULT_FAILED);
}