summaryrefslogtreecommitdiff
path: root/plugins/NotesAndReminders/src/miscutils.cpp
blob: 5f46db337a841daefee3b51a443a28ed5dd520ce (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
#include "stdafx.h"

/////////////////////////////////////////////////////////////////////
// Email/SMS and WinSock functions

BOOL WS_Init()
{
	WSADATA wsd;
	if (WSAStartup(MAKEWORD(2, 2), &wsd) != 0) return FALSE;
	return TRUE;
}

void WS_CleanUp()
{
	WSACleanup();
}

int WS_Send(SOCKET s, const char *data, int datalen)
{
	int rlen;
	if ((rlen = send(s, data, datalen, 0)) == SOCKET_ERROR) return FALSE;
	return TRUE;
}

unsigned long WS_ResolveName(char *name, WORD *port, int defaultPort)
{
	char *nameCopy = _strdup(name);
	if (port != nullptr)
		*port = defaultPort;
	char *pcolon = strchr(nameCopy, ':');
	if (pcolon != nullptr) {
		if (port != nullptr) *port = atoi(pcolon + 1);
		*pcolon = 0;
	}
	if (inet_addr(nameCopy) == INADDR_NONE) {
		HOSTENT *lk = gethostbyname(nameCopy);
		if (lk == nullptr)
			return SOCKET_ERROR;

		free(nameCopy);
		return *(u_long*)lk->h_addr_list[0];
	}
	DWORD ret = inet_addr(nameCopy);
	free(nameCopy);
	return ret;
}

void Send(char *user, char *host, const char *Msg, char *server)
{
	SOCKET S = socket(AF_INET, SOCK_STREAM, 0);
	if (!server)
		server = host;

	WORD port;
	SOCKADDR_IN sockaddr;
	if ((sockaddr.sin_addr.S_un.S_addr = WS_ResolveName(server, &port, 25)) == SOCKET_ERROR)
		return;

	sockaddr.sin_port = htons(port);
	sockaddr.sin_family = AF_INET;
	if (connect(S, (SOCKADDR*)&sockaddr, sizeof(sockaddr)) == SOCKET_ERROR)
		return;

	CMStringA rcpt(FORMAT, "rcpt to:%s@%s\r\n", user, host);
	WS_Send(S, "mail from: \r\n", 13);
	WS_Send(S, rcpt, rcpt.GetLength());
	WS_Send(S, "data\r\n", 6);
	WS_Send(S, "From:<REM>\r\n\r\n", 14);
	WS_Send(S, Msg, (int)mir_strlen(Msg));
	WS_Send(S, "\r\n.\r\n", 5);
	WS_Send(S, "quit\r\n", 6);
	closesocket(S);
}