Blame view

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

5
   GNU Mailutils is free software; you can redistribute it and/or modify
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)
8 9
   any later version.

10
   GNU Mailutils is distributed in the hope that it will be useful,
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"

20 21 22
size_t pop3d_output_bufsize = 64 * 1024;

void
23
pop3d_send_payload (mu_stream_t stream, mu_stream_t linestr, size_t maxlines)
24
{
25
  mu_stream_t flt;
26 27 28 29
  struct mu_buffer_query oldbuf, newbuf;
  int xscript_level = set_xscript_level (MU_XSCRIPT_PAYLOAD);

  oldbuf.type = MU_TRANSPORT_OUTPUT;
30
  mu_stream_ioctl (iostream, MU_IOCTL_TRANSPORT_BUFFER, MU_IOCTL_OP_GET,
31 32 33 34
		   &oldbuf);
  newbuf.type = MU_TRANSPORT_OUTPUT;
  newbuf.buftype = mu_buffer_full;
  newbuf.bufsize = pop3d_output_bufsize;
35
  mu_stream_ioctl (iostream, MU_IOCTL_TRANSPORT_BUFFER, MU_IOCTL_OP_SET,
36
		   &newbuf);
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  /* FIXME: Return code */
  mu_filter_create (&flt, iostream, "DOT", MU_FILTER_ENCODE,
		    MU_STREAM_WRITE);
  
  mu_stream_copy (flt, stream, 0, NULL);

  if (maxlines)
    {
      char *buf = NULL;
      size_t size = 0, n;

      mu_stream_write (flt, "\n", 1, NULL);
      for (; maxlines > 0 &&
	     mu_stream_getline (linestr, &buf, &size, &n) == 0 &&
	     n > 0; maxlines--)
	mu_stream_write (flt, buf, n, NULL);
      free (buf);
    }
  
  mu_stream_close (flt);
  mu_stream_destroy (&flt);
  
59
  mu_stream_ioctl (iostream, MU_IOCTL_TRANSPORT_BUFFER, MU_IOCTL_OP_SET,
60 61 62 63
		   &oldbuf);
  set_xscript_level (xscript_level);
}

64 65 66
/* Prints out the specified message */

int
67
pop3d_retr (char *arg, struct pop3d_session *sess)
68
{
Sergey Poznyakoff authored
69
  size_t mesgno;
70 71
  mu_message_t msg = NULL;
  mu_attribute_t attr = NULL;
Sergey Poznyakoff authored
72
  mu_stream_t stream;
Sergey Poznyakoff authored
73
  
74 75 76 77 78 79
  if ((strlen (arg) == 0) || (strchr (arg, ' ') != NULL))
    return ERR_BAD_ARGS;

  if (state != TRANSACTION)
    return ERR_WRONG_STATE;

80
  mesgno = strtoul (arg, NULL, 10);
81

82
  if (mu_mailbox_get_message (mbox, mesgno, &msg) != 0)
83 84
    return ERR_NO_MESG;

85
  mu_message_get_attribute (msg, &attr);
86
  if (pop3d_is_deleted (attr))
87
    return ERR_MESG_DELE;
88

Sergey Poznyakoff authored
89
  if (mu_message_get_streamref (msg, &stream))
Sergey Poznyakoff authored
90 91
    return ERR_UNKNOWN;
  
Sergey Poznyakoff authored
92
  pop3d_outf ("+OK\n");
93
  pop3d_send_payload (stream, NULL, 0);
Sergey Poznyakoff authored
94
  mu_stream_destroy (&stream);
95
  
96 97
  if (!mu_attribute_is_read (attr))
    mu_attribute_set_read (attr);
98

99 100
  pop3d_mark_retr (attr);

101
  
102 103
  return OK;
}