###########################################################
# MsgIndexOfHeader_mio: Returns index of a given header
###########################################################
# [IN] $Msg : list containing the message
# $HdrNam: name of header including colon
# $da: riga da cui iniziare la scansione
# [OUT] (result): index, -1 if not found
# Example: $HdrIdx = MsgIndexOfHeader( $Msg, "From:" )

sub MsgIndexOfHeader_mio( $Msg, $HdrNam, $da )
 var( $i, $Idx )
 $Idx = -1
 $i = $da
 $HdrNam = LowerCase( $HdrNam )

 while( $i < ListCount($Msg) )
  #luigi# Modifica mia per arrivare a pescare gli "header" degli allegati
  #luigi# break( ListGet($Msg,$i) = "" ) # end of header
  if( $HdrNam = LowerCase( MsgHeadernameOfIndex( $Msg, $i ) ) )
   $Idx = $i
   break
  endif
  inc( $i )
 endwhile

 return( $Idx )
endsub

###########################################################
# MsgGetHeader_mio: Returns value of a given header
###########################################################
# [IN] $Msg : list containing the message
# $HdrNam: name of header including colon
# $da: riga da cui iniziare la scansione
# [OUT] (result): value of header, "" if not found
# Example: $HdrVal = MsgGetHeader( $Msg, "From:" )

sub MsgGetHeader_mio( $Msg, $HdrNam, $da )
 var( $HdrVal, $Idx, $i, $s)

 # find position of header
 $Idx = MsgIndexOfHeader_mio( $Msg, $HdrNam, $da )

 # if found, return value of header
 if( $Idx >= 0 )
  $HdrVal = ListGet( $Msg, $Idx )
  $i = Pos( ":", $HdrVal )
  $HdrVal = iif( $i=0, "", copy($HdrVal,$i+1) )
  $s = copy( $HdrVal, 1, 1 )
  if( Pos($s,$WSP) > 0 )
   $HdrVal = Delete( $HdrVal, 1, 1 )
  endif

  # append continuation lines separated by CR+LF
  do
   inc( $Idx )
   $s = copy( ListGet( $Msg, $Idx ), 1, 1 )
   break( Pos($s,$WSP)=0 ) # 1st char=WSP?
   $HdrVal = $HdrVal + $CRLF + ListGet( $Msg, $Idx )
  loop
 else
  $HdrVal = ""
 endif

 # return value of header (i.e. not including header-name)
 return( $HdrVal )

endsub