// smtpd/Session.hh
// This file is part of Decimail; see http://decimail.org
// (C) 2004-2007 Philip Endecott
 
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
#ifndef Smtpd_Session_hh
#define Smtpd_Session_hh

#include "SmtpdDb.hh"
#include "SmtpClient.hh"
#include "FileDescriptor.hh"

#include <string>
#include <list>
#include <netinet/in.h>


namespace Smtpd {

  class Session {
  public:
    Session(pbe::FileDescriptor& i, pbe::FileDescriptor& o);
    ~Session();

    void run(void);

    const int num;

  private:
    static int next_num;

    struct in_addr client_ip;
    pbe::FileDescriptor& in_fd;
    pbe::FileDescriptor& out_fd;
    pbe::FileDescriptor::ostream out_strm;
    bool config_log_conversation;

    SmtpdDb db;
    SmtpClient smtpc;

    void wr(std::string s);

    std::string lookup_local_user(std::string recipient);
    void find_matching_addrs(const Message::email_address_list& l,
			     std::string username, std::list<std::string>& matches);
    std::string find_new_sender_for_reply(const mimetic::MimeEntity& m,
				     std::string username);
    std::string lookup_auto_username(std::string sender);
    std::string find_custom_addr(std::string recipient, std::string username);
    std::string random_address(std::string username, std::string description);
    void process_outgoing(std::string ehlo_domain,
			  std::string sender, std::string recipient,
			  std::string data);
    void process_incoming(std::string ehlo_domain,
			  std::string sender, std::string recipient,
			  std::string username, std::string data);
  };

};


#endif
