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: gmowcat4.cpp,v 1.7 2006/05/14 11:45:05 ssianky Exp $
23 //  ------------------------------------------------------------------
24 //  WildCat! 4.x messagebase engine.
25 //  ------------------------------------------------------------------
26 
27 
28 //  ------------------------------------------------------------------
29 
30 #include <gdbgerr.h>
31 #include <gmemdbg.h>
32 #include <gdbgtrk.h>
33 #include <gstrall.h>
34 
35 #include <gmowcat.h>
36 
37 
38 //  ------------------------------------------------------------------
39 
lock()40 void WCatArea::lock() {
41 
42   GFTRK("WCatLock");
43 
44   if(not data->islocked) {
45     if(WideCanLock) {
46       long _tries = 0;
47       while(::lock(data->fhix, 0, 1) == -1) {
48         if(PopupLocked(++_tries, true, real_path()) == false) {
49           WideLog->ErrLock();
50           raw_close();
51           WideLog->printf("! A WildCat! msgbase file could not be locked.");
52           WideLog->printf(": %s.ix.", real_path());
53           WideLog->ErrOSInfo();
54           LockErrorExit();
55         }
56       }
57       if(_tries)
58         PopupLocked(0, 0, NULL);
59     }
60     refresh();
61     data->islocked = true;
62   }
63 
64   GFTRK(0);
65 }
66 
67 
68 //  ------------------------------------------------------------------
69 
unlock()70 void WCatArea::unlock() {
71 
72   GFTRK("WCatUnlock");
73   if(WideCanLock and data->islocked)
74     ::unlock(data->fhix, 0, 1);
75   data->islocked = false;
76 
77   GFTRK(0);
78 }
79 
80 
81 //  ------------------------------------------------------------------
82 
save_message(int __mode,gmsg * __msg,WCatHdr & __hdr)83 void WCatArea::save_message(int __mode, gmsg* __msg, WCatHdr& __hdr) {
84 
85   int _was_locked = data->islocked;
86   if(not _was_locked)
87     lock();
88 
89   memset(&__hdr, 0, sizeof(WCatHdr));
90 
91   if(__mode & GMSG_NEW) {
92     data->base.active++;
93     __msg->msgno = data->base.nextmsgno++;
94     Msgn->Append(__msg->msgno);
95     data->idx = (WCatIdx*)throw_realloc(data->idx, data->base.active*sizeof(WCatIdx));
96     __hdr.mflags |= mfReceiveable;
97     lseekset(data->fhix, 0);
98     write(data->fhix, &data->base, sizeof(WCatBase));
99   }
100   else if(__mode & GMSG_DELETE) {
101     __hdr.mflags |= mfDeleted;
102   }
103 
104   __hdr.magicnumber = MagicHeaderActive;
105   __hdr.msgno = (word)__msg->msgno;
106 
107   strc2p(strupr(strxcpy(__hdr.from, __msg->by, sizeof(__hdr.from))));
108   strc2p(strcpy(__hdr.fromtitle, __msg->wildcat.from_title));
109   __hdr.fromuserid = __msg->wildcat.from_userid;
110 
111   strc2p(strupr(strxcpy(__hdr.to, __msg->to, sizeof(__hdr.to))));
112   strc2p(strcpy(__hdr.totitle, __msg->wildcat.to_title));
113   __hdr.touserid = __msg->wildcat.to_userid;
114 
115   strc2p(strxcpy(__hdr.subject, __msg->re, sizeof(__hdr.subject)));
116 
117   if(not *__msg->wildcat.network and (isnet() or isecho()))
118     strcpy(__msg->wildcat.network, "FTSC");
119   strc2p(strcpy(__hdr.network, __msg->wildcat.network));
120 
121   if (__msg->written)
122   {
123     struct tm _tm; ggmtime(&_tm, &__msg->written);
124     __hdr.msgdate = (word)(YMD2JDN(1900+_tm.tm_year, _tm.tm_mon+1, _tm.tm_mday)-1);
125     __hdr.msgtime = ((_tm.tm_hour*3600L)+(_tm.tm_min*60L)+_tm.tm_sec)+1;
126   }
127 
128   if (__msg->received)
129   {
130     struct tm _tm; ggmtime(&_tm, &__msg->received);
131     __hdr.readdate = (word)YMD2JDN(1900+_tm.tm_year, _tm.tm_mon+1, _tm.tm_mday);
132     __hdr.readtime = ((_tm.tm_hour*3600L)+(_tm.tm_min*60L)+_tm.tm_sec)+1;
133   }
134 
135   __hdr.mflags |= (word)(__msg->attr.pvt() ? mfPrivate     : 0);
136   __hdr.mflags |= (word)(__msg->attr.rab() ? mfReceiveable : 0);
137   __hdr.mflags |= (word)(__msg->attr.rcv() ? mfReceived    : 0);
138   __hdr.mflags |= (word)(__msg->attr.rrc() ? mfReceipt     : 0);
139   __hdr.mflags |= (word)(__msg->attr.car() ? mfCarboned    : 0);
140   __hdr.mflags |= (word)(__msg->attr.fwd() ? mfForwarded   : 0);
141   __hdr.mflags |= (word)(__msg->attr.efl() ? mfEchoFlag    : 0);
142   __hdr.mflags |= (word)(__msg->attr.hrp() ? mfHasReplies  : 0);
143   __hdr.mflags |= (word)(__msg->attr.del() ? mfDeleted     : 0);
144   __hdr.mflags |= (word)(__msg->attr.tag() ? mfTagged      : 0);
145   __hdr.mflags |= (word)(__msg->attr.snt() ? mfSent        : 0);
146   if(__msg->link.first())
147     __hdr.mflags |= mfHasReplies;
148 
149   __hdr.reference = (word)__msg->link.to();
150 
151   __hdr.origaddr = __msg->orig;
152   __hdr.destaddr = __msg->dest;
153 
154   __hdr.msgbytes = (word)__msg->txtlength;
155 
156   strc2p(strcpy(__hdr.internalattach, __msg->wildcat.internal_attach));
157   strc2p(strcpy(__hdr.externalattach, __msg->wildcat.external_attach));
158 
159   __hdr.prevunread = (word)__msg->wildcat.prev_unread;
160   __hdr.nextunread = (word)__msg->wildcat.next_unread;
161 
162   __hdr.fidoflags |= (word)(__msg->attr.pvt() ? FIDO_PVT       : 0);
163   __hdr.fidoflags |= (word)(__msg->attr.cra() ? FIDO_CRASH     : 0);
164   __hdr.fidoflags |= (word)(__msg->attr.rcv() ? FIDO_RECEIVED  : 0);
165   __hdr.fidoflags |= (word)(__msg->attr.snt() ? FIDO_SENT      : 0);
166   __hdr.fidoflags |= (word)(__msg->attr.att() ? FIDO_ATTACH    : 0);
167   __hdr.fidoflags |= (word)(__msg->attr.trs() ? FIDO_TRANSIT   : 0);
168   __hdr.fidoflags |= (word)(__msg->attr.orp() ? FIDO_ORPHAN    : 0);
169   __hdr.fidoflags |= (word)(__msg->attr.k_s() ? FIDO_KILLSENT  : 0);
170   __hdr.fidoflags |= (word)(__msg->attr.loc() ? FIDO_LOCAL     : 0);
171   __hdr.fidoflags |= (word)(__msg->attr.hld() ? FIDO_HOLD      : 0);
172   __hdr.fidoflags |= (word)(__msg->attr.rsv() ? FIDO_RESERVED  : 0);
173   __hdr.fidoflags |= (word)(__msg->attr.frq() ? FIDO_FREQ      : 0);
174   __hdr.fidoflags |= (word)(__msg->attr.rrq() ? FIDO_RETRECREQ : 0);
175   __hdr.fidoflags |= (word)(__msg->attr.rrc() ? FIDO_RETREC    : 0);
176   __hdr.fidoflags |= (word)(__msg->attr.arq() ? FIDO_AUDITREQ  : 0);
177   __hdr.fidoflags |= (word)(__msg->attr.urq() ? FIDO_UPDREQ    : 0);
178 
179   __hdr.cost = __msg->cost;
180 
181   memcpy(__hdr.reserved, __msg->wildcat.reserved, 20);
182 
183   char* _txt = NULL;
184   uint _reln = Msgn->ToReln(__msg->msgno)-1;
185   int32_t _datstart = data->idx[_reln].offset;
186 
187   if(__mode & GMSG_TXT) {
188 
189     _txt = throw_strdup(__msg->txt);
190     word _size = (word)strlen(_txt);
191     __hdr.msgbytes = _size;
192     word n = 0;
193     char* ptr = _txt;
194     while(n < _size) {
195       if(*ptr == CTRL_A)
196         *ptr = NUL;
197       ptr++;
198       n++;
199     }
200 
201     if((__mode & GMSG_NEW) or (_size > __hdr.msgbytes)) {
202       if(_size > __hdr.msgbytes) {
203         uint32_t _magic = MagicHeaderInactive;
204         lseekset(data->fhdat, data->idx[_reln].offset);
205         write(data->fhdat, &_magic, 4);
206       }
207       _datstart = filelength(data->fhdat);
208       data->idx[_reln].msgno = __hdr.msgno;
209       data->idx[_reln].offset = _datstart;
210       lseekset(data->fhix, sizeof(WCatBase)+(_reln*sizeof(WCatIdx)));
211       write(data->fhix, &data->idx[_reln], sizeof(WCatIdx));
212     }
213     __hdr.msgbytes = _size;
214   }
215 
216   lseekset(data->fhdat, _datstart);
217   write(data->fhdat, &__hdr, sizeof(WCatHdr));
218   if(_txt) {
219     write(data->fhdat, _txt, __hdr.msgbytes);
220     throw_free(_txt);
221   }
222 
223   if(not _was_locked)
224     unlock();
225 
226   GFTRK(0);
227 }
228 
229 
230 //  ------------------------------------------------------------------
231 
save_hdr(int __mode,gmsg * __msg)232 void WCatArea::save_hdr(int __mode, gmsg* __msg) {
233 
234   GFTRK("WCatSaveHdr");
235 
236   WCatHdr _hdr;
237   save_message(__mode|GMSG_HDR, __msg, _hdr);
238 }
239 
240 
241 //  ------------------------------------------------------------------
242 
save_msg(int __mode,gmsg * __msg)243 void WCatArea::save_msg(int __mode, gmsg* __msg) {
244 
245   GFTRK("WCatSaveMsg");
246 
247   WCatHdr _hdr;
248   save_message(__mode|GMSG_HDRTXT, __msg, _hdr);
249 }
250 
251 
252 //  ------------------------------------------------------------------
253 
del_msg(gmsg * __msg)254 void WCatArea::del_msg(gmsg* __msg) {
255 
256   GFTRK("WCatDelMsg");
257 
258   WCatHdr _hdr;
259   save_message(GMSG_HDR | GMSG_DELETE, __msg, _hdr);
260 }
261 
262 
263 //  ------------------------------------------------------------------
264 
new_msgno(gmsg * __msg)265 void WCatArea::new_msgno(gmsg* __msg) {
266 
267   GFTRK("WCatNewMsgno");
268 
269   __msg->msgno = data->base.nextmsgno;
270 
271   GFTRK(0);
272 }
273 
274 
275 //  ------------------------------------------------------------------
276 
update_timesread(gmsg *)277 void WCatArea::update_timesread(gmsg*) {
278 
279 }
280 
281 
282 //  ------------------------------------------------------------------
283 
284