pilerimport skiplist skips my inbox

Issue #295 resolved
Tom Daley created an issue

Our IMAP server names our mailboxes like this:

INBOX (Main inbox) INBOX.Drafts INBOX.spam INBOX.Junk INBOX.Trash

etc.

If I run pilerimport with the following command line:

$ pilerimport -I myimapserver -u my@email.com -p mypassword -s INBOX,spam,INBOX.Junk,INBOX.Trash,INBOX.Drafts

It will also skip my "INBOX" because the strstr() function finds "INBOX" in the skiplist.

Maybe we could rename our main inbox to something like INBOX.Inbox to eliminate the issue, but (a) I'm not sure how to do that for everyone automatically when we create a new user's email account; and (b) I don't want to because I'm worried about side-effects that I can't predict.

I wanted to suggest a different method for determining of a mailbox folder should be skipped, below. I have not been a C programmer for many, many years so I am prepared for folks to snicker at my example, but, in testing here, it solves my problem. (I used different return values explaining why the function thinks the folder should be skipped to help in debugging.)

I thought of other ways to do it, but I wanted to suggest something that would be least invasive to the program as it stands today. For example, I thought about preparing the skiplist into an array and then looking for the candidate folder in the array. But that would have required more changes to the pilerimport program than I wanted to test. This just required the addition of a new function and changing one line of code in the imap import function.

/*
 * RETURNS:
 *   0 . . . Folder is NOT in the skiplist
 *   1 . . . Folder name was found at the BEGINNING of the skiplist
 *   2 . . . Folder is the ONLY item in the skiplist
 *   3 . . . Folder is in the MIDDLE of the skiplist
 *   4 . . . Folder is at the END of the skiplist
 */
int should_skip_folder(char *folder, char *skiplist)
{
  char *with_comma;

  //Is folder at the beginning of thte list?
  if ((with_comma = malloc(strlen(folder)+2)) != NULL) {
    with_comma[0] = '\0';
    strcat(with_comma, folder);
    strcat(with_comma, ",");
    if (strncmp(skiplist, with_comma, strlen(with_comma)) == 0) return 1;
  } else {
    fprintf(stderr, "100 - malloc failed\n");
    return 0;
  }

  //Is folder the ONLY folder in the list?
  if (strcmp(folder, skiplist) == 0) return 2;

  //Is folder in the middle of the list?
  if ((with_comma = malloc(strlen(folder)+3)) != NULL) {
    with_comma[0] = '\0';
    strcat(with_comma, ",");
    strcat(with_comma, folder);
    strcat(with_comma, ",");
    if (strstr(skiplist, with_comma)) return 3;
  } else {
    fprintf(stderr, "101 - malloc failed\n");
    return 0;
  }

  //Is folder at the END of the list?
  int slen = strlen(skiplist);
  int flen = strlen(folder);
  if (strcmp(skiplist+slen-flen, folder) == 0) return 4;

  return 0;
}

Comments (8)

  1. Tom Daley reporter

    My example command line should have used "-x" instead of "-s".

    $ pilerimport -i myimapserver -u my@email.com -p mypassword -x INBOX,spam,INBOX.Junk,INBOX.Trash,INBOX.Drafts

  2. Janos SUTO repo owner

    I think there might be a typo in the arguments. You typed the following:

    "INBOX,spam,INBOX.Junk,INBOX.Trash,INBOX.Drafts"

    but I think you probably meant this:

    "INBOX.spam,INBOX.Junk,INBOX.Trash,INBOX.Drafts"

    Notice the dot (.) after "INBOX", you typed a comma (,). Can you try running pilerimport with this argument as well? I think this time strstr() shouldn't skip your INBOX folder.

  3. Tom Daley reporter

    You are correct that I had a typo in what I posted to the issue tracker. Sorry for that. I am pretty sure I didn't have that error in my actual command line, but I re-ran it as you suggested to make certain. It still skipped my inbox. I think the reason is that with strstr() it just looks to see if the needle is anywhere in the haystack. The needle, "INBOX" is in the haystack "INBOX.Trash,INBOX.spam".

    That our main inbox folder is called INBOX and the spam,junk,trash,drafts are all prefixed by "INBOX." is a goofy naming convention in my opinion, but it's the naming convention that we have through hostmonster (our ISP) so, at least for the foreseeable future, it's the convention I have to live with.

    Thank you for your quick and thoughtful response.

  4. Janos SUTO repo owner

    OK, please download the latest master branch, and it fixes this issue. The only requirement is to add a trailing comma to the -x option, eg.

    -x folder1,folder2,...,folderN,

  5. Tom Daley reporter

    Thank you for your quick response. I grabbed the latest master branch. Somebody would have to have a crazy long mailbox name for your revision to fail.

    I have not built it because, like I said, I haven't programmed with C and make in a very long time and I can't get the "make pilerimport" to work with this branch, so I haven't tested it. I'll just keep using the version I modified as per above until it's time to do an upgrade.

    Thanks again for your quick response.

  6. Janos SUTO repo owner

    How long is that imap folder name? I allocate an 512 byte long buffer to hold the comma suffixed name. If it's not enough then you are right, the name must be a crazy long one :-)

  7. Log in to comment