[go: up one dir, main page]

Menu

[r57]: / trunk / libmail / commons.c  Maximize  Restore  History

Download this file

110 lines (86 with data), 2.6 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 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
107
108
109
/* This file is part of libmail.
(c) 2009 - Dimitris Mandalidis <mandas@users.sourceforge.net>
libmail is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
libmail is distributed in the hope that it will be useful,
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
along with libmail. If not, see <http://www.gnu.org/licenses/>.
*/
#include <config.h>
#include <stdio.h>
#include <unistd.h>
#include <libmail/libmail.h>
#include <libmail/error.h>
#ifdef LIBMAIL_USE_TLS
#include <gnutls/gnutls.h>
#endif
int send_command(const char *sndbuffer, int sndlen, libmail_remote_server_t *server)
{
int n_write;
if (sndlen != 0) {
#ifdef NDEBUG
fprintf(stderr, "libmail: Sending command: %s\n", sndbuffer);
#endif
#ifdef LIBMAIL_USE_TLS
if (server->use_secure == LIBMAIL_USE_SECURE && (server->state == TRANSACTION || server->state == AUTHORIZATION))
n_write = gnutls_record_send(server->session, sndbuffer, sndlen);
else
n_write = write(server->sfd, sndbuffer, sndlen);
#else
n_write = write(server->sfd, sndbuffer, sndlen);
#endif
if (n_write != sndlen)
return LIBMAIL_SYSERROR;
}
return LIBMAIL_SUCCESS;
}
int readln(char *rcvbuffer, int *rcvlen, int maxlen, libmail_remote_server_t *server)
{
int n_read;
char *buf;
buf = rcvbuffer;
n_read = 0;
while (n_read <= maxlen)
{
if ((n_read += read(server->sfd, buf, 1)) < 0)
return LIBMAIL_SYSERROR;
if (*buf++ == '\n')
break;
}
rcvbuffer[n_read] = '\0';
if (rcvlen != NULL)
*rcvlen = n_read;
#ifdef NDEBUG
fprintf(stderr, "libmail: Received %s\n", rcvbuffer);
#endif
return LIBMAIL_SUCCESS;
}
int read_response(char *rcvbuffer, int *rcvlen, int maxlen, libmail_remote_server_t *server)
{
int n_read;
if (server->use_secure == LIBMAIL_USE_SECURE && (server->state == TRANSACTION || server->state == AUTHORIZATION))
{
if ((n_read = gnutls_record_recv(server->session, rcvbuffer, maxlen)) < 0)
{
gnutls_perror(n_read);
return LIBMAIL_TLSERROR;
}
}
else
{
if ((n_read = read(server->sfd, rcvbuffer, maxlen)) < 0)
return LIBMAIL_SYSERROR;
rcvbuffer[n_read] = '\0';
}
if (rcvlen != NULL)
*rcvlen = n_read;
#ifdef NDEBUG
fprintf(stderr, "libmail: Received %s\n", rcvbuffer);
#endif
return LIBMAIL_SUCCESS;
}