|
|
1.1 root 1: /* UTIIMPRT.C */
2:
3: /* Developed 1990-1997 by Rob Swindell; PO Box 501, Yorba Linda, CA 92885 */
4:
5: #include "sbbs.h"
6: #include "uti.h"
7: #include "crc32.h"
8:
9: smb_t smb;
10:
11: /****************************************************************************/
12: /* Updates 16-bit "rcrc" with character 'ch' */
13: /****************************************************************************/
14: void ucrc16(uchar ch, ushort *rcrc) {
15: ushort i, cy;
16: uchar nch=ch;
17:
18: for (i=0; i<8; i++) {
19: cy=*rcrc & 0x8000;
20: *rcrc<<=1;
21: if (nch & 0x80) *rcrc |= 1;
22: nch<<=1;
23: if (cy) *rcrc ^= 0x1021; }
24: }
25:
26: /****************************************************************************/
27: /* Returns 16-crc of string (not counting terminating NULL) */
28: /****************************************************************************/
29: ushort crc16(char *str)
30: {
31: int i=0;
32: ushort crc=0;
33:
34: ucrc16(0,&crc);
35: while(str[i])
36: ucrc16(str[i++],&crc);
37: ucrc16(0,&crc);
38: ucrc16(0,&crc);
39: return(crc);
40: }
41:
42:
43: #define PRIVATE 0
44: #define PUBLIC 1
45:
46: #define DEBUG 0
47:
48:
49: char *wday[]={"Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
50: char *mon[]={"Jan","Feb","Mar","Apr","May","Jun"
51: ,"Jul","Aug","Sep","Oct","Nov","Dec"};
52:
53: void remove_re(char *str)
54: {
55: while(!strnicmp(str,"RE:",3)) {
56: strcpy(str,str+3);
57: while(str[0]==SP)
58: strcpy(str,str+1); }
59: }
60:
61: /****************************************************************************/
62: /* Converts a date string in format MM/DD/YY into unix time format */
63: /****************************************************************************/
64: time_t dstrtounix(char *str)
65: {
66: struct time curtime;
67: struct date date;
68:
69: #if DEBUG
70: printf("\rdstrtounix ");
71: #endif
72:
73: curtime.ti_hour=curtime.ti_min=curtime.ti_sec=0;
1.1.1.2 ! root 74: date.da_year=((str[6]&0xf)*10)+(str[7]&0xf);
! 75: if(date.da_year<Y2K_2DIGIT_WINDOW)
! 76: date.da_year+=100;
! 77: date.da_year+=1900;
1.1 root 78: date.da_mon=((str[0]&0xf)*10)+(str[1]&0xf);
79: date.da_day=((str[3]&0xf)*10)+(str[4]&0xf);
80: return(dostounix(&date,&curtime));
81: }
82:
83: /****************************************************************************/
84: /* Checks the disk drive for the existence of a file. Returns 1 if it */
85: /* exists, 0 if it doesn't. */
86: /* Called from upload */
87: /****************************************************************************/
88: char fexist(char *filespec)
89: {
90: struct ffblk f;
91:
92: if(findfirst(filespec,&f,0)==NULL)
93: return(1);
94: return(0);
95: }
96:
97: /****************************************************************************/
98: /* This function reads files that are potentially larger than 32k. */
99: /* Up to one megabyte of data can be read with each call. */
100: /****************************************************************************/
101: long lread(int file, char huge *buf,long bytes)
102: {
103: long count;
104:
105: for(count=bytes;count>32767;count-=32767,buf+=32767)
106: if(read(file,(char *)buf,32767)!=32767)
107: return(-1L);
108: if(read(file,(char *)buf,(int)count)!=count)
109: return(-1L);
110: return(bytes);
111: }
112:
113:
114: int main(int argc, char **argv)
115: {
116: char str[256],to[256],from[256],title[256],*p,*buf,*outbuf;
117: ushort xlat,net=0;
118: int i,j,file,lzh,storage;
119: uint subnum,imported=0;
120: ulong l,length,lzhlen,offset,crc;
121: FILE *stream;
122: smbmsg_t msg;
123: smbstatus_t status;
124:
125: PREPSCREEN;
126:
127: printf("Synchronet UTIIMPRT v%s\n",VER);
128:
129: if(argc<3)
130: exit(1);
131:
132: if((argc>3 && !stricmp(argv[3],"/NETWORK"))
133: || (argc>4 && !stricmp(argv[4],"/NETWORK")))
134: net=NET_POSTLINK;
135:
136: uti_init("UTIIMPRT",argc,argv);
137:
138: if((file=nopen(argv[2],O_RDONLY))==-1)
139: bail(2);
140: if((stream=fdopen(file,"rb"))==NULL)
141: bail(2);
142:
143: subnum=getsubnum(argv[1]);
144: if((int)subnum==-1)
145: bail(7);
146:
147: sprintf(smb.file,"%s%s",sub[subnum]->data_dir,sub[subnum]->code);
148: smb.retry_time=30;
149: if((i=smb_open(&smb))!=0) {
150: errormsg(WHERE,ERR_OPEN,smb.file,i);
151: bail(5); }
152:
153: if(filelength(fileno(smb.shd_fp))<1) { /* Create it if it doesn't exist */
154: smb.status.max_crcs=sub[subnum]->maxcrcs;
155: smb.status.max_msgs=sub[subnum]->maxmsgs;
156: smb.status.max_age=sub[subnum]->maxage;
157: smb.status.attr=sub[subnum]->misc&SUB_HYPER ? SMB_HYPERALLOC : 0;
158: if((i=smb_create(&smb))!=0) {
159: errormsg(WHERE,ERR_CREATE,smb.file,i);
160: bail(5); } }
161:
162: printf("\r\nImporting ");
163:
164: while(!feof(stream) && !ferror(stream)) {
165: memset(&msg,0,sizeof(smbmsg_t));
166: memcpy(msg.hdr.id,"SHD\x1a",4);
167: msg.hdr.version=SMB_VERSION;
168: msg.hdr.when_imported.time=time(NULL);
169: msg.hdr.when_imported.zone=sys_timezone;
170: if(sub[subnum]->misc&SUB_AONLY)
171: msg.hdr.attr|=MSG_ANONYMOUS;
172:
173: if(!fgets(to,250,stream))
174: break;
175: if(!fgets(from,250,stream))
176: break;
177: if(!fgets(title,250,stream))
178: break;
179: imported++;
180: printf("%-5u\b\b\b\b\b",imported);
181: truncsp(to);
182: truncsp(from);
183: truncsp(title);
184:
185: smb_hfield(&msg,RECIPIENT,strlen(to),to);
186: strlwr(to);
187: msg.idx.to=crc16(to);
188:
189: smb_hfield(&msg,SENDER,strlen(from),from);
190: strlwr(from);
191: msg.idx.from=crc16(from);
192:
193: if(net)
194: i=smb_hfield(&msg,SENDERNETTYPE,2,&net);
195:
196: i=smb_hfield(&msg,SUBJECT,strlen(title),title);
197: strlwr(title);
198: remove_re(title);
199: msg.idx.subj=crc16(title);
200:
201: fgets(str,128,stream); /* skip msg # */
202: fgets(str,128,stream); /* ref # */
203: msg.hdr.thread_orig=atol(str);
204: fgets(str,128,stream); /* date */
205: msg.hdr.when_written.time=dstrtounix(str);
206: fgets(str,128,stream); /* time */
207: msg.hdr.when_written.time+=atoi(str)*60*60; /* hours */
208: p=strchr(str,':');
209: if(p)
210: msg.hdr.when_written.time+=atoi(p+1)*60; /* mins */
211: fgets(str,128,stream); /* private/public */
212: if(!stricmp(str,"PRIVATE"))
213: msg.hdr.attr|=MSG_PRIVATE;
214: fgets(str,128,stream); /* Read? Y/N */
215: if(toupper(str[0])=='Y')
216: msg.hdr.attr|=MSG_READ;
217: fgets(str,128,stream); /* Net? Y/N - ignore */
218: if(toupper(str[0])=='Y')
219: msg.hdr.netattr|=MSG_TYPELOCAL;
220: while(!feof(stream) && !ferror(stream)) {
221: fgets(str,128,stream);
222: if(!strcmp(str,"TEXT:\r\n"))
223: break; }
224:
225: buf=NULL;
226: length=0;
227: crc=0xffffffff;
228: while(!feof(stream) && !ferror(stream)) {
229: fgets(str,128,stream);
230: if(!strcmp(str,"\xff\r\n")) /* end of text */
231: break;
232: j=strlen(str);
233: if((buf=REALLOC(buf,length+j+1))==NULL) {
234: errormsg(WHERE,ERR_ALLOC,argv[1],length+j+1);
235: bail(3); }
236: if(sub[subnum]->maxcrcs) {
237: for(i=0;i<j;i++)
238: crc=ucrc32(str[i],crc); }
239: strcpy(buf+length,str);
240: length+=strlen(str); }
241: crc=~crc;
242:
243: if((i=smb_locksmbhdr(&smb))!=0) {
244: errormsg(WHERE,ERR_LOCK,smb.file,i);
245: bail(11); }
246:
247: if((i=smb_getstatus(&smb))!=0) {
248: errormsg(WHERE,ERR_READ,smb.file,i);
249: bail(12); }
250:
251: if(sub[subnum]->maxcrcs) {
252: i=smb_addcrc(&smb,crc);
253: if(i) {
254: printf("\nDuplicate message!\n");
255: FREE(buf);
256: smb_unlocksmbhdr(&smb);
257: smb_freemsgmem(&msg);
258: continue; } }
259:
260: if(length>=2 && buf[length-1]==LF && buf[length-2]==CR)
261: length-=2;
262: if(length>=2 && buf[length-1]==LF && buf[length-2]==CR)
263: length-=2;
264:
265: lzh=0;
266: if(sub[subnum]->misc&SUB_LZH && length+2>=SDT_BLOCK_LEN) {
267: if((outbuf=(char *)MALLOC(length*2))==NULL) {
268: errormsg(WHERE,ERR_ALLOC,"lzh",length*2);
269: smb_unlocksmbhdr(&smb);
270: smb_freemsgmem(&msg);
271: bail(3); }
272: lzhlen=lzh_encode(buf,length,outbuf);
273: if(lzhlen>1
274: && smb_datblocks(lzhlen+4)
275: <smb_datblocks(length+2)) { /* Compressable */
276: length=lzhlen+2;
277: FREE(buf);
278: lzh=1;
279: buf=outbuf; }
280: else /* Uncompressable */
281: FREE(outbuf); }
282:
283: length+=2; /* for translation string */
284:
285: if(status.attr&SMB_HYPERALLOC) {
286: offset=smb_hallocdat(&smb);
287: storage=SMB_HYPERALLOC; }
288: else {
289: if((i=smb_open_da(&smb))!=0) {
290: errormsg(WHERE,ERR_OPEN,smb.file,i);
291: bail(5); }
292: if(sub[subnum]->misc&SUB_FAST) {
293: offset=smb_fallocdat(&smb,length,1);
294: storage=SMB_FASTALLOC; }
295: else {
296: offset=smb_allocdat(&smb,length,1);
297: storage=SMB_SELFPACK; }
298: fclose(smb.sda_fp); }
299:
300: msg.hdr.offset=offset;
301:
302: smb_dfield(&msg,TEXT_BODY,length);
303:
304: fseek(smb.sdt_fp,offset,SEEK_SET);
305: if(lzh) {
306: xlat=XLAT_LZH;
307: fwrite(&xlat,2,1,smb.sdt_fp); }
308: xlat=XLAT_NONE;
309: fwrite(&xlat,2,1,smb.sdt_fp);
310: j=SDT_BLOCK_LEN-2; /* Don't read/write more than 255 */
311: if(lzh)
312: j-=2;
313: l=0;
314: length-=2;
315: if(lzh)
316: length-=2;
317: while(l<length) {
318: if(l+j>length)
319: j=length-l;
320: fwrite(buf+l,j,1,smb.sdt_fp);
321: l+=j;
322: j=SDT_BLOCK_LEN; }
323: fflush(smb.sdt_fp);
324: FREE(buf);
325: smb_unlocksmbhdr(&smb);
326: smb_addmsghdr(&smb,&msg,storage);
327: smb_freemsgmem(&msg); }
328:
329: sprintf(str,"%20s Imported %u\r\n","",imported);
330: write(logfile,str,strlen(str));
331: printf("\nDone.\n");
332: bail(0);
333: return(0);
334: }
335:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.