// smtpd/Session.hh
// This file is part of Decimail; see http://decimail.org
// (C) 2004 Philip Endecott
 
// This is version $Name$
//   (if there is no version (e.g. V0-1) mentioned in the previous line,
//    this is probably a snapshot from between "official" releases.)
 
// 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 "DmDatabase.hh"
#include "SmtpClient.hh"

#include <string>
#include <list>
#include <netinet/in.h>
using namespace std;


namespace Smtpd {

  class Session {
  public:
    Session(int i, int o);
    ~Session();

    void run(void);

    const int num;

  private:
    static int next_num;

    struct in_addr client_ip;
    int in_fd;
    int out_fd;
    FILE* in_f;
    FILE* out_f;

    DmDatabase db;
    SmtpClient smtpc;

    void find_client_ip(void);

    void wr(string s);

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

};


#endif
