1 //  This may look like C code, but it is really -*- C++ -*-
2 
3 //  ------------------------------------------------------------------
4 //  The Goldware Library
5 //  Copyright (C) 1990-1999 Odinn Sorensen
6 //  ------------------------------------------------------------------
7 //  This library is free software; you can redistribute it and/or
8 //  modify it under the terms of the GNU Library General Public
9 //  License as published by the Free Software Foundation; either
10 //  version 2 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 //  Library General Public License for more details.
16 //
17 //  You should have received a copy of the GNU Library General Public
18 //  License along with this program; if not, write to the Free
19 //  Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
20 //  MA 02111-1307, USA
21 //  ------------------------------------------------------------------
22 //  $Id: gmoxbbs1.cpp,v 1.11 2006/05/14 11:45:05 ssianky Exp $
23 //  ------------------------------------------------------------------
24 //  AdeptXBBS messagebase engine.
25 //  ------------------------------------------------------------------
26 
27 #include <gdbgerr.h>
28 #include <gmemdbg.h>
29 #include <gdbgtrk.h>
30 #include <gmoxbbs.h>
31 
32 
33 //  ------------------------------------------------------------------
34 
35 XbbsWide* xbbswide = NULL;
36 XbbsData* xbbsdata = NULL;
37 int       xbbsdatano = 0;
38 
39 
40 //  ------------------------------------------------------------------
41 
data_open()42 void XbbsArea::data_open() {
43 
44   wide = xbbswide;
45   data = xbbsdata + (xbbsdatano++);
46 }
47 
48 
49 //  ------------------------------------------------------------------
50 
data_close()51 void XbbsArea::data_close() {
52 
53   xbbsdatano--;
54 }
55 
56 
57 //  ------------------------------------------------------------------
58 
raw_close()59 void XbbsArea::raw_close() {
60 
61   GFTRK("XbbsRawClose");
62 
63   if(data->fhdata != -1)   ::close(data->fhdata);   data->fhdata = -1;
64   if(data->fhindex != -1)  ::close(data->fhindex);  data->fhindex = -1;
65   if(data->fhtext != -1)   ::close(data->fhtext);   data->fhtext = -1;
66 
67   if (wide->isopen)
68   {
69     if (wide->isopen == 1)
70     {
71       if (wide->user->gufh != -1)
72       {
73         ::close(wide->user->gufh);
74         wide->user->gufh = -1;
75       }
76     }
77     wide->isopen--;
78   }
79 
80   GFTRK(0);
81 }
82 
83 
84 //  ------------------------------------------------------------------
85 
test_open(const char * __file,int sharemode)86 int XbbsArea::test_open(const char* __file, int sharemode) {
87 
88   GFTRK("XbbsTestOpen");
89 
90   int _fh;
91   long _tries = 0;
92   if(sharemode == -1)
93     sharemode = WideSharemode;
94 
95   do {
96 
97     _fh = ::sopen(__file, O_RDWR|O_BINARY|O_CREAT, sharemode, S_STDRW);
98     if(_fh == -1) {
99 
100       // Tell the world
101       if((errno != EACCES) or (PopupLocked(++_tries, false, __file) == false)) {
102 
103         // User requested to exit
104         WideLog->ErrOpen();
105         raw_close();
106         WideLog->printf("! An AdeptXBBS msgbase file could not be opened.");
107         WideLog->printf(": %s.", __file);
108         WideLog->ErrOSInfo();
109         OpenErrorExit();
110       }
111     }
112   } while(_fh == -1);
113 
114   // Remove the popup window
115   if(_tries)
116     PopupLocked(0, 0, NULL);
117 
118   GFTRK(0);
119 
120   return _fh;
121 }
122 
123 
124 //  ------------------------------------------------------------------
125 
raw_open()126 void XbbsArea::raw_open() {
127 
128   GFTRK("XbbsRawOpen");
129 
130   data->fhdata  = test_open(AddPath(real_path(), ".Data"));
131   data->fhindex = test_open(AddPath(real_path(), ".Index"));
132   data->fhtext  = test_open(AddPath(real_path(), ".Text"));
133   wide->isopen++;
134   if (wide->isopen == 1)
135     wide->user->gufh = ::sopen(AddPath(wide->path, "Users"), O_RDONLY|O_BINARY, WideSharemode, S_STDRW);
136 
137   GFTRK(0);
138 }
139 
140 
141 //  ------------------------------------------------------------------
142 
XbbsExit()143 void XbbsExit() {
144 
145   if(xbbswide)
146     delete xbbswide->user;
147   throw_release(xbbswide);
148   throw_release(xbbsdata);
149 }
150 
151 
152 //  ------------------------------------------------------------------
153 
XbbsInit(const char * path,int userno)154 void XbbsInit(const char* path, int userno) {
155 
156   xbbsdata = (XbbsData*)throw_calloc(3, sizeof(XbbsData));
157   xbbswide = (XbbsWide*)throw_calloc(1, sizeof(XbbsWide));
158 
159   xbbswide->path = path;
160   xbbswide->userno = userno;
161 
162   xbbswide->user = new XbbsUser;
163   throw_new(xbbswide->user);
164 
165   xbbswide->user->gufh = -1;
166   xbbswide->fhpmi = -1;
167   xbbswide->pmi = NULL;
168   xbbswide->isopen = 0;
169 
170   const char* _username = WideUsername[0];
171   if (xbbswide->userno == -1)
172   {
173     xbbswide->user->gufh = ::sopen(AddPath(xbbswide->path, "Users"), O_RDONLY|O_BINARY, WideSharemode, S_STDRD);
174     if (xbbswide->user->gufh != -1)
175     {
176       xbbswide->user->find(_username);
177       if(not xbbswide->user->found) {
178         xbbswide->userno = 0;
179         //WideLog->printf("* User \"%s\" not found in %sUsers.", _username, xbbswide->path);
180         //xbbswide->user->add(_username);
181         //WideLog->printf("* Now added with user number %u.", xbbswide->user->index);
182       }
183       close(xbbswide->user->gufh);
184     }
185     xbbswide->userno = xbbswide->user->index;
186   }
187 }
188 
189 
190 //  ------------------------------------------------------------------
191 
open()192 void XbbsArea::open() {
193 
194   GFTRK("XbbsOpen");
195 
196   isopen++;
197   if(isopen > 2) {
198     WideLog->ErrTest();
199     WideLog->printf("! Trying to open an AdeptXBBS msgbase more than twice.");
200     WideLog->printf(": %s, %s.", echoid(), path());
201     WideLog->printf("+ Info: This indicates a serious bug.");
202     WideLog->printf("+ Advice: Report to the Author immediately.");
203     TestErrorExit();
204   }
205   if(isopen == 1) {
206     if(ispacked()) {
207       isopen--;
208       const char* newpath = Unpack(path());
209       if(newpath == NULL)
210         packed(false);
211       set_real_path(newpath ? newpath : path());
212       isopen++;
213     }
214     data_open();
215     raw_open();
216     refresh();
217     scan();
218   }
219 
220   GFTRK(0);
221 }
222 
223 
224 //  ------------------------------------------------------------------
225 
save_lastread()226 void XbbsArea::save_lastread() {
227 
228   GFTRK("XbbsSaveLastread");
229 
230   int _fh = ::sopen(AddPath(real_path(), ".lmr"), O_RDWR|O_CREAT|O_BINARY, WideSharemode, S_STDRW);
231   if(_fh != -1) {
232     uint32_t _lastread = Msgn->CvtReln(lastread);
233     lseekset(_fh, wide->userno+1, sizeof(uint32_t));
234     write(_fh, &_lastread, sizeof(uint32_t));
235     ::close(_fh);
236   }
237 
238   GFTRK(0);
239 }
240 
241 
242 //  ------------------------------------------------------------------
243 
close()244 void XbbsArea::close() {
245 
246   GFTRK("XbbsClose");
247 
248   if(isopen) {
249     if(isopen == 1) {
250       save_lastread();
251       raw_close();
252       Msgn->Reset();
253       throw_release(data->idx);
254       data_close();
255       if(ispacked()) {
256         CleanUnpacked(real_path());
257       }
258     }
259     isopen--;
260   }
261   else {
262     WideLog->ErrTest();
263     WideLog->printf("! Trying to close an already closed AdeptXBBS msgbase.");
264     WideLog->printf(": %s, %s.", echoid(), path());
265     WideLog->printf("+ Info: This indicates a potentially serious bug.");
266     WideLog->printf("+ Advice: Report to the Author immediately.");
267     TestErrorExit();
268   }
269 
270   GFTRK(0);
271 }
272 
273 
274 //  ------------------------------------------------------------------
275 
suspend()276 void XbbsArea::suspend() {
277 
278   GFTRK("XbbsSuspend");
279 
280   save_lastread();
281 
282   GFTRK(0);
283 }
284 
285 
286 //  ------------------------------------------------------------------
287 
resume()288 void XbbsArea::resume() {
289 
290   GFTRK("XbbsResume");
291 
292   GFTRK(0);
293 }
294 
295 
296 //  ------------------------------------------------------------------
297