/*                        Tagline.rexx
                         v1.4 05-Dec-96

 -Adds a random tagline to all messages in the outgoing folder.
 -Tagfile should contain taglines separated by one separator line.  You
  can define the separator yourself.
 -No tagline is used again until they are all used once. (During one execution
  of the script, naturally)
 -It's possible not to use taglines with certain adresses.  Just list them
  in variable no_tags
 -Variable permanent contains text which is added to every message.  It may
  contain linefeeds, for example:
          permanent='Yours truly,'NL'   Kai Nikulainen'
 -hedrickd@bikerider.com added pretag. It's added after permanent but before tag
  The difference between permanent and pretag is: permanent is added to every
  message, pretag only to in messages which taglines are allowed.

   As always comments and bug reports are welcomed at knikulai@utu.fi
*/
options results
NL='0a'x		    /* Newline */

/* 
** Change these values to your liking
*/
  tagfiles=11			/* Number of tagfiles */
  tagfile.1='packed:txt/taglines_1'  /* Their names */
  tagfile.2='packed:txt/taglines_2'  
  tagfile.3='packed:txt/taglines_3'  
  tagfile.4='packed:txt/taglines_4'  
  tagfile.5='packed:txt/taglines_5'  
  tagfile.6='packed:txt/taglines_6'  
  tagfile.7='packed:txt/taglines_7'  
  tagfile.8='packed:txt/taglines_8'  
  tagfile.9='packed:txt/taglines_9'  
  tagfile.10='packed:txt/taglines_10'  
  tagfile.11='packed:txt/taglines_11'  

separator='$'
permanent='' 
  no_tags='yam@dsdelft.nl ' /* list of addresses which will not be able to enjoy your tags */
   pretag=NL/* Your Custom line to go before your tagline */



call random(,,time(s))      /* Let's make this really random */

/* let's first read all taglines */
tc=1            /* How many tags are there? */
tags.1=''       /* Set the first to empty */
tagnum=random(1,tagfiles)              /* Select a tagfile */
if open(in,tagfile.tagnum,'R') then do /* A tagfile was found */
  do until eof(in)              /* Read everything in it */
      rivi=readln(in)             /* Read one line */
      if rivi=separator then do   /* If the line was separator line */
            tc=tc+1               /* Next nonblank line will begin a new tag.. */
            tags.tc=''            /* which will be empty first */
         end
      else                                /* The line was not a separator */
         tags.tc=tags.tc || rivi || NL    /* Add it to the current tagline */
      end /* do until eof*/
  call close(1)                 /* Close tagfile */
  end
else do
  address 'YAM' 'Request "Could not open ' ||tagfile.tagnum||'!" "OK"'
  exit
  end

if tags.tc='' then tc=tc-1 /* Let's just be sure there are no blanks... */

maxtag=tc               /* maxtag tells how many taglines are unused */

address 'YAM'           /* Let's tell YAM what to do.. */

'SetFolder 1'
'GetFolderInfo Max'     /* How many messages are there? */
n=result                /* Remember it! */
do m=0 to n-1           /* Do for each message in folder: */
   'SetMail' m                          /* Select it */
   'GetMailInfo File'                   /* Get name of the mailfile */
   call open(out,result,'A')            /* Open it for appending */
   if permanent~='' then                /* If there is a line for every mail,*/
     writeln(out,permanent)             /* then write it */
   'GetMailInfo To'                     /* Get receiver */
   if ok_to_add_tag(result) then do     /* Is the address in the forbidden list? */
     i=rnd(maxtag)                      /* Get a random number 1..maxtag */
     if pretag~='' then
        writeln(out,pretag)             /* Add customized line before Tagline */
     writeln(out,tags.i)                /* Write the tagline */
     temp=tags.maxtag                   /* Swap the last unused.. */
     tags.maxtag=tags.i                 /* ...and just used tag */
     tags.i=temp                        /* Last used tag is now last */
     maxtag=maxtag-1                    /* Decrease counter ==> it wont be used again*/
     if maxtag=0 then maxtag=tc         /* All tags were used, so reset counter */
   end /* if ok_to_add_tag(result)*/
   call close(out)                    /* Close message file */
end /* do m= */

exit

ok_to_add_tag:
/* Check if the address is listed in no_tags list */
        parse arg email
        email=translate(email,' ','<>()",',' ') /* Remove unwanted chars */ 
	email=word(email,words(email))
        return pos(email,no_tags)=0     	/* Was it in the list? */

rnd:
/* random can't handle big numbers... This will create a big number and get
** modulo n+1.  This needs editing, if you have more than 30999 taglines... :-)
*/
  parse arg n
  r=1+(1000*random(1,30)+random(1,999))//n
return r
