pop2.c
上传用户:xxcykj
上传日期:2007-01-04
资源大小:727k
文件大小:4k
源码类别:

Email客户端

开发平台:

Unix_Linux

  1. /*
  2.  * pop2.c -- POP2 protocol methods
  3.  *
  4.  * Copyright 1997 by Eric S. Raymond
  5.  * For license terms, see the file COPYING in this directory.
  6.  */
  7. #include  "config.h"
  8. #ifdef POP2_ENABLE
  9. #include  <stdio.h>
  10. #if defined(STDC_HEADERS)
  11. #include <stdlib.h>
  12. #endif
  13. #include  "fetchmail.h"
  14. #include  "socket.h"
  15. static int pound_arg, equal_arg;
  16. int pop2_ok (int sock, char *argbuf)
  17. /* parse POP2 command response */
  18. {
  19.     int ok;
  20.     char buf [POPBUFSIZE+1];
  21.     pound_arg = equal_arg = -1;
  22.     if ((ok = gen_recv(sock, buf, sizeof(buf))) == 0)
  23.     {
  24. if (buf[0] == '+')
  25.     ok = 0;
  26. else if (buf[0] == '#')
  27. {
  28.     pound_arg = atoi(buf+1);
  29.     ok = 0;
  30. }
  31. else if (buf[0] == '=')
  32. {
  33.     equal_arg = atoi(buf+1);
  34.     ok = 0;
  35. }
  36. else if (buf[0] == '-')
  37.     ok = PS_ERROR;
  38. else
  39.     ok = PS_PROTOCOL;
  40. if (argbuf != NULL)
  41.     strcpy(argbuf,buf);
  42.     }
  43.     return(ok);
  44. }
  45. int pop2_getauth(int sock, struct query *ctl, char *buf)
  46. /* apply for connection authorization */
  47. {
  48.     return(gen_transact(sock,
  49.   "HELO %s %s",
  50.   ctl->remotename, ctl->password));
  51. }
  52. static int pop2_getrange(int sock, struct query *ctl, const char *folder, 
  53.  int *countp, int *newp, int *bytes)
  54. /* get range of messages to be fetched */
  55. {
  56.     /* maybe the user wanted a non-default folder */
  57.     if (folder)
  58.     {
  59. int ok = gen_transact(sock, "FOLD %s", folder);
  60. if (ok != 0)
  61.     return(ok);
  62. if (pound_arg == -1)
  63.     return(PS_ERROR);
  64.     }
  65.     else
  66. /*
  67.  * We should have picked up a count of messages in the user's
  68.  * default inbox from the pop2_getauth() response. 
  69.  *
  70.  * Note: this logic only works because there is no way to select
  71.  * both the unnamed folder and named folders within a single
  72.  * fetchmail run.  If that assumption ever becomes invalid, the
  73.  * pop2_getauth code will have to stash the pound response away
  74.  * explicitly in case it gets stepped on.
  75.  */
  76. if (pound_arg == -1)
  77.     return(PS_ERROR);
  78.     *countp = pound_arg;
  79.     *bytes = *newp = -1;
  80.     return(0);
  81. }
  82. static int pop2_fetch(int sock, struct query *ctl, int number, int *lenp)
  83. /* request nth message */
  84. {
  85.     int ok;
  86.     *lenp = 0;
  87.     ok = gen_transact(sock, "READ %d", number);
  88.     if (ok)
  89. return(0);
  90.     *lenp = equal_arg;
  91.     gen_send(sock, "RETR");
  92.     return(ok);
  93. }
  94. static int pop2_trail(int sock, struct query *ctl, int number)
  95. /* send acknowledgement for message data */
  96. {
  97.     return(gen_transact(sock, ctl->keep ? "ACKS" : "ACKD"));
  98. }
  99. static int pop2_logout(int sock, struct query *ctl)
  100. /* send logout command */
  101. {
  102.     return(gen_transact(sock, "QUIT"));
  103. }
  104. const static struct method pop2 =
  105. {
  106.     "POP2", /* Post Office Protocol v2 */
  107. #if INET6_ENABLE
  108.     "pop2", /* standard POP2 port */
  109.     "pop2", /* ssl POP2 port */
  110. #else /* INET6_ENABLE */
  111.     109, /* standard POP2 port */
  112.     109, /* ssl POP2 port - not */
  113. #endif /* INET6_ENABLE */
  114.     FALSE, /* this is not a tagged protocol */
  115.     FALSE, /* does not use message delimiter */
  116.     pop2_ok, /* parse command response */
  117.     NULL, /* no password canonicalization */
  118.     pop2_getauth, /* get authorization */
  119.     pop2_getrange, /* query range of messages */
  120.     NULL, /* no way to get sizes */
  121.     NULL, /* messages are always new */
  122.     pop2_fetch, /* request given message */
  123.     NULL, /* no way to fetch body alone */
  124.     pop2_trail, /* eat message trailer */
  125.     NULL, /* no POP2 delete method */
  126.     pop2_logout, /* log out, we're done */
  127.     FALSE, /* no, we can't re-poll */
  128. };
  129. int doPOP2 (struct query *ctl)
  130. /* retrieve messages using POP2 */
  131. {
  132.     peek_capable = FALSE;
  133.     return(do_protocol(ctl, &pop2));
  134. }
  135. #endif /* POP2_ENABLE */
  136. /* pop2.c ends here */