Browse Source

work on sample, added pop sample

dvh-chacham15-master
DINH Viet Hoa 10 years ago
parent
commit
daf87a2a10
4 changed files with 90 additions and 8 deletions
  1. +1
    -1
      tests/imap-sample.c
  2. +1
    -1
      tests/mime-create.c
  3. +6
    -6
      tests/mime-parse.c
  4. +82
    -0
      tests/pop-sample.c

+ 1
- 1
tests/imap-sample.c View File

@ -66,7 +66,7 @@ static void fetch_msg(struct mailimap * imap, uint32_t uid)
{
struct mailimap_set * set;
struct mailimap_section * section;
char filename[20];
char filename[512];
size_t msg_len;
char * msg_content;
FILE * f;


+ 1
- 1
tests/mime-create.c View File

@ -272,7 +272,7 @@ static char * generate_boundary(const char * boundary_prefix)
return strdup(id);
}
struct mailmime * part_multiple_new(const char * type, const char * boundary_prefix)
static struct mailmime * part_multiple_new(const char * type, const char * boundary_prefix)
{
struct mailmime_fields * mime_fields;
struct mailmime_content * content;


+ 6
- 6
tests/mime-parse.c View File

@ -17,12 +17,12 @@ static void display_mime_data(struct mailmime_data * data)
}
}
void display_mime_parameter(struct mailmime_parameter * param)
static void display_mime_parameter(struct mailmime_parameter * param)
{
printf("%s = %s\n", param->pa_name, param->pa_value);
}
void display_mime_dsp_parm(struct mailmime_disposition_parm * param)
static void display_mime_dsp_parm(struct mailmime_disposition_parm * param)
{
switch (param->pa_type) {
case MAILMIME_DISPOSITION_PARM_FILENAME:
@ -31,7 +31,7 @@ void display_mime_dsp_parm(struct mailmime_disposition_parm * param)
}
}
void display_mime_disposition(struct mailmime_disposition * disposition)
static void display_mime_disposition(struct mailmime_disposition * disposition)
{
clistiter * cur;
@ -216,7 +216,7 @@ static void display_fields(struct mailimf_fields * fields)
}
}
void display_mime_discrete_type(struct mailmime_discrete_type * discrete_type)
static void display_mime_discrete_type(struct mailmime_discrete_type * discrete_type)
{
switch (discrete_type->dt_type) {
case MAILMIME_DISCRETE_TYPE_TEXT:
@ -240,7 +240,7 @@ void display_mime_discrete_type(struct mailmime_discrete_type * discrete_type)
}
}
void display_mime_composite_type(struct mailmime_composite_type * ct)
static void display_mime_composite_type(struct mailmime_composite_type * ct)
{
switch (ct->ct_type) {
case MAILMIME_COMPOSITE_TYPE_MESSAGE:
@ -255,7 +255,7 @@ void display_mime_composite_type(struct mailmime_composite_type * ct)
}
}
void display_mime_type(struct mailmime_type * type)
static void display_mime_type(struct mailmime_type * type)
{
switch (type->tp_type) {
case MAILMIME_TYPE_DISCRETE_TYPE:


+ 82
- 0
tests/pop-sample.c View File

@ -0,0 +1,82 @@
#include <libetpan/libetpan.h>
#include <stdlib.h>
#include <sys/stat.h>
static void check_error(int r, char * msg)
{
if (r == MAILPOP3_NO_ERROR)
return;
fprintf(stderr, "%s\n", msg);
exit(EXIT_FAILURE);
}
int main(int argc, char ** argv)
{
mailpop3 * pop3;
int r;
carray * list;
unsigned int i;
if (argc < 3) {
fprintf(stderr, "syntax: pop-sample [gmail-email-address] [gmail-password]\n");
exit(EXIT_FAILURE);
}
mkdir("download", 0700);
pop3 = mailpop3_new(0, NULL);
r = mailpop3_ssl_connect(pop3, "pop.gmail.com", 995);
check_error(r, "connect failed");
r = mailpop3_user(pop3, argv[1]);
check_error(r, "user failed");
r = mailpop3_pass(pop3, argv[2]);
check_error(r, "pass failed");
r = mailpop3_list(pop3, &list);
check_error(r, "list failed");
for(i = 0 ; i < carray_count(list) ; i ++) {
struct mailpop3_msg_info * info;
char * msg_content;
size_t msg_size;
FILE * f;
char filename[512];
struct stat stat_info;
info = carray_get(list, i);
if (info->msg_uidl == NULL) {
continue;
}
snprintf(filename, sizeof(filename), "download/%s.eml", info->msg_uidl);
r = stat(filename, &stat_info);
if (r == 0) {
printf("already fetched %u %s\n", info->msg_index, info->msg_uidl);
continue;
}
r = mailpop3_retr(pop3, info->msg_index, &msg_content, &msg_size);
check_error(r, "get failed");
f = fopen(filename, "w");
fwrite(msg_content, 1, msg_size, f);
fclose(f);
mailpop3_retr_free(msg_content);
if (info->msg_uidl != NULL) {
printf("fetched %u %s\n", info->msg_index, info->msg_uidl);
}
else {
printf("fetched %u\n", info->msg_index);
}
}
mailpop3_quit(pop3);
mailpop3_free(pop3);
exit(EXIT_SUCCESS);
}

Loading…
Cancel
Save