cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Question regarding multiple IMAP operations

From: Adam <simplesec2012_at_gmail.com>
Date: Wed, 27 May 2015 16:09:48 -0300

Ray, I've actually written C89 code to parse IMAP responses. libcurl IMAP
is indeed garbage, because the only thing it does for you that a straight
socket doesn't is encryption (which is well documented w/tons of code
samples anyway). So as soon as I remove these crashes I will post my code
to hopefully make it a bit more of a complete IMAP library. I've already
come this far w/libcurl despite it's awkward callback schemes so don't feel
like switching now.

Let's say I want first list folders, the default, then 1 second later I
want to "EXAMINE OUTBOX", then 1 second later I want to read first message
""imap://imap.example.com/INBOX/;UID=1" There are no code samples showing
how to do multiple requests like this.

Would something like this be the correct way? Or is this what the multi_
function families are for? Not this exactly, but doing stuff like this I'm
getting a lot of different crashes, and I'm not using any handles after
calling cleanup.

Please specify if this is the intended usage, or if I need to be doing
something else, thanks.

int main(void)
{
  CURL *curl;
  CURLcode res = CURLE_OK;

  curl = curl_easy_init();
  if(curl) {
    /* Set username and password */
    curl_easy_setopt(curl, CURLOPT_USERNAME, "user");
    curl_easy_setopt(curl, CURLOPT_PASSWORD, "secret");

    /* This will fetch message 1 from the user's inbox */
    curl_easy_setopt(curl, CURLOPT_URL, "imap://
imap.example.com/INBOX/;UID=1");

    /* Perform the fetch */
    res = curl_easy_perform(curl);

    /* Check for errors */
    if(res != CURLE_OK)
      fprintf(stderr, "curl_easy_perform() failed: %s\n",
              curl_easy_strerror(res));

   curl_easy_setopt(curl, CURLOPT_URL, "EXAMINE OUTBOX");

   res = curl_easy_perform(curl);

    /* Check for errors */
    if(res != CURLE_OK)
      printf("fuck\n");

    /* Always cleanup */
    curl_easy_cleanup(curl);
  }

  return (int)res;
}

On Wed, May 27, 2015 at 3:33 AM, Ray Satiro via curl-library <
curl-library_at_cool.haxx.se> wrote:

> On 5/26/2015 9:31 AM, Adam wrote:
>
>> What is the proper way to send multiple IMAP commands? For example, first
>> I'd like to get a listing of all the folder names, then I want to list the
>> unread files in the folder names.
>>
>> I've tried using the same CURL* curl handle, sometimes it crashes on
>> curl_easy_cleanup() (single threaded app), and sometimes it crashes on the
>> second call to curl_easy_perform(). It depends on the IMAP server, gmail
>> causes a crash, hotmail doesn't. I've even tried opening/closing a new
>> handle for each operation and this causes crashes also.
>>
>> What is the proper way to do this? There are no code samples for this on
>> the libcurl homepage. Thanks.
>>
>
> The proper way is multiple requests and don't call curl_easy_cleanup until
> after you're done with the handle. You want to reuse the handle until then.
> Please note as mentioned in the curl_easy_cleanup doc that "Any use of the
> handle after this function has been called and have returned, is illegal."
> [1]
>
> So you have custom request(s) to get the unread ids then you get the
> subjects. Getting the subjects from what I remember is tricky, they only
> show up in the header section and not the body unless behavior was improved
> in the last year or two. As I think I mentioned to you off-list somewhere
> libcurl has minimal support for IMAP command parsing. You'll have to do a
> lot of the parsing yourself. The actual command to get the subject for an
> id is:
> "FETCH " + id + " BODY.PEEK[HEADER.FIELDS (SUBJECT)]"
>
> You may want to check out LibEtPan [2] or take a closer look at Vmime
> which you've mentioned.
>
> [1]: http://curl.haxx.se/libcurl/c/curl_easy_cleanup.html
> [2]: https://github.com/dinhviethoa/libetpan
>
> -------------------------------------------------------------------
> List admin: http://cool.haxx.se/list/listinfo/curl-library
> Etiquette: http://curl.haxx.se/mail/etiquette.html

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2015-05-27