summaryrefslogtreecommitdiff
path: root/protocols/MSN/msn_links.cpp
blob: 1d54afd64c124e7f315f63b986b2d87b430184fc (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
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
164
165
166
167
168
169
170
171
/*
Plugin of Miranda IM for communicating with users of the MSN Messenger protocol.
Copyright (c) 2008-2012 Boris Krasnovskiy.

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "msn_global.h"
#include "msn_proto.h"

#include <m_addcontact.h>

#include "m_assocmgr.h"

static HANDLE hServiceParseLink;

static HANDLE GetContact(TCHAR *arg, TCHAR **pemail, CMsnProto *proto)
{
	TCHAR* email = NULL;
	do 
	{
		TCHAR *tok = _tcschr(arg, '&'); /* next token */
		if (tok != NULL) *tok++ = '\0';

		if (_tcsnicmp(arg, _T("contact="), 8) == 0)
		{
			arg += 8;
			UrlDecode(arg);
			email = arg;
		}
		arg = tok;
	} 
	while(arg != NULL);

	if (email == NULL || email[0] == '\0')
	{
		if (pemail) *pemail = NULL;
		return NULL;
	}
	if (pemail) *pemail = email;
	HANDLE hContact = proto->MSN_HContactFromEmail(UTF8(email), NULL, true, true);
	return hContact;
}

/* 
	add user:      msnim:add?contact=netpassport@emailaddress.com
	send message:  msnim:chat?contact=netpassport@emailaddress.com
	voice chat:    msnim:voice?contact=netpassport@emailaddress.com
	video chat:    msnim:video?contact=netpassport@emailaddress.com
*/

static INT_PTR ServiceParseMsnimLink(WPARAM, LPARAM lParam)
{
	if (lParam == 0) return 1; /* sanity check */

	TCHAR *arg = (TCHAR*)lParam;

	/* skip leading prefix */
	arg = _tcschr(arg, ':');
	if (arg == NULL) return 1; /* parse failed */

	for (++arg; *arg == '/'; ++arg) {}

	arg = NEWTSTR_ALLOCA(arg);

	if (g_Instances.getCount() == 0) return 0;

	CMsnProto *proto = &g_Instances[0];
	for (int i = 0; i < g_Instances.getCount(); ++i)
	{
		if (g_Instances[i].m_iStatus > ID_STATUS_OFFLINE)
		{
			proto = &g_Instances[i];
			break;
		}
	}
	if (proto == NULL) return 1;


	/* add a contact to the list */
	if(_tcsnicmp(arg, _T("add?"), 4) == 0) 
	{ 
		arg += 4;

		TCHAR *email;
		HANDLE hContact = GetContact(arg, &email, proto);
		if (email == NULL) return 1;

		/* does not yet check if email is current user */
		if (hContact == NULL) 
		{
			ADDCONTACTSTRUCT acs = {0};
			PROTOSEARCHRESULT psr = {0};

			acs.handleType = HANDLE_SEARCHRESULT;
			acs.szProto = proto->m_szModuleName;
			acs.psr = &psr;

			psr.cbSize = sizeof(psr);
			psr.flags = PSR_TCHAR;
			psr.nick = email;
			psr.email = email;
			CallService(MS_ADDCONTACT_SHOW, 0, (LPARAM)&acs);
		}
		return 0;
	}
	/* send a message to a contact */
	/* "voice" and "video" not yet implemented, perform same action as "chat" */
	else if(_tcsnicmp(arg, _T("chat?"), 5) == 0)
	{
		arg += 5;

		HANDLE hContact = GetContact(arg, NULL, proto);

		if (hContact != NULL)
		{
			CallService(MS_MSG_SENDMESSAGE, (WPARAM)hContact, 0);
			return 0;
		}
	}
	else if(_tcsnicmp(arg, _T("voice?"), 6) == 0)
	{
		arg += 6;

		HANDLE hContact = GetContact(arg, NULL, proto);

		if (hContact != NULL)
		{
			CallService(MS_MSG_SENDMESSAGE, (WPARAM)hContact, 0);
			return 0;
		}
	}
	else if(_tcsnicmp(arg, _T("video?"), 6) == 0)
	{
		arg += 6;

		HANDLE hContact = GetContact(arg, NULL, proto);

		if (hContact != NULL)
		{
			CallService(MS_MSG_SENDMESSAGE, (WPARAM)hContact, 0);
			return 0;
		}
	}
	return 1; /* parse failed */
}

void MsnLinks_Init(void)
{
	static const char szService[] = "MSN/ParseMsnimLink";

	hServiceParseLink = CreateServiceFunction(szService, ServiceParseMsnimLink);
	AssocMgr_AddNewUrlTypeT("msnim:", TranslateT("MSN Link Protocol"), hInst, IDI_MSN, szService, 0);
}

void MsnLinks_Destroy(void)
{
	DestroyServiceFunction(hServiceParseLink);
	CallService(MS_ASSOCMGR_REMOVEURLTYPE, 0, (LPARAM)"msnim:");
}