/* Program: MOM.CPP Author : Kim Moser Date : August 21st, 1992 System : IBM PC / Borland C++ 3.0 / Windows 3.1 Descrip: Magpie Offline Mailer */ #include #include #include #include #include // exit() #include #include "mom.h" //======================================================================== static int WriteComm(int nCid, char* s); static int WriteComm(int nCid, char* s) { return (WriteComm(nCid, s, strlen(s))); } #ifdef __cplusplus inline int max(int a, int b); inline int max(int a, int b) { return (a > b ? a : b); } inline int min(int a, int b); inline int min(int a, int b) { return (a < b ? a : b); } #endif //======================================================================== static BOOL InitWin(HANDLE hInstance); static long FAR PASCAL MomWndProc(HWND hWnd, unsigned message, WORD wParam, LONG lParam); //------------------------------------------------------------------------ static char COM_STRING[] = "COM1"; // '1' may get changed in config(). static int COMM_PORT_ID; // Handle of comm port static char BUFFER[512]; // Poll() fills this up when reading from comm port static int next_avail=0; // Index of next avail spot in BUFFER static int CONNECTED = 0; // When non-zero, Poll() polls comm port static ofstream LOGFILE; static int Poll(void); static int Poll(void) { COMSTAT stat; int r=0; if (CONNECTED) { if (ReadComm(COMM_PORT_ID, (LPSTR) (BUFFER+next_avail), 1) > 0) { LOGFILE << BUFFER[next_avail]; if (++next_avail >= sizeof(BUFFER)) { next_avail = 0; } } r = GetCommError(COMM_PORT_ID, (COMSTAT FAR *) &stat); } return (r); } //------------------------------------------------------------------------ #ifdef __BORLANDC__ // Suppress "Parameter 'lpszCmdLine' is never used" warning #pragma argsused #endif int PASCAL WinMain(HANDLE hInstance, HANDLE hPrevInstance, LPSTR lpszCmdLine, int cmdShow) { MSG msg; // Message (duh) HWND hWnd; // Window's handle if (!hPrevInstance) { // Init first instance if (!InitWin(hInstance)) return FALSE; } hWnd = CreateWindow("Mom", // Win class name "Magpie Offline Mailer", // Win caption WS_OVERLAPPEDWINDOW, // Win style CW_USEDEFAULT, // Upper-left x pos 0, // Upper-left y pos CW_USEDEFAULT, // Initial x-size 0, // Initial y-size NULL, // No parent window LoadMenu(hInstance, "MomMenuMain"), // Window menu hInstance, // Application instance NULL); // No creation params ShowWindow(hWnd, cmdShow); // Make win visible UpdateWindow(hWnd); // Update on screen while (1) { if (PeekMessage((LPMSG) &msg, hWnd, 0, 0, PM_NOREMOVE)) { // Msg(s) avail if (GetMessage(&msg, NULL, 0, 0)) { // Process msgs if ((hWnd == NULL) || (!IsDialogMessage(hWnd, &msg))) { TranslateMessage(&msg); DispatchMessage(&msg); } } else { break; } } else { // No msg(s) avail if (Poll()) { MessageBox(hWnd, (LPSTR) "Poll() failed.", NULL, MB_ICONSTOP | MB_OK); } } } return ((int) (msg.wParam)); } static BOOL InitWin(HANDLE hInstance) { WNDCLASS wcMomClass; wcMomClass.hCursor = LoadCursor(NULL, IDC_ARROW); // Mouse crsr wcMomClass.hIcon = LoadIcon(hInstance, (LPSTR) "MomIcon"); wcMomClass.lpszMenuName = NULL; // No menu wcMomClass.lpszClassName = "Mom"; // Win class wcMomClass.hbrBackground = GetStockObject(WHITE_BRUSH); // White bgrnd wcMomClass.hInstance = hInstance; wcMomClass.style = CS_VREDRAW | CS_HREDRAW; // Horiz & vert redraw of client area wcMomClass.lpfnWndProc = MomWndProc; // Window function wcMomClass.cbClsExtra = 0; // No extra bytes wcMomClass.cbWndExtra = 0; // No extra bytes if (!RegisterClass(&wcMomClass)) return FALSE; return (TRUE); } #pragma argsused static BOOL FAR PASCAL AboutProc(HWND hDlg, unsigned message, WORD wParam, LONG lParam); static BOOL FAR PASCAL AboutProc(HWND hDlg, unsigned message, WORD wParam, LONG lParam) { switch (message) { case WM_COMMAND: switch (wParam) { case IDOK: EndDialog(hDlg, 1); return (TRUE); default: return (FALSE); } default: return (FALSE); } } static int start_downloading(HWND hWnd); static int start_downloading(HWND hWnd) { int r; char* str; int nCid; DCB dcb; char ch; int err=0; // Whether any errors occurred if ((str = new char[80]) == NULL) { MessageBox(hWnd, (LPSTR) "download(): new char[80] failed.", NULL, MB_ICONSTOP | MB_OK); return (0); } str[0] = '\0'; r = COMM_PORT_ID = nCid = OpenComm((LPSTR) COM_STRING, 512, 512); // Arbitrary in- and out- buffer sizes switch (r) { case IE_BADID: wsprintf(str, "Bad ID '%s'.", COM_STRING); break; case IE_BAUDRATE: wsprintf(str, "Unsupported baud rate."); break; case IE_BYTESIZE: wsprintf(str, "Invalid byte size."); break; case IE_DEFAULT: wsprintf(str, "Error in default parameters."); break; case IE_HARDWARE: wsprintf(str, "Hardware (serial port) not present."); break; case IE_MEMORY: wsprintf(str, "Unable to allocate queues."); break; case IE_OPEN: wsprintf(str, "Device not open."); break; case IE_NOPEN: wsprintf(str, "Device already open."); break; default: break; } if (str[0]) { MessageBox(hWnd, (LPSTR) str, NULL, MB_ICONSTOP | MB_OK); err = 1; } else if ((r = GetCommState(nCid, &dcb)) != 0) { wsprintf(str, "download(): GetCommState() returned non-zero (%d).", r); MessageBox(hWnd, (LPSTR) str, NULL, MB_ICONSTOP | MB_OK); err = 1; } else { dcb.BaudRate = 2400; dcb.ByteSize = 8; dcb.Parity = NOPARITY; dcb.StopBits = ONESTOPBIT; if ((r = SetCommState((DCB FAR *) &dcb)) != 0) { wsprintf(str, "download(): SetCommState() returned non-zero (%d).", r); MessageBox(hWnd, (LPSTR) str, NULL, MB_ICONSTOP | MB_OK); err = 1; } else { LOGFILE.open("mom.log", ios::binary | ios::app); if (!LOGFILE) { MessageBox(hWnd, (LPSTR) "LOGFILE.open() failed.", NULL, MB_ICONSTOP | MB_OK); err = 1; } else { LOGFILE << "Log:\n"; WriteComm(nCid, "ATDT4200527\r"); CONNECTED = 1; } } } delete (str); return (!err); } static int stop_downloading(HWND hWnd); static int stop_downloading(HWND hWnd) { int err=0; WriteComm(COMM_PORT_ID, "~~~+++~~~ATH\r"); if (CloseComm(COMM_PORT_ID)) { MessageBox(hWnd, (LPSTR) "stop_downloading(): CloseComm() failed", NULL, MB_ICONSTOP | MB_OK); err = 1; } LOGFILE << "(Log closing...)\n\n"; LOGFILE.close(); return (!err); } static long FAR PASCAL MomWndProc(HWND hWnd, unsigned message, WORD wParam, LONG lParam) { static HANDLE hInst; // For use by MakeProcInstance() FARPROC lpprocAbout; static int IS_DOWNLOADING = 0; switch (message) { case WM_CREATE: // Get instance handle of module which owns window, // so that MakeProcInstance() has access to it when // we want to display the "About..." dialog box: hInst = GetWindowWord(hWnd, GWW_HINSTANCE); break; case WM_PAINT: PAINTSTRUCT ps; HDC hDC; RECT clientrect; hDC = BeginPaint(hWnd, &ps); //hDC = ps.hdc; EndPaint(hWnd, &ps); break; case WM_DESTROY: PostQuitMessage(0); break; case WM_COMMAND: switch (wParam) { case IDM_DOWNLOAD: if (IS_DOWNLOADING) { if (stop_downloading(hWnd)) { IS_DOWNLOADING = 0; } else { MessageBox(hWnd, (LPSTR) "stop_downloading() failed.", NULL, MB_ICONSTOP | MB_OK); } } else { if (start_downloading(hWnd)) { IS_DOWNLOADING = 1; } else { MessageBox(hWnd, (LPSTR) "start_downloading() failed.", NULL, MB_ICONSTOP | MB_OK); } } break; case IDM_EXIT: PostMessage(hWnd, WM_DESTROY, 0, 0); // Assume success break; case IDM_ABOUT: lpprocAbout = MakeProcInstance((FARPROC) AboutProc, hInst); if (DialogBox(hInst, "MomAboutDialog", hWnd, lpprocAbout)) { InvalidateRect(hWnd, NULL, FALSE); } FreeProcInstance(lpprocAbout); break; default: break; } break; default: return (DefWindowProc(hWnd, message, wParam, lParam)); } return (0L); }