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

  POPular -- A POP3 server and proxy for large mail systems

  $Id: info.c,v 1.10 2002/09/15 12:27:16 sqrt Exp $

  http://www.remote.org/jochen/mail/popular/

******************************************************************************

  Copyright (C) 1999-2002  Jochen Topf <jochen@remote.org>

  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
  (at your option) 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

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

#if HAVE_CONFIG_H
# include <config.h>
#endif

#include "popular.h"


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

  find_backend_state_info()

*****************************************************************************/
struct backend_state_info *
find_backend_state_info(backend_state_t t, const char *n)
{
  struct backend_state_info *p;
  static struct backend_state_info list[] = {
    { bstFree,    "FREE" },
    { bstOffline, "OFFLINE" },
    { bstFake,    "FAKE" },
    { bstOnline,  "ONLINE" },
    { -1,         NULL }
  };

  for (p = list; p->name; p++) {
    if ((t >= 0) && p->type == t) return p;
    if ((n != NULL) && !strcasecmp(p->name, n)) return p;
  }
  return NULL;
}


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

  find_virt_serv_state_info()

*****************************************************************************/
struct virt_serv_state_info *
find_virt_serv_state_info(virt_serv_state_t t, const char *n)
{
  struct virt_serv_state_info *p;
  static struct virt_serv_state_info list[] = {
    { vsstFree,     "FREE",     0 },
    { vsstOffline,  "OFFLINE",  0 },
    { vsstDisabled, "DISABLED", 1 },
    { vsstFake,     "FAKE",     1 },
    { vsstOnline,   "ONLINE",   1 },
    { -1,           NULL,       0 }
  };

  for (p = list; p->name; p++) {
    if ((t >= 0) && p->type == t) return p;
    if ((n != NULL) && !strcasecmp(p->name, n)) return p;
  }
  return NULL;
}


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

  find_protocol_info()

*****************************************************************************/
struct protocol_info *
find_protocol_info(protocol_t t, const char *n)
{
  struct protocol_info *p;
  static struct protocol_info list[] = {
    { ptPOP3,     "POP3",     0, DEFAULT_PORT_POP3,     1, 1 },
    { ptIMAP4,    "IMAP4",    0, DEFAULT_PORT_IMAP4,    0, 0 },
    { ptXPOP,     "XPOP",     0, DEFAULT_PORT_XPOP,     0, 1 },
    { ptCXPOP,    "CXPOP",    0, DEFAULT_PORT_XPOP,     0, 1 },
#ifdef USE_TLS
    { ptPOP3TLS,  "POP3s",    1, DEFAULT_PORT_POP3TLS,  1, 0 },
#else
    { ptPOP3TLS,  "POP3s",    1, DEFAULT_PORT_POP3TLS,  0, 0 },
#endif
    { -1,         NULL,       0, 0,                     0, 0 }
  };

  for (p = list; p->name; p++) {
    if ((t >= 0) && p->type == t) return p;
    if ((n != NULL) && !strcasecmp(p->name, n)) return p;
  }
  return NULL;
}


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

  is_tls_protocol()

  Returns 1 if the named protocol uses TLS and 0 otherwise.

*****************************************************************************/
int
is_tls_protocol(protocol_t t)
{
  struct protocol_info *pi = find_protocol_info(t, NULL);
  if (! pi) {
    /* XLOG-DOC:BUG:019c:unknown_protocol
     * No protocol information for this virtual server is found. This
     * should never happen. */
    xlog_printf(xlog_bug, 0x019c, "unknown_protocol");
    exit(RCODE_ERR);
  }
  return pi->tls;
}


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

  find_session_state_info()

*****************************************************************************/
struct session_state_info *
find_session_state_info(session_state_t t, const char *n)
{
  struct session_state_info *p;
  static struct session_state_info list[] = {
    { sstFree,               "-----" },
    { sstNew,                "NEW" },
    { sstIdle,               "IDLE" },
    { sstRead,               "READ" },
    { sstDone,               "DONE" },
    { sstAuthPhase,          "AUTH" },
    { sstVirtServDisabled,   "VSDIS" },
    { sstVirtServFake,       "VSFAK" },
    { sstCheck,              "CHECK" },
    { sstConnecting,         "CONNS" },
    { sstBackendUnreachable, "BEUNR" },
    { sstBackendDisabled,    "BEDIS" },
    { sstBackendFake,        "BEFAK" },
    { sstProxy,              "PROXY" },
    { sstMailboxEmpty,       "EMPTY" },
    { sstUnknownBackend,     "UKNBE" },
    { sstConnQuit,           "CQUIT" },
    { sstConnBroken,         "CBROK" },
    { -1,                    NULL }
  };

  for (p = list; p->name; p++) {
    if ((t >= 0) && p->type == t) return p;
    if ((n != NULL) && !strcasecmp(p->name, n)) return p;
  }
  return NULL;
}


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

  find_debug_info()

*****************************************************************************/
static struct debug_info di_list[] = {
  { DG_MAIN,   "MAIN"   },
  { DG_AUTH,   "AUTH"   },
  { DG_PASS,   "PASS"   },
  { DG_POP,    "POP"    },
  { DG_CTRL,   "CTRL"   },
  { DG_NET,    "NET"    },
  { DG_RINGD,  "RINGD"  },
  { DG_TLS,    "TLS"    },
  { DG_IO,     "IO"     },
  { DG_ALL,    "ALL"    },
  { 0,         "NONE"   },
  { -1,        NULL     }
};

struct debug_info *
find_debug_info(int t, const char *n)
{
  struct debug_info *p;

  for (p = di_list; p->name; p++) {
    if ((t >= 0) && p->type == t) return p;
    if ((n != NULL) && !strcasecmp(p->name, n)) return p;
  }
  return NULL;
}

char *
print_debug_list(int debug)
{
  struct debug_info *p;
  static char buf[sizeof(di_list)];

  buf[0] = '\0';

  for (p = di_list; p->name; p++) {
    if (debug & p->type && p->type != DG_ALL) {
      (void) strlcat(buf, " ", sizeof(buf));
      (void) strlcat(buf, p->name, sizeof(buf));
    }
  }

  return buf;
}


/** THE END *****************************************************************/


syntax highlighted by Code2HTML, v. 0.9.1