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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
|
/*
Copyright © 2003 Robert Rainwater, 2007 Scott Ellis
adapted for MySpace, 2007 Scott Ellis
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, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "common.h"
#include "links.h"
#include "server_con.h"
#include <malloc.h>
#include <cstdlib>
#define MYSPACEWATCHERCLASS _T("__MYSPACEWatcherClass__")
static ATOM aWatcherClass = 0;
static HWND hWatcher = NULL;
static LRESULT CALLBACK myspace_links_watcherwndproc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg) {
case WM_COPYDATA:
{
char *szData, *s;
COPYDATASTRUCT *cds = (COPYDATASTRUCT *) lParam;
//LOG(LOG_DEBUG, "Links: WM_COPYDATA");
// Check to see if link support is enabled
// We shouldn't need this check since the class instance shouldn't be running
// but lets be safe
if (!DBGetContactSettingByte(NULL, MODULE, "EnableLinkHandling", 0))
break;
if (!(char *) cds->lpData)
break;
s = szData = strdup((char *) cds->lpData);
s += 5;
if (!_strnicmp(s, "addContact?", 11)) {
s =strstr(s, "cID=");
if(s) {
s += 4;
char *t = strchr(s, '&');
if(t) *t = 0;
HANDLE hContact;
int uid;
for(t = strtok(s, ","); t ;t = strtok(0, ",")) {
uid = atoi(t);
hContact = FindContact(uid);
if(!hContact) {
hContact = CreateContact(uid, 0, 0, true);
LookupUID(uid);
}
}
}
}
else if (_strnicmp(s, "sendIM?", 7) == 0 && ServiceExists(MS_MSG_SENDMESSAGE))
{
s=strrchr(s, '=');
if(s) {
s++;
int uid = atoi(s);
HANDLE hContact = FindContact(uid);
if(!hContact) {
hContact = CreateContact(uid, 0, 0, false);
DBWriteContactSettingByte(hContact, "CList", "NotOnList", 1);
DBWriteContactSettingByte(hContact, "CList", "Hidden", 1);
LookupUID(uid);
}
CallService(MS_MSG_SENDMESSAGE, (WPARAM)hContact, 0);
}
}
free(szData);
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
static void myspace_links_regwatcher()
{
WNDCLASS wc;
//LOG(LOG_DEBUG, "Links: regwatcher");
if (hWatcher || aWatcherClass) {
return;
}
memset(&wc, 0, sizeof(WNDCLASS));
wc.lpfnWndProc = myspace_links_watcherwndproc;
wc.hInstance = hInst;
wc.lpszClassName = MYSPACEWATCHERCLASS;
aWatcherClass = RegisterClass(&wc);
hWatcher = CreateWindowEx(0, MYSPACEWATCHERCLASS, _T(""), 0, 0, 0, 0, 0, NULL, NULL, hInst, NULL);
return;
}
static void myspace_links_unregwatcher()
{
//LOG(LOG_DEBUG, "Links: unregwatcher");
if (hWatcher) {
DestroyWindow(hWatcher);
hWatcher = NULL;
}
if (aWatcherClass) {
UnregisterClass(MYSPACEWATCHERCLASS, hInst);
aWatcherClass = 0;
}
return;
}
static BOOL CALLBACK myspace_linsk_enumthreadwindowsproc(HWND hwnd, LPARAM lParam)
{
TCHAR szBuf[64];
if (GetClassName(hwnd, szBuf, 64)) {
if (!lstrcmp(szBuf, MYSPACEWATCHERCLASS)) {
COPYDATASTRUCT cds;
//LOG(LOG_DEBUG, "Links: enumthreadwindowsproc - found AIMWATCHERCLASS");
cds.dwData = 1;
cds.cbData = strlen((char *) lParam) + 1;
cds.lpData = (char *) lParam;
SendMessageTimeout(hwnd, WM_COPYDATA, (WPARAM) hwnd, (LPARAM) & cds, SMTO_ABORTIFHUNG | SMTO_BLOCK, 150, NULL);
}
};
return TRUE;
}
static BOOL CALLBACK myspace_links_enumwindowsproc(HWND hwnd, LPARAM lParam)
{
char szBuf[32];
//LOG(LOG_DEBUG, "Links: enumwindowsproc");
if (GetClassNameA(hwnd, szBuf, 32))
{
if (!strcmp(szBuf, MIRANDACLASS)) {
//LOG(LOG_DEBUG, "Links: enumwindowsproc - found Miranda window");
EnumThreadWindows(GetWindowThreadProcessId(hwnd, NULL), myspace_linsk_enumthreadwindowsproc, lParam);
}
}
return TRUE;
}
static void myspace_links_register()
{
HKEY hkey;
char szBuf[MAX_PATH], szExe[MAX_PATH * 2], szShort[MAX_PATH];
GetModuleFileNameA(hInst, szBuf, sizeof(szBuf));
GetShortPathNameA(szBuf, szShort, sizeof(szShort));
//LOG(LOG_DEBUG, "Links: register");
if (RegCreateKeyA(HKEY_CLASSES_ROOT, "myim", &hkey) == ERROR_SUCCESS) {
RegSetValueA(hkey, NULL, REG_SZ, "URL:MySpace IM Protocol", strlen("URL:MySpace IM Protocol"));
RegSetValueExA(hkey, "URL Protocol", 0, REG_SZ, (PBYTE) "", 1);
RegCloseKey(hkey);
}
else {
//LOG(LOG_ERROR, "Links: register - unable to create registry key (root)");
return;
}
if (RegCreateKeyA(HKEY_CLASSES_ROOT, "myim\\DefaultIcon", &hkey) == ERROR_SUCCESS) {
char szIcon[MAX_PATH];
mir_snprintf(szIcon, sizeof(szIcon), "%s,0", szShort);
RegSetValueA(hkey, NULL, REG_SZ, szIcon, strlen(szIcon));
RegCloseKey(hkey);
}
else {
//LOG(LOG_ERROR, "Links: register - unable to create registry key (DefaultIcon)");
return;
}
if (RegCreateKeyA(HKEY_CLASSES_ROOT, "myim\\shell\\open\\command", &hkey) == ERROR_SUCCESS) {
// MSVC exports differently than gcc/mingw
#ifdef _MSC_VER
mir_snprintf(szExe, sizeof(szExe), "RUNDLL32.EXE %s,_myspace_links_exec@16 %%1", szShort);
//LOG(LOG_INFO, "Links: registering (%s)", szExe);
#else
mir_snprintf(szExe, sizeof(szExe), "RUNDLL32.EXE %s,myspace_links_exec@16 %%1", szShort);
//LOG(LOG_INFO, "Links: registering (%s)", szExe);
#endif
RegSetValueA(hkey, NULL, REG_SZ, szExe, strlen(szExe));
RegCloseKey(hkey);
}
else {
//LOG(LOG_ERROR, "Links: register - unable to create registry key (command)");
return;
}
}
void myspace_links_unregister()
{
//LOG(LOG_DEBUG, "Links: unregister");
RegDeleteKeyA(HKEY_CLASSES_ROOT, "myim\\shell\\open\\command");
RegDeleteKeyA(HKEY_CLASSES_ROOT, "myim\\shell\\open");
RegDeleteKeyA(HKEY_CLASSES_ROOT, "myim\\shell");
RegDeleteKeyA(HKEY_CLASSES_ROOT, "myim\\DefaultIcon");
RegDeleteKeyA(HKEY_CLASSES_ROOT, "myim");
}
void myspace_links_init()
{
//LOG(LOG_DEBUG, "Links: init");
if (DBGetContactSettingByte(NULL, MODULE, "EnableLinkHandling", 0)) {
//LOG(LOG_DEBUG, "Links: init - links support is on");
myspace_links_register();
myspace_links_regwatcher();
}
}
void myspace_links_destroy()
{
// LOG(LOG_DEBUG, "Links: destroy");
myspace_links_unregwatcher();
}
extern "C" void __declspec(dllexport)
CALLBACK myspace_links_exec(HWND /*hwnd*/, HINSTANCE /*hInst*/, char *lpszCmdLine, int /*nCmdShow*/)
{
EnumWindows(myspace_links_enumwindowsproc, (LPARAM) lpszCmdLine);
}
|