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 * */);
81 #include <krb5/krb5.h>
82 #include <krb5/ext-proto.h>
87 extern char *getenv (/* char * */);
88 extern char *getlogin (/* void */);
89 extern char *getpass (/* char * */);
90 extern char *strerror (/* int */);
91 extern char *index ();
95 extern int krb_sendauth (/* long, int, KTEXT, char *, char *, char *,
96 u_long, MSG_DAT *, CREDENTIALS *, Key_schedule,
97 struct sockaddr_in *, struct sockaddr_in *,
99 extern char *krb_realmofhost (/* char * */);
101 #endif /* KERBEROS */
104 #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H)
109 static int socket_connection (/* char *, int */);
110 static char *getline (/* popserver */);
111 static int sendline (/* popserver, char * */);
112 static int fullwrite (/* int, char *, int */);
113 static int getok (/* popserver */);
115 static int gettermination (/* popserver */);
117 static void pop_trash (/* popserver */);
118 static char *find_crlf (/* char * */);
120 #define ERROR_MAX 80 /* a pretty arbitrary size */
122 #define KPOP_PORT 1109
124 #define POP_SERVICE "pop3" /* we don't want the POP2 port! */
126 #define POP_SERVICE "pop"
130 #define KPOP_SERVICE "k5pop";
132 #define KPOP_SERVICE "kpop"
136 char pop_error
[ERROR_MAX
];
140 #define min(a,b) (((a) < (b)) ? (a) : (b))
144 * Function: pop_open (char *host, char *username, char *password,
147 * Purpose: Establishes a connection with a post-office server, and
148 * completes the authorization portion of the session.
151 * host The server host with which the connection should be
152 * established. Optional. If omitted, internal
153 * heuristics will be used to determine the server host,
156 * The username of the mail-drop to access. Optional.
157 * If omitted, internal heuristics will be used to
158 * determine the username, if possible.
160 * The password to use for authorization. If omitted,
161 * internal heuristics will be used to determine the
162 * password, if possible.
163 * flags A bit mask containing flags controlling certain
164 * functions of the routine. Valid flags are defined in
167 * Return value: Upon successful establishment of a connection, a
168 * non-null popserver will be returned. Otherwise, null will be
169 * returned, and the string variable pop_error will contain an
170 * explanation of the error.
173 pop_open (host
, username
, password
, flags
)
182 /* Determine the user name */
185 username
= getenv ("USER");
186 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");
207 * Determine the mail host.
212 host
= getenv ("MAILHOST");
216 if ((! host
) && (! (flags
& POP_NO_HESIOD
)))
218 struct hes_postoffice
*office
;
219 office
= hes_getmailhost (username
);
220 if (office
&& office
->po_type
&& (! strcmp (office
->po_type
, "POP"))
221 && office
->po_name
&& *office
->po_name
&& office
->po_host
224 host
= office
->po_host
;
225 username
= office
->po_name
;
239 strcpy (pop_error
, "Could not determine POP server");
243 /* Determine the password */
245 #define DONT_NEED_PASSWORD (! (flags & POP_NO_KERBEROS))
247 #define DONT_NEED_PASSWORD 0
250 if ((! password
) && (! DONT_NEED_PASSWORD
))
252 if (! (flags
& POP_NO_GETPASS
))
254 password
= getpass ("Enter POP password:");
258 strcpy (pop_error
, "Could not determine POP password");
263 flags
|= POP_NO_KERBEROS
;
267 sock
= socket_connection (host
, flags
);
271 server
= (popserver
) malloc (sizeof (struct _popserver
));
274 strcpy (pop_error
, "Out of memory in pop_open");
277 server
->buffer
= (char *) malloc (GETLINE_MIN
);
278 if (! server
->buffer
)
280 strcpy (pop_error
, "Out of memory in pop_open");
281 free ((char *) server
);
287 server
->buffer_index
= 0;
288 server
->buffer_size
= GETLINE_MIN
;
289 server
->in_multi
= 0;
290 server
->trash_started
= 0;
296 * I really shouldn't use the pop_error variable like this, but....
298 if (strlen (username
) > ERROR_MAX
- 6)
302 "Username too long; recompile pop.c with larger ERROR_MAX");
305 sprintf (pop_error
, "USER %s", username
);
307 if (sendline (server
, pop_error
) || getok (server
))
312 if (strlen (password
) > ERROR_MAX
- 6)
316 "Password too long; recompile pop.c with larger ERROR_MAX");
319 sprintf (pop_error
, "PASS %s", password
);
321 if (sendline (server
, pop_error
) || getok (server
))
332 * Purpose: Issue the STAT command to the server and return (in the
333 * value parameters) the number of messages in the maildrop and
334 * the total size of the maildrop.
336 * Return value: 0 on success, or non-zero with an error in pop_error
339 * Side effects: On failure, may make further operations on the
340 * connection impossible.
343 pop_stat (server
, count
, size
)
350 if (server
->in_multi
)
352 strcpy (pop_error
, "In multi-line query in pop_stat");
356 if (sendline (server
, "STAT") || (! (fromserver
= getline (server
))))
359 if (strncmp (fromserver
, "+OK ", 4))
361 if (0 == strncmp (fromserver
, "-ERR", 4))
363 strncpy (pop_error
, fromserver
, ERROR_MAX
);
368 "Unexpected response from POP server in pop_stat");
374 *count
= atoi (&fromserver
[4]);
376 fromserver
= index (&fromserver
[4], ' ');
380 "Badly formatted response from server in pop_stat");
385 *size
= atoi (fromserver
+ 1);
393 * Purpose: Performs the POP "list" command and returns (in value
394 * parameters) two malloc'd zero-terminated arrays -- one of
395 * message IDs, and a parallel one of sizes.
398 * server The pop connection to talk to.
399 * message The number of the one message about which to get
400 * information, or 0 to get information about all
403 * Return value: 0 on success, non-zero with error in pop_error on
406 * Side effects: On failure, may make further operations on the
407 * connection impossible.
410 pop_list (server
, message
, IDs
, sizes
)
419 if (server
->in_multi
)
421 strcpy (pop_error
, "In multi-line query in pop_list");
430 if (pop_stat (server
, &count
, &size
))
435 *IDs
= (int *) malloc ((how_many
+ 1) * sizeof (int));
436 *sizes
= (int *) malloc ((how_many
+ 1) * sizeof (int));
437 if (! (*IDs
&& *sizes
))
439 strcpy (pop_error
, "Out of memory in pop_list");
445 sprintf (pop_error
, "LIST %d", message
);
446 if (sendline (server
, pop_error
))
448 free ((char *) *IDs
);
449 free ((char *) *sizes
);
452 if (! (fromserver
= getline (server
)))
454 free ((char *) *IDs
);
455 free ((char *) *sizes
);
458 if (strncmp (fromserver
, "+OK ", 4))
460 if (! strncmp (fromserver
, "-ERR", 4))
461 strncpy (pop_error
, fromserver
, ERROR_MAX
);
465 "Unexpected response from server in pop_list");
468 free ((char *) *IDs
);
469 free ((char *) *sizes
);
472 (*IDs
)[0] = atoi (&fromserver
[4]);
473 fromserver
= index (&fromserver
[4], ' ');
477 "Badly formatted response from server in pop_list");
479 free ((char *) *IDs
);
480 free ((char *) *sizes
);
483 (*sizes
)[0] = atoi (fromserver
);
484 (*IDs
)[1] = (*sizes
)[1] = 0;
489 if (pop_multi_first (server
, "LIST", &fromserver
))
491 free ((char *) *IDs
);
492 free ((char *) *sizes
);
495 for (i
= 0; i
< how_many
; i
++)
497 if (pop_multi_next (server
, &fromserver
))
499 free ((char *) *IDs
);
500 free ((char *) *sizes
);
503 (*IDs
)[i
] = atoi (fromserver
);
504 fromserver
= index (fromserver
, ' ');
508 "Badly formatted response from server in pop_list");
509 free ((char *) *IDs
);
510 free ((char *) *sizes
);
514 (*sizes
)[i
] = atoi (fromserver
);
516 if (pop_multi_next (server
, &fromserver
))
518 free ((char *) *IDs
);
519 free ((char *) *sizes
);
525 "Too many response lines from server in pop_list");
526 free ((char *) *IDs
);
527 free ((char *) *sizes
);
530 (*IDs
)[i
] = (*sizes
)[i
] = 0;
536 * Function: pop_retrieve
538 * Purpose: Retrieve a specified message from the maildrop.
541 * server The server to retrieve from.
542 * message The message number to retrieve.
544 * If true, then mark the string "From " at the beginning
547 * Return value: A string pointing to the message, if successful, or
548 * null with pop_error set if not.
550 * Side effects: May kill connection on error.
553 pop_retrieve (server
, message
, markfrom
)
558 int *IDs
, *sizes
, bufsize
, fromcount
= 0, cp
= 0;
559 char *ptr
, *fromserver
;
562 if (server
->in_multi
)
564 strcpy (pop_error
, "In multi-line query in pop_retrieve");
568 if (pop_list (server
, message
, &IDs
, &sizes
))
571 if (pop_retrieve_first (server
, message
, &fromserver
))
577 * The "5" below is an arbitrary constant -- I assume that if
578 * there are "From" lines in the text to be marked, there
579 * probably won't be more than 5 of them. If there are, I
580 * allocate more space for them below.
582 bufsize
= sizes
[0] + (markfrom
? 5 : 0);
583 ptr
= (char *)malloc (bufsize
);
585 free ((char *) sizes
);
589 strcpy (pop_error
, "Out of memory in pop_retrieve");
590 pop_retrieve_flush (server
);
594 while (! (ret
= pop_retrieve_next (server
, &fromserver
)))
603 if (markfrom
&& fromserver
[0] == 'F' && fromserver
[1] == 'r' &&
604 fromserver
[2] == 'o' && fromserver
[3] == 'm' &&
605 fromserver
[4] == ' ')
607 if (++fromcount
== 5)
610 ptr
= (char *)realloc (ptr
, bufsize
);
613 strcpy (pop_error
, "Out of memory in pop_retrieve");
614 pop_retrieve_flush (server
);
621 linesize
= strlen (fromserver
);
622 bcopy (fromserver
, &ptr
[cp
], linesize
);
635 pop_retrieve_first (server
, message
, response
)
640 sprintf (pop_error
, "RETR %d", message
);
641 return (pop_multi_first (server
, pop_error
, response
));
645 pop_retrieve_next (server
, line
)
649 return (pop_multi_next (server
, line
));
653 pop_retrieve_flush (server
)
656 return (pop_multi_flush (server
));
660 pop_top_first (server
, message
, lines
, response
)
665 sprintf (pop_error
, "TOP %d %d", message
, lines
);
666 return (pop_multi_first (server
, pop_error
, response
));
670 pop_top_next (server
, line
)
674 return (pop_multi_next (server
, line
));
678 pop_top_flush (server
)
681 return (pop_multi_flush (server
));
685 pop_multi_first (server
, command
, response
)
690 if (server
->in_multi
)
693 "Already in multi-line query in pop_multi_first");
697 if (sendline (server
, command
) || (! (*response
= getline (server
))))
702 if (0 == strncmp (*response
, "-ERR", 4))
704 strncpy (pop_error
, *response
, ERROR_MAX
);
707 else if (0 == strncmp (*response
, "+OK", 3))
709 for (*response
+= 3; **response
== ' '; (*response
)++) /* empty */;
710 server
->in_multi
= 1;
716 "Unexpected response from server in pop_multi_first");
722 pop_multi_next (server
, line
)
728 if (! server
->in_multi
)
730 strcpy (pop_error
, "Not in multi-line query in pop_multi_next");
734 fromserver
= getline (server
);
740 if (fromserver
[0] == '.')
745 server
->in_multi
= 0;
750 *line
= fromserver
+ 1;
762 pop_multi_flush (server
)
767 if (! server
->in_multi
)
772 while (! pop_multi_next (server
, &line
))
783 /* Function: pop_delete
785 * Purpose: Delete a specified message.
788 * server Server from which to delete the message.
789 * message Message to delete.
791 * Return value: 0 on success, non-zero with error in pop_error
795 pop_delete (server
, message
)
799 if (server
->in_multi
)
801 strcpy (pop_error
, "In multi-line query in pop_delete");
805 sprintf (pop_error
, "DELE %d", message
);
807 if (sendline (server
, pop_error
) || getok (server
))
816 * Purpose: Send a noop command to the server.
819 * server The server to send to.
821 * Return value: 0 on success, non-zero with error in pop_error
824 * Side effects: Closes connection on error.
830 if (server
->in_multi
)
832 strcpy (pop_error
, "In multi-line query in pop_noop");
836 if (sendline (server
, "NOOP") || getok (server
))
845 * Purpose: Find out the highest seen message from the server.
850 * Return value: If successful, the highest seen message, which is
851 * greater than or equal to 0. Otherwise, a negative number with
852 * the error explained in pop_error.
854 * Side effects: Closes the connection on error.
862 if (server
->in_multi
)
864 strcpy (pop_error
, "In multi-line query in pop_last");
868 if (sendline (server
, "LAST"))
871 if (! (fromserver
= getline (server
)))
874 if (! strncmp (fromserver
, "-ERR", 4))
876 strncpy (pop_error
, fromserver
, ERROR_MAX
);
879 else if (strncmp (fromserver
, "+OK ", 4))
881 strcpy (pop_error
, "Unexpected response from server in pop_last");
887 return (atoi (&fromserver
[4]));
892 * Function: pop_reset
894 * Purpose: Reset the server to its initial connect state
899 * Return value: 0 for success, non-0 with error in pop_error
902 * Side effects: Closes the connection on error.
908 if (pop_retrieve_flush (server
))
913 if (sendline (server
, "RSET") || getok (server
))
922 * Purpose: Quit the connection to the server,
925 * server The server to quit.
927 * Return value: 0 for success, non-zero otherwise with error in
930 * Side Effects: The popserver passed in is unusable after this
931 * function is called, even if an error occurs.
939 if (server
->file
>= 0)
941 if (pop_retrieve_flush (server
))
946 if (sendline (server
, "QUIT") || getok (server
))
951 close (server
->file
);
955 free (server
->buffer
);
956 free ((char *) server
);
962 static int have_winsock
= 0;
966 * Function: socket_connection
968 * Purpose: Opens the network connection with the mail host, without
969 * doing any sort of I/O with it or anything.
972 * host The host to which to connect.
973 * flags Option flags.
975 * Return value: A file descriptor indicating the connection, or -1
976 * indicating failure, in which case an error has been copied
980 socket_connection (host
, flags
)
984 struct hostent
*hostent
;
985 struct servent
*servent
;
986 struct sockaddr_in addr
;
994 krb5_principal client
, server
;
1001 Key_schedule schedule
;
1004 #endif /* KERBEROS */
1010 WSADATA winsockData
;
1011 if (WSAStartup (0x101, &winsockData
) == 0)
1018 hostent
= gethostbyname (host
);
1020 if ((! hostent
) && ((h_errno
!= TRY_AGAIN
) || (try_count
== 5)))
1022 strcpy (pop_error
, "Could not determine POP server's address");
1025 } while (! hostent
);
1027 bzero ((char *) &addr
, sizeof (addr
));
1028 addr
.sin_family
= AF_INET
;
1031 service
= (flags
& POP_NO_KERBEROS
) ? POP_SERVICE
: KPOP_SERVICE
;
1033 service
= POP_SERVICE
;
1037 if (! (flags
& POP_NO_HESIOD
))
1039 servent
= hes_getservbyname (service
, "tcp");
1042 addr
.sin_port
= servent
->s_port
;
1049 servent
= getservbyname (service
, "tcp");
1052 addr
.sin_port
= servent
->s_port
;
1057 addr
.sin_port
= htons ((flags
& POP_NO_KERBEROS
) ?
1058 POP_PORT
: KPOP_PORT
);
1060 addr
.sin_port
= htons (POP_PORT
);
1065 #define SOCKET_ERROR "Could not create socket for POP connection: "
1067 sock
= socket (PF_INET
, SOCK_STREAM
, 0);
1070 strcpy (pop_error
, SOCKET_ERROR
);
1071 strncat (pop_error
, strerror (errno
),
1072 ERROR_MAX
- sizeof (SOCKET_ERROR
));
1077 while (*hostent
->h_addr_list
)
1079 bcopy (*hostent
->h_addr_list
, (char *) &addr
.sin_addr
,
1081 if (! connect (sock
, (struct sockaddr
*) &addr
, sizeof (addr
)))
1083 hostent
->h_addr_list
++;
1086 #define CONNECT_ERROR "Could not connect to POP server: "
1088 if (! *hostent
->h_addr_list
)
1091 strcpy (pop_error
, CONNECT_ERROR
);
1092 strncat (pop_error
, strerror (errno
),
1093 ERROR_MAX
- sizeof (CONNECT_ERROR
));
1099 #define KRB_ERROR "Kerberos error connecting to POP server: "
1100 if (! (flags
& POP_NO_KERBEROS
))
1105 if (rem
= krb5_cc_default (&ccdef
))
1108 strcpy (pop_error
, KRB_ERROR
);
1109 strncat (pop_error
, error_message (rem
),
1110 ERROR_MAX
- sizeof(KRB_ERROR
));
1115 if (rem
= krb5_cc_get_principal (ccdef
, &client
))
1120 for (cp
= hostent
->h_name
; *cp
; cp
++)
1124 *cp
= tolower (*cp
);
1128 if (rem
= krb5_sname_to_principal (hostent
->h_name
, POP_SERVICE
,
1134 rem
= krb5_sendauth ((krb5_pointer
) &sock
, "KPOPV1.0", client
, server
,
1135 AP_OPTS_MUTUAL_REQUIRED
,
1136 0, /* no checksum */
1137 0, /* no creds, use ccache instead */
1139 0, /* don't need seq # */
1140 0, /* don't need subsession key */
1142 0); /* don't need reply */
1143 krb5_free_principal (server
);
1146 if (err_ret
&& err_ret
->text
.length
)
1148 strcpy (pop_error
, KRB_ERROR
);
1149 strncat (pop_error
, error_message (rem
),
1150 ERROR_MAX
- sizeof (KRB_ERROR
));
1151 strncat (pop_error
, " [server says '",
1152 ERROR_MAX
- strlen (pop_error
) - 1);
1153 strncat (pop_error
, err_ret
->text
.data
,
1154 min (ERROR_MAX
- strlen (pop_error
) - 1,
1155 err_ret
->text
.length
));
1156 strncat (pop_error
, "']",
1157 ERROR_MAX
- strlen (pop_error
) - 1);
1161 strcpy (pop_error
, KRB_ERROR
);
1162 strncat (pop_error
, error_message (rem
),
1163 ERROR_MAX
- sizeof (KRB_ERROR
));
1166 krb5_free_error (err_ret
);
1172 ticket
= (KTEXT
) malloc (sizeof (KTEXT_ST
));
1173 rem
= krb_sendauth (0L, sock
, ticket
, "pop", hostent
->h_name
,
1174 (char *) krb_realmofhost (hostent
->h_name
),
1175 (unsigned long) 0, &msg_data
, &cred
, schedule
,
1176 (struct sockaddr_in
*) 0,
1177 (struct sockaddr_in
*) 0,
1179 free ((char *) ticket
);
1180 if (rem
!= KSUCCESS
)
1182 strcpy (pop_error
, KRB_ERROR
);
1183 strncat (pop_error
, krb_err_txt
[rem
],
1184 ERROR_MAX
- sizeof (KRB_ERROR
));
1190 #endif /* KERBEROS */
1193 } /* socket_connection */
1198 * Purpose: Get a line of text from the connection and return a
1199 * pointer to it. The carriage return and linefeed at the end of
1200 * the line are stripped, but periods at the beginnings of lines
1201 * are NOT dealt with in any special way.
1204 * server The server from which to get the line of text.
1206 * Returns: A non-null pointer if successful, or a null pointer on any
1207 * error, with an error message copied into pop_error.
1209 * Notes: The line returned is overwritten with each call to getline.
1211 * Side effects: Closes the connection on error.
1217 #define GETLINE_ERROR "Error reading from server: "
1220 int search_offset
= 0;
1224 char *cp
= find_crlf (server
->buffer
+ server
->buffer_index
);
1230 found
= server
->buffer_index
;
1231 data_used
= (cp
+ 2) - server
->buffer
- found
;
1233 *cp
= '\0'; /* terminate the string to be returned */
1234 server
->data
-= data_used
;
1235 server
->buffer_index
+= data_used
;
1238 fprintf (stderr
, "<<< %s\n", server
->buffer
+ found
);
1239 return (server
->buffer
+ found
);
1243 bcopy (server
->buffer
+ server
->buffer_index
,
1244 server
->buffer
, server
->data
);
1245 /* Record the fact that we've searched the data already in
1246 the buffer for a CRLF, so that when we search below, we
1247 don't have to search the same data twice. There's a "-
1248 1" here to account for the fact that the last character
1249 of the data we have may be the CR of a CRLF pair, of
1250 which we haven't read the second half yet, so we may have
1251 to search it again when we read more data. */
1252 search_offset
= server
->data
- 1;
1253 server
->buffer_index
= 0;
1258 server
->buffer_index
= 0;
1263 /* There's a "- 1" here to leave room for the null that we put
1264 at the end of the read data below. We put the null there so
1265 that find_crlf knows where to stop when we call it. */
1266 if (server
->data
== server
->buffer_size
- 1)
1268 server
->buffer_size
+= GETLINE_INCR
;
1269 server
->buffer
= (char *)realloc (server
->buffer
, server
->buffer_size
);
1270 if (! server
->buffer
)
1272 strcpy (pop_error
, "Out of memory in getline");
1277 ret
= RECV (server
->file
, server
->buffer
+ server
->data
,
1278 server
->buffer_size
- server
->data
- 1, 0);
1281 strcpy (pop_error
, GETLINE_ERROR
);
1282 strncat (pop_error
, strerror (errno
),
1283 ERROR_MAX
- sizeof (GETLINE_ERROR
));
1289 strcpy (pop_error
, "Unexpected EOF from server in getline");
1296 server
->data
+= ret
;
1297 server
->buffer
[server
->data
] = '\0';
1299 cp
= find_crlf (server
->buffer
+ search_offset
);
1302 int data_used
= (cp
+ 2) - server
->buffer
;
1304 server
->data
-= data_used
;
1305 server
->buffer_index
= data_used
;
1308 fprintf (stderr
, "<<< %s\n", server
->buffer
);
1309 return (server
->buffer
);
1311 search_offset
+= ret
;
1319 * Function: sendline
1321 * Purpose: Sends a line of text to the POP server. The line of text
1322 * passed into this function should NOT have the carriage return
1323 * and linefeed on the end of it. Periods at beginnings of lines
1324 * will NOT be treated specially by this function.
1327 * server The server to which to send the text.
1328 * line The line of text to send.
1330 * Return value: Upon successful completion, a value of 0 will be
1331 * returned. Otherwise, a non-zero value will be returned, and
1332 * an error will be copied into pop_error.
1334 * Side effects: Closes the connection on error.
1337 sendline (server
, line
)
1341 #define SENDLINE_ERROR "Error writing to POP server: "
1344 ret
= fullwrite (server
->file
, line
, strlen (line
));
1346 { /* 0 indicates that a blank line was written */
1347 ret
= fullwrite (server
->file
, "\r\n", 2);
1353 strcpy (pop_error
, SENDLINE_ERROR
);
1354 strncat (pop_error
, strerror (errno
),
1355 ERROR_MAX
- sizeof (SENDLINE_ERROR
));
1360 fprintf (stderr
, ">>> %s\n", line
);
1366 * Procedure: fullwrite
1368 * Purpose: Just like write, but keeps trying until the entire string
1371 * Return value: Same as write. Pop_error is not set.
1374 fullwrite (fd
, buf
, nbytes
)
1383 while ((ret
= SEND (fd
, cp
, nbytes
, 0)) > 0)
1395 * Purpose: Reads a line from the server. If the return indicator is
1396 * positive, return with a zero exit status. If not, return with
1397 * a negative exit status.
1400 * server The server to read from.
1402 * Returns: 0 for success, else for failure and puts error in pop_error.
1404 * Side effects: On failure, may make the connection unusable.
1412 if (! (fromline
= getline (server
)))
1417 if (! strncmp (fromline
, "+OK", 3))
1419 else if (! strncmp (fromline
, "-ERR", 4))
1421 strncpy (pop_error
, fromline
, ERROR_MAX
);
1422 pop_error
[ERROR_MAX
-1] = '\0';
1428 "Unexpected response from server; expecting +OK or -ERR");
1436 * Function: gettermination
1438 * Purpose: Gets the next line and verifies that it is a termination
1439 * line (nothing but a dot).
1441 * Return value: 0 on success, non-zero with pop_error set on error.
1443 * Side effects: Closes the connection on error.
1446 gettermination (server
)
1451 fromserver
= getline (server
);
1455 if (strcmp (fromserver
, "."))
1458 "Unexpected response from server in gettermination");
1468 * Function pop_close
1470 * Purpose: Close a pop connection, sending a "RSET" command to try to
1471 * preserve any changes that were made and a "QUIT" command to
1472 * try to get the server to quit, but ignoring any responses that
1475 * Side effects: The server is unusable after this function returns.
1476 * Changes made to the maildrop since the session was started (or
1477 * since the last pop_reset) may be lost.
1484 free ((char *) server
);
1490 * Function: pop_trash
1492 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1493 * memory associated with the server. It is legal to call
1494 * pop_close or pop_quit after this function has been called.
1500 if (server
->file
>= 0)
1502 /* avoid recursion; sendline can call pop_trash */
1503 if (server
->trash_started
)
1505 server
->trash_started
= 1;
1507 sendline (server
, "RSET");
1508 sendline (server
, "QUIT");
1510 CLOSESOCKET (server
->file
);
1514 free (server
->buffer
);
1525 /* Return a pointer to the first CRLF in IN_STRING,
1526 or 0 if it does not contain one. */
1529 find_crlf (in_string
)
1536 else if (*in_string
== '\r')
1538 if (*++in_string
== '\n')
1539 return (in_string
- 1);
1547 #endif /* MAIL_USE_POP */