Clone of Akilla's acserver @ https://github.com/deregtd/ACServer

ACServer.cpp 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. // ACServer.cpp : Defines the entry point for the application.
  2. //
  3. #include "stdafx.h"
  4. #include "ACServer.h"
  5. cACServer *theServer;
  6. // Global Variables:
  7. HINSTANCE hInst; // current instance
  8. bool QuitNow = false, QuitDone = false;
  9. NOTIFYICONDATA tnid;
  10. SOCKADDR_IN DirServer;
  11. LPHOSTENT lpHostEntry = 0;
  12. DWORD DirTimerID;
  13. char ServerNameCorr[300] = { 0 };
  14. bool RecentAction = false;
  15. HANDLE DirThread;
  16. int APIENTRY WinMain(HINSTANCE hInstance,
  17. HINSTANCE hPrevInstance,
  18. LPSTR lpCmdLine,
  19. int nCmdShow)
  20. {
  21. // TODO: Place code here.
  22. MSG msg;
  23. HACCEL hAccelTable;
  24. HWND hWnd;
  25. hInst = hInstance; // Store instance handle in our global variable
  26. WSADATA wsaData;
  27. WORD wVersionRequested = 0x0202;
  28. int err;
  29. err = WSAStartup(wVersionRequested, &wsaData); //for winsock startup
  30. TCHAR szTetrisWindowClass[] = "MyInvisCharWndClass";
  31. WNDCLASS myc; ZeroMemory(&myc, sizeof(myc));
  32. myc.lpszClassName = szTetrisWindowClass;
  33. myc.hInstance = hInst;
  34. myc.lpfnWndProc = InvisWndProc;
  35. int a = RegisterClass(&myc);
  36. HWND backwindow = CreateWindow(szTetrisWindowClass, _T(""), WS_POPUP, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, hInst, NULL);
  37. hWnd = CreateDialog(hInstance,(LPCTSTR) IDD_MAINDLG, backwindow, &WndProc);
  38. SendMessage(hWnd, WM_SETICON, ICON_SMALL, (LPARAM) LoadIcon(hInst, (LPCTSTR) IDI_NOACT));
  39. SendMessage(hWnd, WM_SETICON, ICON_BIG, (LPARAM) LoadIcon(hInst, (LPCTSTR) IDI_ACSERVER));
  40. //Shell Icon Start
  41. char lpszTip[30] = "ACServer!";
  42. BOOL res;
  43. int maintimer = SetTimer(hWnd, 758, 250, NULL);
  44. tnid.cbSize = sizeof(NOTIFYICONDATA);
  45. tnid.hWnd = hWnd;
  46. tnid.uID = 4097; //uID;
  47. tnid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
  48. tnid.uCallbackMessage = WM_USER + 1000;
  49. HICON hicon = (HICON) LoadImage(hInst, (LPCSTR) IDI_NOACT, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE);
  50. tnid.hIcon = hicon;
  51. if (lpszTip)
  52. lstrcpyn(tnid.szTip, lpszTip, sizeof(tnid.szTip));
  53. else
  54. tnid.szTip[0] = '\0';
  55. res = Shell_NotifyIcon(NIM_ADD, &tnid);
  56. if (hicon)
  57. DestroyIcon(hicon);
  58. //Shell Icon End
  59. //Initialize Server Class
  60. theServer = new cACServer;
  61. theServer->MainWnd = hWnd;
  62. srand((unsigned) time(NULL));
  63. char myhost[51]; myhost[50] = 0;
  64. gethostname(myhost, 50);
  65. HOSTENT *tphost = gethostbyname(myhost);
  66. char tpip[64]; BYTE ip[4];
  67. memcpy(ip, tphost->h_addr_list[0],4);
  68. sprintf(tpip, "%i.%i.%i.%i:%i",ip[0],ip[1],ip[2],ip[3],theServer->ListenPort);
  69. SetDlgItemText(hWnd, IDC_CSIP, tpip);
  70. int tabstops[2] = {
  71. 30,
  72. 120
  73. };
  74. int ret = SendDlgItemMessage(hWnd, IDC_CHARLIST, LB_SETTABSTOPS, 2, (long) &tabstops[0]);
  75. char tps[80];
  76. sprintf(tps, "ACServer - %s", theServer->ServerName);
  77. SetWindowText(hWnd, tps);
  78. hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_ACSERVER);
  79. SetPriorityClass(GetCurrentProcess(), HIGH_PRIORITY_CLASS);
  80. SetTimer(hWnd, 0x00AC, 1000, IncTimer);
  81. while (!QuitNow)
  82. {
  83. GetMessage(&msg, NULL, 0, 0);
  84. if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
  85. {
  86. TranslateMessage(&msg);
  87. DispatchMessage(&msg);
  88. }
  89. }
  90. KillTimer(hWnd, 0x00AC);
  91. KillTimer(hWnd, 758);
  92. return msg.wParam;
  93. }
  94. VOID CALLBACK IncTimer( HWND hwnd, UINT uMsg, UINT idEvent, DWORD dwTime )
  95. {
  96. theServer->IncTimes();
  97. }
  98. DWORD WINAPI DirTimer( LPVOID lpParam )
  99. {
  100. //http://www.forotech.com/scripts/dirserv.exe
  101. // lpHostEntry = gethostbyname("localhost");
  102. lpHostEntry = gethostbyname("www.forotech.com");
  103. while (!lpHostEntry)
  104. {
  105. Sleep(3000);
  106. // lpHostEntry = gethostbyname("localhost");
  107. lpHostEntry = gethostbyname("www.forotech.com");
  108. }
  109. ZeroMemory(&DirServer, sizeof(DirServer));
  110. DirServer.sin_family = AF_INET;
  111. DirServer.sin_port = htons(80);
  112. DirServer.sin_addr = *((LPIN_ADDR)*lpHostEntry->h_addr_list);
  113. char *tpp = ServerNameCorr;
  114. for (int i=0;i<strlen(theServer->ServerName);i++)
  115. {
  116. if (theServer->ServerName[i] != ' ')
  117. {
  118. *tpp = theServer->ServerName[i];
  119. tpp++;
  120. } else {
  121. strcat(tpp, "%20");
  122. tpp+=3;
  123. }
  124. }
  125. *tpp = 0;
  126. while (!QuitNow)
  127. {
  128. if (!theServer->Private)
  129. {
  130. //Update Directory Server
  131. SOCKET DirSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  132. if (connect(DirSocket, (LPSOCKADDR) &DirServer, sizeof(sockaddr)) != SOCKET_ERROR)
  133. {
  134. char HostString[80] = { 0 };
  135. if (strlen(theServer->Host) > 0)
  136. sprintf(HostString, "&Host=%s", theServer->Host);
  137. char constring[256];
  138. sprintf(constring, "GET /scripts/dirserv.exe?Name=%s&Port=%i&Type=1&Users=%i&Admin=%s&Speed=%s%s\r\n", ServerNameCorr, theServer->ListenPort, theServer->NumConnected, theServer->Admin, theServer->Speed, HostString);
  139. send(DirSocket, constring, strlen(constring), NULL);
  140. // char lala[1000]; recv(DirSocket, lala, 1000, NULL);
  141. }
  142. closesocket(DirSocket);
  143. }
  144. for (int i=0;i<15;i++)
  145. {
  146. Sleep(1000);
  147. if (QuitNow)
  148. break;
  149. }
  150. }
  151. //Send Quit Message
  152. SOCKET DirSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  153. if (connect(DirSocket, (LPSOCKADDR) &DirServer, sizeof(sockaddr)) != SOCKET_ERROR)
  154. {
  155. char HostString[80] = { 0 };
  156. if (strlen(theServer->Host) > 0)
  157. sprintf(HostString, "&Host=%s", theServer->Host);
  158. char constring[256];
  159. sprintf(constring, "GET /scripts/dirserv.exe?Quit=1&Port=%i&%s\r\n", theServer->ListenPort, HostString);
  160. send(DirSocket, constring, strlen(constring), NULL);
  161. }
  162. closesocket(DirSocket);
  163. QuitDone = true;
  164. return(0);
  165. }
  166. int CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  167. {
  168. int wmId, wmEvent;
  169. int i;
  170. if (message == WM_USER+1)
  171. {
  172. RecentAction = true;
  173. theServer->AcceptUser();
  174. RecentAction = true;
  175. return false;
  176. }
  177. if ((message >= WM_USER+2) && (message < WM_USER+1000))
  178. {
  179. RecentAction = true;
  180. theServer->NewRead(message - WM_USER - 2);
  181. RecentAction = true;
  182. return false;
  183. }
  184. switch (message)
  185. {
  186. case WM_COMMAND:
  187. wmId = LOWORD(wParam);
  188. wmEvent = HIWORD(wParam);
  189. // Parse the menu selections:
  190. switch (wmId)
  191. {
  192. case ID_OPTIONS_CLEAROBJECTSLIST:
  193. {
  194. char tps[300];
  195. sprintf(tps, "Clearing Internal Objects List..."); theServer->LogDisp(tps);
  196. theServer->ObjectList.clear();
  197. sprintf(tps, "Deleting Objects.Dat..."); theServer->LogDisp(tps);
  198. DeleteFile("Objects.Dat");
  199. sprintf(tps, "Done!"); theServer->LogDisp(tps);
  200. }
  201. break;
  202. case ID_OPTIONS_CLEARUSERLIST:
  203. {
  204. char tps[300];
  205. sprintf(tps, "Clearing Internal User List..."); theServer->LogDisp(tps);
  206. theServer->NumPasses = 0;
  207. sprintf(tps, "Deleting Password.Dat..."); theServer->LogDisp(tps);
  208. DeleteFile("Password.Dat");
  209. sprintf(tps, "Deleting CharList.Dat..."); theServer->LogDisp(tps);
  210. DeleteFile("CharList.Dat");
  211. sprintf(tps, "Done!"); theServer->LogDisp(tps);
  212. }
  213. break;
  214. case ID_COMMANDS_STARTLISTEN:
  215. SendMessage(hWnd, WM_COMMAND, MAKELONG(IDC_STARTLISTEN, 0), 0);
  216. break;
  217. case ID_COMMANDS_SERVERMESSAGE:
  218. {
  219. HWND newdiag = CreateDialog(hInst, (LPCTSTR) IDD_SRVMSG, hWnd, SrvMsgProc);
  220. SendDlgItemMessage(newdiag, IDC_EDIT1, EM_SETLIMITTEXT, 1024, 0);
  221. }
  222. break;
  223. case ID_OPTIONS_OPENOPTIONS:
  224. {
  225. HWND newdiag = CreateDialog(hInst, (LPCTSTR) IDD_OPTIONS, hWnd, OptionsProc);
  226. SetDlgItemText(newdiag, IDC_EDIT1, theServer->ServerName);
  227. SetDlgItemInt(newdiag, IDC_EDIT2, theServer->ListenPort, false);
  228. char MOTDtp[2048]; strcpy(MOTDtp, theServer->MOTDb);
  229. for (int i=0;i<strlen(MOTDtp);i++)
  230. {
  231. if (MOTDtp[i] == '\n')
  232. MOTDtp[i-1] = '\r';
  233. }
  234. SetDlgItemText(newdiag, IDC_EDIT3, MOTDtp);
  235. SetDlgItemText(newdiag, IDC_EDIT4, theServer->Speed);
  236. SetDlgItemText(newdiag, IDC_EDIT5, theServer->Admin);
  237. SetDlgItemText(newdiag, IDC_EDIT6, theServer->Host);
  238. SendDlgItemMessage(newdiag, IDC_EDIT1, EM_SETLIMITTEXT, 64, 0);
  239. SendDlgItemMessage(newdiag, IDC_EDIT2, EM_SETLIMITTEXT, 5, 0);
  240. SendDlgItemMessage(newdiag, IDC_EDIT3, EM_SETLIMITTEXT, 2048, 0);
  241. SendDlgItemMessage(newdiag, IDC_EDIT4, EM_SETLIMITTEXT, 16, 0);
  242. SendDlgItemMessage(newdiag, IDC_EDIT5, EM_SETLIMITTEXT, 64, 0);
  243. SendDlgItemMessage(newdiag, IDC_EDIT6, EM_SETLIMITTEXT, 64, 0);
  244. SendDlgItemMessage(newdiag, IDC_CHECK1, BM_SETCHECK, BST_UNCHECKED, 0);
  245. if (theServer->Private)
  246. SendDlgItemMessage(newdiag, IDC_CHECK1, BM_SETCHECK, BST_CHECKED, 0);
  247. if (theServer->MOTDaShown)
  248. SendDlgItemMessage(newdiag, IDC_CHECK2, BM_SETCHECK, BST_CHECKED, 0);
  249. }
  250. break;
  251. case IDM_EXIT:
  252. DestroyWindow(hWnd);
  253. break;
  254. case ID_FILE_RESETLOG:
  255. SendDlgItemMessage(hWnd, IDC_LIST1, LB_RESETCONTENT, 0, 0);
  256. break;
  257. case ID_FILE_RESETALLCONNECTIONS:
  258. for (i=0;i<128;i++)
  259. theServer->DisconnectUser(i);
  260. break;
  261. case IDC_STARTLISTEN:
  262. {
  263. theServer->LoadAccounts();
  264. theServer->StartListen();
  265. HWND tpi = GetDlgItem(hWnd, IDC_STARTLISTEN);
  266. ShowWindow(tpi, SW_HIDE);
  267. SetWindowLong(tpi, GWL_STYLE, GetWindowLong(tpi, GWL_STYLE) | WS_DISABLED);
  268. HMENU tpm = GetMenu(hWnd);
  269. // int GetMenuItemID(tpm, 0);
  270. EnableMenuItem(tpm, ID_COMMANDS_STARTLISTEN, MF_DISABLED | MF_GRAYED);
  271. DirThread = CreateThread(NULL, NULL, DirTimer, NULL, NULL, &DirTimerID);
  272. // SetThreadPriority(DirThread, THREAD_PRIORITY_LOWEST);
  273. }
  274. break;
  275. case ID_FILE_KICKSELECTEDUSER:
  276. i = SendDlgItemMessage(hWnd, IDC_CHARLIST, LB_GETCURSEL, 0, 0);
  277. if ((i != LB_ERR) && (i > 0))
  278. theServer->DisconnectUser(i-1);
  279. break;
  280. case ID_KICK_USER:
  281. i = SendDlgItemMessage(hWnd, IDC_CHARLIST, LB_GETCURSEL, 0, 0);
  282. if ((i != LB_ERR) && (i > 0))
  283. theServer->DisconnectUser(i-1);
  284. break;
  285. default:
  286. return DefWindowProc(hWnd, message, wParam, lParam);
  287. }
  288. break;
  289. case WM_CONTEXTMENU:
  290. {
  291. if ((HWND) wParam == GetDlgItem(hWnd, IDC_CHARLIST))
  292. {
  293. int xPos = LOWORD(lParam); // horizontal position of cursor
  294. int yPos = HIWORD(lParam); // vertical position of cursor
  295. HMENU tpmenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_USERPOPUP));
  296. HMENU submenu = GetSubMenu(tpmenu, 0);
  297. TrackPopupMenu(submenu, TPM_LEFTALIGN, xPos, yPos, NULL, hWnd, NULL);
  298. }
  299. }
  300. break;
  301. case WM_CLOSE:
  302. DestroyWindow(hWnd);
  303. break;
  304. case WM_DESTROY:
  305. QuitNow = true;
  306. {
  307. DWORD a; GetExitCodeThread(DirThread, &a);
  308. TerminateThread(DirThread, a);
  309. }
  310. delete theServer;
  311. Shell_NotifyIcon(NIM_DELETE, &tnid);
  312. PostQuitMessage(0);
  313. break;
  314. case WM_TIMER:
  315. {
  316. HICON hicon;
  317. if (RecentAction)
  318. hicon = (HICON) LoadImage(hInst, (LPCSTR) IDI_ACT, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE);
  319. else
  320. hicon = (HICON) LoadImage(hInst, (LPCSTR) IDI_NOACT, IMAGE_ICON, 0, 0, LR_DEFAULTSIZE);
  321. tnid.hIcon = hicon;
  322. sprintf(tnid.szTip, "%s - %i/128", theServer->ServerName, theServer->NumConnected);
  323. Shell_NotifyIcon(NIM_MODIFY, &tnid);
  324. if (hicon)
  325. DestroyIcon(hicon);
  326. RecentAction = false;
  327. }
  328. break;
  329. case WM_USER+1000:
  330. switch (lParam)
  331. {
  332. case 512:
  333. //Mouseover
  334. break;
  335. case 513:
  336. //LButtondown
  337. {
  338. WINDOWPLACEMENT wp; wp.length = sizeof(wp);
  339. GetWindowPlacement(hWnd, &wp);
  340. if (wp.showCmd == SW_SHOWMINIMIZED)
  341. {
  342. ShowWindow(hWnd, SW_RESTORE);
  343. ShowWindow(hWnd, SW_SHOWNORMAL);
  344. SetForegroundWindow(hWnd);
  345. }
  346. else
  347. {
  348. if (GetForegroundWindow() != hWnd)
  349. SetForegroundWindow(hWnd);
  350. }
  351. }
  352. break;
  353. case 516:
  354. //RButtondown
  355. ShowWindow(hWnd, SW_MINIMIZE);
  356. ShowWindow(hWnd, SW_HIDE);
  357. break;
  358. }
  359. break;
  360. case WM_SIZE:
  361. if (wParam == SIZE_MINIMIZED)
  362. SendMessage(hWnd, WM_USER+1000, 0, 516);
  363. break;
  364. }
  365. return false;
  366. }
  367. long CALLBACK InvisWndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  368. {
  369. return(DefWindowProc(hWnd, message, wParam, lParam));
  370. }
  371. int CALLBACK OptionsProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  372. {
  373. int wmId, wmEvent;
  374. switch (message)
  375. {
  376. case WM_COMMAND:
  377. wmId = LOWORD(wParam);
  378. wmEvent = HIWORD(wParam);
  379. // Parse the menu selections:
  380. switch (wmId)
  381. {
  382. case IDOK:
  383. {
  384. GetDlgItemText(hWnd, IDC_EDIT4, theServer->Speed, 16);
  385. GetDlgItemText(hWnd, IDC_EDIT5, theServer->Admin, 64);
  386. GetDlgItemText(hWnd, IDC_EDIT6, theServer->Host, 64);
  387. int NewCheck = SendDlgItemMessage(hWnd, IDC_CHECK1, BM_GETCHECK, 0, 0);
  388. if (NewCheck == BST_CHECKED)
  389. theServer->Private = true;
  390. if (NewCheck == BST_UNCHECKED)
  391. theServer->Private = false;
  392. NewCheck = SendDlgItemMessage(hWnd, IDC_CHECK2, BM_GETCHECK, 0, 0);
  393. if (NewCheck == BST_CHECKED)
  394. theServer->MOTDaShown = true;
  395. if (NewCheck == BST_UNCHECKED)
  396. theServer->MOTDaShown = false;
  397. GetDlgItemText(hWnd, IDC_EDIT1, theServer->ServerName, 64);
  398. int bout;
  399. int OldListen = theServer->ListenPort;
  400. theServer->ListenPort = GetDlgItemInt(hWnd, IDC_EDIT2, &bout, false);
  401. if ((!bout) || (theServer->ListenPort > 32767))
  402. theServer->ListenPort = 9002;
  403. GetDlgItemText(hWnd, IDC_EDIT3, theServer->MOTDb, 2048);
  404. char * tpc = strchr(theServer->MOTDb,'\r');
  405. while (tpc)
  406. {
  407. *tpc = ' ';
  408. // *(tpc+1) = '\n';
  409. tpc = strchr(theServer->MOTDb,'\r');
  410. }
  411. theServer->SaveOptions();
  412. DestroyWindow(hWnd);
  413. char myhost[51]; myhost[50] = 0;
  414. gethostname(myhost, 50);
  415. HOSTENT *tphost = gethostbyname(myhost);
  416. char tpip[64]; BYTE ip[4];
  417. memcpy(ip, tphost->h_addr_list[0],4);
  418. sprintf(tpip, "%i.%i.%i.%i:%i",ip[0],ip[1],ip[2],ip[3],theServer->ListenPort);
  419. SetDlgItemText(theServer->MainWnd, IDC_CSIP, tpip);
  420. char tps[80];
  421. sprintf(tps, "ACServer - %s", theServer->ServerName);
  422. SetWindowText(theServer->MainWnd, tps);
  423. //Update Listen Port
  424. if ((theServer->Listening) && (theServer->ListenPort != OldListen))
  425. {
  426. closesocket(theServer->charSocket);
  427. struct sockaddr_in localAddress; //IP and port of the local machine
  428. localAddress.sin_family = AF_INET;
  429. localAddress.sin_addr.s_addr = inet_addr(theServer->LocalIP);
  430. localAddress.sin_port = htons(theServer->ListenPort);
  431. theServer->charSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  432. bind(theServer->charSocket, (struct sockaddr *)&localAddress, sizeof(sockaddr_in) );
  433. listen(theServer->charSocket, SOMAXCONN);
  434. char tps[300];
  435. sprintf(tps, "ACServer Relistening on port %i...", theServer->ListenPort); theServer->LogDisp(tps);
  436. //Set up event notification on main listen socket
  437. int err = WSAAsyncSelect(theServer->charSocket, hWnd, WM_USER+1, FD_ACCEPT/* | FD_READ*/);
  438. }
  439. }
  440. break;
  441. case IDCANCEL:
  442. DestroyWindow(hWnd);
  443. break;
  444. }
  445. break;
  446. }
  447. return false;
  448. }
  449. int CALLBACK SrvMsgProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  450. {
  451. int wmId, wmEvent;
  452. switch (message)
  453. {
  454. case WM_COMMAND:
  455. wmId = LOWORD(wParam);
  456. wmEvent = HIWORD(wParam);
  457. // Parse the menu selections:
  458. switch (wmId)
  459. {
  460. case IDOK:
  461. {
  462. char SrvMsg[1024];
  463. GetDlgItemText(hWnd, IDC_EDIT1, SrvMsg, 1024);
  464. theServer->ServerMessage(GLOBAL_MESSAGE, SrvMsg, COLOR_GREEN);
  465. DestroyWindow(hWnd);
  466. }
  467. break;
  468. case IDCANCEL:
  469. DestroyWindow(hWnd);
  470. break;
  471. }
  472. break;
  473. }
  474. return false;
  475. }