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 #define DONT_ENCAPSULATE
32 #include <sys/types.h>
36 #define RECV(s,buf,len,flags) recv(s,buf,len,flags)
37 #define SEND(s,buf,len,flags) send(s,buf,len,flags)
38 #define CLOSESOCKET(s) closesocket(s)
40 #include <netinet/in.h>
41 #include <sys/socket.h>
42 #define RECV(s,buf,len,flags) read(s,buf,len)
43 #define SEND(s,buf,len,flags) write(s,buf,len)
44 #define CLOSESOCKET(s) close(s)
55 * It really shouldn't be necessary to put this declaration here, but
56 * the version of hesiod.h that Athena has installed in release 7.2
57 * doesn't declare this function; I don't know if the 7.3 version of
60 extern struct servent *hes_getservbyname (/* char *, char * */);
63 #include "../src/syspwd.h"
77 #include "../src/syswait.h"
79 #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 unsigned 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(WIN32_NATIVE) || defined(CYGWIN)
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))
187 username = getlogin ();
188 if (! (username && *username))
190 struct passwd *passwd;
191 passwd = getpwuid (getuid ());
192 if (passwd && passwd->pw_name && *passwd->pw_name)
194 username = passwd->pw_name;
198 strcpy (pop_error, "Could not determine username");
203 strcpy (pop_error, "Could not determine username");
210 * Determine the mail host.
215 host = getenv ("MAILHOST");
219 if ((! host) && (! (flags & POP_NO_HESIOD)))
221 struct hes_postoffice *office;
222 office = hes_getmailhost (username);
223 if (office && office->po_type && (! strcmp (office->po_type, "POP"))
224 && office->po_name && *office->po_name && office->po_host
227 host = office->po_host;
228 username = office->po_name;
242 strcpy (pop_error, "Could not determine POP server");
246 /* Determine the password */
248 #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
250 #define DONT_NEED_PASSWORD 0
253 if ((! password) && (! DONT_NEED_PASSWORD))
256 if (! (flags & POP_NO_GETPASS))
258 password = getpass ("Enter POP password:");
263 strcpy (pop_error, "Could not determine POP password");
268 flags |= POP_NO_KERBEROS;
272 sock = socket_connection (host, flags);
276 server = (popserver) malloc (sizeof (struct _popserver));
279 strcpy (pop_error, "Out of memory in pop_open");
282 server->buffer = (char *) malloc (GETLINE_MIN);
283 if (! server->buffer)
285 strcpy (pop_error, "Out of memory in pop_open");
286 free ((char *) server);
292 server->buffer_index = 0;
293 server->buffer_size = GETLINE_MIN;
294 server->in_multi = 0;
295 server->trash_started = 0;
301 * I really shouldn't use the pop_error variable like this, but....
303 if (strlen (username) > ERROR_MAX - 6)
307 "Username too long; recompile pop.c with larger ERROR_MAX");
310 sprintf (pop_error, "USER %s", username);
312 if (sendline (server, pop_error) || getok (server))
317 if (strlen (password) > ERROR_MAX - 6)
321 "Password too long; recompile pop.c with larger ERROR_MAX");
324 sprintf (pop_error, "PASS %s", password);
326 if (sendline (server, pop_error) || getok (server))
337 * Purpose: Issue the STAT command to the server and return (in the
338 * value parameters) the number of messages in the maildrop and
339 * the total size of the maildrop.
341 * Return value: 0 on success, or non-zero with an error in pop_error
344 * Side effects: On failure, may make further operations on the
345 * connection impossible.
348 pop_stat (popserver server, int *count, int *size)
352 if (server->in_multi)
354 strcpy (pop_error, "In multi-line query in pop_stat");
358 if (sendline (server, "STAT") || (! (fromserver = pop_getline (server))))
361 if (strncmp (fromserver, "+OK ", 4))
363 if (0 == strncmp (fromserver, "-ERR", 4))
365 strncpy (pop_error, fromserver, ERROR_MAX);
370 "Unexpected response from POP server in pop_stat");
376 *count = atoi (&fromserver[4]);
378 fromserver = strchr (&fromserver[4], ' ');
382 "Badly formatted response from server in pop_stat");
387 *size = atoi (fromserver + 1);
395 * Purpose: Performs the POP "list" command and returns (in value
396 * parameters) two malloc'd zero-terminated arrays -- one of
397 * message IDs, and a parallel one of sizes.
400 * server The pop connection to talk to.
401 * message The number of the one message about which to get
402 * information, or 0 to get information about all
405 * Return value: 0 on success, non-zero with error in pop_error on
408 * Side effects: On failure, may make further operations on the
409 * connection impossible.
412 pop_list (popserver server, int message, int **IDs, int **sizes)
417 if (server->in_multi)
419 strcpy (pop_error, "In multi-line query in pop_list");
428 if (pop_stat (server, &count, &size))
433 *IDs = (int *) malloc ((how_many + 1) * sizeof (int));
434 *sizes = (int *) malloc ((how_many + 1) * sizeof (int));
435 if (! (*IDs && *sizes))
437 strcpy (pop_error, "Out of memory in pop_list");
443 sprintf (pop_error, "LIST %d", message);
444 if (sendline (server, pop_error))
446 free ((char *) *IDs);
447 free ((char *) *sizes);
450 if (! (fromserver = pop_getline (server)))
452 free ((char *) *IDs);
453 free ((char *) *sizes);
456 if (strncmp (fromserver, "+OK ", 4))
458 if (! strncmp (fromserver, "-ERR", 4))
459 strncpy (pop_error, fromserver, ERROR_MAX);
463 "Unexpected response from server in pop_list");
466 free ((char *) *IDs);
467 free ((char *) *sizes);
470 (*IDs)[0] = atoi (&fromserver[4]);
471 fromserver = strchr (&fromserver[4], ' ');
475 "Badly formatted response from server in pop_list");
477 free ((char *) *IDs);
478 free ((char *) *sizes);
481 (*sizes)[0] = atoi (fromserver);
482 (*IDs)[1] = (*sizes)[1] = 0;
487 if (pop_multi_first (server, "LIST", &fromserver))
489 free ((char *) *IDs);
490 free ((char *) *sizes);
493 for (i = 0; i < how_many; i++)
495 if (pop_multi_next (server, &fromserver))
497 free ((char *) *IDs);
498 free ((char *) *sizes);
501 (*IDs)[i] = atoi (fromserver);
502 fromserver = strchr (fromserver, ' ');
506 "Badly formatted response from server in pop_list");
507 free ((char *) *IDs);
508 free ((char *) *sizes);
512 (*sizes)[i] = atoi (fromserver);
514 if (pop_multi_next (server, &fromserver))
516 free ((char *) *IDs);
517 free ((char *) *sizes);
523 "Too many response lines from server in pop_list");
524 free ((char *) *IDs);
525 free ((char *) *sizes);
528 (*IDs)[i] = (*sizes)[i] = 0;
534 * Function: pop_retrieve
536 * Purpose: Retrieve a specified message from the maildrop.
539 * server The server to retrieve from.
540 * message The message number to retrieve.
542 * If true, then mark the string "From " at the beginning
545 * Return value: A string pointing to the message, if successful, or
546 * null with pop_error set if not.
548 * Side effects: May kill connection on error.
551 pop_retrieve (popserver server, int message, int markfrom)
553 int *IDs, *sizes, bufsize, fromcount = 0, cp = 0;
554 char *ptr, *fromserver;
557 if (server->in_multi)
559 strcpy (pop_error, "In multi-line query in pop_retrieve");
563 if (pop_list (server, message, &IDs, &sizes))
566 if (pop_retrieve_first (server, message, &fromserver))
572 * The "5" below is an arbitrary constant -- I assume that if
573 * there are "From" lines in the text to be marked, there
574 * probably won't be more than 5 of them. If there are, I
575 * allocate more space for them below.
577 bufsize = sizes[0] + (markfrom ? 5 : 0);
578 ptr = (char *)malloc (bufsize);
580 free ((char *) sizes);
584 strcpy (pop_error, "Out of memory in pop_retrieve");
585 pop_retrieve_flush (server);
589 while (! (ret = pop_retrieve_next (server, &fromserver)))
598 if (markfrom && fromserver[0] == 'F' && fromserver[1] == 'r' &&
599 fromserver[2] == 'o' && fromserver[3] == 'm' &&
600 fromserver[4] == ' ')
602 if (++fromcount == 5)
605 ptr = (char *)realloc (ptr, bufsize);
608 strcpy (pop_error, "Out of memory in pop_retrieve");
609 pop_retrieve_flush (server);
616 linesize = strlen (fromserver);
617 memcpy (&ptr[cp], fromserver, linesize);
627 /* This function used to fall off the end, but that doesn't make any sense */
632 pop_retrieve_first (popserver server, int message, char **response)
634 sprintf (pop_error, "RETR %d", message);
635 return (pop_multi_first (server, pop_error, response));
639 pop_retrieve_next (popserver server, char **line)
641 return (pop_multi_next (server, line));
645 pop_retrieve_flush (popserver server)
647 return (pop_multi_flush (server));
651 pop_top_first (popserver server, int message, int lines, char **response)
653 sprintf (pop_error, "TOP %d %d", message, lines);
654 return (pop_multi_first (server, pop_error, response));
658 pop_top_next (popserver server, char **line)
660 return (pop_multi_next (server, line));
664 pop_top_flush (popserver server)
666 return (pop_multi_flush (server));
670 pop_multi_first (popserver server, char *command, char **response)
672 if (server->in_multi)
675 "Already in multi-line query in pop_multi_first");
679 if (sendline (server, command) || (! (*response = pop_getline (server))))
684 if (0 == strncmp (*response, "-ERR", 4))
686 strncpy (pop_error, *response, ERROR_MAX);
689 else if (0 == strncmp (*response, "+OK", 3))
691 for (*response += 3; **response == ' '; (*response)++) /* empty */;
692 server->in_multi = 1;
698 "Unexpected response from server in pop_multi_first");
704 pop_multi_next (popserver server, char **line)
708 if (! server->in_multi)
710 strcpy (pop_error, "Not in multi-line query in pop_multi_next");
714 fromserver = pop_getline (server);
720 if (fromserver[0] == '.')
725 server->in_multi = 0;
730 *line = fromserver + 1;
742 pop_multi_flush (popserver server)
746 if (! server->in_multi)
751 while (! pop_multi_next (server, &line))
762 /* Function: pop_delete
764 * Purpose: Delete a specified message.
767 * server Server from which to delete the message.
768 * message Message to delete.
770 * Return value: 0 on success, non-zero with error in pop_error
774 pop_delete (popserver server, int message)
776 if (server->in_multi)
778 strcpy (pop_error, "In multi-line query in pop_delete");
782 sprintf (pop_error, "DELE %d", message);
784 if (sendline (server, pop_error) || getok (server))
793 * Purpose: Send a noop command to the server.
796 * server The server to send to.
798 * Return value: 0 on success, non-zero with error in pop_error
801 * Side effects: Closes connection on error.
804 pop_noop (popserver server)
806 if (server->in_multi)
808 strcpy (pop_error, "In multi-line query in pop_noop");
812 if (sendline (server, "NOOP") || getok (server))
821 * Purpose: Find out the highest seen message from the server.
826 * Return value: If successful, the highest seen message, which is
827 * greater than or equal to 0. Otherwise, a negative number with
828 * the error explained in pop_error.
830 * Side effects: Closes the connection on error.
833 pop_last (popserver server)
837 if (server->in_multi)
839 strcpy (pop_error, "In multi-line query in pop_last");
843 if (sendline (server, "LAST"))
846 if (! (fromserver = pop_getline (server)))
849 if (! strncmp (fromserver, "-ERR", 4))
851 strncpy (pop_error, fromserver, ERROR_MAX);
854 else if (strncmp (fromserver, "+OK ", 4))
856 strcpy (pop_error, "Unexpected response from server in pop_last");
862 return (atoi (&fromserver[4]));
867 * Function: pop_reset
869 * Purpose: Reset the server to its initial connect state
874 * Return value: 0 for success, non-0 with error in pop_error
877 * Side effects: Closes the connection on error.
880 pop_reset (popserver server)
882 if (pop_retrieve_flush (server))
887 if (sendline (server, "RSET") || getok (server))
896 * Purpose: Quit the connection to the server,
899 * server The server to quit.
901 * Return value: 0 for success, non-zero otherwise with error in
904 * Side Effects: The popserver passed in is unusable after this
905 * function is called, even if an error occurs.
908 pop_quit (popserver server)
912 if (server->file >= 0)
914 if (pop_retrieve_flush (server))
919 if (sendline (server, "QUIT") || getok (server))
924 CLOSESOCKET (server->file);
928 free (server->buffer);
929 free ((char *) server);
935 static int have_winsock = 0;
939 * Function: socket_connection
941 * Purpose: Opens the network connection with the mail host, without
942 * doing any sort of I/O with it or anything.
945 * host The host to which to connect.
946 * flags Option flags.
948 * Return value: A file descriptor indicating the connection, or -1
949 * indicating failure, in which case an error has been copied
953 socket_connection (char *host, int flags)
955 struct hostent *hostent;
956 struct servent *servent;
957 struct sockaddr_in addr;
965 krb5_principal client, server;
972 Key_schedule schedule;
975 #endif /* KERBEROS */
982 if (WSAStartup (0x101, &winsockData) == 0)
989 hostent = gethostbyname (host);
992 #ifndef BROKEN_CYGWIN
993 && ((h_errno != TRY_AGAIN) || (try_count == 5))
997 strcpy (pop_error, "Could not determine POP server's address");
1000 } while (! hostent);
1002 memset (&addr, 0, sizeof (addr));
1003 addr.sin_family = AF_INET;
1006 service = (flags & POP_NO_KERBEROS) ? POP_SERVICE : KPOP_SERVICE;
1008 service = POP_SERVICE;
1012 if (! (flags & POP_NO_HESIOD))
1014 servent = hes_getservbyname (service, "tcp");
1017 addr.sin_port = servent->s_port;
1024 servent = getservbyname (service, "tcp");
1027 addr.sin_port = servent->s_port;
1032 addr.sin_port = htons ((flags & POP_NO_KERBEROS) ?
1033 POP_PORT : KPOP_PORT);
1035 addr.sin_port = htons (POP_PORT);
1040 #define SOCKET_ERROR "Could not create socket for POP connection: "
1042 sock = socket (PF_INET, SOCK_STREAM, 0);
1045 strcpy (pop_error, SOCKET_ERROR);
1046 strncat (pop_error, strerror (errno),
1047 ERROR_MAX - sizeof (SOCKET_ERROR));
1052 while (*hostent->h_addr_list)
1054 memcpy (&addr.sin_addr, *hostent->h_addr_list, hostent->h_length);
1055 if (! connect (sock, (struct sockaddr *) &addr, sizeof (addr)))
1057 hostent->h_addr_list++;
1060 #define CONNECT_ERROR "Could not connect to POP server: "
1062 if (! *hostent->h_addr_list)
1065 strcpy (pop_error, CONNECT_ERROR);
1066 strncat (pop_error, strerror (errno),
1067 ERROR_MAX - sizeof (CONNECT_ERROR));
1073 #define KRB_ERROR "Kerberos error connecting to POP server: "
1074 if (! (flags & POP_NO_KERBEROS))
1079 if (rem = krb5_cc_default (&ccdef))
1082 strcpy (pop_error, KRB_ERROR);
1083 strncat (pop_error, error_message (rem),
1084 ERROR_MAX - sizeof(KRB_ERROR));
1089 if (rem = krb5_cc_get_principal (ccdef, &client))
1094 for (cp = hostent->h_name; *cp; cp++)
1098 *cp = tolower (*cp);
1102 if (rem = krb5_sname_to_principal (hostent->h_name, POP_SERVICE,
1108 rem = krb5_sendauth ((krb5_pointer) &sock, "KPOPV1.0", client, server,
1109 AP_OPTS_MUTUAL_REQUIRED,
1110 0, /* no checksum */
1111 0, /* no creds, use ccache instead */
1113 0, /* don't need seq # */
1114 0, /* don't need subsession key */
1116 0); /* don't need reply */
1117 krb5_free_principal (server);
1120 if (err_ret && err_ret->text.length)
1122 strcpy (pop_error, KRB_ERROR);
1123 strncat (pop_error, error_message (rem),
1124 ERROR_MAX - sizeof (KRB_ERROR));
1125 strncat (pop_error, " [server says '",
1126 ERROR_MAX - strlen (pop_error) - 1);
1127 strncat (pop_error, err_ret->text.data,
1128 min (ERROR_MAX - strlen (pop_error) - 1,
1129 err_ret->text.length));
1130 strncat (pop_error, "']",
1131 ERROR_MAX - strlen (pop_error) - 1);
1135 strcpy (pop_error, KRB_ERROR);
1136 strncat (pop_error, error_message (rem),
1137 ERROR_MAX - sizeof (KRB_ERROR));
1140 krb5_free_error (err_ret);
1146 ticket = (KTEXT) malloc (sizeof (KTEXT_ST));
1147 rem = krb_sendauth (0L, sock, ticket, "pop", hostent->h_name,
1148 (char *) krb_realmofhost (hostent->h_name),
1149 (unsigned long) 0, &msg_data, &cred, schedule,
1150 (struct sockaddr_in *) 0,
1151 (struct sockaddr_in *) 0,
1153 free ((char *) ticket);
1154 if (rem != KSUCCESS)
1156 strcpy (pop_error, KRB_ERROR);
1157 strncat (pop_error, krb_err_txt[rem],
1158 ERROR_MAX - sizeof (KRB_ERROR));
1164 #endif /* KERBEROS */
1167 } /* socket_connection */
1170 * Function: pop_getline
1172 * Purpose: Get a line of text from the connection and return a
1173 * pointer to it. The carriage return and linefeed at the end of
1174 * the line are stripped, but periods at the beginnings of lines
1175 * are NOT dealt with in any special way.
1178 * server The server from which to get the line of text.
1180 * Returns: A non-null pointer if successful, or a null pointer on any
1181 * error, with an error message copied into pop_error.
1183 * Notes: The line returned is overwritten with each call to pop_getline.
1185 * Side effects: Closes the connection on error.
1188 pop_getline (popserver server)
1190 #define GETLINE_ERROR "Error reading from server: "
1193 int search_offset = 0;
1197 char *cp = find_crlf (server->buffer + server->buffer_index);
1203 found = server->buffer_index;
1204 data_used = (cp + 2) - server->buffer - found;
1206 *cp = '\0'; /* terminate the string to be returned */
1207 server->data -= data_used;
1208 server->buffer_index += data_used;
1211 fprintf (stderr, "<<< %s\n", server->buffer + found);
1212 return (server->buffer + found);
1216 memcpy (server->buffer,
1217 server->buffer + server->buffer_index,
1219 /* Record the fact that we've searched the data already in
1220 the buffer for a CRLF, so that when we search below, we
1221 don't have to search the same data twice. There's a "-
1222 1" here to account for the fact that the last character
1223 of the data we have may be the CR of a CRLF pair, of
1224 which we haven't read the second half yet, so we may have
1225 to search it again when we read more data. */
1226 search_offset = server->data - 1;
1227 server->buffer_index = 0;
1232 server->buffer_index = 0;
1237 /* There's a "- 1" here to leave room for the null that we put
1238 at the end of the read data below. We put the null there so
1239 that find_crlf knows where to stop when we call it. */
1240 if (server->data == server->buffer_size - 1)
1242 server->buffer_size += GETLINE_INCR;
1243 server->buffer = (char *)realloc (server->buffer, server->buffer_size);
1244 if (! server->buffer)
1246 strcpy (pop_error, "Out of memory in pop_getline");
1251 ret = RECV (server->file, server->buffer + server->data,
1252 server->buffer_size - server->data - 1, 0);
1255 strcpy (pop_error, GETLINE_ERROR);
1256 strncat (pop_error, strerror (errno),
1257 ERROR_MAX - sizeof (GETLINE_ERROR));
1263 strcpy (pop_error, "Unexpected EOF from server in pop_getline");
1270 server->data += ret;
1271 server->buffer[server->data] = '\0';
1273 cp = find_crlf (server->buffer + search_offset);
1276 int data_used = (cp + 2) - server->buffer;
1278 server->data -= data_used;
1279 server->buffer_index = data_used;
1282 fprintf (stderr, "<<< %s\n", server->buffer);
1283 return (server->buffer);
1285 search_offset += ret;
1293 * Function: sendline
1295 * Purpose: Sends a line of text to the POP server. The line of text
1296 * passed into this function should NOT have the carriage return
1297 * and linefeed on the end of it. Periods at beginnings of lines
1298 * will NOT be treated specially by this function.
1301 * server The server to which to send the text.
1302 * line The line of text to send.
1304 * Return value: Upon successful completion, a value of 0 will be
1305 * returned. Otherwise, a non-zero value will be returned, and
1306 * an error will be copied into pop_error.
1308 * Side effects: Closes the connection on error.
1311 sendline (popserver server, char *line)
1313 #define SENDLINE_ERROR "Error writing to POP server: "
1316 ret = fullwrite (server->file, line, strlen (line));
1318 { /* 0 indicates that a blank line was written */
1319 ret = fullwrite (server->file, "\r\n", 2);
1325 strcpy (pop_error, SENDLINE_ERROR);
1326 strncat (pop_error, strerror (errno),
1327 ERROR_MAX - sizeof (SENDLINE_ERROR));
1332 fprintf (stderr, ">>> %s\n", line);
1338 * Procedure: fullwrite
1340 * Purpose: Just like write, but keeps trying until the entire string
1343 * Return value: Same as write. Pop_error is not set.
1346 fullwrite (int fd, char *buf, int nbytes)
1352 while ((ret = SEND (fd, cp, nbytes, 0)) > 0)
1364 * Purpose: Reads a line from the server. If the return indicator is
1365 * positive, return with a zero exit status. If not, return with
1366 * a negative exit status.
1369 * server The server to read from.
1371 * Returns: 0 for success, else for failure and puts error in pop_error.
1373 * Side effects: On failure, may make the connection unusable.
1376 getok (popserver server)
1380 if (! (fromline = pop_getline (server)))
1385 if (! strncmp (fromline, "+OK", 3))
1387 else if (! strncmp (fromline, "-ERR", 4))
1389 strncpy (pop_error, fromline, ERROR_MAX);
1390 pop_error[ERROR_MAX-1] = '\0';
1396 "Unexpected response from server; expecting +OK or -ERR");
1404 * Function: gettermination
1406 * Purpose: Gets the next line and verifies that it is a termination
1407 * line (nothing but a dot).
1409 * Return value: 0 on success, non-zero with pop_error set on error.
1411 * Side effects: Closes the connection on error.
1414 gettermination (popserver server)
1418 fromserver = pop_getline (server);
1422 if (strcmp (fromserver, "."))
1425 "Unexpected response from server in gettermination");
1435 * Function pop_close
1437 * Purpose: Close a pop connection, sending a "RSET" command to try to
1438 * preserve any changes that were made and a "QUIT" command to
1439 * try to get the server to quit, but ignoring any responses that
1442 * Side effects: The server is unusable after this function returns.
1443 * Changes made to the maildrop since the session was started (or
1444 * since the last pop_reset) may be lost.
1447 pop_close (popserver server)
1450 free ((char *) server);
1456 * Function: pop_trash
1458 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1459 * memory associated with the server. It is legal to call
1460 * pop_close or pop_quit after this function has been called.
1463 pop_trash (popserver server)
1465 if (server->file >= 0)
1467 /* avoid recursion; sendline can call pop_trash */
1468 if (server->trash_started)
1470 server->trash_started = 1;
1472 sendline (server, "RSET");
1473 sendline (server, "QUIT");
1475 CLOSESOCKET (server->file);
1479 free (server->buffer);
1490 /* Return a pointer to the first CRLF in IN_STRING,
1491 or 0 if it does not contain one. */
1494 find_crlf (char *in_string)
1500 else if (*in_string == '\r')
1502 if (*++in_string == '\n')
1503 return (in_string - 1);
1511 #endif /* MAIL_USE_POP */