-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add libcurl example for fetching pop e and imap messages
- Loading branch information
Showing
2 changed files
with
66 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
CPPFLAGS += -Wp,-MD,-MG | ||
CFLAGS += -g -Wall | ||
LIBS+= -lcurl | ||
|
||
all: fetchmail | ||
|
||
fetchmail: fetchmail.c | ||
$(CC) $(LDFLAGS) -o $@ $? $(LIBS) | ||
|
||
clean: | ||
rm -f *.o fetchmail |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
#include <stdio.h> | ||
|
||
#include <curl/curl.h> | ||
|
||
int main(int argc, char *argv[]) | ||
{ | ||
char buf[128]; | ||
CURLcode res; | ||
CURL *curl; | ||
int use_pop = 1; | ||
|
||
if (argc < 4) { | ||
fprintf(stderr, "syntax %s <pop> <user> <pwd> [imap]\n",argv[0]); | ||
return -1; | ||
} | ||
|
||
use_pop = (argc == 4); | ||
curl = curl_easy_init(); | ||
if (!curl) { | ||
fprintf(stderr, "can't init curl\n"); | ||
return -1; | ||
} | ||
|
||
/* Set username and password */ | ||
snprintf(buf, 128, "%s:%s", argv[2], argv[3]); | ||
curl_easy_setopt(curl, CURLOPT_USERPWD, buf); | ||
|
||
/* This will list every message of the given mailbox */ | ||
snprintf(buf, 128, "%ss://%s@%s/%s", use_pop? "pop3":"imap", argv[2], argv[1], use_pop?"":"INBOX"); | ||
printf("XXXX %s\n", buf); | ||
curl_easy_setopt(curl, CURLOPT_URL, buf); | ||
|
||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0L); | ||
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYHOST, 0L); | ||
|
||
/* Perform the request, res will get the return code */ | ||
res = curl_easy_perform(curl); | ||
/* Check for errors */ | ||
if(res != CURLE_OK) | ||
fprintf(stderr, "curl_easy_perform() failed: %s\n", | ||
curl_easy_strerror(res)); | ||
|
||
snprintf(buf, 128, "%ss://%s@%s/%s1", use_pop? "pop3":"imap", argv[2], argv[1], use_pop?"":"INBOX;UID="); | ||
printf("XXXX %s\n", buf); | ||
curl_easy_setopt(curl, CURLOPT_URL, buf); | ||
res = curl_easy_perform(curl); | ||
/* Check for errors */ | ||
if(res != CURLE_OK) | ||
fprintf(stderr, "curl_easy_perform() failed: %s\n", | ||
curl_easy_strerror(res)); | ||
|
||
/* always cleanup */ | ||
curl_easy_cleanup(curl); | ||
return 0; | ||
} |