summaryrefslogtreecommitdiff
path: root/protocols/AimOscar/src/links.cpp
blob: c4eec4ae696f484c817aa0056c9e3233ec4dccd5 (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
185
186
187
188
189
190
191
192
193
194
195
/*
Plugin of Miranda IM for communicating with users of the AIM protocol.
Copyright (c) 2008-2010 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 "aim.h"
#include "links.h"
#include "m_assocmgr.h"

static HANDLE hServiceParseLink;

extern OBJLIST<CAimProto> g_Instances;


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 TCHAR* 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;
	return str;
}

static INT_PTR ServiceParseAimLink(WPARAM /*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;

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

	/*
		add user:      aim:addbuddy?screenname=NICK&groupname=GROUP
		send message:  aim:goim?screenname=NICK&message=MSG
		open chatroom: aim:gochat?roomname=ROOM&exchange=NUM
	*/
	/* add a contact to the list */
	if (!_tcsnicmp(arg, _T("addbuddy?"), 9)) 
	{
		TCHAR *tok, *tok2; 
		char *sn = NULL, *group = NULL;
		
		for (tok = arg + 8; tok != NULL; tok = tok2) 
		{
			tok2 = _tcschr(++tok, '&'); /* first token */
			if (tok2) *tok2 = 0;
			if (!_tcsnicmp(tok, _T("screenname="), 11) && *(tok + 11) != 0)
				sn = mir_t2a(url_decode(tok + 11));
			if (!_tcsnicmp(tok, _T("groupname="), 10) && *(tok + 10) != 0)
				group = mir_utf8encodeT(url_decode(tok + 10));  /* group is currently ignored */
		}
		if (sn == NULL) 
		{
			mir_free(group);
			return 1;
		}

		if (!proto->contact_from_sn(sn)) /* does not yet check if sn is current user */
		{
			HANDLE hContact = proto->contact_from_sn(sn, true);
			proto->add_contact_to_group(hContact, group && group[0] ? group : AIM_DEFAULT_GROUP);
		}
		mir_free(group);
		mir_free(sn);
		return 0;
	}
	/* send a message to a contact */
	else if (!_tcsnicmp(arg, _T("goim?"), 5)) 
	{
		TCHAR *tok, *tok2, *msg = NULL;
		char *sn = NULL;

		for (tok = arg + 4; tok != NULL; tok = tok2) 
		{
			tok2 = _tcschr(++tok, '&'); /* first token */
			if (tok2) *tok2=0;
			if (!_tcsnicmp(tok, _T("screenname="), 11) && *(tok + 11) != 0)
				sn = mir_t2a(url_decode(tok + 11));
			if (!_tcsnicmp(tok, _T("message="), 8) && *(tok + 8) != 0)
				msg = url_decode(tok + 8);
		}
		if (sn == NULL) return 1; /* parse failed */

		HANDLE hContact = proto->contact_from_sn(sn, true, true);
		if (hContact)
			CallService(MS_MSG_SENDMESSAGET, (WPARAM)hContact, (LPARAM)msg);

		mir_free(sn);
		return 0;
	}
	/* open a chatroom */
	else if (!_tcsnicmp(arg, _T("gochat?"), 7)) 
	{
		TCHAR *tok, *tok2;
		char *rm = NULL;
		int exchange = 0;

		for (tok = arg + 6; tok != NULL; tok = tok2) 
		{
			tok2 = _tcschr(++tok, '&'); /* first token */
			if (tok2) *tok2 = 0;
			if (!_tcsnicmp(tok, _T("roomname="), 9) && *(tok + 9) != 0)
			{
				rm = mir_t2a(url_decode(tok + 9));
				for (char *ch = rm; *ch; ++ch)
					if (*ch == '+') *ch = ' ';
			}
			if (!_tcsnicmp(tok, _T("exchange="), 9))
				exchange = _ttoi(tok + 9); 
		}
		if (rm == NULL || exchange <= 0)
		{
			mir_free(rm);
			return 1;
		}

		chatnav_param* par = new chatnav_param(rm, (unsigned short)exchange);
		proto->ForkThread(&CAimProto::chatnav_request_thread, par);
		
		mir_free(rm);
		return 0;
	}
	return 1; /* parse failed */
}

void aim_links_init(void)
{
	static const char szService[] = "AIM/ParseAimLink";

	hServiceParseLink = CreateServiceFunction(szService, ServiceParseAimLink);
	AssocMgr_AddNewUrlTypeT("aim:", TranslateT("AIM Link Protocol"), hInstance, IDI_AOL, szService, 0);
}

void aim_links_destroy(void)
{
	DestroyServiceFunction(hServiceParseLink);
}