|
|
1.1 root 1: /* MainFormUnit.cpp */
2:
3: /* Local sysop chat module (GUI Borland C++ Builder Project for Win32) */
4:
5: /* $Id: MainFormUnit.cpp,v 1.3 2000/11/03 01:03:51 rswindell Exp $ */
6:
7: /****************************************************************************
8: * @format.tab-size 4 (Plain Text/Source Code File Header) *
9: * @format.use-tabs true (see http://www.synchro.net/ptsc_hdr.html) *
10: * *
11: * Copyright 2000 Rob Swindell - http://www.synchro.net/copyright.html *
12: * *
13: * This program is free software; you can redistribute it and/or *
14: * modify it under the terms of the GNU General Public License *
15: * as published by the Free Software Foundation; either version 2 *
16: * of the License, or (at your option) any later version. *
17: * See the GNU General Public License for more details: gpl.txt or *
18: * http://www.fsf.org/copyleft/gpl.html *
19: * *
20: * Anonymous FTP access to the most recent released source is available at *
21: * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net *
22: * *
23: * Anonymous CVS access to the development source and modification history *
24: * is available at cvs.synchro.net:/cvsroot/sbbs, example: *
25: * cvs -d :pserver:[email protected]:/cvsroot/sbbs login *
26: * (just hit return, no password is necessary) *
27: * cvs -d :pserver:[email protected]:/cvsroot/sbbs checkout src *
28: * *
29: * For Synchronet coding style and modification guidelines, see *
30: * http://www.synchro.net/source.html *
31: * *
32: * You are encouraged to submit any modifications (preferably in Unix diff *
33: * format) via e-mail to [email protected] *
34: * *
35: * Note: If this box doesn't appear square, then you need to fix your tabs. *
36: ****************************************************************************/
37:
38: //---------------------------------------------------------------------------
39:
40: #include <vcl.h>
41: #include <io.h>
42: #include <sys\stat.h>
43: #include <fcntl.h>
44: #include <share.h>
45: #include <sys/locking.h>
46: #include <vcl\Registry.hpp> /* TRegistry */
47: #pragma hdrstop
48:
49: #include "gen_defs.h" /* BS and DEL */
50:
51: #define PCHAT_LEN 1000 /* Size of Private chat file */
52: #define REG_KEY "\\Software\\Swindell\\Synchronet Chat\\"
53:
54: #include "MainFormUnit.h"
55: //---------------------------------------------------------------------------
56: #pragma package(smart_init)
57: #pragma resource "*.dfm"
58: TMainForm *MainForm;
59: extern int node_num;
60: extern char ctrl_dir[];
61: extern char node_dir[];
62: extern char user_name[];
63: //---------------------------------------------------------------------------
64: __fastcall TMainForm::TMainForm(TComponent* Owner)
65: : TForm(Owner)
66: {
67: in=-1;
68: out=-1;
69: nodedab=-1;
70: }
71: bool __fastcall TMainForm::ToggleChat(bool on)
72: {
73: int n=node_num-1;
74: static int org_act;
75:
76: if(nodedab==-1)
77: return(false);
78:
79: lseek(nodedab, n*sizeof(node_t), SEEK_SET);
80: int i=locking(nodedab, LK_LOCK, sizeof(node_t));
81: if(i) {
82: Remote->Lines->Add("!Error "+AnsiString(i)+" reading record for"
83: "node "+AnsiString(node_num));
84: return(false);
85: }
86: read(nodedab, &node, sizeof(node_t));
87: if(on) {
88: org_act=node.action;
89: if(org_act==NODE_PCHT)
90: org_act=NODE_MAIN;
91: node.misc|=NODE_LCHAT;
92: } else {
93: node.action=org_act;
94: node.misc&=~NODE_LCHAT;
95: }
96:
97: lseek(nodedab, n*sizeof(node_t), SEEK_SET);
98: write(nodedab, &node, sizeof(node_t));
99: lseek(nodedab, n*sizeof(node_t), SEEK_SET);
100: locking(nodedab, LK_UNLCK, sizeof(node_t));
101:
102: return(true);
103: }
104: //---------------------------------------------------------------------------
105: void __fastcall TMainForm::FormShow(TObject *Sender)
106: {
107: char path[MAX_PATH+1];
108: char* p;
109:
110: Caption="Waiting for "
111: +AnsiString(user_name)+" on Node "+AnsiString(node_num);
112:
113: wsprintf(path,"%sNODE.DAB",ctrl_dir);
114: nodedab=_sopen(path,O_RDWR|O_BINARY|O_CREAT, SH_DENYNONE,S_IREAD|S_IWRITE);
115:
116: if(nodedab==-1) {
117: Remote->Lines->Add("!Error opening NODE.DAB");
118: return;
119: }
120:
121: ToggleChat(true);
122:
123: wsprintf(path,"%sLCHAT.DAB",node_dir);
124: if((out=_sopen(path,O_RDWR|O_CREAT|O_BINARY,O_DENYNONE
125: ,S_IREAD|S_IWRITE))==-1) {
126: Remote->Lines->Add("!Error opening LCHAT.DAB");
127: return; }
128:
129: wsprintf(path,"%sCHAT.DAB",node_dir);
130: #if 0
131: if(!fexist(path)) /* Wait while it's created for the first time */
132: mswait(2000);
133: #endif
134:
135: if((in=_sopen(path,O_RDWR|O_CREAT|O_BINARY,O_DENYNONE
136: ,S_IREAD|S_IWRITE))==-1) {
137: close(out);
138: Remote->Lines->Add("!Error opening CHAT.DAB");
139: return;
140: }
141:
142: if((p=(char *)MALLOC(PCHAT_LEN))==NULL) {
143: close(in);
144: close(out);
145: Remote->Lines->Add("!Error allocating memory");
146: return;
147: }
148: memset(p,0,PCHAT_LEN);
149: write(in,p,PCHAT_LEN);
150: write(out,p,PCHAT_LEN);
151: FREE(p);
152: lseek(in,0L,SEEK_SET);
153: lseek(out,0L,SEEK_SET);
154:
155: Timer->Enabled=true;
156: }
157: //---------------------------------------------------------------------------
158: void __fastcall TMainForm::FormClose(TObject *Sender, TCloseAction &Action)
159: {
160: ToggleChat(false);
161:
162: if(in!=-1)
163: close(in);
164: if(out!=-1)
165: close(out);
166: if(nodedab!=-1)
167: close(nodedab);
168:
169: // Write Registry keys
170: TRegistry* Registry=new TRegistry;
171: if(!Registry->OpenKey(REG_KEY,true)) {
172: Application->MessageBox("Error opening registry key"
173: ,REG_KEY,MB_OK|MB_ICONEXCLAMATION);
174: Application->Terminate();
175: }
176: Registry->WriteInteger("FormHeight",Height);
177: Registry->WriteInteger("FormWidth",Width);
178: Registry->WriteInteger("RemoteHeight",Remote->Height);
179:
180: Registry->CloseKey();
181: delete Registry;
182:
183: }
184: //---------------------------------------------------------------------------
185: void __fastcall TMainForm::LocalKeyPress(TObject *Sender, char &Key)
186: {
187: char c;
188:
189: if(out==-1 || Local->ReadOnly==true) {
190: Beep();
191: return;
192: }
193: read(out,&c,1);
194: lseek(out,-1L,SEEK_CUR);
195: if(!c) /* hasn't wrapped */
196: write(out,&Key,1);
197: else {
198: if(!tell(out))
199: lseek(out,0L,SEEK_END);
200: lseek(out,-1L,SEEK_CUR);
201: c=0;
202: write(out,&c,1);
203: lseek(out,-1L,SEEK_CUR);
204: }
205: if(tell(out)>=PCHAT_LEN)
206: lseek(out,0L,SEEK_SET);
207: }
208: //---------------------------------------------------------------------------
209: void __fastcall TMainForm::InputTimerTick(TObject *Sender)
210: {
211: char ch=0;
212:
213: Timer->Interval=100;
214:
215: while(in!=-1) {
216: if(tell(in)>=PCHAT_LEN)
217: lseek(in,0L,SEEK_SET);
218: read(in,&ch,1);
219: lseek(in,-1L,SEEK_CUR);
220: if(!ch)
221: break; /* char from other node */
222:
223: Timer->Interval=1;
224:
225: /* Got char, display it */
226: if(ch=='\r')
227: Remote->Lines->Add("");
228: else if(ch==BS || ch==DEL) { // backspace
229: Remote->Text
230: =Remote->Text.SetLength(Remote->Text.Length()-1);
231: /* Need to scroll window down here */
232: } else {
233: Remote->SelLength=0;
234: Remote->SelStart=Remote->Text.Length();
235: Remote->SelText=AnsiString(ch);
236: }
237:
238: /* mark char as rx'd */
239: ch=0;
240: write(in,&ch,1);
241: }
242: }
243: //---------------------------------------------------------------------------
244: void __fastcall TMainForm::TimerTick(TObject *Sender)
245: {
246: if(nodedab==-1)
247: return;
248:
249: int n=node_num-1;
250: lseek(nodedab, n*sizeof(node_t), SEEK_SET);
251: read(nodedab, &node, sizeof(node_t));
252: if(node.misc&NODE_LCHAT)
253: ;
254: else if(Local->ReadOnly==true) {
255: MainForm->Caption=AnsiString(user_name)+" on Node "
256: +AnsiString(node_num);
257: Local->ReadOnly=false;
258: }
259: else if(!node.status || node.status>NODE_QUIET || node.action!=NODE_PCHT) {
260: OutputDebugString("CHAT: User Disconnected\r\n");
261: MainForm->Caption=MainForm->Caption+" - Disconnected!";
262: Timer->Enabled=false;
263: Local->ReadOnly=true;
264: }
265: }
266: //---------------------------------------------------------------------------
267:
268: void __fastcall TMainForm::FormCreate(TObject *Sender)
269: {
270: // Read Registry keys
271: TRegistry* Registry=new TRegistry;
272: if(!Registry->OpenKey(REG_KEY,true)) {
273: Application->MessageBox("Error opening registry key"
274: ,REG_KEY,MB_OK|MB_ICONEXCLAMATION);
275: Application->Terminate();
276: }
277: if(Registry->ValueExists("FormHeight"))
278: Height=Registry->ReadInteger("FormHeight");
279: if(Registry->ValueExists("FormWidth"))
280: Width=Registry->ReadInteger("FormWidth");
281: if(Registry->ValueExists("RemoteHeight"))
282: Remote->Height=Registry->ReadInteger("RemoteHeight");
283:
284: Registry->CloseKey();
285: delete Registry;
286:
287: }
288: //---------------------------------------------------------------------------
289:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.