Adding header (envelope) fields to messages

David Bremner <[email protected]> Thu, 17 Dec 2009 11:33:41 -0400
Newsgroups gmane.mail.libetpan.user
Message-ID <[email protected]>
--=-=-=


I have been trying to write a simple command-line example that adds or
removes a keyword from all of the messages in a mailbox (at the moment,
maildir).

I get to the point of adding a new field using 

  mailimf_fields_add(fields,newfield);

But now I'm stuck as far actually writing the changes back to disk.  I
call mailmessage_check(), and mailfolder_check(), but that doesn't seem
to do it (the documentation only mentions flags in fact).  

Please CC me with any ideas.  I attach the complete example.


--=-=-=
Content-Type: text/x-csrc
Content-Disposition: inline; filename=maildirex.c
Content-Description: non-working header modification example

#include <libetpan/libetpan.h>
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>

void plus_minus_keyword(struct mailimf_fields * fields, char *keyword, int remove)
{
  clistiter * cur;
  struct mailimf_keywords *keywords;
  clist *newlist;
  struct mailimf_field *newfield;

  for(cur = clist_begin(fields->fld_list) ; cur != NULL ;
    cur = clist_next(cur)) {
    struct mailimf_field * f;
    
    f = clist_content(cur);
    
    if (f->fld_type==MAILIMF_FIELD_KEYWORDS){
      clistiter *cur_kw;
       for(cur_kw = clist_begin(keywords->kw_list) ; cur_kw != NULL ;
	   cur_kw = clist_next(cur_kw)) {
	 char * str;
    
	 str = clist_content(cur_kw);
	 if (strcmp(str,keyword)==0){
	   if (remove) 
	     cur_kw=clist_delete(keywords->kw_list,cur_kw);
	   return;
	 }
       }
       clist_append(keywords->kw_list, strdup(keyword));
       return;
    }
  }
  newlist=clist_new();
  clist_append(newlist,strdup(keyword));
  keywords=mailimf_keywords_new(newlist);
  newfield=mailimf_field_new(MAILIMF_FIELD_KEYWORDS,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
    NULL, NULL, keywords, NULL);
  mailimf_fields_add(fields,newfield);
  
}


int main(int argc, char** argv)
{
  struct mailstorage * storage;
  int r;

  char *keyword;
  int remove_keyword;

  if (argc<3){
    fprintf(stderr,"usage: %s maildir [+-]keyword\n",argv[0]);
    exit(1);
  }

  switch (argv[2][0]){
  case '+':
    remove_keyword=0;
    break;
  case '-':
    remove_keyword=1;
    break;
  default:
    fprintf(stderr,"operation must start with + or -\n");
    exit(1);
  }

  keyword=argv[2]+1;

  storage = mailstorage_new(NULL);
  
  r=maildir_mailstorage_init(storage, argv[1], 0,
			   NULL, NULL);
  assert(r==MAIL_NO_ERROR);

  r = mailstorage_connect(storage);
  if (r == MAIL_NO_ERROR){
     struct mailfolder * folder;

    folder = mailfolder_new(storage, "INBOX", NULL);
    
    r = mailfolder_connect(folder);
    if (r == MAIL_NO_ERROR) {
      struct mailmessage_list * msg_list;
      int msg_no;

      // should maybe fetch envelope list
      mailfolder_get_messages_list(folder, &msg_list);

      for (msg_no=0; msg_no< carray_count(msg_list->msg_tab); msg_no++){
	mailmessage *msg;

	struct mailimf_fields * f;
	size_t index;

        msg = carray_get(msg_list->msg_tab, msg_no);
	r = mailmessage_fetch_envelope(msg,&f);
	if (r == MAIL_NO_ERROR) {
	  plus_minus_keyword(f,keyword, remove_keyword);
	  // this should really only be called on modified messages
	  mailmessage_check(msg);
	}
      }

      mailfolder_check(folder);      
      mailmessage_list_free(msg_list);

      mailfolder_disconnect(folder);
    }
    
    
    mailstorage_disconnect(storage);
  }
  
  mailstorage_free(storage);
}

--=-=-=
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

------------------------------------------------------------------------------
This SF.Net email is sponsored by the Verizon Developer Community
Take advantage of Verizon's best-in-class app development support
A streamlined, 14 day to market process makes app distribution fast and easy
Join now and get one step closer to millions of Verizon customers
http://p.sf.net/sfu/verizon-dev2dev 
--=-=-=
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

_______________________________________________
Libetpan-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/libetpan-users

--=-=-=--