imap-multi.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2014, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at http://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include <stdio.h>
  23. #include <curl/curl.h>
  24. /* This is a simple example showing how to fetch mail using libcurl's IMAP
  25. * capabilities. It builds on the imap-fetch.c example to demonstrate how to
  26. * use libcurl's multi interface.
  27. *
  28. * Note that this example requires libcurl 7.30.0 or above.
  29. */
  30. #define MULTI_PERFORM_HANG_TIMEOUT 60 * 1000
  31. static struct timeval tvnow(void)
  32. {
  33. struct timeval now;
  34. /* time() returns the value of time in seconds since the epoch */
  35. now.tv_sec = (long)time(NULL);
  36. now.tv_usec = 0;
  37. return now;
  38. }
  39. static long tvdiff(struct timeval newer, struct timeval older)
  40. {
  41. return (newer.tv_sec - older.tv_sec) * 1000 +
  42. (newer.tv_usec - older.tv_usec) / 1000;
  43. }
  44. int main(void)
  45. {
  46. CURL *curl;
  47. CURLM *mcurl;
  48. int still_running = 1;
  49. struct timeval mp_start;
  50. curl_global_init(CURL_GLOBAL_DEFAULT);
  51. curl = curl_easy_init();
  52. if(!curl)
  53. return 1;
  54. mcurl = curl_multi_init();
  55. if(!mcurl)
  56. return 2;
  57. /* Set username and password */
  58. curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
  59. curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");
  60. /* This will fetch message 1 from the user's inbox */
  61. curl_easy_setopt(curl, CURLOPT_URL, "imap://imap.example.com/INBOX/;UID=1");
  62. /* Tell the multi stack about our easy handle */
  63. curl_multi_add_handle(mcurl, curl);
  64. /* Record the start time which we can use later */
  65. mp_start = tvnow();
  66. /* We start some action by calling perform right away */
  67. curl_multi_perform(mcurl, &still_running);
  68. while(still_running) {
  69. struct timeval timeout;
  70. fd_set fdread;
  71. fd_set fdwrite;
  72. fd_set fdexcep;
  73. int maxfd = -1;
  74. int rc;
  75. long curl_timeo = -1;
  76. /* Initialise the file descriptors */
  77. FD_ZERO(&fdread);
  78. FD_ZERO(&fdwrite);
  79. FD_ZERO(&fdexcep);
  80. /* Set a suitable timeout to play around with */
  81. timeout.tv_sec = 1;
  82. timeout.tv_usec = 0;
  83. curl_multi_timeout(mcurl, &curl_timeo);
  84. if(curl_timeo >= 0) {
  85. timeout.tv_sec = curl_timeo / 1000;
  86. if(timeout.tv_sec > 1)
  87. timeout.tv_sec = 1;
  88. else
  89. timeout.tv_usec = (curl_timeo % 1000) * 1000;
  90. }
  91. /* Get file descriptors from the transfers */
  92. curl_multi_fdset(mcurl, &fdread, &fdwrite, &fdexcep, &maxfd);
  93. /* In a real-world program you OF COURSE check the return code of the
  94. function calls. On success, the value of maxfd is guaranteed to be
  95. greater or equal than -1. We call select(maxfd + 1, ...), specially in
  96. case of (maxfd == -1), we call select(0, ...), which is basically equal
  97. to sleep. */
  98. rc = select(maxfd + 1, &fdread, &fdwrite, &fdexcep, &timeout);
  99. if(tvdiff(tvnow(), mp_start) > MULTI_PERFORM_HANG_TIMEOUT) {
  100. fprintf(stderr,
  101. "ABORTING: Since it seems that we would have run forever.\n");
  102. break;
  103. }
  104. switch(rc) {
  105. case -1: /* select error */
  106. break;
  107. case 0: /* timeout */
  108. default: /* action */
  109. curl_multi_perform(mcurl, &still_running);
  110. break;
  111. }
  112. }
  113. /* Always cleanup */
  114. curl_multi_remove_handle(mcurl, curl);
  115. curl_multi_cleanup(mcurl);
  116. curl_easy_cleanup(curl);
  117. curl_global_cleanup();
  118. return 0;
  119. }