Blame view

pop3d/top.c 2.86 KB
1
/* GNU Mailutils -- a suite of utilities for electronic mail
2
   Copyright (C) 1999, 2000, 2001, 2005 Free Software Foundation, Inc.
3

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

9
   GNU Mailutils is distributed in the hope that it will be useful,
10 11 12 13 14
   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
15
   along with GNU Mailutils; if not, write to the Free Software
16 17
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
   MA 02110-1301 USA */
18 19 20

#include "pop3d.h"

21
/* Prints the header of a message plus a specified number of lines.  */
22 23

int
24
pop3d_top (const char *arg)
25
{
26 27
  size_t mesgno;
  int lines;
28 29 30 31 32
  mu_message_t msg;
  mu_attribute_t attr;
  mu_header_t hdr;
  mu_body_t body;
  mu_stream_t stream;
33
  char *mesgc, *linesc;
34 35
  char *buf;
  size_t buflen = BUFFERSIZE;
36 37
  size_t n;
  off_t off;
38 39 40 41 42 43 44

  if (strlen (arg) == 0)
    return ERR_BAD_ARGS;

  if (state != TRANSACTION)
    return ERR_WRONG_STATE;

45 46
  mesgc = pop3d_cmd (arg);
  linesc = pop3d_args (arg);
47
  mesgno = strtoul (mesgc, NULL, 10);
48
  lines = strlen (linesc) > 0 ? strtol (linesc, NULL, 10) : -1;
49 50 51 52 53 54
  free (mesgc);
  free (linesc);

  if (lines < 0)
    return ERR_BAD_ARGS;

55
  if (mu_mailbox_get_message (mbox, mesgno, &msg) != 0)
56 57
    return ERR_NO_MESG;

58
  mu_message_get_attribute (msg, &attr);
59
  if (pop3d_is_deleted (attr))
60 61
    return ERR_MESG_DELE;

62
  pop3d_outf ("+OK\r\n");
63

64
  /* Header.  */
65
  mu_message_get_header (msg, &hdr);
66
  mu_header_get_stream (hdr, &stream);
67 68
  buf = malloc (buflen * sizeof (*buf));
  if (buf == NULL)
69
    pop3d_abquit (ERR_NO_MEM);
70
  off = n = 0;
71
  while (mu_stream_readline (stream, buf, buflen, off, &n) == 0
72
	 && n > 0)
73 74 75
    {
      /* Nuke the trainline newline.  */
      if (buf[n - 1] == '\n')
76
	{
77
	  buf[n - 1] = '\0';
78
	  pop3d_outf ("%s\r\n", buf);
79
	}
80
      else
81
	pop3d_outf ("%s", buf);
82 83 84 85 86 87
      off += n;
    }

  /* Lines of body.  */
  if (lines)
    {
88
      mu_message_get_body (msg, &body);
89
      mu_body_get_stream (body, &stream);
90
      n = off = 0;
91
      while (mu_stream_readline (stream, buf, buflen, off, &n) == 0
92
	     && n > 0 && lines > 0)
93
	{
94
	  /* Nuke the trailing newline.  */
95 96
	  if (buf[n - 1] == '\n')
	    buf[n - 1] = '\0';
97 98 99 100 101
	  else /* make room for the line.  */
	    {
	      buflen *= 2;
	      buf = realloc (buf, buflen * sizeof (*buf));
	      if (buf == NULL)
102
		pop3d_abquit (ERR_NO_MEM);
103 104
	      continue;
	    }
105
	  if (buf[0] == '.')
106
	    pop3d_outf (".%s\r\n", buf);
107
	  else
108
	    pop3d_outf ("%s\r\n", buf);
109 110
	  lines--;
	  off += n;
111 112
	}
    }
113 114

  free (buf);
115
  pop3d_outf (".\r\n");
116

117 118
  return OK;
}