1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 2003-2021 Free Software Foundation, Inc.
3 
4    This library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 3 of the License, or (at your option) any later version.
8 
9    This library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13 
14    You should have received a copy of the GNU Lesser General
15    Public License along with this library.  If not, see
16    <http://www.gnu.org/licenses/>. */
17 
18 #ifdef HAVE_CONFIG_H
19 # include <config.h>
20 #endif
21 
22 #include <string.h>
23 #include <mailutils/stream.h>
24 #include <mailutils/list.h>
25 #include <mailutils/cctype.h>
26 #include <mailutils/cstr.h>
27 #include <mailutils/sys/pop3.h>
28 
29 int
mu_pop3_stream_to_list(mu_pop3_t pop3,mu_stream_t stream,mu_list_t list)30 mu_pop3_stream_to_list (mu_pop3_t pop3, mu_stream_t stream, mu_list_t list)
31 {
32   int status;
33   size_t n;
34 
35   while ((status = mu_stream_getline (stream, &pop3->rdbuf, &pop3->rdsize, &n))
36 	 == 0
37 	 && n > 0)
38     {
39       char *np = strdup (pop3->rdbuf);
40       if (!np)
41 	{
42 	  status = ENOMEM;
43 	  break;
44 	}
45       mu_rtrim_class (np, MU_CTYPE_SPACE);
46       status = mu_list_append (list, np);
47       if (status)
48 	break;
49     }
50   return status;
51 }
52 
53 int
mu_pop3_read_list(mu_pop3_t pop3,mu_list_t list)54 mu_pop3_read_list (mu_pop3_t pop3, mu_list_t list)
55 {
56   mu_stream_t stream;
57   int status = mu_pop3_stream_create (pop3, &stream);
58   if (status)
59     return status;
60   status = mu_pop3_stream_to_list (pop3, stream, list);
61   mu_stream_destroy (&stream);
62   return status;
63 }
64 
65