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>
44 #define RECV(s,buf,len,flags) recv(s,buf,len,flags)
45 #define SEND(s,buf,len,flags) send(s,buf,len,flags)
46 #define CLOSESOCKET(s) closesocket(s)
48 #include <netinet/in.h>
49 #include <sys/socket.h>
50 #define RECV(s,buf,len,flags) read(s,buf,len)
51 #define SEND(s,buf,len,flags) write(s,buf,len)
52 #define CLOSESOCKET(s) close(s)
63 * It really shouldn't be necessary to put this declaration here, but
64 * the version of hesiod.h that Athena has installed in release 7.2
65 * doesn't declare this function; I don't know if the 7.3 version of
68 extern struct servent *hes_getservbyname (/* char *, char * */);
79 #include "../src/syswait.h"
80 #include "../src/systime.h"
89 #include <krb5/krb5.h>
90 #include <krb5/ext-proto.h>
97 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
98 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule,
99 struct sockaddr_in *, struct sockaddr_in *,
101 extern char *krb_realmofhost (/* char * */);
103 #endif /* KERBEROS */
106 #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H)
111 static int socket_connection (char *, int);
112 static char *pop_getline (popserver);
113 static int sendline (popserver, char *);
114 static int fullwrite (int, char *, int);
115 static int getok (popserver);
117 static int gettermination (popserver);
119 static void pop_trash (popserver);
120 static char *find_crlf (char *);
122 #define ERROR_MAX 80 /* a pretty arbitrary size */
124 #define KPOP_PORT 1109
125 #if defined(WINDOWSNT) || defined(__CYGWIN32__)
126 #define POP_SERVICE "pop3" /* we don't want the POP2 port! */
128 #define POP_SERVICE "pop"
132 #define KPOP_SERVICE "k5pop";
134 #define KPOP_SERVICE "kpop"
138 char pop_error[ERROR_MAX];
142 #define min(a,b) (((a) < (b)) ? (a) : (b))
146 * Function: pop_open (char *host, char *username, char *password,
149 * Purpose: Establishes a connection with a post-office server, and
150 * completes the authorization portion of the session.
153 * host The server host with which the connection should be
154 * established. Optional. If omitted, internal
155 * heuristics will be used to determine the server host,
158 * The username of the mail-drop to access. Optional.
159 * If omitted, internal heuristics will be used to
160 * determine the username, if possible.
162 * The password to use for authorization. If omitted,
163 * internal heuristics will be used to determine the
164 * password, if possible.
165 * flags A bit mask containing flags controlling certain
166 * functions of the routine. Valid flags are defined in
169 * Return value: Upon successful establishment of a connection, a
170 * non-null popserver will be returned. Otherwise, null will be
171 * returned, and the string variable pop_error will contain an
172 * explanation of the error.
175 pop_open (char *host, char *username, char *password, int flags)
180 /* Determine the user name */
183 username = getenv ("USER");
184 if (! (username && *username))
186 username = getlogin ();
187 if (! (username && *username))
189 struct passwd *passwd;
190 passwd = getpwuid (getuid ());
191 if (passwd && passwd->pw_name && *passwd->pw_name)
193 username = passwd->pw_name;
197 strcpy (pop_error, "Could not determine username");
205 * Determine the mail host.
210 host = getenv ("MAILHOST");
214 if ((! host) && (! (flags & POP_NO_HESIOD)))
216 struct hes_postoffice *office;
217 office = hes_getmailhost (username);
218 if (office && office->po_type && (! strcmp (office->po_type, "POP"))
219 && office->po_name && *office->po_name && office->po_host
222 host = office->po_host;
223 username = office->po_name;
237 strcpy (pop_error, "Could not determine POP server");
241 /* Determine the password */
243 #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
245 #define DONT_NEED_PASSWORD 0
248 if ((! password) && (! DONT_NEED_PASSWORD))
250 if (! (flags & POP_NO_GETPASS))
252 password = getpass ("Enter POP password:");
256 strcpy (pop_error, "Could not determine POP password");
261 flags |= POP_NO_KERBEROS;
265 sock = socket_connection (host, flags);
269 server = (popserver) malloc (sizeof (struct _popserver));
272 strcpy (pop_error, "Out of memory in pop_open");
275 server->buffer = (char *) malloc (GETLINE_MIN);
276 if (! server->buffer)
278 strcpy (pop_error, "Out of memory in pop_open");
279 free ((char *) server);
285 server->buffer_index = 0;
286 server->buffer_size = GETLINE_MIN;
287 server->in_multi = 0;
288 server->trash_started = 0;
294 * I really shouldn't use the pop_error variable like this, but....
296 if (strlen (username) > ERROR_MAX - 6)
300 "Username too long; recompile pop.c with larger ERROR_MAX");
303 sprintf (pop_error, "USER %s", username);
305 if (sendline (server, pop_error) || getok (server))
310 if (strlen (password) > ERROR_MAX - 6)
314 "Password too long; recompile pop.c with larger ERROR_MAX");
317 sprintf (pop_error, "PASS %s", password);
319 if (sendline (server, pop_error) || getok (server))
330 * Purpose: Issue the STAT command to the server and return (in the
331 * value parameters) the number of messages in the maildrop and
332 * the total size of the maildrop.
334 * Return value: 0 on success, or non-zero with an error in pop_error
337 * Side effects: On failure, may make further operations on the
338 * connection impossible.
341 pop_stat (popserver server, int *count, int *size)
345 if (server->in_multi)
347 strcpy (pop_error, "In multi-line query in pop_stat");
351 if (sendline (server, "STAT") || (! (fromserver = pop_getline (server))))
354 if (strncmp (fromserver, "+OK ", 4))
356 if (0 == strncmp (fromserver, "-ERR", 4))
358 strncpy (pop_error, fromserver, ERROR_MAX);
363 "Unexpected response from POP server in pop_stat");
369 *count = atoi (&fromserver[4]);
371 fromserver = strchr (&fromserver[4], ' ');
375 "Badly formatted response from server in pop_stat");
380 *size = atoi (fromserver + 1);
388 * Purpose: Performs the POP "list" command and returns (in value
389 * parameters) two malloc'd zero-terminated arrays -- one of
390 * message IDs, and a parallel one of sizes.
393 * server The pop connection to talk to.
394 * message The number of the one message about which to get
395 * information, or 0 to get information about all
398 * Return value: 0 on success, non-zero with error in pop_error on
401 * Side effects: On failure, may make further operations on the
402 * connection impossible.
405 pop_list (popserver server, int message, int **IDs, int **sizes)
410 if (server->in_multi)
412 strcpy (pop_error, "In multi-line query in pop_list");
421 if (pop_stat (server, &count, &size))
426 *IDs = (int *) malloc ((how_many + 1) * sizeof (int));
427 *sizes = (int *) malloc ((how_many + 1) * sizeof (int));
428 if (! (*IDs && *sizes))
430 strcpy (pop_error, "Out of memory in pop_list");
436 sprintf (pop_error, "LIST %d", message);
437 if (sendline (server, pop_error))
439 free ((char *) *IDs);
440 free ((char *) *sizes);
443 if (! (fromserver = pop_getline (server)))
445 free ((char *) *IDs);
446 free ((char *) *sizes);
449 if (strncmp (fromserver, "+OK ", 4))
451 if (! strncmp (fromserver, "-ERR", 4))
452 strncpy (pop_error, fromserver, ERROR_MAX);
456 "Unexpected response from server in pop_list");
459 free ((char *) *IDs);
460 free ((char *) *sizes);
463 (*IDs)[0] = atoi (&fromserver[4]);
464 fromserver = strchr (&fromserver[4], ' ');
468 "Badly formatted response from server in pop_list");
470 free ((char *) *IDs);
471 free ((char *) *sizes);
474 (*sizes)[0] = atoi (fromserver);
475 (*IDs)[1] = (*sizes)[1] = 0;
480 if (pop_multi_first (server, "LIST", &fromserver))
482 free ((char *) *IDs);
483 free ((char *) *sizes);
486 for (i = 0; i < how_many; i++)
488 if (pop_multi_next (server, &fromserver))
490 free ((char *) *IDs);
491 free ((char *) *sizes);
494 (*IDs)[i] = atoi (fromserver);
495 fromserver = strchr (fromserver, ' ');
499 "Badly formatted response from server in pop_list");
500 free ((char *) *IDs);
501 free ((char *) *sizes);
505 (*sizes)[i] = atoi (fromserver);
507 if (pop_multi_next (server, &fromserver))
509 free ((char *) *IDs);
510 free ((char *) *sizes);
516 "Too many response lines from server in pop_list");
517 free ((char *) *IDs);
518 free ((char *) *sizes);
521 (*IDs)[i] = (*sizes)[i] = 0;
527 * Function: pop_retrieve
529 * Purpose: Retrieve a specified message from the maildrop.
532 * server The server to retrieve from.
533 * message The message number to retrieve.
535 * If true, then mark the string "From " at the beginning
538 * Return value: A string pointing to the message, if successful, or
539 * null with pop_error set if not.
541 * Side effects: May kill connection on error.
544 pop_retrieve (popserver server, int message, int markfrom)
546 int *IDs, *sizes, bufsize, fromcount = 0, cp = 0;
547 char *ptr, *fromserver;
550 if (server->in_multi)
552 strcpy (pop_error, "In multi-line query in pop_retrieve");
556 if (pop_list (server, message, &IDs, &sizes))
559 if (pop_retrieve_first (server, message, &fromserver))
565 * The "5" below is an arbitrary constant -- I assume that if
566 * there are "From" lines in the text to be marked, there
567 * probably won't be more than 5 of them. If there are, I
568 * allocate more space for them below.
570 bufsize = sizes[0] + (markfrom ? 5 : 0);
571 ptr = (char *)malloc (bufsize);
573 free ((char *) sizes);
577 strcpy (pop_error, "Out of memory in pop_retrieve");
578 pop_retrieve_flush (server);
582 while (! (ret = pop_retrieve_next (server, &fromserver)))
591 if (markfrom && fromserver[0] == 'F' && fromserver[1] == 'r' &&
592 fromserver[2] == 'o' && fromserver[3] == 'm' &&
593 fromserver[4] == ' ')
595 if (++fromcount == 5)
598 ptr = (char *)realloc (ptr, bufsize);
601 strcpy (pop_error, "Out of memory in pop_retrieve");
602 pop_retrieve_flush (server);
609 linesize = strlen (fromserver);
610 memcpy (&ptr[cp], fromserver, linesize);
620 /* This function used to fall off the end, but that doesn't make any sense */
625 pop_retrieve_first (popserver server, int message, char **response)
627 sprintf (pop_error, "RETR %d", message);
628 return (pop_multi_first (server, pop_error, response));
632 pop_retrieve_next (popserver server, char **line)
634 return (pop_multi_next (server, line));
638 pop_retrieve_flush (popserver server)
640 return (pop_multi_flush (server));
644 pop_top_first (popserver server, int message, int lines, char **response)
646 sprintf (pop_error, "TOP %d %d", message, lines);
647 return (pop_multi_first (server, pop_error, response));
651 pop_top_next (popserver server, char **line)
653 return (pop_multi_next (server, line));
657 pop_top_flush (popserver server)
659 return (pop_multi_flush (server));
663 pop_multi_first (popserver server, char *command, char **response)
665 if (server->in_multi)
668 "Already in multi-line query in pop_multi_first");
672 if (sendline (server, command) || (! (*response = pop_getline (server))))
677 if (0 == strncmp (*response, "-ERR", 4))
679 strncpy (pop_error, *response, ERROR_MAX);
682 else if (0 == strncmp (*response, "+OK", 3))
684 for (*response += 3; **response == ' '; (*response)++) /* empty */;
685 server->in_multi = 1;
691 "Unexpected response from server in pop_multi_first");
697 pop_multi_next (popserver server, char **line)
701 if (! server->in_multi)
703 strcpy (pop_error, "Not in multi-line query in pop_multi_next");
707 fromserver = pop_getline (server);
713 if (fromserver[0] == '.')
718 server->in_multi = 0;
723 *line = fromserver + 1;
735 pop_multi_flush (popserver server)
739 if (! server->in_multi)
744 while (! pop_multi_next (server, &line))
755 /* Function: pop_delete
757 * Purpose: Delete a specified message.
760 * server Server from which to delete the message.
761 * message Message to delete.
763 * Return value: 0 on success, non-zero with error in pop_error
767 pop_delete (popserver server, int message)
769 if (server->in_multi)
771 strcpy (pop_error, "In multi-line query in pop_delete");
775 sprintf (pop_error, "DELE %d", message);
777 if (sendline (server, pop_error) || getok (server))
786 * Purpose: Send a noop command to the server.
789 * server The server to send to.
791 * Return value: 0 on success, non-zero with error in pop_error
794 * Side effects: Closes connection on error.
797 pop_noop (popserver server)
799 if (server->in_multi)
801 strcpy (pop_error, "In multi-line query in pop_noop");
805 if (sendline (server, "NOOP") || getok (server))
814 * Purpose: Find out the highest seen message from the server.
819 * Return value: If successful, the highest seen message, which is
820 * greater than or equal to 0. Otherwise, a negative number with
821 * the error explained in pop_error.
823 * Side effects: Closes the connection on error.
826 pop_last (popserver server)
830 if (server->in_multi)
832 strcpy (pop_error, "In multi-line query in pop_last");
836 if (sendline (server, "LAST"))
839 if (! (fromserver = pop_getline (server)))
842 if (! strncmp (fromserver, "-ERR", 4))
844 strncpy (pop_error, fromserver, ERROR_MAX);
847 else if (strncmp (fromserver, "+OK ", 4))
849 strcpy (pop_error, "Unexpected response from server in pop_last");
855 return (atoi (&fromserver[4]));
860 * Function: pop_reset
862 * Purpose: Reset the server to its initial connect state
867 * Return value: 0 for success, non-0 with error in pop_error
870 * Side effects: Closes the connection on error.
873 pop_reset (popserver server)
875 if (pop_retrieve_flush (server))
880 if (sendline (server, "RSET") || getok (server))
889 * Purpose: Quit the connection to the server,
892 * server The server to quit.
894 * Return value: 0 for success, non-zero otherwise with error in
897 * Side Effects: The popserver passed in is unusable after this
898 * function is called, even if an error occurs.
901 pop_quit (popserver server)
905 if (server->file >= 0)
907 if (pop_retrieve_flush (server))
912 if (sendline (server, "QUIT") || getok (server))
917 close (server->file);
921 free (server->buffer);
922 free ((char *) server);
928 static int have_winsock = 0;
932 * Function: socket_connection
934 * Purpose: Opens the network connection with the mail host, without
935 * doing any sort of I/O with it or anything.
938 * host The host to which to connect.
939 * flags Option flags.
941 * Return value: A file descriptor indicating the connection, or -1
942 * indicating failure, in which case an error has been copied
946 socket_connection (char *host, int flags)
948 struct hostent *hostent;
949 struct servent *servent;
950 struct sockaddr_in addr;
958 krb5_principal client, server;
965 Key_schedule schedule;
968 #endif /* KERBEROS */
975 if (WSAStartup (0x101, &winsockData) == 0)
982 hostent = gethostbyname (host);
985 #ifndef BROKEN_CYGWIN
986 && ((h_errno != TRY_AGAIN) || (try_count == 5))
990 strcpy (pop_error, "Could not determine POP server's address");
995 memset (&addr, 0, sizeof (addr));
996 addr.sin_family = AF_INET;
999 service = (flags & POP_NO_KERBEROS) ? POP_SERVICE : KPOP_SERVICE;
1001 service = POP_SERVICE;
1005 if (! (flags & POP_NO_HESIOD))
1007 servent = hes_getservbyname (service, "tcp");
1010 addr.sin_port = servent->s_port;
1017 servent = getservbyname (service, "tcp");
1020 addr.sin_port = servent->s_port;
1025 addr.sin_port = htons ((flags & POP_NO_KERBEROS) ?
1026 POP_PORT : KPOP_PORT);
1028 addr.sin_port = htons (POP_PORT);
1033 #define SOCKET_ERROR "Could not create socket for POP connection: "
1035 sock = socket (PF_INET, SOCK_STREAM, 0);
1038 strcpy (pop_error, SOCKET_ERROR);
1039 strncat (pop_error, strerror (errno),
1040 ERROR_MAX - sizeof (SOCKET_ERROR));
1045 while (*hostent->h_addr_list)
1047 memcpy (&addr.sin_addr, *hostent->h_addr_list, hostent->h_length);
1048 if (! connect (sock, (struct sockaddr *) &addr, sizeof (addr)))
1050 hostent->h_addr_list++;
1053 #define CONNECT_ERROR "Could not connect to POP server: "
1055 if (! *hostent->h_addr_list)
1058 strcpy (pop_error, CONNECT_ERROR);
1059 strncat (pop_error, strerror (errno),
1060 ERROR_MAX - sizeof (CONNECT_ERROR));
1066 #define KRB_ERROR "Kerberos error connecting to POP server: "
1067 if (! (flags & POP_NO_KERBEROS))
1072 if (rem = krb5_cc_default (&ccdef))
1075 strcpy (pop_error, KRB_ERROR);
1076 strncat (pop_error, error_message (rem),
1077 ERROR_MAX - sizeof(KRB_ERROR));
1082 if (rem = krb5_cc_get_principal (ccdef, &client))
1087 for (cp = hostent->h_name; *cp; cp++)
1091 *cp = tolower (*cp);
1095 if (rem = krb5_sname_to_principal (hostent->h_name, POP_SERVICE,
1101 rem = krb5_sendauth ((krb5_pointer) &sock, "KPOPV1.0", client, server,
1102 AP_OPTS_MUTUAL_REQUIRED,
1103 0, /* no checksum */
1104 0, /* no creds, use ccache instead */
1106 0, /* don't need seq # */
1107 0, /* don't need subsession key */
1109 0); /* don't need reply */
1110 krb5_free_principal (server);
1113 if (err_ret && err_ret->text.length)
1115 strcpy (pop_error, KRB_ERROR);
1116 strncat (pop_error, error_message (rem),
1117 ERROR_MAX - sizeof (KRB_ERROR));
1118 strncat (pop_error, " [server says '",
1119 ERROR_MAX - strlen (pop_error) - 1);
1120 strncat (pop_error, err_ret->text.data,
1121 min (ERROR_MAX - strlen (pop_error) - 1,
1122 err_ret->text.length));
1123 strncat (pop_error, "']",
1124 ERROR_MAX - strlen (pop_error) - 1);
1128 strcpy (pop_error, KRB_ERROR);
1129 strncat (pop_error, error_message (rem),
1130 ERROR_MAX - sizeof (KRB_ERROR));
1133 krb5_free_error (err_ret);
1139 ticket = (KTEXT) malloc (sizeof (KTEXT_ST));
1140 rem = krb_sendauth (0L, sock, ticket, "pop", hostent->h_name,
1141 (char *) krb_realmofhost (hostent->h_name),
1142 (unsigned long) 0, &msg_data, &cred, schedule,
1143 (struct sockaddr_in *) 0,
1144 (struct sockaddr_in *) 0,
1146 free ((char *) ticket);
1147 if (rem != KSUCCESS)
1149 strcpy (pop_error, KRB_ERROR);
1150 strncat (pop_error, krb_err_txt[rem],
1151 ERROR_MAX - sizeof (KRB_ERROR));
1157 #endif /* KERBEROS */
1160 } /* socket_connection */
1163 * Function: pop_getline
1165 * Purpose: Get a line of text from the connection and return a
1166 * pointer to it. The carriage return and linefeed at the end of
1167 * the line are stripped, but periods at the beginnings of lines
1168 * are NOT dealt with in any special way.
1171 * server The server from which to get the line of text.
1173 * Returns: A non-null pointer if successful, or a null pointer on any
1174 * error, with an error message copied into pop_error.
1176 * Notes: The line returned is overwritten with each call to pop_getline.
1178 * Side effects: Closes the connection on error.
1181 pop_getline (popserver server)
1183 #define GETLINE_ERROR "Error reading from server: "
1186 int search_offset = 0;
1190 char *cp = find_crlf (server->buffer + server->buffer_index);
1196 found = server->buffer_index;
1197 data_used = (cp + 2) - server->buffer - found;
1199 *cp = '\0'; /* terminate the string to be returned */
1200 server->data -= data_used;
1201 server->buffer_index += data_used;
1204 fprintf (stderr, "<<< %s\n", server->buffer + found);
1205 return (server->buffer + found);
1209 memcpy (server->buffer,
1210 server->buffer + server->buffer_index,
1212 /* Record the fact that we've searched the data already in
1213 the buffer for a CRLF, so that when we search below, we
1214 don't have to search the same data twice. There's a "-
1215 1" here to account for the fact that the last character
1216 of the data we have may be the CR of a CRLF pair, of
1217 which we haven't read the second half yet, so we may have
1218 to search it again when we read more data. */
1219 search_offset = server->data - 1;
1220 server->buffer_index = 0;
1225 server->buffer_index = 0;
1230 /* There's a "- 1" here to leave room for the null that we put
1231 at the end of the read data below. We put the null there so
1232 that find_crlf knows where to stop when we call it. */
1233 if (server->data == server->buffer_size - 1)
1235 server->buffer_size += GETLINE_INCR;
1236 server->buffer = (char *)realloc (server->buffer, server->buffer_size);
1237 if (! server->buffer)
1239 strcpy (pop_error, "Out of memory in pop_getline");
1244 ret = RECV (server->file, server->buffer + server->data,
1245 server->buffer_size - server->data - 1, 0);
1248 strcpy (pop_error, GETLINE_ERROR);
1249 strncat (pop_error, strerror (errno),
1250 ERROR_MAX - sizeof (GETLINE_ERROR));
1256 strcpy (pop_error, "Unexpected EOF from server in pop_getline");
1263 server->data += ret;
1264 server->buffer[server->data] = '\0';
1266 cp = find_crlf (server->buffer + search_offset);
1269 int data_used = (cp + 2) - server->buffer;
1271 server->data -= data_used;
1272 server->buffer_index = data_used;
1275 fprintf (stderr, "<<< %s\n", server->buffer);
1276 return (server->buffer);
1278 search_offset += ret;
1286 * Function: sendline
1288 * Purpose: Sends a line of text to the POP server. The line of text
1289 * passed into this function should NOT have the carriage return
1290 * and linefeed on the end of it. Periods at beginnings of lines
1291 * will NOT be treated specially by this function.
1294 * server The server to which to send the text.
1295 * line The line of text to send.
1297 * Return value: Upon successful completion, a value of 0 will be
1298 * returned. Otherwise, a non-zero value will be returned, and
1299 * an error will be copied into pop_error.
1301 * Side effects: Closes the connection on error.
1304 sendline (popserver server, char *line)
1306 #define SENDLINE_ERROR "Error writing to POP server: "
1309 ret = fullwrite (server->file, line, strlen (line));
1311 { /* 0 indicates that a blank line was written */
1312 ret = fullwrite (server->file, "\r\n", 2);
1318 strcpy (pop_error, SENDLINE_ERROR);
1319 strncat (pop_error, strerror (errno),
1320 ERROR_MAX - sizeof (SENDLINE_ERROR));
1325 fprintf (stderr, ">>> %s\n", line);
1331 * Procedure: fullwrite
1333 * Purpose: Just like write, but keeps trying until the entire string
1336 * Return value: Same as write. Pop_error is not set.
1339 fullwrite (int fd, char *buf, int nbytes)
1345 while ((ret = SEND (fd, cp, nbytes, 0)) > 0)
1357 * Purpose: Reads a line from the server. If the return indicator is
1358 * positive, return with a zero exit status. If not, return with
1359 * a negative exit status.
1362 * server The server to read from.
1364 * Returns: 0 for success, else for failure and puts error in pop_error.
1366 * Side effects: On failure, may make the connection unusable.
1369 getok (popserver server)
1373 if (! (fromline = pop_getline (server)))
1378 if (! strncmp (fromline, "+OK", 3))
1380 else if (! strncmp (fromline, "-ERR", 4))
1382 strncpy (pop_error, fromline, ERROR_MAX);
1383 pop_error[ERROR_MAX-1] = '\0';
1389 "Unexpected response from server; expecting +OK or -ERR");
1397 * Function: gettermination
1399 * Purpose: Gets the next line and verifies that it is a termination
1400 * line (nothing but a dot).
1402 * Return value: 0 on success, non-zero with pop_error set on error.
1404 * Side effects: Closes the connection on error.
1407 gettermination (popserver server)
1411 fromserver = pop_getline (server);
1415 if (strcmp (fromserver, "."))
1418 "Unexpected response from server in gettermination");
1428 * Function pop_close
1430 * Purpose: Close a pop connection, sending a "RSET" command to try to
1431 * preserve any changes that were made and a "QUIT" command to
1432 * try to get the server to quit, but ignoring any responses that
1435 * Side effects: The server is unusable after this function returns.
1436 * Changes made to the maildrop since the session was started (or
1437 * since the last pop_reset) may be lost.
1440 pop_close (popserver server)
1443 free ((char *) server);
1449 * Function: pop_trash
1451 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1452 * memory associated with the server. It is legal to call
1453 * pop_close or pop_quit after this function has been called.
1456 pop_trash (popserver server)
1458 if (server->file >= 0)
1460 /* avoid recursion; sendline can call pop_trash */
1461 if (server->trash_started)
1463 server->trash_started = 1;
1465 sendline (server, "RSET");
1466 sendline (server, "QUIT");
1468 CLOSESOCKET (server->file);
1472 free (server->buffer);
1483 /* Return a pointer to the first CRLF in IN_STRING,
1484 or 0 if it does not contain one. */
1487 find_crlf (char *in_string)
1493 else if (*in_string == '\r')
1495 if (*++in_string == '\n')
1496 return (in_string - 1);
1504 #endif /* MAIL_USE_POP */