1 /*****************************************************************************/
2 /*                                                                           */
3 /*                 (C) Copyright 1991-1997  Alberto Pasquale                 */
4 /*                                                                           */
5 /*                   A L L   R I G H T S   R E S E R V E D                   */
6 /*                                                                           */
7 /*****************************************************************************/
8 /*                                                                           */
9 /* This source code is NOT in the public domain and it CANNOT be used or     */
10 /* distributed without written permission from the author.                   */
11 /*                                                                           */
12 /*****************************************************************************/
13 /*                                                                           */
14 /*   How to contact the author:  Alberto Pasquale of 2:332/504@fidonet       */
15 /*                               Viale Verdi 106                             */
16 /*                               41100 Modena                                */
17 /*                               Italy                                       */
18 /*                                                                           */
19 /*****************************************************************************/
20 
21 // Response.Cpp
22 
23 
24 #ifdef __OS2__
25     #define INCL_DOS
26     #include <os2.h>
27 #endif
28 
29 #include <apgenlib.hpp>
30 #include <stdarg.h>
31 #include <string.h>
32 #include <stdlib.h>
33 #include <time.h>
34 #include <string.h>
35 #include "response.hpp"
36 #include "misc.hpp"
37 #include "msgapier.hpp"
38 
39 void closekeeprsp (_RSP *rsp);
40 
41 
_RSP(void)42 _RSP::_RSP (void)
43 {
44     rsptxt = new char[MsgSize + 160];
45 }
46 
~_RSP(void)47 _RSP::~_RSP (void)
48 {
49     delete[] rsptxt;
50 }
51 
52 
writerspheader(HAREA msg,char * from,ADR * fmadr,char * to,ADR * toadr,char * subject,word flags,char * origin)53 _RSP *writerspheader (HAREA msg, char *from, ADR *fmadr, char *to, ADR *toadr, char *subject, word flags, char *origin)
54 {
55     time_t timer;
56     _RSP *rsp;
57     XMSG *rsphd;
58 
59     rsp = new _RSP;
60     rsp->msg = msg;
61     rsphd = &rsp->rsphd;
62 
63     memset (rsphd, 0, sizeof (XMSG));
64     rsphd->attr = flags | MSGLOCAL;
65     strzcpy ((char *)rsphd->from, from, XMSG_FROM_SIZE);
66     rsphd->orig = *fmadr;
67     strzcpy ((char *)rsphd->to, to, XMSG_TO_SIZE);
68     rsphd->dest = *toadr;
69     strzcpy ((char *)rsphd->subj, subject, XMSG_SUBJ_SIZE);
70 
71     timer = time (NULL);
72     rsp->unix4ascii = timer;   // save time for ASCII, to avoid dupes with Squish
73     UnixToFTime ((char *)rsphd->__ftsc_date, timer);  // FTS-0001 Datime
74     getmsgdate (&(rsphd->date_written), timer);
75     rsphd->date_arrived = rsphd->date_written;
76 
77     rsp->rsplen = 0;
78     rsp->part = 0;
79     rsp->partpos = strlen ((char *)rsphd->subj);
80     rsp->origin = origin;
81 
82     return rsp;
83 }
84 
85 
vwritersp(_RSP * rsp,char * strfmt,va_list args)86 void vwritersp (_RSP *rsp, char *strfmt, va_list args)
87 {
88     if (!rsp)
89         return;
90 
91     char format[100];
92 
93     char *c = strfmt;     // convert \n to \r
94     char *d = format;
95     while (*c) {
96         if (*c == '\n')
97             *d = '\r';
98         else
99             *d = *c;
100         c ++;
101         d ++;
102     }
103     *d = '\0';
104 
105     char *rspline = new char[2048];
106 
107     vsprintf (rspline, format, args);
108 
109     uint linelen = strlen (rspline);
110 
111     if ((rsp->rsplen + linelen) > MsgSize) { /* assure correct termination */
112         if (rsp->part == 0)
113             rsp->part = 1;
114         closekeeprsp (rsp);
115         rsp->rsplen = 0;
116     }
117     strcpy (rsp->rsptxt+rsp->rsplen, rspline);
118     rsp->rsplen += linelen;
119 
120     if ((rsp->rsplen > (MsgSize - 80)) &&   /* keep the full line together */
121         (rspline[linelen-1] == '\r')) {
122 		if (rsp->part == 0)
123 			rsp->part = 1;
124 		closekeeprsp (rsp);
125         rsp->rsplen = 0;
126     }
127 
128     delete[] rspline;
129 }
130 
131 
writersp(_RSP * rsp,char * strfmt,...)132 void writersp (_RSP *rsp, char *strfmt,...)
133 {
134     if (!rsp)
135         return;
136 
137     va_list args;
138 
139     va_start (args, strfmt);
140     vwritersp (rsp, strfmt, args);
141     va_end (args);
142 }
143 
144 
closekeeprsp(_RSP * rsp)145 void closekeeprsp (_RSP *rsp)
146 {
147     char parts[30];
148     char msgids[80];   /* ^A Kludge (ASCIIZ) for ^AMSGID */
149     char buff[81];     /* Tear line or origin */
150     HMSG msgh;
151 
152     if (rsp->part != 0) {
153         sprintf (parts, " (Part #%u)", rsp->part++);
154         strcpy ((char *)rsp->rsphd.subj + __min (rsp->partpos, XMSG_SUBJ_SIZE - 1 - strlen (parts)), parts);
155         UnixToFTime ((char *)rsp->rsphd.__ftsc_date, rsp->unix4ascii - 1 + rsp->part);  // Unique ASCII time to avoid false dupes
156     }
157 
158     sprintf (msgids, "MSGID: %s %08lX", addrs (&rsp->rsphd.orig), uid ());
159 
160     sprintf (buff, "\r\r--- FastLst "VER"\r");
161     strcpy (rsp->rsptxt+rsp->rsplen, buff);
162     rsp->rsplen += strlen (buff);
163     if (rsp->origin) {
164         SetOrigin (buff, rsp->origin, &rsp->rsphd.orig);
165         strcpy (rsp->rsptxt+rsp->rsplen, buff);
166         rsp->rsplen += strlen (buff);
167     }
168 
169     msgh = MsgOpenMsg (rsp->msg, MOPEN_CREATE, 0);
170     if (msgh == NULL) {
171         wr_mapi_err ();
172         return;
173     }
174 
175     if (MsgWriteMsg (msgh, 0, &rsp->rsphd, (byte *)rsp->rsptxt, rsp->rsplen + 1, rsp->rsplen + 1, strlen (msgids) + 1, (byte *)msgids))
176         wr_mapi_err ();
177 
178     if (MsgCloseMsg (msgh))
179         wr_mapi_err ();
180 }
181 
182 
closersp(_RSP * rsp,BOOL thrash)183 void closersp (_RSP *rsp, BOOL thrash)
184 {
185     if (!thrash)
186         closekeeprsp (rsp);
187     delete rsp;
188 }
189 
190 
getmsgdate(struct _stamp * date,time_t timer)191 void getmsgdate(struct _stamp *date, time_t timer)
192 {
193     struct tm *timeptr;
194 
195     timeptr=localtime(&timer);
196 
197     date->date.da = (word) timeptr->tm_mday;
198     date->date.mo = (word) (timeptr->tm_mon + 1);
199     date->date.yr = (word) (timeptr->tm_year - 80);
200     date->time.ss = (word) (timeptr->tm_sec/2);
201     date->time.mm = (word) (timeptr->tm_min);
202     date->time.hh = (word) (timeptr->tm_hour);
203 }
204 
205 
uid(void)206 dword uid (void)
207 {
208     static dword lastid = 0;
209     dword timeid;
210 
211     timeid = time (NULL) << 4;
212     while (timeid <= lastid)
213         timeid++;
214 
215     lastid = timeid;
216 
217     return (timeid);
218 }
219 
220 
SetOrigin(char * buffer,char * origin,ADR * adr)221 void SetOrigin (char *buffer, char *origin, ADR *adr)
222 {
223     char *c;
224 
225     c = addrs (adr);            // max 79chars in origin
226     sprintf (buffer, " * Origin: %.*s (%s)\r", 65 - (int)strlen(c), origin, c);
227 }
228 
229 
230 
231