rohrpost

A commandline mail client to change the world as we see it.
git clone git://r-36.net/rohrpost
Log | Files | Refs | README | LICENSE

remove.c (2704B)


      1 /*
      2  * Copy me if you can.
      3  * by 20h
      4  */
      5 
      6 #include <unistd.h>
      7 #include <stdio.h>
      8 #include <stdlib.h>
      9 #include <string.h>
     10 
     11 #include "ind.h"
     12 #include "arg.h"
     13 #include "cfg.h"
     14 #include "llist.h"
     15 #include "folder.h"
     16 #include "imap.h"
     17 #include "remove.h"
     18 
     19 void
     20 removeusage(char *argv0)
     21 {
     22 	die("usage: %s [-hqdre] [-c cfg] [-m folder] msgs\n", argv0);
     23 }
     24 
     25 int
     26 removemain(int argc, char *argv[])
     27 {
     28 	config_t *cfg;
     29 	imap_t *imap;
     30 	int status;
     31 	char *user, *pass, *netspec, *selected, *dispose, *cfgn, *argv0;
     32 	llist_t *ids;
     33 
     34 	enum {
     35 		BEQUIET = 0x01,
     36 		DODISPOSE = 0x02,
     37 		DOEXPUNGE = 0x04,
     38 		DOREMOVE = 0x08,
     39 
     40 		NOARGS = 0x10
     41 	};
     42 
     43 	status = 0;
     44 	selected = NULL;
     45 	cfgn = NULL;
     46 
     47 	ARGBEGIN(argv0) {
     48 	case 'c':
     49 		cfgn = EARGF(removeusage(argv0));
     50 		break;
     51 	case 'd':
     52 		status |= DODISPOSE;
     53 		break;
     54 	case 'e':
     55 		status |= DOEXPUNGE;
     56 		break;
     57 	case 'm':
     58 		selected = EARGF(removeusage(argv0));
     59 		break;
     60 	case 'q':
     61 		status |= BEQUIET;
     62 		break;
     63 	case 'r':
     64 		status &= ~DODISPOSE;
     65 		status |= DOREMOVE;
     66 		break;
     67 	default:
     68 		removeusage(argv0);
     69 	} ARGEND;
     70 
     71 	if (argc < 1 && !(status & DOEXPUNGE))
     72 		removeusage(argv0);
     73 
     74 	cfg = config_init(cfgn);
     75 	user = config_checkgetstr(cfg, "imapuser");
     76 	pass = config_checkgetstr(cfg, "imappass");
     77 	netspec = config_checkgetstr(cfg, "imapnet");
     78 	if (selected == NULL) {
     79 		selected = config_checkgetstr(cfg, "selected");
     80 	} else {
     81 		selected = memdup(selected, strlen(selected)+1);
     82 	}
     83 	dispose = config_getstr(cfg, "dispose");
     84 	if (dispose != NULL && strlen(dispose) > 0 && !(status & DOREMOVE))
     85 		status |= DODISPOSE;
     86 	config_free(cfg);
     87 
     88 	imap = imap_new(netspec, user, pass);
     89 	free(user);
     90 	free(pass);
     91 	free(netspec);
     92 
     93 	if (imap_init(imap))
     94 		imap_die(imap, "imap_init");
     95 	if (imap_select(imap, selected))
     96 		imap_die(imap, "imap_select");
     97 
     98 	if (status & DOEXPUNGE) {
     99 		if (imap_expunge(imap))
    100 			imap_die(imap, "imap_expungen");
    101 		if (!(status & BEQUIET))
    102 			printf("Ran expunge.\n");
    103 	} else {
    104 		ids = imap_argv2ids(cfgn, selected, argc, argv);
    105 		user = imap_ids2str(ids);
    106 		if (status & DODISPOSE) {
    107 			if (dispose == NULL || strlen(dispose) < 1) {
    108 				fprintf(stderr, "Could not dispose, because "
    109 					"no 'dispose' configuration was "
    110 					"given.\n");
    111 			} else {
    112 				if (imap_move(imap, ids, dispose))
    113 					imap_die(imap, "imap_move");
    114 				if (!(status & BEQUIET)) {
    115 					printf("%s disposed to %s from %s.\n",
    116 						user, dispose, selected);
    117 				}
    118 			}
    119 		} else {
    120 			if (imap_delete(imap, ids))
    121 				imap_die(imap, "imap_delete");
    122 			if (!(status & BEQUIET)) {
    123 				printf("%s removed from %s.\n", user,
    124 						selected);
    125 			}
    126 		}
    127 		free(user);
    128 		llist_efree(ids);
    129 	}
    130 
    131 	if (dispose != NULL)
    132 		free(dispose);
    133 	free(selected);
    134 	imap_close(imap);
    135 	imap_free(imap);
    136 
    137 	return 0;
    138 }
    139