home *** CD-ROM | disk | FTP | other *** search
/ Power Programming / powerprogramming1994.iso / progtool / bss / pup.arc / FIDO2PUP.C < prev    next >
C/C++ Source or Header  |  1987-11-17  |  6KB  |  266 lines

  1. #include <puppy.h>
  2. #include <ascii.h>
  3. #include <xfbuf.h>
  4.  
  5. /* Message header structure. The message text is just a long string. The
  6. zone:net/node of course do not conform to the new (v12) standard. */
  7.  
  8. /* PUBLIC */
  9. struct _omsg {
  10.     char from[36];        /* who from, */
  11.     char to[36];        /* who to, */
  12.     char subj[72];        /* message subject, */
  13.     char date[20];        /* creation date, */
  14.     int times;        /* number of times read, */
  15.     int dest_node;        /* destination node, */
  16.     int orig_node;        /* originating node */
  17.     int cost;        /* actual cost of this msg */
  18.     int orig_net;        /* v10 originating net */
  19.     int dest_net;        /* v10 destination net */
  20.     int dest_zone;        /* v12 originating zone */
  21.     int orig_zone;        /* v12 destination zone */
  22.     int dest_point;
  23.     int orig_point;
  24.     unsigned reply;        /* thread to previous msg. (reply-to) */
  25.     unsigned attr;        /* message type, see below */
  26.     unsigned up;        /* thread to next msg. (replied-to) */
  27. };
  28.  
  29. struct _pup pup;        /* main system file */
  30.  
  31. struct _omsg omsg;    /* Fido message header */
  32. struct _msg *msg;    /* msg base index */
  33. int oldfile;        /* Fido msg file */
  34. int msgfile;        /* message index file */
  35. int txtfile;        /* message text file */
  36. int pupfile;        /* PUPPY.SYS file */
  37.  
  38. int msg_total;
  39. int msg_highest;
  40.  
  41. /* Local text buffer */
  42.  
  43. char *text;        /* work buffer */
  44. unsigned textsize;    /* and its size */
  45.  
  46. main(argc,argv)
  47. int argc;
  48. char **argv;
  49. {
  50. int i,n;
  51. int msgnbr;        /* Fido message number */
  52. long o;
  53. char *cp;
  54.  
  55.     printf("Fido to Puppy message converter, 17 Nov 87\r\n");
  56.     printf("Tom Jennings, 164 Shipley\r\n");
  57.     printf("San Francisco CA 94107 USA\r\n");
  58.     printf("(k) all rights reversed\r\n");
  59.  
  60.     allmem();                /* MSDOS get all available memory */
  61.     textsize= sizmem();            /* MSDOS how much mem we have */
  62.     text= getmem(textsize);            /* MSDOS get it all, */
  63.  
  64.     i= pup.messages * sizeof(struct _msg);
  65.     if (i > textsize) {            /* allocate room for msg index */
  66.         printf("You have too many messages!\r\n");
  67.         exit(1);
  68.     }
  69.     msg= (struct _msg *) text;        /* ptr to message file index */
  70.     textsize -= i;                /* account for it, */
  71.     text += i;                /* advance the pointer */
  72.  
  73.     if (pup.msgsize > textsize) {        /* room for message entry */
  74.         printf("Message-size is too big!\r\n");
  75.         exit(1);
  76.     }
  77.  
  78.     pupfile= open("puppy.sys",2);        /* load the system file */
  79.     if (pupfile == -1) {
  80.         printf("Can't find PUPPY.SYS\r\n");
  81.         exit(1);
  82.     }
  83.     read(pupfile,&pup,sizeof(struct _pup));    /* read it in, */
  84.  
  85.     msgcount();                /* count Fido messages */
  86.     printf("There are %d Fido messages\r\n",msg_total);
  87.  
  88.     openmsg();                /* open the message base */
  89.     msgnbr= 1;
  90.     while (msgnbr= findmsg(msgnbr)) {
  91.         if (--pup.top < 0) 
  92.             pup.top= pup.messages - 1;/* next message ... */
  93.         i= recd(0);            /* new goes at the top */
  94.         strncpy(msg[i].to,omsg.to,35);
  95.         strncpy(msg[i].from,omsg.from,35);
  96.         strncpy(msg[i].subj,omsg.subj,35);
  97.         fixtime(omsg.date,&msg[i].time);
  98.         msg[i].attr= 0;
  99.         msg[i].topic= 1;
  100.         msg[i].topic_map= 0;
  101.  
  102.         n= read(oldfile,text,textsize);    /* read the Fido msg text, */
  103.         if (n >= pup.msgsize)         /* fix max size */
  104.             n= pup.msgsize - 1;    /* plus room for ^Z */
  105.  
  106.         msg[i].attr |= MSGEXISTS;    /* new message */
  107.         writemsg(i);            /* write out the msg header, */
  108.  
  109.         o= 0L + i;
  110.         o *= pup.msgsize;        /* copy the msg body */
  111.         lseek(txtfile,o,0);        /* seek there, */
  112.         write(txtfile,text,n);
  113.  
  114.         n= pup.msgsize - n;        /* amt we need to fill out msg */
  115.         cp= text; for (i= n; i--;) 
  116.             *cp++= SUB;        /* fill with ^Zs */
  117.         write(txtfile,text,n);        /* pad it out */
  118.  
  119.         close(oldfile);            /* done with it, */
  120.         ++msgnbr;            /* next Fido message ... */
  121.     }
  122.     closemsg();
  123.     lseek(pupfile,0L,0);            /* keep top */
  124.     write(pupfile,&pup,sizeof(struct _pup));
  125.     close(pupfile);
  126. }
  127.  
  128. /* Convert message number (1 - N) into record number (0 - N). */
  129.  
  130. recd(n)
  131. int n;
  132. {
  133. int i;
  134.     i= ((n - 1 + pup.top) % pup.messages);
  135.     return(i);
  136. }
  137.  
  138. /* Open the message base; open both files, load the index. */
  139.  
  140. openmsg() {
  141.  
  142.     msgfile= open("message.idx",2);        /* open the msg base */
  143.     txtfile= open("message.dat",2);
  144.     if ((msgfile < 0) || (txtfile < 0)) {
  145.         printf("Message base file(s) missing!\r\n");
  146.  
  147.     } else read(msgfile,msg,pup.messages * sizeof(struct _msg));
  148. }
  149.  
  150. /* Close the message base. */
  151.  
  152. closemsg() {
  153.  
  154.     if (msgfile != -1) close(msgfile);
  155.     if (txtfile != -1) close(txtfile);
  156.     msgfile= txtfile= -1;
  157. }
  158.  
  159. /* Count the number of messages. */
  160.  
  161. msgcount() {
  162. char spec[SS];
  163. int n;
  164. struct _xfbuf xfbuf;
  165.  
  166.     msg_total= msg_highest= 0;
  167.     xfbuf.s_attrib= 0;
  168.     while (_find("*.MSG",msg_total,&xfbuf)) {
  169.         ++msg_total;         /* count another, */
  170.         n= atoi(xfbuf.name);    /* change name to number */
  171.         if (n > msg_highest) msg_highest= n;    /* pick highest one, */
  172.     }
  173. }
  174.  
  175. /* Find a message by number. This will loop up or down, or not at all,
  176. depending on the flag passed. The file, if found, is left open (global
  177. 'msgfile' so that it can be worked on. This returns "not found" or the 
  178. message is skipped if it doesnt meet various criteria, such as privacy,
  179. being forwarded, etc. best to look at the code. */
  180.  
  181. findmsg(num)
  182. int num;
  183. {
  184. char *cp,name[SS];
  185.  
  186.     while (1) {
  187.         if (num > msg_highest) return(0);
  188.         sprintf(name,"%d.MSG",num);
  189.         oldfile= open(name,0);
  190.         if (oldfile != -1) {
  191.             read(oldfile,&omsg,sizeof(struct _omsg));
  192.             return(num);
  193.         }
  194.         ++num;
  195.     }
  196. }
  197.  
  198. /* Functions to calculate differences in days and minutes from an ASCII string:
  199.  
  200.     "dd Mon yy  hh:mm:ss"    (exactly)
  201.  
  202.     [0]    day of month,
  203.     [3]    Month, (3 char ASCII MSDOS name)
  204.     [7]    year, two digits,
  205.     [11]    hours,
  206.     [14]    minutes,
  207.     [17]    seconds.
  208.  
  209. */
  210.  
  211. /* Convert a Fido string date into a Puppy struct time. */
  212.  
  213. fixtime(s,t)
  214. char *s;
  215. struct _time *t;
  216. {
  217.  
  218.     t-> year= atoi(&s[7]);
  219.     t-> month= getmn(&s[3]); 
  220.     t-> day= atoi(&s[0]);
  221.     t-> hour= atoi(&s[11]);
  222.     t-> minute= atoi(&s[14]); 
  223.     t-> second= 0;
  224. }
  225.  
  226. /* Return which month this is. */
  227.  
  228.  
  229. char months[13][4] = {
  230.     "XXX",        /* make index 1 - 12 */
  231.     "Jan",
  232.     "Feb",
  233.     "Mar",
  234.     "Apr",
  235.     "May",
  236.     "Jun",
  237.     "Jul",
  238.     "Aug",
  239.     "Sep",
  240.     "Oct",
  241.     "Nov",
  242.     "Dec"
  243. };
  244.  
  245. getmn(s)
  246. char *s;
  247. {
  248. int i;
  249.  
  250.     for (i= 1; i < 13; i++) {
  251.         if (stcpma(s,months[i])) return(i);
  252.     }
  253.     return(1);
  254. }
  255. /* Write out the specified message header record. */
  256.  
  257. writemsg(n)
  258. int n;
  259. {
  260. long o;
  261.     o= 0L + n;
  262.     o *= sizeof(struct _msg);
  263.     lseek(msgfile,o,0);
  264.     write(msgfile,&msg[n],sizeof(struct _msg));
  265. }
  266.