aboutsummaryrefslogtreecommitdiff
path: root/Src/Plugins/General/gen_crasher/feedback/operations.cpp
blob: 5919c6905c8d9931c6642baa93176b0f8eac3f9f (plain) (blame)
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
#include ".main.h"
#include ".\xzip\xzip.h"
#include ".\smtp\smtp.h"
#include ".\email\sendemail.h"
#include <strsafe.h>

const wchar_t* GetFileName(const wchar_t *fullname)
{
	if (!fullname) return NULL;
	const wchar_t *start = wcsrchr(fullname, L'\\');
	if (start && start != fullname) start = CharNext(start);
	
	return start; 
}

BOOL ZipData(void)
{
	BOOL retCode = FALSE;
	HZIP hz = CreateZip(settings.zipPath, 0, ZIP_FILENAME);
	if (hz)
	{
		retCode = TRUE;
		if (settings.createLOG && settings.ReadLogCollectResult()) retCode = (ZR_OK == ZipAdd(hz, GetFileName(settings.logPath),  settings.logPath, 0, ZIP_FILENAME));
		if (retCode && settings.createDMP && settings.ReadDmpCollectResult()) retCode = (ZR_OK == ZipAdd(hz, GetFileName(settings.dumpPath),  settings.dumpPath, 0, ZIP_FILENAME));
	}
	CloseZip(hz);
	return retCode;
}

LPCTSTR BuildSubjectString(LPCTSTR subject)
{
	static wchar_t subject_str[512] = {L"Winamp Error Report"};
	wchar_t uid_str[64] = {0}, path[MAX_PATH] = {0};
	if (GetModuleFileName(0, path, MAX_PATH))
	{
		PathRemoveFileSpec(path);
		wchar_t *p = path + wcslen(path) - 1;
		while(p && *p && *p != L'\\')
		{
			p = CharPrev(path, p);
		}
		if (p) *p = 0;
		PathAppend(path, L"winamp.exe");

		HKEY hkey = NULL;
		if (RegCreateKeyEx(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Nullsoft\\Winamp", 0, 0, 0, KEY_READ, NULL, &hkey, NULL) == ERROR_SUCCESS)
		{
			DWORD s = 512, t = REG_SZ;
			if (RegQueryValueEx(hkey, path, 0, &t, (LPBYTE)uid_str, &s) != ERROR_SUCCESS || t != REG_SZ) uid_str[0] = 0;
			RegCloseKey(hkey);
		}
	}

	// if it fails then we'll need to make something...
	if (!uid_str[0])
	{
		GUID guid;
		UuidCreate(&guid);
		StringCbPrintf(uid_str, ARRAYSIZE(uid_str), L"%08X%04X%04X%02X%02X%02X%02X%02X%02X%02X%02X",
					   (int)guid.Data1, (int)guid.Data2, (int)guid.Data3, (int)guid.Data4[0],
					   (int)guid.Data4[1], (int)guid.Data4[2], (int)guid.Data4[3],
					   (int)guid.Data4[4], (int)guid.Data4[5], (int)guid.Data4[6], (int)guid.Data4[7]);
	}

	if (StringCchPrintfW(subject_str, ARRAYSIZE(subject_str), L"%s [%s]", subject, uid_str) == S_OK)
		return subject_str;
	else
		return subject;
}

BOOL SendData(HWND hwnd)
{
	BOOL retCode = FALSE;
	const wchar_t *subject = L"Winamp Error Report";
	const wchar_t *senderName = L"Winamp Error Reporter";
	const wchar_t *recipientAddress = L"bug@winamp.com";
	const wchar_t *recipientName = L"Nullsoft Bug Reporting";
	wchar_t *msgInfo = _wcsdup(settings.ReadBody());

	wchar_t *p = msgInfo, *end = p + wcslen(msgInfo);
	while(p != end)
	{
		if (*p == 1) *p =  L'\r';
		if (*p == 2) *p =  L'\n';
		p++;
	}

	if (settings.sendBySMTP)
	{
		CSmtp smtp;
		CSmtpMessage msg;
		CSmtpMessageBody body;
		CSmtpAttachment attach;

		msg.Subject = BuildSubjectString(subject);

		// Who the message is from
		msg.Sender.Name  = senderName;
		msg.Sender.Address = settings.smtpAddress;
		msg.Recipient.Address = recipientAddress;
		msg.Recipient.Name = recipientName;
		if(settings.zipData ) 
		{
			attach.FileName = settings.zipPath;
			msg.Attachments.Add(attach);
		}
		else
		{
			if (settings.createLOG && settings.ReadLogCollectResult()) attach.FileName = settings.logPath;
			msg.Attachments.Add(attach);
			if (settings.createDMP && settings.ReadDmpCollectResult()) attach.FileName = settings.dumpPath;
			msg.Attachments.Add(attach);
		}

		//	smtp.m_wSmtpPort = settings.smtpPort; - not working for some reasons
		if (settings.smtpAuth)
		{
			smtp.m_strUser = settings.smtpUser;
			smtp.m_strPass = settings.smtpPwd;;
		}

		body = L"This message was generated by Winamp Error Reporter v1.09.\r\nPlease check attachments for viruses.\r\n";
		body.Data.append(L"\r\n");
		body.Data.append(msgInfo);

		msg.Message.Add(body);
		retCode = smtp.Connect(settings.smtpServer);
		if ( retCode )
		{
			// Send the message and close the connection afterwards
			retCode = (smtp.SendMessage(msg) == 0);
			smtp.Close();
		}
	}
	else if(settings.sendByClient)
	{
		retCode = SendEmail(hwnd, recipientAddress, recipientName, BuildSubjectString(subject), msgInfo, settings.zipPath);
	}

	return retCode;		
}

BOOL Restart(void)
{
	STARTUPINFO si = {0};
	si.cb = sizeof(si);
	si.dwFlags = STARTF_USESHOWWINDOW;
	si.wShowWindow = SW_SHOW;

	PROCESS_INFORMATION pi = {0};
	return  CreateProcess(
						settings.ReadWinamp(),	// name of executable module
						NULL,					// command line string
						NULL,					// process attributes
						NULL,					// thread attributes
						FALSE,					// handle inheritance option
						0,						// creation flags
						NULL,					// new environment block
						NULL,					// current directory name
						&si,					// startup information
						&pi						// process information
						);					
}