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: gmofido3.cpp,v 1.10 2006/05/14 11:45:05 ssianky Exp $
23 //  ------------------------------------------------------------------
24 //  Fido/Opus/FTSC (*.MSG) type handling
25 //  ------------------------------------------------------------------
26 
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <gmemdbg.h>
30 #include <gdbgtrk.h>
31 #include <gstrall.h>
32 #include <gmofido.h>
33 
34 
35 //  ------------------------------------------------------------------
36 
load_message(int __mode,gmsg * __msg,FidoHdr & __hdr)37 int FidoArea::load_message(int __mode, gmsg* __msg, FidoHdr& __hdr) {
38 
39   // Build message filename
40   Path _msgfile;
41   build_msgname(_msgfile, __msg->msgno);
42 
43   // Open the message file
44   int _fh = test_open(_msgfile, O_RDONLY|O_BINARY, WideSharemode);
45   if(_fh == -1) {
46     GFTRK(0);
47     return false;
48   }
49 
50   // Get file info
51   struct stat st;
52   stat(_msgfile, &st);
53 
54   // Read header
55   memset(&__hdr, 0, sizeof(FidoHdr));
56   read(_fh, &__hdr, sizeof(FidoHdr));
57 
58   __msg->link.to_set(__hdr.replyto);
59   __msg->link.first_set(__hdr.reply1st);
60 
61   __msg->cost      = __hdr.cost;
62   __msg->timesread = __hdr.timesread;
63 
64   strxcpy(__msg->by, __hdr.by, 36);
65   strxcpy(__msg->to, __hdr.to, 36);
66   strxcpy(__msg->re, __hdr.re, 72);
67 
68   if(isopus()) {
69     __msg->orig.zone  = __msg->oorig.zone  = 0;
70     __msg->orig.net   = __msg->oorig.net   = __hdr.orignet;
71     __msg->orig.node  = __msg->oorig.node  = __hdr.orignode;
72     __msg->orig.point = __msg->oorig.point = 0;
73 
74     __msg->dest.zone  = __msg->odest.zone  = 0;
75     __msg->dest.net   = __msg->odest.net   = __hdr.destnet;
76     __msg->dest.node  = __msg->odest.node  = __hdr.destnode;
77     __msg->dest.point = __msg->odest.point = 0;
78 
79     // Convert datetime
80     __msg->written = FTimeToTime(&__hdr.opus.written);
81     __msg->arrived = FTimeToTime(&__hdr.opus.arrived);
82   }
83   else {
84     __msg->orig.zone  = __msg->oorig.zone  = __hdr.ftsc.origzone;
85     __msg->orig.net   = __msg->oorig.net   = __hdr.orignet;
86     __msg->orig.node  = __msg->oorig.node  = __hdr.orignode;
87     __msg->orig.point = __msg->oorig.point = __hdr.ftsc.origpoint;
88 
89     __msg->dest.zone  = __msg->odest.zone  = __hdr.ftsc.destzone;
90     __msg->dest.net   = __msg->odest.net   = __hdr.destnet;
91     __msg->dest.node  = __msg->odest.node  = __hdr.destnode;
92     __msg->dest.point = __msg->odest.point = __hdr.ftsc.destpoint;
93 
94     __msg->written = __msg->arrived = 0;
95   }
96 
97   __msg->written = __msg->written ? __msg->written : FidoTimeToUnix(__hdr.datetime);
98 
99   if (__msg->arrived == 0)
100   {
101     time32_t a  = gtime(NULL);
102     struct tm tp; ggmtime(&tp, &a);
103     tp.tm_isdst = -1;
104     time32_t b  = gmktime(&tp);
105     __msg->arrived = a + a - b;
106   }
107 
108   // Transfer attributes
109   __msg->attr.pvt(__hdr.attr & FIDO_PVT);
110   __msg->attr.cra(__hdr.attr & FIDO_CRASH);
111   __msg->attr.rcv(__hdr.attr & FIDO_RECEIVED);
112   __msg->attr.snt(__hdr.attr & FIDO_SENT);
113   __msg->attr.att(__hdr.attr & FIDO_ATTACH);
114   __msg->attr.trs(__hdr.attr & FIDO_TRANSIT);
115   __msg->attr.orp(__hdr.attr & FIDO_ORPHAN);
116   __msg->attr.k_s(__hdr.attr & FIDO_KILLSENT);
117   __msg->attr.loc(__hdr.attr & FIDO_LOCAL);
118   __msg->attr.hld(__hdr.attr & FIDO_HOLD);
119   __msg->attr.rsv(__hdr.attr & FIDO_RESERVED);
120   __msg->attr.frq(__hdr.attr & FIDO_FREQ);
121   __msg->attr.rrq(__hdr.attr & FIDO_RETRECREQ);
122   __msg->attr.rrc(__hdr.attr & FIDO_RETREC);
123   __msg->attr.arq(__hdr.attr & FIDO_AUDITREQ);
124   __msg->attr.urq(__hdr.attr & FIDO_UPDREQ);
125   __msg->attr.lok(not (st.st_mode & S_IWUSR));
126 
127   // Set the unsent attribute
128   if(isnet() or (isecho() and not wide->fidohwmarks))
129     __msg->attr.uns((__hdr.attr & FIDO_LOCAL) and not (__hdr.attr & FIDO_SENT));
130   else if(isecho() and wide->fidohwmarks) {
131     __msg->attr.uns(data->highwatermark <  __msg->msgno);
132     __msg->attr.snt(data->highwatermark >= __msg->msgno);
133   }
134   else
135     __msg->attr.uns0();
136 
137   // If message text is used
138   if(__mode & GMSG_TXT) {
139 
140     // Get length of message text
141     size_t _fillen = filelength(_fh);
142     uint _txtlen = (uint) ((_fillen >= sizeof(FidoHdr)) ? (_fillen - sizeof(FidoHdr)) : 0);
143 
144     // Allocate space for the message text
145     __msg->txt = (char*)throw_calloc(1, _txtlen+256);
146 
147     // Read the message text
148     read(_fh, __msg->txt, (uint)_txtlen);
149 
150     // Fix msgs handled by brain-dead programs
151     register char* _ptr = __msg->txt;
152     if(!*_ptr)
153       if(_txtlen)
154         *_ptr = LF;  // Quick fix
155 
156     // Fix complete msg
157     if(wide->fidonullfix) {
158       for(uint _count=0; _count<_txtlen; _count++,_ptr++)
159         if(!*_ptr)
160           *_ptr = LF;
161     }
162   }
163 
164   // Close the message file
165   ::close(_fh);
166 
167   GFTRK(0);
168 
169   // Success
170   return true;
171 }
172 
173 
174 //  ------------------------------------------------------------------
175 
load_hdr(gmsg * __msg)176 int FidoArea::load_hdr(gmsg* __msg) {
177 
178   GFTRK("FidoLoadHdr");
179 
180   FidoHdr _hdr;
181   return load_message(GMSG_HDR, __msg, _hdr);
182 }
183 
184 
185 //  ------------------------------------------------------------------
186 
load_msg(gmsg * __msg)187 int FidoArea::load_msg(gmsg* __msg) {
188 
189   GFTRK("FidoLoadMsg");
190 
191   FidoHdr _hdr;
192   return load_message(GMSG_HDRTXT, __msg, _hdr);
193 }
194 
195 
196 //  ------------------------------------------------------------------
197