home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / adrstuff.zip / adrnames.cmd < prev    next >
OS/2 REXX Batch file  |  1997-01-17  |  5KB  |  150 lines

  1. /* mr2ice address name utility - jt Jan 1997 level proglevel */
  2. call RxFuncAdd 'SysLoadFuncs','RexxUtil','SysLoadFuncs'
  3. call SysLoadFuncs
  4. proglevel = 1.0
  5. say 'adrnames.cmd level' proglevel
  6.  
  7. arg inarg test .
  8.  
  9. if \ ( (inarg=0) | (inarg=1)|(inarg=2)|(inarg=3))  then do
  10.    say "Usage is adrnames 0|1|2|3 [test]  This utility manipulates an MR2ICE"
  11.    say "address book to extract a NAME from the email address"
  12.    say "in one of the forms suggested by the RFC or in common use:"
  13.    say '1     "Joe Blow" <jb123@windy.com'
  14.    say '2     Joe Blow <jb123@windy.com'
  15.    say '3     jb123@windy.com (Joe Blow)'
  16.    say ' '  
  17.    say 'and reformat all names to one of these forms (0 deletes names);'
  18.    say 'you must select one of these options'
  19.    say ''
  20.    say 'This reads mr2i.adr as input and rewrites it (the old one is saved'
  21.    say 'as mr2iadr.bk unless the 2nd argument is test, in which case it'
  22.    say 'will read mr2i.adr and create a file named mr2inew.adr'
  23.    say 'To use:  exit mr2ice, cd to your mr2ice directory.'
  24.    say 'invoke adrnames with an argument of 0, 1,2, or 3 '
  25.    say 'Then start mr2ice and review the entries in the address book'
  26.    say ''
  27.    say 'There is some error checking, but this may barff on some ill-formed'
  28.    say 'email addresses.  Bug reports to jt@epix.net; flames to /dev/null'
  29.    exit
  30.    end
  31.  
  32. /* based on these assumptions: */
  33. /* address file entries are of the form name \ email \ flag \ tag */
  34. /* but anything after flag will be preserved if Nick adds additional fields */
  35. /* the comment field is optional, */
  36. /* begins with a ^A, and consists of 3 lines total */
  37.  
  38. adrfile = 'mr2i.adr'
  39.  
  40. signal on notready
  41. which = adrfile
  42. if lines(adrfile) = 0 then signal notready
  43. call lineout(adrfile)   /* close file */
  44. which = ''
  45.  
  46. if test <> "TEST" then do
  47.    infile = 'mr2iadr.bk'
  48.    outfile = adrfile
  49.    if stream(infile,C,QUERY EXISTS) > '' then '@del' infile '> NUL'
  50.    '@rename' outfile infile   '>NUL' 
  51.    end
  52. else do
  53.     infile = adrfile
  54.     outfile = 'mr2inew.adr'
  55.     if stream(outfile,C,QUERY EXISTS) > '' then '@del' outfile '> NUL'
  56.     end
  57.    
  58.  
  59. do while lines(infile)
  60.    line = linein(infile)
  61.    if substr(line,1,1) = '' then do
  62.       call lineout outfile,line
  63.       line = linein(infile)  /* also the next 2 lines */
  64.       call lineout outfile,line
  65.       line = linein(infile)  
  66.       call lineout outfile,line
  67.       end
  68.    else do
  69.       parse var line name '\' email '\' rest
  70.       if (check4name(email) = 0)| (name='') then outline = line
  71.          else call buildline 
  72.       call lineout outfile,outline
  73.       end
  74.     end /* processing of line */
  75. call lineout(outfile)   /* close output file */
  76. say "all done"
  77. exit
  78.  
  79. check4name: PROCEDURE
  80.     ARG  x
  81.     /* check for " < or (  */
  82.     if (pos('"',x) + pos('<',x) + pos('(',x))>0 then rv=1
  83.     else rv = 0
  84.     return rv
  85.  
  86. buildline: PROCEDURE expose inarg name nam email rest outline adr
  87.  
  88.    /* 1     "Joe Blow" <jb123@windy.com  */
  89.    /* 2     Joe Blow <jb123@windy.com    */
  90.    /* 3     jb123@windy.com (Joe Blow)   */
  91.    /* 0     jb123@windy.com              */
  92.    call splitter    /* splitter splits email into nam and adr */
  93.    select
  94.        when (inarg = 0) then do 
  95.            newemail = adr
  96.        end
  97.        when (inarg = 1) then do 
  98.        if pos('"',nam)>0 then do
  99.           newemail = adr
  100.           say name email 'not processed due to " in name'
  101.           end
  102.            else newemail = '"'||nam||'" <'||adr||'>'
  103.        end
  104.        when (inarg = 2) then do
  105.             if (pos('.',nam)+pos('!',nam)+pos('@',nam))>0 then do
  106.           newemail = adr
  107.           say name email 'not processed due to ! @ or . in name'
  108.           end
  109.             else newemail = nam||' <'||adr||'>'
  110.             end
  111.        when (inarg = 3) then do 
  112.        if (pos('(',nam)+pos(')',nam))>0 then do
  113.           newemail = adr
  114.           say name email 'not processed due to ( or ) in name'
  115.           end
  116.            else newemail = adr '('||nam||')'
  117.        end
  118.        otherwise ;
  119.        end
  120.  
  121.     outline = name||'\'||newemail||'\'||rest
  122.     return 
  123.  
  124. splitter: PROCEDURE expose inarg name nam email rest outline adr
  125. /*  has an email field of the form    Joe Blow <jb123@windy.com> */
  126. /*  or                                jb123@windy.com (Joe Blow) */
  127. /* name may or may not be quoted; quotes are stripped            */
  128. /* nam holds extracted name; adr extracted address               */
  129. parse var email w1 residue
  130. if pos('@',w1)>0 then do
  131.    /* we have form 3 or no name */
  132.    adr = w1
  133.    if (pos('(',residue) <>1)|(pos(')',residue)<length(residue)) then do
  134.       /* either no name or bad format */
  135.       nam = ''
  136.       if length(residue)>0 then say name email 'not processed; email fmt error'
  137.       end
  138.    else nam=substr(residue,2,length(residue)-2)
  139.    end
  140. else do 
  141.    /* something of the form name <jb123@windy.com> */
  142.    parse var email w1 '<' adr '>' residue
  143.    if pos('"',w1)=1 then parse var w1 '"' nam '"'  
  144.      else nam = w1
  145.    end
  146. return
  147. notready:
  148. say which " file error"
  149. exit
  150.