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

copy.c (2321B)


      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 "inc.h"
     18 #include "copy.h"
     19 
     20 void
     21 copyusage(char *argv0)
     22 {
     23 	die("usage: %s [-hq] [-c cfg] [-m folder] folder msgs\n", argv0);
     24 }
     25 
     26 int
     27 movemain(int argc, char *argv[])
     28 {
     29 	argv[0] = "rpmv";
     30 	return copymain(argc, argv);
     31 }
     32 
     33 int
     34 copymain(int argc, char *argv[])
     35 {
     36 	config_t *cfg;
     37 	imap_t *imap;
     38 	int status;
     39 	char *user, *pass, *netspec, *selected, *folder, *cfgn, *argv0;
     40 	llist_t *ids;
     41 	inc_t *incs;
     42 
     43 	enum {
     44 		BEQUIET = 0x01,
     45 		DOMOVE = 0x02,
     46 
     47 		NOARGS = 0x04
     48 	};
     49 
     50 	selected = NULL;
     51 	cfgn = NULL;
     52 
     53 	status = 0;
     54 	if (argc > 0 && argv[0][2] == 'm')
     55 		status |= DOMOVE;
     56 
     57 	ARGBEGIN(argv0) {
     58 	case 'c':
     59 		cfgn = EARGF(copyusage(argv0));
     60 		break;
     61 	case 'm':
     62 		selected = EARGF(copyusage(argv0));
     63 		break;
     64 	case 'q':
     65 		status |= BEQUIET;
     66 		break;
     67 	case 'h':
     68 	default:
     69 		copyusage(argv0);
     70 	} ARGEND;
     71 
     72 	if (argc < 2)
     73 		copyusage(argv0);
     74 	folder = argv[0];
     75 	argv++;
     76 	argc--;
     77 
     78 	cfg = config_init(cfgn);
     79 	user = config_checkgetstr(cfg, "imapuser");
     80 	pass = config_checkgetstr(cfg, "imappass");
     81 	netspec = config_checkgetstr(cfg, "imapnet");
     82 	if (selected == NULL) {
     83 		selected = config_checkgetstr(cfg, "selected");
     84 	} else {
     85 		selected = memdups(selected);
     86 	}
     87 	config_free(cfg);
     88 
     89 	imap = imap_new(netspec, user, pass);
     90 	free(user);
     91 	free(pass);
     92 	free(netspec);
     93 
     94 	if (imap_init(imap))
     95 		imap_die(imap, "imap_init");
     96 	if (imap_select(imap, selected))
     97 		imap_die(imap, "imap_select");
     98 
     99 	ids = imap_argv2ids(cfgn, selected, argc, argv);
    100 	if (ids == NULL)
    101 		die("No msgids selected. Aborting.\n");
    102 	user = imap_ids2str(ids);
    103 
    104 	incs = inc_init(cfgn);
    105 	if (status & DOMOVE) {
    106 		if (imap_move(imap, ids, folder))
    107 			imap_die(imap, "imap_move");
    108 		if (!(status & BEQUIET))
    109 			printf("%s moved to %s.\n", user, folder);
    110 		imap_closefolder(imap);
    111 
    112 		inc_updatefolder(imap, selected, incs);
    113 	} else {
    114 		if (imap_copy(imap, ids, folder))
    115 			imap_die(imap, "imap_copy");
    116 		if (!(status & BEQUIET))
    117 			printf("%s copied to %s.\n", user, folder);
    118 		imap_closefolder(imap);
    119 	}
    120 	inc_updatefolder(imap, folder, incs);
    121 	inc_stop(incs);
    122 
    123 	free(user);
    124 	llist_efree(ids);
    125 	free(selected);
    126 	imap_free(imap);
    127 	return 0;
    128 }
    129