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: gmoezyc1.cpp,v 1.9 2006/05/14 11:45:05 ssianky Exp $
23 //  ------------------------------------------------------------------
24 //  Ezycom msgbase handling
25 //  ------------------------------------------------------------------
26 
27 #include <cstdlib>
28 #include <gdbgerr.h>
29 #include <gmemdbg.h>
30 #include <gdbgtrk.h>
31 #include <gstrall.h>
32 #include <glog.h>
33 #include <gmoezyc.h>
34 
35 
36 //  ------------------------------------------------------------------
37 
38 EzycWide* ezycomwide = NULL;
39 EzycData* ezycomdata = NULL;
40 int       ezycomdatano = 0;
41 
42 
43 //  ------------------------------------------------------------------
44 
data_open()45 void EzycomArea::data_open() {
46 
47   wide = ezycomwide;
48   data = ezycomdata + (ezycomdatano++);
49   data->fhhdr = data->fhtxt = data->fhnow = -1;
50   data->omode = O_RDONLY;
51   data->smode = SH_DENYNO;
52   data->timesposted = 0;
53 }
54 
55 
56 //  ------------------------------------------------------------------
57 
data_close()58 void EzycomArea::data_close() {
59 
60   ezycomdatano--;
61 }
62 
63 
64 //  ------------------------------------------------------------------
65 
ret_mess_xxx(char * __path,byte __type)66 char* EzycomArea::ret_mess_xxx(char* __path, byte __type) {
67 
68   if(wide->ver >= 110) {
69     sprintf(__path, "%sAREA%u\\M%c%05u.BBS",
70       wide->msgbasepath,
71       ((board()-1) / 100) + 1,
72       (__type == 1) ? 'H' : 'T',
73       board()
74     );
75   }
76   else {
77     sprintf(__path, "%sAREA%u\\MSG%c%03u.BBS",
78       wide->msgbasepath,
79       ((board()-1) / 100) + 1,
80       (__type == 1) ? 'H' : 'T',
81       (board() > 999) ? board()/10 : board()
82     );
83   }
84   return __path;
85 }
86 
87 
88 //  ------------------------------------------------------------------
89 
ret_mess_area(char * __path)90 char* EzycomArea::ret_mess_area(char* __path) {
91 
92   sprintf(__path, "%sAREA%u",
93     wide->msgbasepath,
94     ((board()-1) / 100) + 1
95   );
96   return __path;
97 }
98 
99 
100 //  ------------------------------------------------------------------
101 
raw_close()102 void EzycomArea::raw_close() {
103 
104   GFTRK("EzycomRawClose");
105 
106   if(data->fhhdr != -1)  ::close(data->fhhdr);  data->fhhdr = -1;
107   if(data->fhtxt != -1)  ::close(data->fhtxt);  data->fhtxt = -1;
108   if(data->fhnow != -1)  ::close(data->fhnow);  data->fhnow = -1;
109   if(data->omode == O_WRONLY)
110     remove(AddPath(wide->msgbasepath, "EZYMSG.NOW"));
111 
112   GFTRK(0);
113 }
114 
115 
116 //  ------------------------------------------------------------------
117 
test_open(const char * __file,int __mode,int __share)118 int EzycomArea::test_open(const char* __file, int __mode, int __share) {
119 
120   GFTRK("EzycomTestOpen");
121 
122   int _fh;
123   long _tries = 0;
124   Path _path;
125   strcpy(_path, __file);
126 
127   do {
128 
129     _fh = ::sopen(_path, __mode, __share, S_STDRW);
130     if(_fh == -1) {
131 
132       // Tell the world
133       if((errno != EACCES) or (PopupLocked(++_tries, false, _path) == false)) {
134         WideLog->ErrOpen();
135         raw_close();
136         WideLog->printf("! A Ezycom msgbase file could not be opened.");
137         WideLog->printf(": %s.", _path);
138         WideLog->ErrOSInfo();
139         OpenErrorExit();
140       }
141     }
142   } while(_fh == -1);
143 
144   // Remove the popup window
145   if(_tries)
146     PopupLocked(0, 0, NULL);
147 
148   GFTRK(0);
149 
150   return _fh;
151 }
152 
153 
154 //  ------------------------------------------------------------------
155 
raw_open()156 int EzycomArea::raw_open() {
157 
158   GFTRK("EzycomRawOpen");
159 
160   int _tryagain = 0;
161 
162   do {
163 
164     int _sopen_access = data->omode | O_BINARY;
165     int _sopen_permit = 0;
166 
167     if(not fexist(ret_mess_xxx(data->ezyfile,1))) {
168       _sopen_access |= O_CREAT;
169       _sopen_permit = S_STDRW;
170     }
171 
172     ret_mess_xxx(data->ezyfile, 1);
173     data->fhhdr = ::sopen(data->ezyfile, _sopen_access, data->smode, _sopen_permit);
174     if(data->fhhdr != -1) {
175       ret_mess_xxx(data->ezyfile, 2);
176       data->fhtxt = ::sopen(data->ezyfile, _sopen_access, data->smode, _sopen_permit);
177       if(data->fhtxt != -1) {
178         if(data->omode == O_WRONLY) {
179 
180           // Create semaphore file
181           byte _sema = 0;
182           data->fhnow = ::sopen(AddPath(wide->msgbasepath, "EZYMSG.NOW"), O_WRONLY|O_CREAT|O_TRUNC|O_BINARY, WideSharemode, S_STDRW);
183           write(data->fhnow, &_sema, 1);  // Write some dummy data
184         }
185 
186         GFTRK(0);
187         return true;
188       }
189     }
190     raw_close();
191     if(fexist(AddPath(wide->msgbasepath, "EZYMSG.NOW"))) {
192       errno = EACCES;
193       break;
194     }
195     Path _path;
196     if(not is_dir(ret_mess_area(_path))) {
197       mkdir(ret_mess_area(_path), S_IWUSR);
198       _tryagain++;
199     }
200   } while(_tryagain == 1);
201 
202   GFTRK(0);
203 
204   return false;
205 }
206 
207 
208 //  ------------------------------------------------------------------
209 
test_raw_open(int __fileline)210 void EzycomArea::test_raw_open(int __fileline) {
211 
212   GFTRK("EzycomTestRawOpen");
213 
214   int _isopen;
215   long _tries = 0;
216 
217   do {
218 
219     _isopen = raw_open();
220 
221     if(not _isopen) {
222 
223       // Tell the world
224       if((errno != EACCES) or PopupLocked(++_tries, false, data->ezyfile) == false) {
225 
226         // User requested to exit
227         WideLog->erropen(__FILE__, __fileline);
228         WideLog->printf("! A Ezycom msgbase file could not be opened.");
229         WideLog->printf(": %s.", data->ezyfile);
230         WideLog->ErrOSInfo();
231         OpenErrorExit();
232       }
233     }
234   } while(not _isopen);
235 
236   // Remove the popup window
237   if(_tries)
238     PopupLocked(0, 0, NULL);
239 
240   GFTRK(0);
241 }
242 
243 
244 //  ------------------------------------------------------------------
245 
EzycomExit()246 void EzycomExit() {
247 
248   if(ezycomwide)
249     delete ezycomwide->user;
250   throw_xrelease(ezycomwide);
251   throw_xrelease(ezycomdata);
252 }
253 
254 
255 //  ------------------------------------------------------------------
256 
EzycomInit(const char * msgbasepath,const char * userbasepath,int userno)257 void EzycomInit(const char* msgbasepath, const char* userbasepath, int userno) {
258 
259   ezycomdata = (EzycData*)throw_calloc(3, sizeof(EzycData));
260   ezycomwide = (EzycWide*)throw_calloc(1, sizeof(EzycWide));
261 
262   ezycomwide->msgbasepath = msgbasepath;
263   ezycomwide->userbasepath = userbasepath;
264   ezycomwide->userno = userno;
265 
266   Path _path;
267   *_path = NUL;
268   char* _ptr = getenv("EZY");
269   if(_ptr and *_ptr) {
270     _ptr = strcpy(_path, _ptr);
271     char* _ptr2 = strchr(_ptr, ' ');
272     if(_ptr2)
273       *_ptr2 = NUL;
274     AddBackslash(_path);
275   }
276   const char* _file = "";
277   _ptr = getenv("TASK");
278   if(_ptr and *_ptr) {
279     char _tmp[20];
280     sprintf(_tmp, "CONFIG.%u", atoi(_ptr));
281     _file = AddPath(_path, _tmp);
282   }
283   if(not fexist(_file))
284     _file = AddPath(_path, "CONFIG.EZY");
285 
286   ezycomwide->ver = 102;
287   ezycomwide->maxmess = EZYC_MAXMESS102;
288   int _fh = ::sopen(_file, O_RDONLY|O_BINARY, WideSharemode, S_STDRD);
289   if(_fh != -1) {
290     char _verstr[9];
291     read(_fh, _verstr, 9);
292     close(_fh);
293     strp2c(_verstr);
294     if(strnicmp(_verstr, "1.10", 4) >= 0) {
295       ezycomwide->ver = 110;
296       ezycomwide->maxmess = EZYC_MAXMESS110;
297     }
298   }
299 
300   ezycomwide->user = new EzycomUser;
301   throw_new(ezycomwide->user);
302 
303   const char* _username = WideUsername[0];
304   ezycomwide->user->ver = ezycomwide->ver;
305   if (ezycomwide->userno == -1)
306   {
307     ezycomwide->user->gufh = ::sopen(AddPath(ezycomwide->userbasepath, "USERS.BBS"), O_RDWR|O_CREAT|O_BINARY, WideSharemode, S_STDRW);
308     if (ezycomwide->user->gufh != -1)
309     {
310       ezycomwide->user->extfh = ::sopen(AddPath(ezycomwide->userbasepath, "USERSEXT.BBS"), O_RDWR|O_CREAT|O_BINARY, WideSharemode, S_STDRW);
311       if(ezycomwide->user->extfh != -1) {
312         ezycomwide->user->find(_username);
313         if(not ezycomwide->user->found) {
314           WideLog->printf("* User \"%s\" not found in %sUSERS.BBS.", _username, ezycomwide->userbasepath);
315           ezycomwide->user->add(_username);
316           WideLog->printf("* Now added with user number %u.", ezycomwide->user->index);
317         }
318         close(ezycomwide->user->extfh);
319       }
320       close(ezycomwide->user->gufh);
321     }
322     ezycomwide->userno = ezycomwide->user->index;
323   }
324 }
325 
326 
327 //  ------------------------------------------------------------------
328 
open()329 void EzycomArea::open() {
330 
331   GFTRK("EzycomOpen");
332 
333   isopen++;
334   if(isopen > 2) {
335     WideLog->ErrTest();
336     WideLog->printf("! Trying to open a Ezycom msgbase more than twice.");
337     WideLog->printf(": %s, board %u.", echoid(), board());
338     WideLog->printf("+ Info: This indicates a serious bug.");
339     WideLog->printf("+ Advice: Report to the Author immediately.");
340     TestErrorExit();
341   }
342   if(isopen == 1) {
343     if(ispacked()) {
344       isopen--;
345       const char* newpath = Unpack(path());
346       if(newpath == NULL)
347         packed(false);
348       set_real_path(newpath ? newpath : path());
349       isopen++;
350     }
351     data_open();
352     test_raw_open(__LINE__);
353     scan();
354   }
355 
356   GFTRK(0);
357 }
358 
359 
360 //  ------------------------------------------------------------------
361 
save_lastread()362 void EzycomArea::save_lastread() {
363 
364   GFTRK("EzycomSaveLastread");
365 
366   int _fh = test_open(AddPath(wide->userbasepath, "LASTCOMB.BBS"), O_RDWR|O_CREAT|O_BINARY, SH_DENYNO);
367   if(_fh != -1) {
368     word _lastread = (word)(Msgn->CvtReln(lastread)+1);
369     lseekset(_fh, wide->userno * (wide->maxmess / 16) * sizeof(EzycLast) +
370       (((board() - 1) / 16) * sizeof(EzycLast) + sizeof(word)) +
371       (board()-1) % 16 * sizeof(word)
372     );
373     write(_fh, &_lastread, sizeof(word));
374     ::close(_fh);
375   }
376 
377   if(data->timesposted) {
378     wide->user->extfh = ::sopen(AddPath(wide->userbasepath, "USERSEXT.BBS"), O_RDWR|O_BINARY, SH_DENYNO, S_STDRW);
379     if(wide->user->extfh != -1) {
380       wide->user->moveto(wide->userno);
381       wide->user->inctimesposted(data->timesposted);
382       data->timesposted = 0;
383       ::close(wide->user->extfh);
384     }
385   }
386 
387   GFTRK(0);
388 }
389 
390 
391 //  ------------------------------------------------------------------
392 
close()393 void EzycomArea::close() {
394 
395   GFTRK("EzycomClose");
396 
397   if(isopen) {
398     if(isopen == 1) {
399       save_lastread();
400       raw_close();
401       Msgn->Reset();
402       data_close();
403       if(ispacked()) {
404         CleanUnpacked(real_path());
405       }
406     }
407     isopen--;
408   }
409   else {
410     WideLog->ErrTest();
411     WideLog->printf("! Trying to close an already closed Ezycom msgbase.");
412     WideLog->printf(": %s, board %u.", echoid(), board());
413     WideLog->printf("+ Info: This indicates a potentially serious bug.");
414     WideLog->printf("+ Advice: Report to the Author immediately.");
415     TestErrorExit();
416   }
417 
418   GFTRK(0);
419 }
420 
421 
422 //  ------------------------------------------------------------------
423 
suspend()424 void EzycomArea::suspend() {
425 
426   GFTRK("EzycomSuspend");
427 
428   save_lastread();
429   raw_close();
430 
431   GFTRK(0);
432 }
433 
434 
435 //  ------------------------------------------------------------------
436 
resume()437 void EzycomArea::resume() {
438 
439   GFTRK("EzycomResume");
440   if(not raw_open()) {
441     Path _path;
442     WideLog->ErrOpen();
443     WideLog->printf("! A Ezycom msgbase file could not be opened.");
444     WideLog->printf(": %s.", ret_mess_xxx(_path, 1));
445     WideLog->ErrOSInfo();
446     OpenErrorExit();
447   }
448 
449   GFTRK(0);
450 }
451 
452 
453 //  ------------------------------------------------------------------
454