<?php
/*******************************************************************************

    Author ......... Jimmy Conner
    Contact ........ jimmy@sqmail.org
    Home Site ...... http://www.sqmail.org/
    Program ........ Address Add
    Version ........ 2.1
    Purpose ........ Allows you to add the From address to the address book

*******************************************************************************/

      global $address;
      $atpos = strpos($this->froms, "@");
      $this->nick = substr($this->froms, 0, $atpos);

      $ret = $address->lookup($this->nick);
      $x = 1;
      $tn = $this->nick;
      while (!empty($ret)) {
         $tn = $this->nick . $x;
         $ret = $address->lookup($tn);
         $x++;
      }
      $this->nick = $tn;
      $this->decodedfrom = str_replace("&quot;", chr(34), $this->decodedfrom); 
      $this->decodedfrom = str_replace("&nbsp;", " ", $this->decodedfrom);
      $this->decodedfrom = str_replace("&#32;", " ", $this->decodedfrom);
      $this->decodedfrom = str_replace("&lt;", "<", $this->decodedfrom);
      $this->decodedfrom = str_replace("&gt;", ">", $this->decodedfrom);

      $spacepos = strpos($this->decodedfrom, " ");
      $spacepos2 = strpos($this->decodedfrom, " ", $spacepos+1);
      $qoutepos = strpos($this->decodedfrom, chr(34), 1);

      $bracketpos = strpos($this->decodedfrom, "<",0);

      if (!is_integer($bracketpos))
         $bracketpos = strpos($this->decodedfrom, "(",0);

      if ($spacepos2 > $bracketpos)
         $spacepos2 = -1;

      if (is_integer($spacepos) && !is_integer($qoutepos))
         $this->first = ucfirst(substr($this->decodedfrom, 0, $spacepos));

      if (is_integer($spacepos) && is_integer($qoutepos))
         $this->first = ucfirst(substr($this->decodedfrom, 1, $spacepos-1));

      if (is_integer($spacepos) && is_integer($qoutepos) && $qoutepos < $spacepos)
         $this->first = ucfirst(substr($this->decodedfrom, 1, $qoutepos-1));

      if (!is_integer($spacepos) && !is_integer($qoutepos))
         $this->first = "";

      if (is_integer($qoutepos) && $qoutepos > $spacepos)
         $this->last = ucfirst(substr($this->decodedfrom, $spacepos+1, ($qoutepos-$spacepos)-1));

      if (!is_integer($qoutepos) && is_integer($spacepos))
         $this->last = ucfirst(substr($this->decodedfrom, $spacepos+1, ($spacepos2-$spacepos)-1));

      if ($spacepos == $bracketpos - 1)
         $this->last = "";
      if ($this->froms != "") 
         $this->froms = urlencode(urlencode($this->froms));
      if ($this->nick != "")
         $this->nick = urlencode(urlencode($this->nick));
      if ($this->first != "")
         $this->first = urlencode(urlencode($this->first));
      if ($this->last != "")
         $this->last = urlencode(urlencode($this->last));

      $this->froms = clean($this->froms);
      $this->nick = clean($this->nick);
      $this->first = clean($this->first);
      $this->last = clean($this->last);

?>