comparison lib-src/pop.c @ 109423:ae5ef13849d8

Merge from mainline.
author Katsumi Yamaoka <yamaoka@jpl.org>
date Sun, 04 Jul 2010 22:11:22 +0000
parents 52b76722152a
children 750db9f3e6d8
comparison
equal deleted inserted replaced
109422:bcedeeedc5fb 109423:ae5ef13849d8
106 #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H) 106 #if !defined(HAVE_H_ERRNO) || !defined(HAVE_CONFIG_H)
107 extern int h_errno; 107 extern int h_errno;
108 #endif 108 #endif
109 #endif 109 #endif
110 110
111 #ifndef __P 111 static int socket_connection (char *, int);
112 # ifdef __STDC__ 112 static int pop_getline (popserver, char **);
113 # define __P(a) a 113 static int sendline (popserver, char *);
114 # else 114 static int fullwrite (int, char *, int);
115 # define __P(a) () 115 static int getok (popserver);
116 # endif /* __STDC__ */
117 #endif /* ! __P */
118
119 static int socket_connection __P((char *, int));
120 static int pop_getline __P((popserver, char **));
121 static int sendline __P((popserver, char *));
122 static int fullwrite __P((int, char *, int));
123 static int getok __P((popserver));
124 #if 0 116 #if 0
125 static int gettermination __P((popserver)); 117 static int gettermination (popserver);
126 #endif 118 #endif
127 static void pop_trash __P((popserver)); 119 static void pop_trash (popserver);
128 static char *find_crlf __P((char *, int)); 120 static char *find_crlf (char *, int);
129 121
130 #define ERROR_MAX 160 /* a pretty arbitrary size, but needs 122 #define ERROR_MAX 160 /* a pretty arbitrary size, but needs
131 to be bigger than the original 123 to be bigger than the original
132 value of 80 */ 124 value of 80 */
133 #define POP_PORT 110 125 #define POP_PORT 110
172 * non-null popserver will be returned. Otherwise, null will be 164 * non-null popserver will be returned. Otherwise, null will be
173 * returned, and the string variable pop_error will contain an 165 * returned, and the string variable pop_error will contain an
174 * explanation of the error. 166 * explanation of the error.
175 */ 167 */
176 popserver 168 popserver
177 pop_open (host, username, password, flags) 169 pop_open (char *host, char *username, char *password, int flags)
178 char *host;
179 char *username;
180 char *password;
181 int flags;
182 { 170 {
183 int sock; 171 int sock;
184 popserver server; 172 popserver server;
185 173
186 /* Determine the user name */ 174 /* Determine the user name */
343 * 331 *
344 * Side effects: On failure, may make further operations on the 332 * Side effects: On failure, may make further operations on the
345 * connection impossible. 333 * connection impossible.
346 */ 334 */
347 int 335 int
348 pop_stat (server, count, size) 336 pop_stat (popserver server, int *count, int *size)
349 popserver server;
350 int *count;
351 int *size;
352 { 337 {
353 char *fromserver; 338 char *fromserver;
354 char *end_ptr; 339 char *end_ptr;
355 340
356 if (server->in_multi) 341 if (server->in_multi)
419 * 404 *
420 * Side effects: On failure, may make further operations on the 405 * Side effects: On failure, may make further operations on the
421 * connection impossible. 406 * connection impossible.
422 */ 407 */
423 int 408 int
424 pop_list (server, message, IDs, sizes) 409 pop_list (popserver server, int message, int **IDs, int **sizes)
425 popserver server;
426 int message;
427 int **IDs;
428 int **sizes;
429 { 410 {
430 int how_many, i; 411 int how_many, i;
431 char *fromserver; 412 char *fromserver;
432 413
433 if (server->in_multi) 414 if (server->in_multi)
565 * with pop_error set. 546 * with pop_error set.
566 * 547 *
567 * Side effects: May kill connection on error. 548 * Side effects: May kill connection on error.
568 */ 549 */
569 int 550 int
570 pop_retrieve (server, message, markfrom, msg_buf) 551 pop_retrieve (popserver server, int message, int markfrom, char **msg_buf)
571 popserver server;
572 int message;
573 int markfrom;
574 char **msg_buf;
575 { 552 {
576 int *IDs, *sizes, bufsize, fromcount = 0, cp = 0; 553 int *IDs, *sizes, bufsize, fromcount = 0, cp = 0;
577 char *ptr, *fromserver; 554 char *ptr, *fromserver;
578 int ret; 555 int ret;
579 556
643 free (ptr); 620 free (ptr);
644 return (-1); 621 return (-1);
645 } 622 }
646 623
647 int 624 int
648 pop_retrieve_first (server, message, response) 625 pop_retrieve_first (popserver server, int message, char **response)
649 popserver server;
650 int message;
651 char **response;
652 { 626 {
653 sprintf (pop_error, "RETR %d", message); 627 sprintf (pop_error, "RETR %d", message);
654 return (pop_multi_first (server, pop_error, response)); 628 return (pop_multi_first (server, pop_error, response));
655 } 629 }
656 630
661 returned buffer (which is null-terminated and may contain embedded 635 returned buffer (which is null-terminated and may contain embedded
662 nulls, but the returned bytecount doesn't include the final null). 636 nulls, but the returned bytecount doesn't include the final null).
663 */ 637 */
664 638
665 int 639 int
666 pop_retrieve_next (server, line) 640 pop_retrieve_next (popserver server, char **line)
667 popserver server;
668 char **line;
669 { 641 {
670 return (pop_multi_next (server, line)); 642 return (pop_multi_next (server, line));
671 } 643 }
672 644
673 int 645 int
674 pop_retrieve_flush (server) 646 pop_retrieve_flush (popserver server)
675 popserver server;
676 { 647 {
677 return (pop_multi_flush (server)); 648 return (pop_multi_flush (server));
678 } 649 }
679 650
680 int 651 int
681 pop_top_first (server, message, lines, response) 652 pop_top_first (popserver server, int message, int lines, char **response)
682 popserver server;
683 int message, lines;
684 char **response;
685 { 653 {
686 sprintf (pop_error, "TOP %d %d", message, lines); 654 sprintf (pop_error, "TOP %d %d", message, lines);
687 return (pop_multi_first (server, pop_error, response)); 655 return (pop_multi_first (server, pop_error, response));
688 } 656 }
689 657
694 returned buffer (which is null-terminated and may contain embedded 662 returned buffer (which is null-terminated and may contain embedded
695 nulls, but the returned bytecount doesn't include the final null). 663 nulls, but the returned bytecount doesn't include the final null).
696 */ 664 */
697 665
698 int 666 int
699 pop_top_next (server, line) 667 pop_top_next (popserver server, char **line)
700 popserver server;
701 char **line;
702 { 668 {
703 return (pop_multi_next (server, line)); 669 return (pop_multi_next (server, line));
704 } 670 }
705 671
706 int 672 int
707 pop_top_flush (server) 673 pop_top_flush (popserver server)
708 popserver server;
709 { 674 {
710 return (pop_multi_flush (server)); 675 return (pop_multi_flush (server));
711 } 676 }
712 677
713 int 678 int
714 pop_multi_first (server, command, response) 679 pop_multi_first (popserver server, char *command, char **response)
715 popserver server;
716 char *command;
717 char **response;
718 { 680 {
719 if (server->in_multi) 681 if (server->in_multi)
720 { 682 {
721 strcpy (pop_error, 683 strcpy (pop_error,
722 "Already in multi-line query in pop_multi_first"); 684 "Already in multi-line query in pop_multi_first");
755 (not including the final null). The data in that buffer may contain 717 (not including the final null). The data in that buffer may contain
756 embedded nulls, but does not contain the final CRLF. When returning 718 embedded nulls, but does not contain the final CRLF. When returning
757 0, LINE is set to null. */ 719 0, LINE is set to null. */
758 720
759 int 721 int
760 pop_multi_next (server, line) 722 pop_multi_next (popserver server, char **line)
761 popserver server;
762 char **line;
763 { 723 {
764 char *fromserver; 724 char *fromserver;
765 int ret; 725 int ret;
766 726
767 if (! server->in_multi) 727 if (! server->in_multi)
795 return (ret); 755 return (ret);
796 } 756 }
797 } 757 }
798 758
799 int 759 int
800 pop_multi_flush (server) 760 pop_multi_flush (popserver server)
801 popserver server;
802 { 761 {
803 char *line; 762 char *line;
804 int ret; 763 int ret;
805 764
806 if (! server->in_multi) 765 if (! server->in_multi)
827 * 786 *
828 * Return value: 0 on success, non-zero with error in pop_error 787 * Return value: 0 on success, non-zero with error in pop_error
829 * otherwise. 788 * otherwise.
830 */ 789 */
831 int 790 int
832 pop_delete (server, message) 791 pop_delete (popserver server, int message)
833 popserver server;
834 int message;
835 { 792 {
836 if (server->in_multi) 793 if (server->in_multi)
837 { 794 {
838 strcpy (pop_error, "In multi-line query in pop_delete"); 795 strcpy (pop_error, "In multi-line query in pop_delete");
839 return (-1); 796 return (-1);
859 * otherwise. 816 * otherwise.
860 * 817 *
861 * Side effects: Closes connection on error. 818 * Side effects: Closes connection on error.
862 */ 819 */
863 int 820 int
864 pop_noop (server) 821 pop_noop (popserver server)
865 popserver server;
866 { 822 {
867 if (server->in_multi) 823 if (server->in_multi)
868 { 824 {
869 strcpy (pop_error, "In multi-line query in pop_noop"); 825 strcpy (pop_error, "In multi-line query in pop_noop");
870 return (-1); 826 return (-1);
889 * the error explained in pop_error. 845 * the error explained in pop_error.
890 * 846 *
891 * Side effects: Closes the connection on error. 847 * Side effects: Closes the connection on error.
892 */ 848 */
893 int 849 int
894 pop_last (server) 850 pop_last (popserver server)
895 popserver server;
896 { 851 {
897 char *fromserver; 852 char *fromserver;
898 853
899 if (server->in_multi) 854 if (server->in_multi)
900 { 855 {
947 * otherwise. 902 * otherwise.
948 * 903 *
949 * Side effects: Closes the connection on error. 904 * Side effects: Closes the connection on error.
950 */ 905 */
951 int 906 int
952 pop_reset (server) 907 pop_reset (popserver server)
953 popserver server;
954 { 908 {
955 if (pop_retrieve_flush (server)) 909 if (pop_retrieve_flush (server))
956 { 910 {
957 return (-1); 911 return (-1);
958 } 912 }
976 * 930 *
977 * Side Effects: The popserver passed in is unusable after this 931 * Side Effects: The popserver passed in is unusable after this
978 * function is called, even if an error occurs. 932 * function is called, even if an error occurs.
979 */ 933 */
980 int 934 int
981 pop_quit (server) 935 pop_quit (popserver server)
982 popserver server;
983 { 936 {
984 int ret = 0; 937 int ret = 0;
985 938
986 if (server->file >= 0) 939 if (server->file >= 0)
987 { 940 {
1021 * Return value: A file descriptor indicating the connection, or -1 974 * Return value: A file descriptor indicating the connection, or -1
1022 * indicating failure, in which case an error has been copied 975 * indicating failure, in which case an error has been copied
1023 * into pop_error. 976 * into pop_error.
1024 */ 977 */
1025 static int 978 static int
1026 socket_connection (host, flags) 979 socket_connection (char *host, int flags)
1027 char *host;
1028 int flags;
1029 { 980 {
1030 #ifdef HAVE_GETADDRINFO 981 #ifdef HAVE_GETADDRINFO
1031 struct addrinfo *res, *it; 982 struct addrinfo *res, *it;
1032 struct addrinfo hints; 983 struct addrinfo hints;
1033 int ret; 984 int ret;
1333 * Side effects: Closes the connection on error. 1284 * Side effects: Closes the connection on error.
1334 * 1285 *
1335 * THE RETURNED LINE MAY CONTAIN EMBEDDED NULLS! 1286 * THE RETURNED LINE MAY CONTAIN EMBEDDED NULLS!
1336 */ 1287 */
1337 static int 1288 static int
1338 pop_getline (server, line) 1289 pop_getline (popserver server, char **line)
1339 popserver server;
1340 char **line;
1341 { 1290 {
1342 #define GETLINE_ERROR "Error reading from server: " 1291 #define GETLINE_ERROR "Error reading from server: "
1343 1292
1344 int ret; 1293 int ret;
1345 int search_offset = 0; 1294 int search_offset = 0;
1465 * an error will be copied into pop_error. 1414 * an error will be copied into pop_error.
1466 * 1415 *
1467 * Side effects: Closes the connection on error. 1416 * Side effects: Closes the connection on error.
1468 */ 1417 */
1469 static int 1418 static int
1470 sendline (server, line) 1419 sendline (popserver server, char *line)
1471 popserver server;
1472 char *line;
1473 { 1420 {
1474 #define SENDLINE_ERROR "Error writing to POP server: " 1421 #define SENDLINE_ERROR "Error writing to POP server: "
1475 int ret; 1422 int ret;
1476 char *buf; 1423 char *buf;
1477 1424
1514 * has been written. 1461 * has been written.
1515 * 1462 *
1516 * Return value: Same as write. Pop_error is not set. 1463 * Return value: Same as write. Pop_error is not set.
1517 */ 1464 */
1518 static int 1465 static int
1519 fullwrite (fd, buf, nbytes) 1466 fullwrite (int fd, char *buf, int nbytes)
1520 int fd;
1521 char *buf;
1522 int nbytes;
1523 { 1467 {
1524 char *cp; 1468 char *cp;
1525 int ret = 0; 1469 int ret = 0;
1526 1470
1527 cp = buf; 1471 cp = buf;
1547 * Returns: 0 for success, else for failure and puts error in pop_error. 1491 * Returns: 0 for success, else for failure and puts error in pop_error.
1548 * 1492 *
1549 * Side effects: On failure, may make the connection unusable. 1493 * Side effects: On failure, may make the connection unusable.
1550 */ 1494 */
1551 static int 1495 static int
1552 getok (server) 1496 getok (popserver server)
1553 popserver server;
1554 { 1497 {
1555 char *fromline; 1498 char *fromline;
1556 1499
1557 if (pop_getline (server, &fromline) < 0) 1500 if (pop_getline (server, &fromline) < 0)
1558 { 1501 {
1619 * Side effects: The server is unusable after this function returns. 1562 * Side effects: The server is unusable after this function returns.
1620 * Changes made to the maildrop since the session was started (or 1563 * Changes made to the maildrop since the session was started (or
1621 * since the last pop_reset) may be lost. 1564 * since the last pop_reset) may be lost.
1622 */ 1565 */
1623 void 1566 void
1624 pop_close (server) 1567 pop_close (popserver server)
1625 popserver server;
1626 { 1568 {
1627 pop_trash (server); 1569 pop_trash (server);
1628 free ((char *) server); 1570 free ((char *) server);
1629 1571
1630 return; 1572 return;
1636 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the 1578 * Purpose: Like pop_close or pop_quit, but doesn't deallocate the
1637 * memory associated with the server. It is valid to call 1579 * memory associated with the server. It is valid to call
1638 * pop_close or pop_quit after this function has been called. 1580 * pop_close or pop_quit after this function has been called.
1639 */ 1581 */
1640 static void 1582 static void
1641 pop_trash (server) 1583 pop_trash (popserver server)
1642 popserver server;
1643 { 1584 {
1644 if (server->file >= 0) 1585 if (server->file >= 0)
1645 { 1586 {
1646 /* avoid recursion; sendline can call pop_trash */ 1587 /* avoid recursion; sendline can call pop_trash */
1647 if (server->trash_started) 1588 if (server->trash_started)
1669 /* Return a pointer to the first CRLF in IN_STRING, which can contain 1610 /* Return a pointer to the first CRLF in IN_STRING, which can contain
1670 embedded nulls and has LEN characters in it not including the final 1611 embedded nulls and has LEN characters in it not including the final
1671 null, or 0 if it does not contain one. */ 1612 null, or 0 if it does not contain one. */
1672 1613
1673 static char * 1614 static char *
1674 find_crlf (in_string, len) 1615 find_crlf (char *in_string, int len)
1675 char *in_string;
1676 int len;
1677 { 1616 {
1678 while (len--) 1617 while (len--)
1679 { 1618 {
1680 if (*in_string == '\r') 1619 if (*in_string == '\r')
1681 { 1620 {