main.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. /*
  2. * winebrowser - winelib app to launch native OS browser or mail client.
  3. *
  4. * Copyright (C) 2004 Chris Morgan
  5. * Copyright (C) 2005 Hans Leidekker
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
  20. *
  21. * NOTES:
  22. * Winebrowser is a winelib application that will start the appropriate
  23. * native browser or mail client for a wine installation that lacks a
  24. * windows browser/mail client. For example, you will be able to open
  25. * URLs via native mozilla if no browser has yet been installed in wine.
  26. *
  27. * The application to launch is chosen from a default set or, if set,
  28. * taken from a registry key.
  29. *
  30. * The argument may be a regular Windows file name, a file URL, an
  31. * URL or a mailto URL. In the first three cases the argument
  32. * will be fed to a web browser. In the last case the argument is fed
  33. * to a mail client. A mailto URL is composed as follows:
  34. *
  35. * mailto:[E-MAIL]?subject=[TOPIC]&cc=[E-MAIL]&bcc=[E-MAIL]&body=[TEXT]
  36. */
  37. #define WIN32_LEAN_AND_MEAN
  38. #define COBJMACROS
  39. #include <stdio.h>
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <ntstatus.h>
  43. #define WIN32_NO_STATUS
  44. #include <windows.h>
  45. #include <winternl.h>
  46. #include <shlwapi.h>
  47. #include <shellapi.h>
  48. #include <urlmon.h>
  49. #include <ddeml.h>
  50. #include "wine/debug.h"
  51. WINE_DEFAULT_DEBUG_CHANNEL(winebrowser);
  52. static char *strdup_unixcp( const WCHAR *str )
  53. {
  54. char *ret;
  55. int len = WideCharToMultiByte( CP_UNIXCP, 0, str, -1, NULL, 0, NULL, NULL );
  56. if ((ret = malloc( len )))
  57. WideCharToMultiByte( CP_UNIXCP, 0, str, -1, ret, len, NULL, NULL );
  58. return ret;
  59. }
  60. /* try to launch a unix app from a comma separated string of app names */
  61. static int launch_app( const WCHAR *candidates, const WCHAR *argv1 )
  62. {
  63. char *cmdline;
  64. int i, count;
  65. char **argv_new;
  66. if (!(cmdline = strdup_unixcp( argv1 ))) return 1;
  67. while (*candidates)
  68. {
  69. WCHAR **args = CommandLineToArgvW( candidates, &count );
  70. if (!(argv_new = malloc( (count + 2) * sizeof(*argv_new) ))) break;
  71. for (i = 0; i < count; i++) argv_new[i] = strdup_unixcp( args[i] );
  72. argv_new[count] = cmdline;
  73. argv_new[count + 1] = NULL;
  74. TRACE( "Trying" );
  75. for (i = 0; i <= count; i++) TRACE( " %s", wine_dbgstr_a( argv_new[i] ));
  76. TRACE( "\n" );
  77. if (!__wine_unix_spawnvp( argv_new, FALSE )) ExitProcess(0);
  78. for (i = 0; i < count; i++) free( argv_new[i] );
  79. free( argv_new );
  80. candidates += lstrlenW( candidates ) + 1; /* grab the next app */
  81. }
  82. WINE_ERR( "could not find a suitable app to open %s\n", debugstr_w( argv1 ));
  83. free( cmdline );
  84. return 1;
  85. }
  86. static LSTATUS get_commands( HKEY key, const WCHAR *value, WCHAR *buffer, DWORD size )
  87. {
  88. DWORD type;
  89. LSTATUS res;
  90. size -= sizeof(WCHAR);
  91. if (!(res = RegQueryValueExW( key, value, 0, &type, (LPBYTE)buffer, &size )) && (type == REG_SZ))
  92. {
  93. /* convert to REG_MULTI_SZ type */
  94. WCHAR *p = buffer;
  95. p[lstrlenW(p) + 1] = 0;
  96. while ((p = wcschr( p, ',' ))) *p++ = 0;
  97. }
  98. return res;
  99. }
  100. static int open_http_url( const WCHAR *url )
  101. {
  102. static const WCHAR defaultbrowsers[] =
  103. L"xdg-open\0"
  104. "/usr/bin/open\0"
  105. "firefox\0"
  106. "konqueror\0"
  107. "mozilla\0"
  108. "opera\0"
  109. "dillo\0";
  110. WCHAR browsers[256];
  111. HKEY key;
  112. LONG r;
  113. /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
  114. if (!(r = RegOpenKeyW( HKEY_CURRENT_USER, L"Software\\Wine\\WineBrowser", &key )))
  115. {
  116. r = get_commands( key, L"Browsers", browsers, sizeof(browsers) );
  117. RegCloseKey( key );
  118. }
  119. if (r != ERROR_SUCCESS)
  120. memcpy( browsers, defaultbrowsers, sizeof(defaultbrowsers) );
  121. return launch_app( browsers, url );
  122. }
  123. static int open_mailto_url( const WCHAR *url )
  124. {
  125. static const WCHAR defaultmailers[] =
  126. L"/usr/bin/open\0"
  127. "xdg-email\0"
  128. "mozilla-thunderbird\0"
  129. "thunderbird\0"
  130. "evolution\0";
  131. WCHAR mailers[256];
  132. HKEY key;
  133. LONG r;
  134. /* @@ Wine registry key: HKCU\Software\Wine\WineBrowser */
  135. if (!(r = RegOpenKeyW( HKEY_CURRENT_USER, L"Software\\Wine\\WineBrowser", &key )))
  136. {
  137. r = get_commands( key, L"Mailers", mailers, sizeof(mailers) );
  138. RegCloseKey( key );
  139. }
  140. if (r != ERROR_SUCCESS)
  141. memcpy( mailers, defaultmailers, sizeof(defaultmailers) );
  142. return launch_app( mailers, url );
  143. }
  144. static int open_invalid_url( const WCHAR *url )
  145. {
  146. WCHAR *url_prefixed;
  147. int ret;
  148. url_prefixed = malloc( (ARRAY_SIZE(L"http://") + lstrlenW( url )) * sizeof(WCHAR) );
  149. if (!url_prefixed)
  150. {
  151. WINE_ERR("Out of memory\n");
  152. return 1;
  153. }
  154. lstrcpyW( url_prefixed, L"http://" );
  155. lstrcatW( url_prefixed, url );
  156. ret = open_http_url( url_prefixed );
  157. free( url_prefixed );
  158. return ret;
  159. }
  160. /*****************************************************************************
  161. * DDE helper functions.
  162. */
  163. static WCHAR *ddeString = NULL;
  164. static HSZ hszTopic = 0, hszReturn = 0;
  165. static DWORD ddeInst = 0;
  166. /* Dde callback, save the execute or request string for processing */
  167. static HDDEDATA CALLBACK ddeCb(UINT uType, UINT uFmt, HCONV hConv,
  168. HSZ hsz1, HSZ hsz2, HDDEDATA hData,
  169. ULONG_PTR dwData1, ULONG_PTR dwData2)
  170. {
  171. DWORD size = 0, ret = 0;
  172. WINE_TRACE("dde_cb: %04x, %04x, %p, %p, %p, %p, %08lx, %08lx\n",
  173. uType, uFmt, hConv, hsz1, hsz2, hData, dwData1, dwData2);
  174. switch (uType)
  175. {
  176. case XTYP_CONNECT:
  177. if (!DdeCmpStringHandles(hsz1, hszTopic))
  178. return (HDDEDATA)TRUE;
  179. return (HDDEDATA)FALSE;
  180. case XTYP_EXECUTE:
  181. if (!(size = DdeGetData(hData, NULL, 0, 0)))
  182. WINE_ERR("DdeGetData returned zero size of execute string\n");
  183. else if (!(ddeString = malloc(size)))
  184. WINE_ERR("Out of memory\n");
  185. else if (DdeGetData(hData, (LPBYTE)ddeString, size, 0) != size)
  186. WINE_WARN("DdeGetData did not return %d bytes\n", size);
  187. DdeFreeDataHandle(hData);
  188. return (HDDEDATA)DDE_FACK;
  189. case XTYP_REQUEST:
  190. ret = -3; /* error */
  191. if (!(size = DdeQueryStringW(ddeInst, hsz2, NULL, 0, CP_WINUNICODE)))
  192. WINE_ERR("DdeQueryString returned zero size of request string\n");
  193. else if (!(ddeString = malloc( (size + 1) * sizeof(WCHAR))))
  194. WINE_ERR("Out of memory\n");
  195. else if (DdeQueryStringW(ddeInst, hsz2, ddeString, size + 1, CP_WINUNICODE) != size)
  196. WINE_WARN("DdeQueryString did not return %d characters\n", size);
  197. else
  198. ret = -2; /* acknowledgment */
  199. return DdeCreateDataHandle(ddeInst, (LPBYTE)&ret, sizeof(ret), 0,
  200. hszReturn, CF_TEXT, 0);
  201. default:
  202. return NULL;
  203. }
  204. }
  205. static WCHAR *get_url_from_dde(void)
  206. {
  207. HSZ hszApplication = 0;
  208. UINT_PTR timer = 0;
  209. int rc;
  210. WCHAR *ret = NULL;
  211. rc = DdeInitializeW(&ddeInst, ddeCb, CBF_SKIP_ALLNOTIFICATIONS | CBF_FAIL_ADVISES | CBF_FAIL_POKES, 0);
  212. if (rc != DMLERR_NO_ERROR)
  213. {
  214. WINE_ERR("Unable to initialize DDE, DdeInitialize returned %d\n", rc);
  215. goto done;
  216. }
  217. hszApplication = DdeCreateStringHandleW(ddeInst, L"IExplore", CP_WINUNICODE);
  218. if (!hszApplication)
  219. {
  220. WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
  221. goto done;
  222. }
  223. hszTopic = DdeCreateStringHandleW(ddeInst, L"WWW_OpenURL", CP_WINUNICODE);
  224. if (!hszTopic)
  225. {
  226. WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
  227. goto done;
  228. }
  229. hszReturn = DdeCreateStringHandleW(ddeInst, L"Return", CP_WINUNICODE);
  230. if (!hszReturn)
  231. {
  232. WINE_ERR("Unable to initialize DDE, DdeCreateStringHandle failed\n");
  233. goto done;
  234. }
  235. if (!DdeNameService(ddeInst, hszApplication, 0, DNS_REGISTER))
  236. {
  237. WINE_ERR("Unable to initialize DDE, DdeNameService failed\n");
  238. goto done;
  239. }
  240. timer = SetTimer(NULL, 0, 5000, NULL);
  241. if (!timer)
  242. {
  243. WINE_ERR("SetTimer failed to create timer\n");
  244. goto done;
  245. }
  246. while (!ddeString)
  247. {
  248. MSG msg;
  249. if (!GetMessageW(&msg, NULL, 0, 0)) break;
  250. if (msg.message == WM_TIMER) break;
  251. DispatchMessageW(&msg);
  252. }
  253. if (ddeString)
  254. {
  255. if (*ddeString == '"')
  256. {
  257. WCHAR *endquote = wcschr(ddeString + 1, '"');
  258. if (!endquote)
  259. {
  260. WINE_ERR("Unable to retrieve URL from string %s\n", wine_dbgstr_w(ddeString));
  261. goto done;
  262. }
  263. *endquote = 0;
  264. ret = ddeString+1;
  265. }
  266. else
  267. ret = ddeString;
  268. }
  269. done:
  270. if (timer) KillTimer(NULL, timer);
  271. if (ddeInst)
  272. {
  273. if (hszTopic && hszApplication) DdeNameService(ddeInst, hszApplication, 0, DNS_UNREGISTER);
  274. if (hszReturn) DdeFreeStringHandle(ddeInst, hszReturn);
  275. if (hszTopic) DdeFreeStringHandle(ddeInst, hszTopic);
  276. if (hszApplication) DdeFreeStringHandle(ddeInst, hszApplication);
  277. DdeUninitialize(ddeInst);
  278. }
  279. return ret;
  280. }
  281. static WCHAR *encode_unix_path(const char *src)
  282. {
  283. WCHAR *dst, *tmp_dst;
  284. static const char safe_chars[] = "/-_.~@&=+$,:";
  285. static const char hex_digits[] = "0123456789ABCDEF";
  286. dst = malloc( sizeof(L"file://") + 3 * strlen(src) * sizeof(WCHAR) );
  287. lstrcpyW(dst, L"file://");
  288. tmp_dst = dst + lstrlenW(dst);
  289. while (*src != 0)
  290. {
  291. if ((*src >= 'a' && *src <= 'z') ||
  292. (*src >= 'A' && *src <= 'Z') ||
  293. (*src >= '0' && *src <= '9') ||
  294. strchr(safe_chars, *src))
  295. {
  296. *tmp_dst++ = *src;
  297. }
  298. else
  299. {
  300. *tmp_dst++ = '%';
  301. *tmp_dst++ = hex_digits[*(unsigned char*)src / 16];
  302. *tmp_dst++ = hex_digits[*src & 0xf];
  303. }
  304. src++;
  305. }
  306. *tmp_dst = 0;
  307. return dst;
  308. }
  309. static WCHAR *convert_file_uri(IUri *uri)
  310. {
  311. UNICODE_STRING nt_name;
  312. OBJECT_ATTRIBUTES attr;
  313. NTSTATUS status;
  314. ULONG size = 256;
  315. char *buffer;
  316. WCHAR *new_path = NULL;
  317. BSTR filename;
  318. HRESULT hres;
  319. hres = IUri_GetPath(uri, &filename);
  320. if(FAILED(hres))
  321. return NULL;
  322. WINE_TRACE("Windows path: %s\n", wine_dbgstr_w(filename));
  323. if (!RtlDosPathNameToNtPathName_U( filename, &nt_name, NULL, NULL )) return NULL;
  324. InitializeObjectAttributes( &attr, &nt_name, 0, 0, NULL );
  325. for (;;)
  326. {
  327. if (!(buffer = malloc( size )))
  328. {
  329. RtlFreeUnicodeString( &nt_name );
  330. return NULL;
  331. }
  332. status = wine_nt_to_unix_file_name( &attr, buffer, &size, FILE_OPEN );
  333. if (status != STATUS_BUFFER_TOO_SMALL) break;
  334. free( buffer );
  335. }
  336. if (!status) new_path = encode_unix_path( buffer );
  337. SysFreeString(filename);
  338. free( buffer );
  339. WINE_TRACE("New path: %s\n", wine_dbgstr_w(new_path));
  340. return new_path;
  341. }
  342. /*****************************************************************************
  343. * Main entry point. This is a console application so we have a wmain() not a
  344. * winmain().
  345. */
  346. int wmain(int argc, WCHAR *argv[])
  347. {
  348. WCHAR *url = argv[1];
  349. BSTR display_uri = NULL;
  350. DWORD scheme;
  351. IUri *uri;
  352. HRESULT hres;
  353. /* DDE used only if -nohome is specified; avoids delay in printing usage info
  354. * when no parameters are passed */
  355. if (url && !wcsicmp( url, L"-nohome" ))
  356. url = argc > 2 ? argv[2] : get_url_from_dde();
  357. if (!url) {
  358. WINE_ERR( "Usage: winebrowser URL\n" );
  359. return -1;
  360. }
  361. hres = CreateUri(url, Uri_CREATE_ALLOW_IMPLICIT_FILE_SCHEME|Uri_CREATE_FILE_USE_DOS_PATH, 0, &uri);
  362. if(FAILED(hres)) {
  363. WINE_ERR("Failed to parse URL %s, treating as HTTP\n", wine_dbgstr_w(url));
  364. return open_invalid_url(url);
  365. }
  366. free(ddeString);
  367. IUri_GetScheme(uri, &scheme);
  368. if(scheme == URL_SCHEME_FILE) {
  369. display_uri = convert_file_uri(uri);
  370. if(!display_uri) {
  371. WINE_ERR("Failed to convert file URL to unix path\n");
  372. }
  373. }
  374. if (!display_uri)
  375. hres = IUri_GetDisplayUri(uri, &display_uri);
  376. IUri_Release(uri);
  377. if(FAILED(hres))
  378. return -1;
  379. WINE_TRACE("opening %s\n", wine_dbgstr_w(display_uri));
  380. if(scheme == URL_SCHEME_MAILTO)
  381. return open_mailto_url(display_uri);
  382. else
  383. /* let the browser decide how to handle the given url */
  384. return open_http_url(display_uri);
  385. }