home *** CD-ROM | disk | FTP | other *** search
-
- /****************************************************************/
- /* Miscellaneous routines to interface with XBBS message areas */
- /* Including routines to interface w/ other Fidonet(tm) structs */
- /* Such as *.MSG's and *.PKT's */
- /* This should be compiled to .OBJ in the TC environment in */
- /* Compact or Large model...you will get some poss. incor. */
- /* assign. warnings and put_us_in_seenbys is unfinished. */
- /* See msg structures (_msg and _xmsg) in MSGG.H */
- /* This code is Turbo C 2.0 */
- /****************************************************************/
-
- #include "msgg.h"
-
- int pascal makepkthdr (FILE *fp,word o_zone,word o_net,word o_node,
- word o_point,word d_zone,word d_net,word d_node,
- word d_point);
- int pascal append_pkt (FILE *fp, struct _msg *msg, char *hold);
- char * pascal read_msg_from_pkt (FILE *fp,struct _xmsg *msg);
- int pascal make_msg (char *filename,struct _msg *msg,char *hold);
- char * pascal read_msg (char *filename,struct _xmsg *msg);
- FILE * pascal open_pkt (char *directory, word net, word node);
- int pascal get_mess (struct _xmsg *msg,char *directory,word areano,
- word messno);
- int pascal put_mess (struct _xmsg *msg,char *directory,word areano,
- word messno,char appendit);
- char * pascal get_text (char *directory,word areano,struct _xmsg *msg);
- word pascal append_text (char far *hold, char *directory,word areano,
- ulong *start);
- void pascal strip_seenbys (char *hold);
- char * pascal find_seenbys (char *hold);
- void pascal strip_junk (char *hold);
- void pascal just_seenby_numbers (char *hold);
-
-
- typedef struct
- {
- word
- orig_node, /* originating node */
- dest_node, /* destination node */
- year, /* 1989 - nnnnn */
- month,
- day,
- hour,
- minute,
- second,
- rate, /* unused */
- ver, /* 2 */
- orig_net, /* originating net */
- dest_net; /* destination net */
- byte
- product,
- rev_lev, /* revision level */
- password[8];
- word
- qm_orig_zone,
- qm_dest_zone;
- byte
- TRASH[8];
- word
- orig_zone, /* originating zone */
- dest_zone, /* destination zone */
- orig_point, /* originating point */
- dest_point; /* destination point */
- long
- pr_data;
- } PACKETHDR;
-
-
-
-
- int pascal makepkthdr (FILE *fp,word o_zone,word o_net,word o_node,
- word o_point,word d_zone,word d_net,word d_node,
- word d_point) {
-
- /* Writes a packet header into the currently open stream fp with the
- address information passed */
-
- PACKETHDR pkt;
- struct date dos_date;
- struct time dos_time;
-
- getdate(&dos_date);
- gettime(&dos_time);
-
- pkt.orig_node=o_node;
- pkt.dest_node=d_node;
- pkt.year=(word)(1989-dos_date.da_year);
- pkt.month=(word)dos_date.da_mon;
- pkt.day=(word)dos_date.da_day;
- pkt.hour=(word)dos_time.ti_hour;
- pkt.minute=(word)dos_time.ti_min;
- pkt.second=(word)(dos_time.ti_sec/2);
- pkt.rate=0;
- pkt.ver=2;
- pkt.orig_net=o_net;
- pkt.dest_net=d_net;
- pkt.product=0;
- pkt.rev_lev=0;
- strset(pkt.password,'\0');
- pkt.qm_orig_zone=o_zone;
- pkt.qm_dest_zone=d_zone;
- strset(pkt.TRASH,'\0');
- pkt.orig_zone=d_zone;
- pkt.dest_zone=d_zone;
- pkt.orig_point=o_point;
- pkt.dest_point=d_point;
- pkt.pr_data=0L;
-
- return(fwrite(&pkt,sizeof(PACKETHDR),1,fp));
- }
-
-
-
- int pascal append_2pkt (FILE *fp, struct _msg *msg, char *hold) {
-
- /* Appends the msg passed to the open packet stream fp */
-
- long pos;
-
- fseek(fp,0L,SEEK_END);
- pos=ftell(fp);
- fseek(fp,(pos-1L),SEEK_SET);
- fwrite(&msg->orig,sizeof(word),1,fp);
- fwrite(&msg->dest,sizeof(word),1,fp);
- fwrite(&msg->orig_net,sizeof(word),1,fp);
- fwrite(&msg->dest_net,sizeof(word),1,fp);
- fwrite(&msg->attr,sizeof(word),1,fp);
- fwrite(&msg->cost,sizeof(int),1,fp);
- fwrite(msg->date,sizeof(char),20,fp);
- fputs(msg->to,fp);
- fputc(0,fp);
- fputs(msg->from,fp);
- fputc(0,fp);
- fputs(msg->subj,fp);
- fputc(0,fp);
- fwrite(hold,sizeof(char),strlen(hold),fp);
- fputc(0,fp);
- fputc(0,fp);
- return 0;
- }
-
-
-
- int pascal make_msg (char *filename,struct _msg *msg,char *hold) {
-
- /* Makes a *.MSG file filename */
-
- FILE *fp;
-
- if((!(fp=fopen(filename,"wb")))) return 0;
- fwrite(msg,sizeof(struct _msg),1,fp);
- fwrite(hold,sizeof(char),strlen(hold),fp);
- fputc(0,fp);
- fclose(fp);
- return 0;
- }
-
-
-
- char * pascal read_msg (char *filename,struct _xmsg *msg) {
-
- /* Reads in a *.MSG file filename. Returns NULL on error.
- Note that this routine dynamically allocates msg text buffer
- which must be freed by caller (i.e. free(hold) */
-
- FILE *fp;
- struct ffblk f;
- struct date dos_date;
- char *hold;
-
- if(findfirst(filename,&f,0))return 0; /* File didn't exist */
- if((!(fp=fopen(filename,"rb")))) return 0;
- hold=(char far*)farmalloc(((word)f.ff_fsize-(word)sizeof(struct _msg))+1);
- if (hold==NULL) {
- fclose(fp);
- return 0;
- }
- memset(hold,0,((word)f.ff_fsize-(word)sizeof(struct _msg))+1);
- fread(msg,sizeof(struct _msg),1,fp);
- fread(hold,sizeof(char),(word)f.ff_fsize-(word)sizeof(struct _msg),fp);
- getdate(&dos_date);
- msg->subj[63]=0;
- msg->length=(word)strlen(hold)+1;
- msg->start=0L; /* Set if needed by caller */
- msg->m_attr=0; /* Set if needed by caller to match area attrib */
- msg->d_zone=0; /* Unreliable in *.MSG Format */
- msg->o_zone=0; /* Unreliable in *.MSG Format */
- msg->d_point=0; /* Unreliable in *.MSG Format */
- msg->o_point=0; /* Unreliable in *.MSG Format */
- msg->indate[0]=(char)dos_date.da_year-1989;
- msg->indate[1]=(char)dos_date.da_mon;
- msg->indate[2]=(char)dos_date.da_day;
- msg->indate[3]=0;
- fclose(fp);
- return hold;
- }
-
-
-
-
- FILE * pascal open_pkt (char *directory, word net, word node) {
-
- /* Opens a packet in directory for net/node
- directory is created if it doesn't exist
- packet is created if it doesn't exist
- Returns file handle for packet stream (must be closed by caller) */
-
- FILE *fp;
- char filename[133];
-
- mkdir(directory);
- sprintf(filename,"%s%04x%04x",directory,net,node);
- if((!(fp=fopen(filename,"a+b")))) return NULL;
- return fp;
- }
-
-
-
- int pascal get_mess (struct _xmsg *msg,char *directory,word areano,word messno) {
-
- /* Gets msg header #messno from areano in directory
- Returns 0 on error, 1 on success */
-
- char filename[133];
- FILE *fp;
- int p;
-
- sprintf(filename,"%sXDATA.%03x",directory,areano);
-
- if((!(fp=fopen(filename,"rb"))))return 0;
- fseek(fp,(long)((long)(messno-1)*(long)sizeof(struct _xmsg)),SEEK_SET);
- p=fread(msg,sizeof(struct _xmsg),1,fp);
- fclose(fp);
- return p;
- }
-
-
-
-
- int pascal put_mess (struct _xmsg *msg,char *directory,word areano,word messno,char appendit) {
-
- /* Writes msg header #messno to areano in directory
- If appendit is non-zero, appends the msg header (messno ignored)
- Returns 0 on error, 1 on success */
-
- char filename[133];
- FILE *fp;
- int p;
-
- sprintf(filename,"%sXDATA.%03x",directory,areano);
- if((!(fp=fopen(filename,"a+b")))) return 0;
- if(!appendit) fseek(fp,(long)((long)(messno-1)*(long)sizeof(struct _xmsg)),SEEK_SET);
- else fseek(fp,0L,SEEK_END);
- p=fwrite(msg,sizeof(struct _xmsg),1,fp);
- fclose(fp);
- return p;
- }
-
-
-
- char * pascal get_text (char *directory,word areano,struct _xmsg *msg) {
-
- /* Gets the msg text of msg from areano
- in directory. Note text buffer is dynamically allocated and
- must be freed by caller. Returns NULL on error. */
-
- char filename[133];
- char *hold;
- FILE *pf;
-
- sprintf(filename,"%sXTEXT.%03x",directory,areano);
-
- if((!(pf=fopen(filename,"rb")))) return NULL;
- fseek(pf,msg->start,SEEK_SET); /* 2 seeks necessary for some reason */
- fseek(pf,msg->start,SEEK_SET);
- hold=(char far*)farmalloc(msg->length+1);
- if (hold==NULL) {
- fclose(pf);
- return NULL;
- }
- memset(hold,0,msg->length);
- fread(hold,msg->length,1,pf);
- fclose(pf);
- hold[msg->length-1]=0;
-
- /*
-
- --This commented-out routine strips out junk chars and makes sure
- there's an ending \r...FYI --
-
- while ((tempo=strstr(hold," \x8d"))) memmove(&tempo[1],&tempo[2],strlen(&tempo[2])+1);
- while ((tempo=strchr(hold,'\x8d'))) *tempo=' ';
- while ((tempo=strchr(hold,'\n'))) memmove(tempo,&tempo[1],strlen(&tempo[1])+1);
- if (hold[strlen(hold)-1]!='\r') strcat(hold,"\r");
-
- */
-
- return hold;
- }
-
-
-
-
- word pascal append_text (char far *hold, char *directory,word areano,ulong *start) {
-
- /* Appends text in hold to areano in directory. Returns 0 on error,
- # of bytes written+1 (msg.length) on success. Doesn't free
- msg text buffer! Note that start is set in the caller by this routine,
- and that start must be passed by reference, not value (usu. &msg.start) */
-
- FILE *pf;
- word messlen=0;
- char filename[133];
-
- sprintf(filename,"%sXTEXT.%03x",directory,areano);
- if((!(pf=fopen(filename,"a+b")))) return 0;
- fseek(pf,0,SEEK_END);
- *start=(ulong)ftell(pf);
- messlen+=fwrite(hold,sizeof(char),strlen(hold),pf);
- fputc('\0',pf);
- messlen+=2;
- fclose(pf);
- return (messlen);
- }
-
-
-
- char * pascal read_msg_from_pkt (FILE *fp,struct _xmsg *msg) {
-
- /* This one I'm not totally sure of, and it needs a lot of work on
- error-checking and the like. It should return the msg text
- and partially fill in the msg structure (with what's available
- in a packed msg header). You'll need to free the dynamically
- allocated 64K block it uses for the text. It returns NULL on
- error. Right now, all it does is increment the file pointer by
- one in event of a grunge, so you can try again at an offset (would
- be slow if run on a totally blasted packet!) */
-
- char *hold;
- char *tempo;
- char *here;
- word p;
- long pos;
- long origpos;
-
- if(feof(fp)) return NULL;
- pos=ftell(fp);
- origpos=pos;
- hold=(char *)farmalloc(65536L);
- if (hold==NULL) {
- fclose(fp);
- return NULL;
- }
- memset(hold,0,(word)65535);
- p=fread(hold,sizeof(char),(word)65535,fp);
- hold[65535]=0;
- if(p==0) {
- farfree(hold);
- return NULL;
- }
- if(((word)*hold)!=2) {
- printf("\nGrunged msg...uh oh...\n");
- fseek(fp,pos+1L,SEEK_SET);
- farfree(hold);
- return NULL;
- }
- msg->orig=(word)hold[2];
- msg->dest=(word)hold[4];
- msg->orig_net=(word)hold[6];
- msg->dest_net=(word)hold[8];
- msg->attr=(word)hold[10];
- msg->cost=(word)hold[12];
- here=(char *)memchr(&hold[14],0,20);
- if(here==NULL) {
- printf("\nGrunged msg...uh oh...\n");
- fseek(fp,origpos+1L,SEEK_SET);
- farfree(hold);
- return NULL;
- }
- strncpy(msg->date,&hold[14],20);
- msg->date[19]=0;
- pos=pos+24;
- tempo=&hold[24];
- here=(char *)memchr(tempo,0,36);
- if(here==NULL) {
- printf("\nGrunged msg...uh oh...\n");
- fseek(fp,origpos+1L,SEEK_SET);
- farfree(hold);
- return NULL;
- }
- strcpy(msg->to,tempo);
- tempo=here+1;
- pos=pos+strlen(msg->to);
- here=(char *)memchr(tempo,0,36);
- if(here==NULL) {
- printf("\nGrunged msg...uh oh...\n");
- fseek(fp,origpos+1L,SEEK_SET);
- farfree(hold);
- return NULL;
- }
- strcpy(msg->from,tempo);
- tempo=here+1;
- pos=pos+strlen(msg->from);
- here=(char *)memchr(tempo,0,72);
- if(here==NULL) {
- printf("\nGrunged msg...uh oh...\n");
- fseek(fp,origpos+1L,SEEK_SET);
- farfree(hold);
- return NULL;
- }
- strncpy(msg->subj,tempo,64);
- msg->subj[63]=0;
- tempo=here+1;
- pos=pos+strlen(msg->subj);
- here=(char *)memchr(tempo,0,((word)65535-(word)(pos-origpos)));
- pos=pos+strlen(here);
- memmove(hold,here,(word)strlen(here)+1);
- fseek(fp,pos,SEEK_SET);
- return (hold);
- }
-
-
-
- void pascal strip_seenbys (char *hold) {
-
- /* This one accomplishes removal of the SEEN-BYs, if any, in the msg
- body */
-
-
- char *path=NULL;
- char *seenby=NULL;
- char *origin=NULL;
-
- origin=strstr(hold,"\r * Origin:");
- if (!origin) origin=strstr(hold,"\r\n * Origin:");
- if (origin) {
- path=strstr(origin,"\r\01PATH:");
- if(!path) path=strstr(origin,"\r\n\01PATH:");
- seenby=strstr(origin,"\rSEEN-BY:");
- if(!seenby)seenby=strstr(origin,"\r\nSEEN-BY:");
- if (!seenby) seenby=strstr(origin,"\r\01SEEN-BY:");
- if (!seenby) seenby=strstr(origin,"\r\n\01SEEN-BY:");
- if (!path) if (seenby) *seenby=0;
- if (seenby && path) memmove (seenby,path,strlen(path)+1);
- }
- }
-
-
-
- char * pascal find_seenbys (char *hold) {
-
- /* This one just returns where the SEEN-BY's start in the msg text */
-
- char *path=NULL;
- char *seenby=NULL;
- char *origin=NULL;
-
- origin=strstr(hold,"\r * Origin:");
- if (!origin) origin=strstr(hold,"\r\n * Origin:");
- if (origin) {
- path=strstr(origin,"\r\01PATH:");
- if(!path) path=strstr(origin,"\r\n\01PATH:");
- seenby=strstr(origin,"\rSEEN-BY:");
- if(!seenby)seenby=strstr(origin,"\r\nSEEN-BY:");
- if (!seenby) seenby=strstr(origin,"\r\01SEEN-BY:");
- if (!seenby) seenby=strstr(origin,"\r\n\01SEEN-BY:");
- }
- return (seenby);
- }
-
-
-
- void pascal strip_junk (char *hold) {
-
- /* This strips linefeeds and soft cr's (useless junk) from the msg body */
-
- char *tempo;
-
- while ((tempo=strstr(hold," \x8d"))) memmove(&tempo[1],&tempo[2],strlen(&tempo[2])+1);
- while ((tempo=strchr(hold,'\x8d'))) *tempo=' ';
- while ((tempo=strchr(hold,'\n'))) memmove(tempo,&tempo[1],strlen(&tempo[1])+1);
- if (hold[strlen(hold)-1]!='\r') strcat(hold,"\r");
- }
-
-
-
- void pascal just_seenby_numbers (char *hold) {
-
- /* This strips lf's, cr's, the word SEEN-BY, 0x01's and extra spaces from
- the seenby text, leaving you with a "pure" list of seenbys */
-
- char *tempo;
-
- while ((tempo=strchr(hold,'\x8d'))) *tempo=' '; /* In case of idiots */
- while ((tempo=strchr(hold,'\n'))) memmove(tempo,&tempo[1],strlen(&tempo[1])+1);
- while ((tempo=strchr(hold,'\r'))) memmove(tempo,&tempo[1],strlen(&tempo[1])+1);
- while ((tempo=strchr(hold,'\01'))) memmove(tempo,&tempo[1],strlen(&tempo[1])+1);
- while ((tempo=strstr(hold,"SEENBY"))) memmove(tempo,&tempo[6],strlen(&tempo[6])+1);
- while ((tempo=strstr(hold," "))) memmove(tempo,&tempo[1],strlen(&tempo[1])+1);
- }
-
-
- void pascal put_us_in_seenbys (char *hold,word net,word node) {
-
- /* This should take a string of seenbys treated by just_seenby_numbers,
- find where the given net/node should go, and put it there. It's really
- only useful as an example, as it doesn't have the ability to put a list
- of net/nodes in as would be really required. Note that the array the
- seenbys are in should be oversized enough to accept the insert! */
-
- char *tempo;
- char *temp;
- char *p;
- char s[32];
-
- sprintf(s,"%u/",net);
- tempo=strstr(hold,s);
- if(!tempo) { /* Our net's not listed, do it the hard way */
- sprintf(s,"%u/%u",net,node);
- temp=strdup(hold); /* Make temporary copy for strtok()ing */
- p=strtok(temp,"/");
- while (p) { /* Check each net... */
-
- }
- }
-
- /* UNFINISHED!!! */
-
- }