Blame view

pop3d/capa.c 4.23 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2
   Copyright (C) 1999, 2001, 2003, 2007, 2009-2012, 2014-2016 Free
3
   Software Foundation, Inc.
Jakob Kaivo authored
4

5
   GNU Mailutils is free software; you can redistribute it and/or modify
Jakob Kaivo authored
6
   it under the terms of the GNU General Public License as published by
7
   the Free Software Foundation; either version 3, or (at your option)
Jakob Kaivo authored
8 9
   any later version.

10
   GNU Mailutils is distributed in the hope that it will be useful,
Jakob Kaivo authored
11 12 13 14 15
   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
16
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */
17 18 19

#include "pop3d.h"

Alain Magloire authored
20 21 22 23 24 25 26 27 28
/*
  The CAPA Command

  The POP3 CAPA command returns a list of capabilities supported by the
  POP3 server.  It is available in both the AUTHORIZATION and
  TRANSACTION states.

  Capabilities available in the AUTHORIZATION state MUST be announced
  in both states.  */
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

static int
print_capa (void *item, void *data)
{
  struct pop3d_capa *cp = item;
  struct pop3d_session *session = data;
  if (cp->type == capa_func)
    {
      cp->value.func (cp->name, session);
    }
  else 
    {
      pop3d_outf ("%s", cp->name);
      if (cp->value.string)
	pop3d_outf ("%s", cp->value.string);
      pop3d_outf ("\n");
    }
  return 0;
}

49
int
50
pop3d_capa (char *arg, struct pop3d_session *sess)
51 52 53 54
{
  if (strlen (arg) != 0)
    return ERR_BAD_ARGS;

Sergey Poznyakoff authored
55
  pop3d_outf ("+OK Capability list follows\n");
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
  mu_list_foreach (sess->capa, print_capa, sess);
  pop3d_outf (".\n");
  return OK;
}

static void
pop3d_append_capa_string (struct pop3d_session *sess, const char *name,
			  const char *value)
{
  struct pop3d_capa *cp;

  cp = mu_alloc (sizeof (*cp));
  cp->type = capa_string;
  cp->name = name;
  cp->value.string = value ? mu_strdup (value) : NULL;
  if (mu_list_append (sess->capa, cp))
    mu_alloc_die ();
}

static void
pop3d_append_capa_func (struct pop3d_session *sess, const char *name,
			void (*func) (const char *, struct pop3d_session *))
{
  struct pop3d_capa *cp;

  if (!func)
    return;
  cp = mu_alloc (sizeof (*cp));
  cp->type = capa_func;
  cp->name = name;
  cp->value.func = func;
  if (mu_list_append (sess->capa, cp))
    mu_alloc_die ();
}  

static void
capa_free (void *p)
{
  struct pop3d_capa *cp = p;
  if (cp->type == capa_string && cp->value.string)
    free (cp->value.string);
  free (cp);
}

static void
capa_implementation (const char *name, struct pop3d_session *session)
{
  if (state == TRANSACTION)	/* let's not advertise to just anyone */
    pop3d_outf ("%s %s\n", name, PACKAGE_STRING);
}

107
#ifdef WITH_TLS
108 109 110 111 112 113 114 115 116
static void
capa_stls (const char *name, struct pop3d_session *session)
{
  if ((session->tls == tls_ondemand || session->tls == tls_required)
      && tls_available && tls_done == 0)
    pop3d_outf ("%s\n", name);
}
#else
# define capa_stls NULL
117 118
#endif /* WITH_TLS */

119 120 121
static void
capa_user (const char *name, struct pop3d_session *session)
{
122
  if (state == INITIAL)
Sergey Poznyakoff authored
123
    pop3d_outf ("XTLSREQUIRED\n");
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  else
    pop3d_outf ("USER\n");
}

void
pop3d_session_init (struct pop3d_session *session)
{
  if (mu_list_create (&session->capa))
    mu_alloc_die ();
  mu_list_set_destroy_item (session->capa, capa_free);

  /* Initialize the capability list */
  pop3d_append_capa_string (session, "TOP", NULL);
  pop3d_append_capa_string (session, "UIDL", NULL);
  pop3d_append_capa_string (session, "RESP-CODES", NULL);
  pop3d_append_capa_string (session, "PIPELINING", NULL);
140
  pop3d_append_capa_string (session, "AUTH-RESP-CODE", NULL);
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
  if (pop3d_xlines)
    pop3d_append_capa_string (session, "XLINES", NULL);

  pop3d_append_capa_func (session, "LOGIN-DELAY", login_delay_capa);
    
  /* This can be implemented by setting an header field on the message.  */
  pop3d_append_capa_string (session, "EXPIRE",
			    (expire == EXPIRE_NEVER) ?
			    "NEVER" : mu_umaxtostr (0, expire));

  pop3d_append_capa_func (session, NULL, capa_user);
  pop3d_append_capa_func (session, "STLS", capa_stls);
  pop3d_append_capa_func (session, "IMPLEMENTATION", capa_implementation);
}

void
pop3d_session_free (struct pop3d_session *session)
{
  mu_list_destroy (&session->capa);
160
}