1 /* pop.c: client routines for talking to a POP3-protocol post-office server
2 Copyright (c) 1991, 1993, 1996 Free Software Foundation, Inc.
3 Written by Jonathan Kamens, jik@security.ov.com.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
23 #define NO_SHORTNAMES /* Tell config not to load remap.h */
24 #include <../src/config.h>
32 /* Cancel these substitutions made in config.h */
39 #include <sys/types.h>
43 #define RECV(s,buf,len,flags) recv(s,buf,len,flags)
44 #define SEND(s,buf,len,flags) send(s,buf,len,flags)
45 #define CLOSESOCKET(s) closesocket(s)
47 #include <netinet/in.h>
48 #include <sys/socket.h>
49 #define RECV(s,buf,len,flags) read(s,buf,len)
50 #define SEND(s,buf,len,flags) write(s,buf,len)
51 #define CLOSESOCKET(s) close(s)
62 * It really shouldn't be necessary to put this declaration here, but
63 * the version of hesiod.h that Athena has installed in release 7.2
64 * doesn't declare this function; I don't know if the 7.3 version of
67 extern struct servent *hes_getservbyname (/* char *, char * */);
78 #include "../src/syswait.h"
80 #include "../src/systime.h"
90 #include <krb5/krb5.h>
91 #include <krb5/ext-proto.h>
98 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
99 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule,
100 struct sockaddr_in *, struct sockaddr_in *,
102 extern char *krb_realmofhost (/* char * */);
104 #endif /* KERBEROS */
107 #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H)
112 static int socket_connection (char *, int);
113 static char *pop_getline (popserver);
114 static int sendline (popserver, char *);
115 static int fullwrite (int, char *, int);
116 static int getok (popserver);
118 static int gettermination (popserver);
120 static void pop_trash (popserver);
121 static char *find_crlf (char *);
123 #define ERROR_MAX 80 /* a pretty arbitrary size */
125 #define KPOP_PORT 1109
126 #if defined(WINDOWSNT) || defined(__CYGWIN32__)
127 #define POP_SERVICE "pop3" /* we don't want the POP2 port! */
129 #define POP_SERVICE "pop"
133 #define KPOP_SERVICE "k5pop";
135 #define KPOP_SERVICE "kpop"
139 char pop_error[ERROR_MAX];
143 #define min(a,b) (((a) < (b)) ? (a) : (b))
147 * Function: pop_open (char *host, char *username, char *password,
150 * Purpose: Establishes a connection with a post-office server, and
151 * completes the authorization portion of the session.
154 * host The server host with which the connection should be
155 * established. Optional. If omitted, internal
156 * heuristics will be used to determine the server host,
159 * The username of the mail-drop to access. Optional.
160 * If omitted, internal heuristics will be used to
161 * determine the username, if possible.
163 * The password to use for authorization. If omitted,
164 * internal heuristics will be used to determine the
165 * password, if possible.
166 * flags A bit mask containing flags controlling certain
167 * functions of the routine. Valid flags are defined in
170 * Return value: Upon successful establishment of a connection, a
171 * non-null popserver will be returned. Otherwise, null will be
172 * returned, and the string variable pop_error will contain an
173 * explanation of the error.
176 pop_open (char *host, char *username, char *password, int flags)
181 /* Determine the user name */
184 username = getenv ("USER");
185 if (! (username && *username))
188 username = getlogin ();
189 if (! (username && *username))
191 struct passwd *passwd;
192 passwd = getpwuid (getuid ());
193 if (passwd && passwd->pw_name && *passwd->pw_name)
195 username = passwd->pw_name;
199 strcpy (pop_error, "Could not determine username");
204 strcpy (pop_error, "Could not determine username");
211 * Determine the mail host.
216 host = getenv ("MAILHOST");
220 if ((! host) && (! (flags & POP_NO_HESIOD)))
222 struct hes_postoffice *office;
223 office = hes_getmailhost (username);
224 if (office && office->po_type && (! strcmp (office->po_type, "POP"))
225 && office->po_name && *office->po_name && office->po_host
228 host = office->po_host;
229 username = office->po_name;
243 strcpy (pop_error, "Could not determine POP server");
247 /* Determine the password */
249 #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
251 #define DONT_NEED_PASSWORD 0
254 if ((! password) && (! DONT_NEED_PASSWORD))
257 if (! (flags & POP_NO_GETPASS))
259 password = getpass ("Enter POP password:");
264 strcpy (pop_error, "Could not determine POP password");
269 flags |= POP_NO_KERBEROS;
273 sock = socket_connection (host, flags);
277 server = (popserver) malloc (sizeof (struct _popserver));
280 strcpy (pop_error, "Out of memory in pop_open");
283 server->buffer = (char *) malloc (GETLINE_MIN);
284 if (! server->buffer)
286 strcpy (pop_error, "Out of memory in pop_open");
287 free ((char *) server);
293 server->buffer_index = 0;
294 server->buffer_size = GETLINE_MIN;
295 server->in_multi = 0;
296 server->trash_started = 0;
302 * I really shouldn't use the pop_error variable like this, but....
304 if (strlen (username) > ERROR_MAX - 6)
308 "Username too long; recompile pop.c with larger ERROR_MAX");
311 sprintf (pop_error, "USER %s", username);
313 if (sendline (server, pop_error) || getok (server))
318 if (strlen (password) > ERROR_MAX - 6)
322 "Password too long; recompile pop.c with larger ERROR_MAX");
325 sprintf (pop_error, "PASS %s", password);
327 if (sendline (server, pop_error) || getok (server))
338 * Purpose: Issue the STAT command to the server and return (in the
339 * value parameters) the number of messages in the maildrop and
340 * the total size of the maildrop.
342 * Return value: 0 on success, or non-zero with an error in pop_error
345 * Side effects: On failure, may make further operations on the
346 * connection impossible.
349 pop_stat (popserver server, int *count, int *size)
353 if (server->in_multi)
355 strcpy (pop_error, "In multi-line query in pop_stat");
359 if (sendline (server, "STAT") || (! (fromserver = pop_getline (server))))
362 if (strncmp (fromserver, "+OK ", 4))
364 if (0 == strncmp (fromserver, "-ERR", 4))
366 strncpy (pop_error, fromserver, ERROR_MAX);
371 "Unexpected response from POP server in pop_stat");
377 *count = atoi (&fromserver[4]);
379 fromserver = strchr (&fromserver[4], ' ');
383 "Badly formatted response from server in pop_stat");
388 *size = atoi (fromserver + 1);
396 * Purpose: Performs the POP "list" command and returns (in value
397 * parameters) two malloc'd zero-terminated arrays -- one of
398 * message IDs, and a parallel one of sizes.
401 * server The pop connection to talk to.
402 * message The number of the one message about which to get
403 * information, or 0 to get information about all
406 * Return value: 0 on success, non-zero with error in pop_error on
409 * Side effects: On failure, may make further operations on the
410 * connection impossible.
413 pop_list (popserver server, int message, int **IDs, int **sizes)
418 if (server->in_multi)
420 strcpy (pop_error, "In multi-line query in pop_list");
429 if (pop_stat (server, &count, &size))
434 *IDs = (int *) malloc ((how_many + 1) * sizeof (int));
435 *sizes = (int *) malloc ((how_many + 1) * sizeof (int));
436 if (! (*IDs && *sizes))
438 strcpy (pop_error, "Out of memory in pop_list");
444 sprintf (pop_error, "LIST %d", message);
445 if (sendline (server, pop_error))
447 free ((char *) *IDs);
448 free ((char *) *sizes);
451 if (! (fromserver = pop_getline (server)))
453 free ((char *) *IDs);
454 free ((char *) *sizes);
457 if (strncmp (fromserver, "+OK ", 4))
459 if (! strncmp (fromserver, "-ERR", 4))
460 strncpy (pop_error, fromserver, ERROR_MAX);
464 "Unexpected response from server in pop_list");
467 free ((char *) *IDs);
468 free ((char *) *sizes);
471 (*IDs)[0] = atoi (&fromserver[4]);
472 fromserver = strchr (&fromserver[4], ' ');
476 "Badly formatted response from server in pop_list");
478 free ((char *) *IDs);
479 free ((char *) *sizes);
482 (*sizes)[0] = atoi (fromserver);
483 (*IDs)[1] = (*sizes)[1] = 0;
488 if (pop_multi_first (server, "LIST", &fromserver))
490 free ((char *) *IDs);
491 free ((char *) *sizes);
494 for (i = 0; i < how_many; i++)
496 if (pop_multi_next (server, &fromserver))
498 free ((char *) *IDs);
499 free ((char *) *sizes);
502 (*IDs)[i] = atoi (fromserver);
503 fromserver = strchr (fromserver, ' ');
507 "Badly formatted response from server in pop_list");
508 free ((char *) *IDs);
509 free ((char *) *sizes);
513 (*sizes)[i] = atoi (fromserver);
515 if (pop_multi_next (server, &fromserver))
517 free ((char *) *IDs);
518 free ((char *) *sizes);
524 "Too many response lines from server in pop_list");
525 free ((char *) *IDs);
526 free ((char *) *sizes);
529 (*IDs)[i] = (*sizes)[i] = 0;
535 * Function: pop_retrieve
537 * Purpose: Retrieve a specified message from the maildrop.
540 * server The server to retrieve from.
541 * message The message number to retrieve.
543 * If true, then mark the string "From " at the beginning
546 * Return value: A string pointing to the message, if successful, or
547 * null with pop_error set if not.
549 * Side effects: May kill connection on error.
552 pop_retrieve (popserver server, int message, int markfrom)
554 int *IDs, *sizes, bufsize, fromcount = 0, cp = 0;
555 char *ptr, *fromserver;
558 if (server->in_multi)
560 strcpy (pop_error, "In multi-line query in pop_retrieve");
564 if (pop_list (server, message, &IDs, &sizes))
567 if (pop_retrieve_first (server, message, &fromserver))
573 * The "5" below is an arbitrary constant -- I assume that if
574 * there are "From" lines in the text to be marked, there
575 * probably won't be more than 5 of them. If there are, I
576 * allocate more space for them below.
578 bufsize = sizes[0] + (markfrom ? 5 : 0);
579 ptr = (char *)malloc (bufsize);
581 free ((char *) sizes);
585 strcpy (pop_error, "Out of memory in pop_retrieve");
586 pop_retrieve_flush (server);
590 while (! (ret = pop_retrieve_next (server, &fromserver)))
599 if (markfrom && fromserver[0] == 'F' && fromserver[1] == 'r' &&
600 fromserver[2] == 'o' && fromserver[3] == 'm' &&
601 fromserver[4] == ' ')
603 if (++fromcount == 5)
606 ptr = (char *)realloc (ptr, bufsize);
609 strcpy (pop_error, "Out of memory in pop_retrieve");
610 pop_retrieve_flush (server);
617 linesize = strlen (fromserver);
618 memcpy (&ptr[cp], fromserver, linesize);
628 /* This function used to fall off the end, but that doesn't make any sense */
633 pop_retrieve_first (popserver server, int message, char **response)
635 sprintf (pop_error, "RETR %d", message);
636 return (pop_multi_first (server, pop_error, response));
640 pop_retrieve_next (popserver server, char **line)
642 return (pop_multi_next (server, line));
646 pop_retrieve_flush (popserver server)
648 return (pop_multi_flush (server));
652 pop_top_first (popserver server, int message, int lines, char **response)
654 sprintf (pop_error, "TOP %d %d", message, lines);
655 return (pop_multi_first (server, pop_error, response));
659 pop_top_next (popserver server, char **line)
661 return (pop_multi_next (server, line));
665 pop_top_flush (popserver server)
667 return (pop_multi_flush (server));
671 pop_multi_first (popserver server, char *command, char **response)
673 if (server->in_multi)
676 "Already in multi-line query in pop_multi_first");
680 if (sendline (server, command) || (! (*response = pop_getline (server))))
685 if (0 == strncmp (*response, "-ERR", 4))
687 strncpy (pop_error, *response, ERROR_MAX);
690 else if (0 == strncmp (*response, "+OK", 3))
692 for (*response += 3; **response == ' '; (*response)++) /* empty */;
693 server->in_multi = 1;
699 "Unexpected response from server in pop_multi_first");
705 pop_multi_next (popserver server, char **line)
709 if (! server->in_multi)
711 strcpy (pop_error, "Not in multi-line query in pop_multi_next");
715 fromserver = pop_getline (server);
721 if (fromserver[0] == '.')
726 server->in_multi = 0;
731 *line = fromserver + 1;
743 pop_multi_flush (popserver server)
747 if (! server->in_multi)
752 while (! pop_multi_next (server, &line))
763 /* Function: pop_delete
765 * Purpose: Delete a specified message.
768 * server Server from which to delete the message.
769 * message Message to delete.
771 * Return value: 0 on success, non-zero with error in pop_error
775 pop_delete (popserver server, int message)
777 if (server->in_multi)
779 strcpy (pop_error, "In multi-line query in pop_delete");
783 sprintf (pop_error, "DELE %d", message);
785 if (sendline (server, pop_error) || getok (server))
794 * Purpose: Send a noop command to the server.
797 * server The server to send to.
799 * Return value: 0 on success, non-zero with error in pop_error
802 * Side effects: Closes connection on error.
805 pop_noop (popserver server)
807 if (server->in_multi)
809 strcpy (pop_error, "In multi-line query in pop_noop");
813 if (sendline (server, "NOOP") || getok (server))
822 * Purpose: Find out the highest seen message from the server.
827 * Return value: If successful, the highest seen message, which is
828 * greater than or equal to 0. Otherwise, a negative number with
829 * the error explained in pop_error.
831 * Side effects: Closes the connection on error.
834 pop_last (popserver server)
838 if (server->in_multi)
840 strcpy (pop_error, "In multi-line query in pop_last");
844 if (sendline (server, "LAST"))
847 if (! (fromserver = pop_getline (server)))
850 if (! strncmp (fromserver, "-ERR", 4))
852 strncpy (pop_error, fromserver, ERROR_MAX);
855 else if (strncmp (fromserver, "+OK ", 4))
857 strcpy (pop_error, "Unexpected response from server in pop_last");
863 return (atoi (&fromserver[4]));
868 * Function: pop_reset
870 * Purpose: Reset the server to its initial connect state
875 * Return value: 0 for success, non-0 with error in pop_error
878 * Side effects: Closes the connection on error.
881 pop_reset (popserver server)
883 if (pop_retrieve_flush (server))
888 if (sendline (server, "RSET") || getok (server))
897 * Purpose: Quit the connection to the server,
900 * server The server to quit.
902 * Return value: 0 for success, non-zero otherwise with error in
905 * Side Effects: The popserver passed in is unusable after this
906 * function is called, even if an error occurs.
909 pop_quit (popserver server)
913 if (server->file >= 0)
915 if (pop_retrieve_flush (server))
920 if (sendline (server, "QUIT") || getok (server))
925 close (server->file);
929 free (server->buffer);
930 free ((char *) server);
936 static int have_winsock = 0;
940 * Function: socket_connection
942 * Purpose: Opens the network connection with the mail host, without
943 * doing any sort of I/O with it or anything.
946 * host The host to which to connect.
947 * flags Option flags.
949 * Return value: A file descriptor indicating the connection, or -1
950 * indicating failure, in which case an error has been copied
954 socket_connection (char *host, int flags)
956 struct hostent *hostent;
957 struct servent *servent;
958 struct sockaddr_in addr;
966 krb5_principal client, server;
973 Key_schedule schedule;
976 #endif /* KERBEROS */
983 if (WSAStartup (0x101, &winsockData) == 0)
990 hostent = gethostbyname (host);
993 #ifndef BROKEN_CYGWIN
994 && ((h_errno != TRY_AGAIN) || (try_count == 5))
998 strcpy (pop_error, "Could not determine POP server's address");
1001 } while (! hostent);
1003 memset (&addr, 0, sizeof (addr));
1004 addr.sin_family = AF_INET;
1007 service = (flags & POP_NO_KERBEROS) ? POP_SERVICE : KPOP_SERVICE;
1009 service = POP_SERVICE;
1013 if (! (flags & POP_NO_HESIOD))
1015 servent = hes_getservbyname (service, "tcp");
1018 addr.sin_port = servent->s_port;
1025 servent = getservbyname (service, "tcp");
1028 addr.sin_port = servent->s_port;
1033 addr.sin_port = htons ((flags & POP_NO_KERBEROS) ?
1034 POP_PORT : KPOP_PORT);
1036 addr.sin_port = htons (POP_PORT);
1041 #define SOCKET_ERROR "Could not create socket for POP connection: "
1043 sock = socket (PF_INET, SOCK_STREAM, 0);
1046 strcpy (pop_error, SOCKET_ERROR);
1047 strncat (pop_error, strerror (errno),
1048 ERROR_MAX - sizeof (SOCKET_ERROR));
1053 while (*hostent->h_addr_list)
1055 memcpy (&addr.sin_addr, *hostent->h_addr_list, hostent->h_length);
1056 if (! connect (sock, (struct sockaddr *) &addr, sizeof (addr)))
1058 hostent->h_addr_list++;
1061 #define CONNECT_ERROR "Could not connect to POP server: "
1063 if (! *hostent->h_addr_list)
1066 strcpy (pop_error, CONNECT_ERROR);
1067 strncat (pop_error, strerror (errno),
1068 ERROR_MAX - sizeof (CONNECT_ERROR));
1074 #define KRB_ERROR "Kerberos error connecting to POP server: "
1075 if (! (flags & POP_NO_KERBEROS))
1080 if (rem = krb5_cc_default (&ccdef))
1083 strcpy (pop_error, KRB_ERROR);
1084 strncat (pop_error, error_message (rem),
1085 ERROR_MAX - sizeof(KRB_ERROR));
1090 if (rem = krb5_cc_get_principal (ccdef, &client))
1095 for (cp = hostent->h_name; *cp; cp++)
1099 *cp = tolower (*cp);
1103 if (rem = krb5_sname_to_principal (hostent->h_name, POP_SERVICE,
1109 rem = krb5_sendauth ((krb5_pointer) &sock, "KPOPV1.0", client, server,
1110 AP_OPTS_MUTUAL_REQUIRED,
1111 0, /* no checksum */
1112 0, /* no creds, use ccache instead */
1114 0, /* don't need seq # */
1115 0, /* don't need subsession key */
1117 0); /* don't need reply */
1118 krb5_free_principal (server);
1121 if (err_ret && err_ret->text.length)
1123 strcpy (pop_error, KRB_ERROR);
1124 strncat (pop_error, error_message (rem),
1125 ERROR_MAX - sizeof (KRB_ERROR));
1126 strncat (pop_error, " [server says '",
1127 ERROR_MAX - strlen (pop_error) - 1);
1128 strncat (pop_error, err_ret->text.data,
1129 min (ERROR_MAX - strlen (pop_error) - 1,
1130 err_ret->text.length));
1131 strncat (pop_error, "']",
1132 ERROR_MAX - strlen (pop_error) - 1);
1136 strcpy (pop_error, KRB_ERROR);
1137 strncat (pop_error, error_message (rem),
1138 ERROR_MAX - sizeof (KRB_ERROR));
1141 krb5_free_error (err_ret);
1147 ticket = (KTEXT) malloc (sizeof (KTEXT_ST));
1148 rem = krb_sendauth (0L, sock, ticket, "pop", hostent->h_name,
1149 (char *) krb_realmofhost (hostent->h_name),
1150 (unsigned long) 0, &msg_data, &cred, schedule,
1151 (struct sockaddr_in *) 0,
1152 (struct sockaddr_in *) 0,
1154 free ((char *) ticket);
1155 if (rem != KSUCCESS)
1157 strcpy (pop_error, KRB_ERROR);
1158 strncat (pop_error, krb_err_txt[rem],
1159 ERROR_MAX - sizeof (KRB_ERROR));
1165 #endif /* KERBEROS */
1168 } /* socket_connection */
1171 * Function: pop_getline
1173 * Purpose: Get a line of text from the connection and return a
1174 * pointer to it. The carriage return and linefeed at the end of
1175 * the line are stripped, but periods at the beginnings of lines
1176 * are NOT dealt with in any special way.
1179 * server The server from which to get the line of text.
1181 * Returns: A non-null pointer if successful, or a null pointer on any
1182 * error, with an error message copied into pop_error.
1184 * Notes: The line returned is overwritten with each call to pop_getline.
1186 * Side effects: Closes the connection on error.
1189 pop_getline (popserver server)
1191 #define GETLINE_ERROR "Error reading from server: "
1194 int search_offset = 0;
1198 char *cp = find_crlf (server->buffer + server->buffer_index);
1204 found = server->buffer_index;
1205 data_used = (cp + 2) - server->buffer - found;
1207 *cp = '\0'; /* terminate the string to be returned */
1208 server->data -= data_used;
1209 server->buffer_index += data_used;
1212 fprintf (stderr, "<<< %s\n", server->buffer + found);
1213 return (server->buffer + found);
1217 memcpy (server->buffer,
1218 server->buffer + server->buffer_index,
1220 /* Record the fact that we've searched the data already in
1221 the buffer for a CRLF, so that when we search below, we
1222 don't have to search the same data twice. There's a "-
1223 1" here to account for the fact that the last character
1224 of the data we have may be the CR of a CRLF pair, of
1225 which we haven't read the second half yet, so we may have
1226 to search it again when we read more data. */
1227 search_offset = server->data - 1;
1228 server->buffer_index = 0;
1233 server->buffer_index = 0;
1238 /* There's a "- 1" here to leave room for the null that we put
1239 at the end of the read data below. We put the null there so
1240 that find_crlf knows where to stop when we call it. */
1241 if (server->data == server->buffer_size - 1)
1243 server->buffer_size += GETLINE_INCR;
1244 server->buffer = (char *)realloc (server->buffer, server->buffer_size);
1245 if (! server->buffer)
1247 strcpy (pop_error, "Out of memory in pop_getline");
1252 ret = RECV (server->file, server->buffer + server->data,
1253 server->buffer_size - server->data - 1, 0);
1256 strcpy (pop_error, GETLINE_ERROR);
1257 strncat (pop_error, strerror (errno),
1258 ERROR_MAX - sizeof (GETLINE_ERROR));
1264 strcpy (pop_error, "Unexpected EOF from server in pop_getline");
1271 server->data += ret;
1272 server->buffer[server->data] = '\0';
1274 cp = find_crlf (server->buffer + search_offset);
1277 int data_used = (cp + 2) - server->buffer;
1279 server->data -= data_used;
1280 server->buffer_index = data_used;
1283 fprintf (stderr, "<<< %s\n", server->buffer);
1284 return (server->buffer);
1286 search_offset += ret;
1294 * Function: sendline
1296 * Purpose: Sends a line of text to the POP server. The line of text
1297 * passed into this function should NOT have the carriage return
1298 * and linefeed on the end of it. Periods at beginnings of lines
1299 * will NOT be treated specially by this function.
1302 * server The server to which to send the text.
1303 * line The line of text to send.
1305 * Return value: Upon successful completion, a value of 0 will be
1306 * returned. Otherwise, a non-zero value will be returned, and
1307 * an error will be copied into pop_error.
1309 * Side effects: Closes the connection on error.
1312 sendline (popserver server, char *line)
1314 #define SENDLINE_ERROR "Error writing to POP server: "
1317 ret = fullwrite (server->file, line, strlen (line));
1319 { /* 0 indicates that a blank line was written */
1320 ret = fullwrite (server->file, "\r\n", 2);
1326 strcpy (pop_error, SENDLINE_ERROR);
1327 strncat (pop_error, strerror (errno),
1328 ERROR_MAX - sizeof (SENDLINE_ERROR));
1333 fprintf (stderr, ">>> %s\n", line);
1339 * Procedure: fullwrite
1341 * Purpose: Just like write, but keeps trying until the entire string
1344 * Return value: Same as write. Pop_error is not set.
1347 fullwrite (int fd, char *buf, int nbytes)
1353 while ((ret = SEND (fd, cp, nbytes, 0)) > 0)
1365 * Purpose: Reads a line from the server. If the return indicator is
1366 * positive, return with a zero exit status. If not, return with
1367 * a negative exit status.
1370 * server The server to read from.
1372 * Returns: 0 for success, else for failure and puts error in pop_error.
1374 * Side effects: On failure, may make the connection unusable.
1377 getok (popserver server)
1381 if (! (fromline = pop_getline (server)))
1386 if (! strncmp (fromline, "+OK", 3))
1388 else if (! strncmp (fromline, "-ERR", 4))
1390 strncpy (pop_error, fromline, ERROR_MAX);
1391 pop_error[ERROR_MAX-1] = '\0';
1397 "Unexpected response from server; expecting +OK or -ERR");
1405 * Function: gettermination
1407 * Purpose: Gets the next line and verifies that it is a termination
1408 * line (nothing but a dot).
1410 * Return value: 0 on success, non-zero with pop_error set on error.
1412 * Side effects: Closes the connection on error.
1415 gettermination (popserver server)
1419 fromserver = pop_getline (server);
1423 if (strcmp (fromserver, "."))
1426 "Unexpected response from server in gettermination");
1436 * Function pop_close
1438 * Purpose: Close a pop connection, sending a "RSET" command to try to
1439 * preserve any changes that were made and a "QUIT" command to
1440 * try to get the server to quit, but ignoring any responses that
1443 * Side effects: The server is unusable after this function returns.
1444 * Changes made to the maildrop since the session was started (or
1445 * since the last pop_reset) may be lost.
1448 pop_close (popserver server)
1451 free ((char *) server);
1457 * Function: pop_trash
1459 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1460 * memory associated with the server. It is legal to call
1461 * pop_close or pop_quit after this function has been called.
1464 pop_trash (popserver server)
1466 if (server->file >= 0)
1468 /* avoid recursion; sendline can call pop_trash */
1469 if (server->trash_started)
1471 server->trash_started = 1;
1473 sendline (server, "RSET");
1474 sendline (server, "QUIT");
1476 CLOSESOCKET (server->file);
1480 free (server->buffer);
1491 /* Return a pointer to the first CRLF in IN_STRING,
1492 or 0 if it does not contain one. */
1495 find_crlf (char *in_string)
1501 else if (*in_string == '\r')
1503 if (*++in_string == '\n')
1504 return (in_string - 1);
1512 #endif /* MAIL_USE_POP */