1 /* GNU Mailutils -- a suite of utilities for electronic mail
2    Copyright (C) 1999-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 <errno.h>
24 #include <stdio.h>
25 #include <mailutils/error.h>
26 #include <mailutils/nls.h>
27 #include <mailutils/stream.h>
28 #include <mailutils/sys/pop3.h>
29 
30 static const char *pop3_prefix[] = {
31   "S: ", "C: "
32 };
33 
34 int
_mu_pop3_trace_enable(mu_pop3_t pop3)35 _mu_pop3_trace_enable (mu_pop3_t pop3)
36 {
37   int rc = 0;
38   mu_stream_t dstr, xstr;
39 
40   if (!pop3->carrier)
41     {
42       MU_POP3_FSET (pop3, MU_POP3_TRACE);
43       return 0;
44     }
45 
46   rc = mu_dbgstream_create (&dstr, MU_DIAG_DEBUG);
47   if (rc)
48     mu_error (_("cannot create debug stream; transcript disabled: %s"),
49 	      mu_strerror (rc));
50   else
51     {
52       rc = mu_xscript_stream_create (&xstr, pop3->carrier, dstr,
53 				     pop3_prefix);
54       if (rc)
55 	mu_error (_("cannot create transcript stream: %s"),
56 		  mu_strerror (rc));
57       else
58 	{
59 	  mu_stream_unref (pop3->carrier);
60 	  pop3->carrier = xstr;
61 	  MU_POP3_FSET (pop3, MU_POP3_TRACE);
62 	}
63     }
64 
65   return rc;
66 }
67 
68 int
_mu_pop3_trace_disable(mu_pop3_t pop3)69 _mu_pop3_trace_disable (mu_pop3_t pop3)
70 {
71   mu_stream_t xstr = pop3->carrier;
72   mu_stream_t stream[2];
73   int rc;
74 
75   if (!xstr)
76     return 0;
77 
78   rc = mu_stream_ioctl (xstr, MU_IOCTL_TRANSPORT, MU_IOCTL_OP_GET, stream);
79   if (rc)
80     return rc;
81 
82   pop3->carrier = stream[0];
83   mu_stream_destroy (&xstr);
84   MU_POP3_FCLR (pop3, MU_POP3_TRACE);
85   return 0;
86 }
87 
88 int
mu_pop3_trace(mu_pop3_t pop3,int op)89 mu_pop3_trace (mu_pop3_t pop3, int op)
90 {
91   int trace_on = MU_POP3_FISSET (pop3, MU_POP3_TRACE);
92 
93   switch (op)
94     {
95     case MU_POP3_TRACE_SET:
96       if (trace_on)
97 	return MU_ERR_EXISTS;
98       return _mu_pop3_trace_enable (pop3);
99 
100     case MU_POP3_TRACE_CLR:
101       if (!trace_on)
102 	return MU_ERR_NOENT;
103       return _mu_pop3_trace_disable (pop3);
104 
105     case MU_POP3_TRACE_QRY:
106       if (!trace_on)
107 	return MU_ERR_NOENT;
108       return 0;
109     }
110   return EINVAL;
111 }
112 
113 int
mu_pop3_trace_mask(mu_pop3_t pop3,int op,int lev)114 mu_pop3_trace_mask (mu_pop3_t pop3, int op, int lev)
115 {
116   switch (op)
117     {
118     case MU_POP3_TRACE_SET:
119       pop3->flags |= MU_POP3_XSCRIPT_MASK(lev);
120       break;
121 
122     case MU_POP3_TRACE_CLR:
123       pop3->flags &= ~MU_POP3_XSCRIPT_MASK(lev);
124       break;
125 
126     case MU_POP3_TRACE_QRY:
127       if (pop3->flags & MU_POP3_XSCRIPT_MASK(lev))
128 	break;
129       return MU_ERR_NOENT;
130 
131     default:
132       return EINVAL;
133     }
134   return 0;
135 }
136 
137 int
_mu_pop3_xscript_level(mu_pop3_t pop3,int xlev)138 _mu_pop3_xscript_level (mu_pop3_t pop3, int xlev)
139 {
140   if (mu_stream_ioctl (pop3->carrier, MU_IOCTL_XSCRIPTSTREAM,
141                        MU_IOCTL_XSCRIPTSTREAM_LEVEL, &xlev) == 0)
142     return xlev;
143   return MU_XSCRIPT_NORMAL;
144 }
145 
146 
147