summaryrefslogtreecommitdiff
path: root/protocols/Yahoo/links.cpp
blob: 8af33e94b31fe7764327b67310d7ab95e7cc528e (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
172
173
174
175
176
177
178
179
180
181
182
183
184
/*
 * $Id: services.c 5376 2007-05-07 16:13:18Z gena01 $
 *
 * myYahoo Miranda Plugin 
 *
 * Authors: Gennady Feldman (aka Gena01) 
 *          Laurent Marechal (aka Peorth)
 *
 * This code is under GPL and is based on AIM, MSN and Miranda source code.
 * I want to thank Robert Rainwater and George Hazan for their code and support
 * and for answering some of my questions during development of this plugin.
 */

#include "yahoo.h"

#include "m_addcontact.h"
#include "m_message.h"
#include "m_assocmgr.h"

#include "resource.h"

static HANDLE hServiceParseLink;

static int SingleHexToDecimal(TCHAR c)
{
	if (c >= '0' && c <= '9') return c - '0';
	if (c >= 'a' && c <= 'f') return c - 'a' + 10;
	if (c >= 'A' && c <= 'F') return c - 'A' + 10;
	return -1;
}

static void url_decode(TCHAR* str)
{
	TCHAR* s = str, *d = str;

	while(*s)
	{
		if (*s == '%') 
		{
			int digit1 = SingleHexToDecimal(s[1]);
			if (digit1 != -1) 
			{
				int digit2 = SingleHexToDecimal(s[2]);
				if (digit2 != -1) 
				{
					s += 3;
					*d++ = (TCHAR)((digit1 << 4) | digit2);
					continue;
				}	
			}	
		}
		*d++ = *s++;
	}

	*d = 0;
}

static char* get_buddy(TCHAR ** arg)
{
	TCHAR *buf = *arg;

	TCHAR *tok = _tcschr(buf, '&'); /* first token */
	if (tok) *tok = 0;

	if (!buf[0]) return NULL;
	url_decode(buf);
	
	*arg = tok ? tok + 1 : NULL;

	return mir_t2a(buf);
}


/* 
	add user:       ymsgr:addfriend?ID
	send message:   ymsgr:sendim?ID&m=MESSAGE
	add chatroom:   ymsgr:chat?ROOM
*/
static INT_PTR ServiceParseYmsgrLink(WPARAM wParam, LPARAM lParam)
{
	TCHAR *arg = (TCHAR*)lParam;
	if (arg == NULL) return 1; /* sanity check */

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

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

	CYahooProto *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) return 1;

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

		char *id = get_buddy(&arg);
		if (!id) return 1;
		
		if (proto->getbuddyH(id) == NULL) /* does not yet check if id is current user */
		{
			ADDCONTACTSTRUCT acs = {0};
			PROTOSEARCHRESULT psr = {0};
			
			acs.handleType = HANDLE_SEARCHRESULT;
			acs.szProto = proto->m_szModuleName;
			acs.psr = &psr;
			
			psr.cbSize = sizeof(PROTOSEARCHRESULT);
			psr.id = (TCHAR*)id;
			CallService(MS_ADDCONTACT_SHOW, 0, (LPARAM)&acs);
		}

		mir_free(id);
		return 0;
	}
	/* send a message to a contact */
	else if (!_tcsnicmp(arg, _T("sendim?"), 7)) 
	{
		arg += 7;

		char *id = get_buddy(&arg);
		if (!id) return 1;

		TCHAR *msg = NULL;

		while (arg) 
		{
			if (!_tcsnicmp(arg, _T("m="), 2))
			{
				msg = arg + 2;
				url_decode(msg);
				break;
			}
				
			arg = _tcschr(arg + 1, '&'); /* first token */
			if (arg) *arg = 0;
		}
			
		HANDLE hContact = proto->add_buddy(id, id, 0, PALF_TEMPORARY); /* ensure contact is on list */
		if (hContact)
			CallService(MS_MSG_SENDMESSAGET, (WPARAM)hContact, (LPARAM)msg);

		mir_free(id);
		return 0;
	}
	/* open a chatroom */
	else if (!_tcsnicmp(arg, _T("chat?"), 5)) 
	{
		arg += 5;

//		char *id = get_buddy(&arg);
//		if (!id) return 1;

		/* not yet implemented (rm contains name of chatroom)*/
		return 0;
	}
	return 1; /* parse failed */
}

void YmsgrLinksInit(void)
{
	static const char szService[] = "YAHOO/ParseYmsgrLink";

	hServiceParseLink = CreateServiceFunction(szService, ServiceParseYmsgrLink);
	AssocMgr_AddNewUrlTypeT("ymsgr:", TranslateT("YAHOO Link Protocol"), hInstance, IDI_YAHOO, szService, 0);
}

void YmsgrLinksUninit(void)
{
	DestroyServiceFunction(hServiceParseLink);
	CallService(MS_ASSOCMGR_REMOVEURLTYPE, 0, (LPARAM)"ymsgr:");
}