[go: up one dir, main page]

Menu

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

Download this file

105 lines (85 with data), 2.5 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
/* 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;
#ifdef LIBMAIL_USE_TLS
short use_secure = (server->use_secure && server->session != NULL);
#endif
if (sndlen != 0) {
#ifdef NDEBUG
fprintf(stderr, "libmail: Sending command: %s\n", sndbuffer);
#endif
#ifdef LIBMAIL_USE_TLS
if (use_secure)
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;
#ifdef LIBMAIL_USE_TLS
short use_secure = (server->use_secure && server->session != NULL);
#endif
#ifdef LIBMAIL_USE_TLS
if (use_secure) {
if ((n_read = gnutls_record_recv(server->session, rcvbuffer, maxlen)) < 0)
return LIBMAIL_TLSERROR;
} else {
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;
}
}
#else
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;
}
#endif
rcvbuffer[n_read] = '\0';
if (rcvlen != NULL)
*rcvlen = n_read;
#ifdef NDEBUG
fprintf(stderr, "libmail: Received %s\n", rcvbuffer);
#endif
return LIBMAIL_SUCCESS;
}