Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

521 lines
14 KiB

  1. #include "isignup.h"
  2. #include "dialutil.h"
  3. #include "autodial.h"
  4. #define NUMRETRIES 3
  5. #define MAXHANGUPDELAY 20
  6. #define ONE_SECOND 1000
  7. #define TIMER_ID 0
  8. #define SMALLBUFLEN 80
  9. static HRASCONN g_hRasConn = NULL;
  10. static UINT g_cDialAttempts = 0;
  11. static UINT g_cHangupDelay = 0;
  12. static TCHAR g_szEntryName[RAS_MaxEntryName + 1] = TEXT("");
  13. static const TCHAR szBrowserClass1[] = TEXT("IExplorer_Frame");
  14. static const TCHAR szBrowserClass2[] = TEXT("Internet Explorer_Frame");
  15. static const TCHAR szBrowserClass3[] = TEXT("IEFrame");
  16. static DWORD AutoDialConnect(HWND hDlg, LPRASDIALPARAMS lpDialParams);
  17. static BOOL AutoDialEvent(HWND hDlg, RASCONNSTATE state, LPDWORD lpdwError);
  18. static VOID SetDialogTitle(HWND hDlg, LPCTSTR pszConnectoidName);
  19. static HWND FindBrowser(void);
  20. static UINT RetryMessage(HWND hDlg, DWORD dwError);
  21. #ifdef WIN16
  22. extern "C" BOOL CALLBACK __export AutoDialDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM Param);
  23. extern "C" BOOL CALLBACK __export PhoneNumberDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM Param);
  24. extern "C" BOOL CALLBACK __export RetryDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM Param);
  25. #else
  26. INT_PTR CALLBACK AutoDialDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM Param);
  27. INT_PTR CALLBACK PhoneNumberDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM Param);
  28. INT_PTR CALLBACK RetryDlgProc(HWND hDlg, UINT uMsg, WPARAM wParam, LPARAM Param);
  29. #endif
  30. #ifdef UNICODE
  31. BOOL WINAPI AutoDialSignupW(HWND, LPCTSTR, DWORD, LPDWORD);
  32. BOOL WINAPI AutoDialSignupA
  33. (
  34. HWND hwndParent,
  35. LPCSTR lpszEntry,
  36. DWORD dwFlags,
  37. LPDWORD pdwRetCode
  38. )
  39. {
  40. TCHAR szEntry[RAS_MaxEntryName + 1];
  41. mbstowcs(szEntry, lpszEntry, lstrlenA(lpszEntry)+1);
  42. return AutoDialSignupW(hwndParent, szEntry, dwFlags, pdwRetCode);
  43. }
  44. BOOL WINAPI AutoDialSignupW
  45. #else
  46. BOOL WINAPI AutoDialSignupA
  47. #endif
  48. (
  49. HWND hwndParent,
  50. LPCTSTR lpszEntry,
  51. DWORD dwFlags,
  52. LPDWORD pdwRetCode
  53. )
  54. {
  55. LoadRnaFunctions(hwndParent);
  56. lstrcpyn(g_szEntryName, lpszEntry, SIZEOF_TCHAR_BUFFER(g_szEntryName));
  57. if (DialogBoxParam(
  58. ghInstance,
  59. TEXT("AutoDial"),
  60. hwndParent,
  61. AutoDialDlgProc,
  62. (DWORD_PTR)pdwRetCode) == -1)
  63. {
  64. *pdwRetCode = ERROR_CANCELLED;
  65. }
  66. if (ERROR_SUCCESS != *pdwRetCode)
  67. {
  68. HWND hwndBrowser;
  69. hwndBrowser = FindBrowser();
  70. if (NULL != hwndBrowser)
  71. {
  72. SendMessage(hwndBrowser, WM_CLOSE, 0, 0);
  73. }
  74. }
  75. UnloadRnaFunctions();
  76. return TRUE;
  77. }
  78. #ifdef WIN16
  79. extern "C" BOOL CALLBACK __export AutoDialDlgProc(
  80. #else
  81. INT_PTR CALLBACK AutoDialDlgProc(
  82. #endif
  83. HWND hDlg,
  84. UINT uMsg,
  85. WPARAM wParam,
  86. LPARAM lParam
  87. )
  88. {
  89. static LPDWORD lpdwRet;
  90. static UINT uEventMsg;
  91. static LPRASDIALPARAMS lpDialParams = NULL;
  92. BOOL fPassword;
  93. switch (uMsg)
  94. {
  95. case WM_INITDIALOG:
  96. lpdwRet = (LPDWORD)lParam;
  97. SetWindowPos(
  98. hDlg,
  99. HWND_TOPMOST,
  100. 0, 0, 0, 0,
  101. SWP_NOMOVE | SWP_NOSIZE);
  102. g_cDialAttempts = 0;
  103. SetDialogTitle(hDlg, g_szEntryName);
  104. CenterWindow(hDlg, GetParent(hDlg));
  105. uEventMsg = RegisterWindowMessageA( RASDIALEVENT );
  106. if (0 == uEventMsg)
  107. {
  108. uEventMsg = WM_RASDIALEVENT;
  109. }
  110. g_hRasConn = NULL;
  111. lpDialParams = (LPRASDIALPARAMS)LocalAlloc(LPTR, sizeof(RASDIALPARAMS));
  112. if (NULL == lpDialParams)
  113. {
  114. *lpdwRet = ERROR_OUTOFMEMORY;
  115. return FALSE;
  116. }
  117. lpDialParams->dwSize = sizeof(RASDIALPARAMS);
  118. lstrcpyn(lpDialParams->szEntryName, g_szEntryName, SIZEOF_TCHAR_BUFFER(lpDialParams->szEntryName));
  119. *lpdwRet = lpfnRasGetEntryDialParams(
  120. NULL,
  121. lpDialParams,
  122. &fPassword);
  123. if (ERROR_SUCCESS == *lpdwRet)
  124. {
  125. lstrcpyn(lpDialParams->szPassword, GetPassword(),
  126. SIZEOF_TCHAR_BUFFER(lpDialParams->szPassword));
  127. GetPhoneNumber(g_szEntryName, lpDialParams->szPhoneNumber);
  128. if (DialogBoxParam(
  129. ghInstance,
  130. TEXT("PhoneNumber"),
  131. hDlg,
  132. PhoneNumberDlgProc,
  133. (LPARAM)GetDisplayPhone(lpDialParams->szPhoneNumber)) != IDOK)
  134. {
  135. *lpdwRet = ERROR_USER_DISCONNECTION;
  136. EndDialog(hDlg, 0);
  137. }
  138. else
  139. {
  140. PostMessage(hDlg, WM_COMMAND, IDC_CONNECT, 0);
  141. *lpdwRet = ERROR_SUCCESS;
  142. }
  143. }
  144. if (ERROR_SUCCESS != *lpdwRet)
  145. {
  146. if (NULL != lpDialParams)
  147. {
  148. LocalFree(lpDialParams);
  149. lpDialParams = NULL;
  150. }
  151. EndDialog(hDlg, 0);
  152. }
  153. return TRUE;
  154. case WM_COMMAND:
  155. switch (wParam)
  156. {
  157. case IDC_CONNECT:
  158. ++g_cDialAttempts;
  159. ShowWindow(hDlg, SW_NORMAL);
  160. *lpdwRet = lpfnRasDial(
  161. NULL,
  162. NULL,
  163. lpDialParams,
  164. (DWORD) -1,
  165. (LPVOID)hDlg,
  166. &g_hRasConn);
  167. if (ERROR_SUCCESS != *lpdwRet)
  168. {
  169. EndDialog(hDlg, 0);
  170. }
  171. return TRUE;
  172. case IDCANCEL:
  173. if (NULL != g_hRasConn)
  174. {
  175. lpfnRasHangUp(g_hRasConn);
  176. g_hRasConn = NULL;
  177. }
  178. *lpdwRet = ERROR_USER_DISCONNECTION;
  179. EndDialog(hDlg, 0);
  180. return TRUE;
  181. case IDOK:
  182. ShowWindow(hDlg, SW_HIDE);
  183. if (MinimizeRNAWindow(g_szEntryName))
  184. {
  185. EndDialog(hDlg, 0);
  186. }
  187. else
  188. {
  189. // try again later
  190. SetTimer(hDlg, TIMER_ID, ONE_SECOND, NULL);
  191. }
  192. return TRUE;
  193. default:
  194. break;
  195. }
  196. case WM_TIMER:
  197. if (ERROR_SUCCESS == *lpdwRet)
  198. {
  199. // try to minimize one more time
  200. MinimizeRNAWindow(g_szEntryName);
  201. EndDialog(hDlg, 0);
  202. }
  203. else if (NULL != g_hRasConn)
  204. {
  205. DWORD dwRet;
  206. RASCONNSTATUS status;
  207. status.dwSize = sizeof(RASCONNSTATUS);
  208. dwRet = lpfnRasGetConnectStatus(g_hRasConn, &status);
  209. if (ERROR_INVALID_HANDLE != dwRet)
  210. {
  211. if (0 == --g_cHangupDelay)
  212. {
  213. // wait no longer
  214. EndDialog(hDlg, 0);
  215. }
  216. return TRUE;
  217. }
  218. // hang up is complete
  219. g_hRasConn = NULL;
  220. PostMessage(hDlg, WM_COMMAND, IDC_CONNECT, 0);
  221. }
  222. KillTimer(hDlg, TIMER_ID);
  223. return TRUE;
  224. case WM_DESTROY:
  225. if (NULL != lpDialParams)
  226. {
  227. LocalFree(lpDialParams);
  228. lpDialParams = NULL;
  229. }
  230. return TRUE;
  231. default:
  232. if (uMsg == uEventMsg)
  233. {
  234. *lpdwRet = (DWORD)lParam;
  235. // AutoDialEvent returns TRUE if complete
  236. if (AutoDialEvent(hDlg, (RASCONNSTATE)wParam, lpdwRet))
  237. {
  238. if (ERROR_SUCCESS == *lpdwRet)
  239. {
  240. // we can't just exit, if we do we won't minimize window
  241. PostMessage(hDlg, WM_COMMAND, IDOK, 0);
  242. }
  243. else
  244. {
  245. EndDialog(hDlg, 0);
  246. }
  247. }
  248. return TRUE;
  249. }
  250. }
  251. return FALSE;
  252. }
  253. static BOOL AutoDialEvent(HWND hDlg, RASCONNSTATE state, LPDWORD lpdwError)
  254. {
  255. TCHAR szMessage[SMALLBUFLEN + 1];
  256. _RasGetStateString(state, szMessage, SIZEOF_TCHAR_BUFFER(szMessage));
  257. SetDlgItemText(hDlg, IDC_STATUS, szMessage);
  258. if (ERROR_SUCCESS == *lpdwError)
  259. {
  260. if (state & RASCS_DONE)
  261. {
  262. // we're done dialing
  263. return TRUE;
  264. }
  265. }
  266. else
  267. {
  268. // looks like we got an error
  269. // if we haven't hungup already, hangup
  270. if (NULL != g_hRasConn)
  271. {
  272. g_cHangupDelay = MAXHANGUPDELAY;
  273. lpfnRasHangUp(g_hRasConn);
  274. }
  275. if ((ERROR_LINE_BUSY == *lpdwError) && (g_cDialAttempts < NUMRETRIES))
  276. {
  277. LoadString(ghInstance, IDS_BUSYREDIAL, szMessage, SIZEOF_TCHAR_BUFFER(szMessage));
  278. SetDlgItemText(hDlg, IDC_STATUS, szMessage);
  279. SetTimer(hDlg, TIMER_ID, ONE_SECOND, NULL);
  280. }
  281. else
  282. {
  283. TCHAR szError[160];
  284. TCHAR szFmt[80];
  285. TCHAR szMessage[240];
  286. ShowWindow(hDlg, SW_HIDE);
  287. #if 1
  288. lpfnRasGetErrorString(
  289. (int)*lpdwError,
  290. szError,
  291. sizeof(szError));
  292. LoadString(ghInstance, IDS_RETRY, szFmt, SIZEOF_TCHAR_BUFFER(szFmt));
  293. wsprintf(szMessage, szFmt, szError);
  294. if (MessageBox(
  295. hDlg,
  296. szMessage,
  297. g_szEntryName,
  298. MB_ICONEXCLAMATION |
  299. MB_RETRYCANCEL) == IDRETRY)
  300. #else
  301. if (DialogBoxParam(
  302. ghInstance,
  303. TEXT("Retry"),
  304. hDlg,
  305. RetryDlgProc,
  306. (LPARAM)*lpdwError) == IDRETRY)
  307. #endif
  308. {
  309. ShowWindow(hDlg, SW_SHOW);
  310. LoadString(ghInstance, IDS_REDIAL, szMessage, SIZEOF_TCHAR_BUFFER(szMessage));
  311. SetDlgItemText(hDlg, IDC_STATUS, szMessage);
  312. g_cDialAttempts = 0;
  313. SetTimer(hDlg, TIMER_ID, ONE_SECOND, NULL);
  314. }
  315. else
  316. {
  317. // user cancelled
  318. *lpdwError = ERROR_USER_DISCONNECTION;
  319. return TRUE;
  320. }
  321. }
  322. }
  323. // keep dialing
  324. return FALSE;
  325. }
  326. static VOID SetDialogTitle(HWND hDlg, LPCTSTR lpszConnectoidName)
  327. {
  328. TCHAR szFmt[SMALLBUFLEN + 1];
  329. TCHAR szTitle[RAS_MaxEntryName + SMALLBUFLEN + 1];
  330. // load the title format ("connecting to <connectoid name>" from resource
  331. LoadString(ghInstance, IDS_CONNECTING_TO, szFmt, SIZEOF_TCHAR_BUFFER(szFmt));
  332. // build the title
  333. wsprintf(szTitle, szFmt, lpszConnectoidName);
  334. SetWindowText(hDlg, szTitle);
  335. }
  336. static HWND FindBrowser(void)
  337. {
  338. HWND hwnd;
  339. //look for all the microsoft browsers under the sun
  340. if ((hwnd = FindWindow(szBrowserClass1, NULL)) == NULL)
  341. {
  342. if ((hwnd = FindWindow(szBrowserClass2, NULL)) == NULL)
  343. {
  344. hwnd = FindWindow(szBrowserClass3, NULL);
  345. }
  346. }
  347. return hwnd;
  348. }
  349. #ifdef WIN16
  350. extern "C" BOOL CALLBACK __export RetryDlgProc(
  351. #else
  352. INT_PTR CALLBACK RetryDlgProc(
  353. #endif
  354. HWND hDlg,
  355. UINT uMsg,
  356. WPARAM wParam,
  357. LPARAM lParam
  358. )
  359. {
  360. switch (uMsg)
  361. {
  362. case WM_INITDIALOG:
  363. {
  364. TCHAR szMessage[SMALLBUFLEN + 1];
  365. lpfnRasGetErrorString(
  366. (int)lParam,
  367. szMessage,
  368. sizeof(szMessage));
  369. SetDlgItemText(hDlg, IDC_ERROR, szMessage);
  370. SetWindowText(hDlg, g_szEntryName);
  371. CenterWindow(hDlg, GetParent(hDlg));
  372. return TRUE;
  373. }
  374. case WM_COMMAND:
  375. switch (wParam)
  376. {
  377. case IDRETRY:
  378. case IDCANCEL:
  379. EndDialog(hDlg, wParam);
  380. return TRUE;
  381. default:
  382. break;
  383. }
  384. default:
  385. break;
  386. }
  387. return FALSE;
  388. }
  389. #ifdef WIN16
  390. extern "C" BOOL CALLBACK __export PhoneNumberDlgProc(
  391. #else
  392. INT_PTR CALLBACK PhoneNumberDlgProc(
  393. #endif
  394. HWND hDlg,
  395. UINT uMsg,
  396. WPARAM wParam,
  397. LPARAM lParam
  398. )
  399. {
  400. static LPTSTR lpszPhone;
  401. switch (uMsg)
  402. {
  403. case WM_INITDIALOG:
  404. lpszPhone = (LPTSTR)lParam;
  405. SetDlgItemText(
  406. hDlg,
  407. IDC_PHONENUMBER,
  408. lpszPhone);
  409. SetWindowText(hDlg, g_szEntryName);
  410. CenterWindow(hDlg, GetParent(hDlg));
  411. return TRUE;
  412. case WM_COMMAND:
  413. switch (wParam)
  414. {
  415. case IDOK:
  416. GetDlgItemText(
  417. hDlg,
  418. IDC_PHONENUMBER,
  419. lpszPhone,
  420. RAS_MaxPhoneNumber - 5);
  421. case IDCANCEL:
  422. EndDialog(hDlg, wParam);
  423. return TRUE;
  424. default:
  425. break;
  426. }
  427. default:
  428. break;
  429. }
  430. return FALSE;
  431. }