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

add.c (2363B)


      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 <time.h>
     10 #include <strings.h>
     11 #include <sys/types.h>
     12 #include <sys/wait.h>
     13 #include <signal.h>
     14 #include <string.h>
     15 
     16 #include "ind.h"
     17 #include "arg.h"
     18 #include "cfg.h"
     19 #include "llist.h"
     20 #include "imap.h"
     21 #include "flag.h"
     22 #include "inc.h"
     23 
     24 void
     25 addusage(char *argv0)
     26 {
     27 	die("usage: %s [-qh] [-c cfg] [-m folder] [-d datetime] [-f flags]"
     28 			" [file]\n", argv0);
     29 }
     30 
     31 int
     32 addmain(int argc, char *argv[])
     33 {
     34 	config_t *cfg;
     35 	imap_t *imap;
     36 	char *user, *pass, *netspec, *folder, *flags, *tdate, *filec, *cfgn, *argv0;
     37 	int filelen, status;
     38 	llist_t *flagl;
     39 	inc_t *incs;
     40 
     41 	folder = NULL;
     42 	flags = NULL;
     43 	tdate = NULL;
     44 	status = 0;
     45 	cfgn = NULL;
     46 
     47 	enum {
     48 		BEQUIET = 0x01,
     49 
     50 		NOARGS = 0x02
     51 	};
     52 
     53 	ARGBEGIN(argv0) {
     54 	case 'c':
     55 		cfgn = EARGF(addusage(argv0));
     56 		break;
     57 	case 'd':
     58 		tdate = EARGF(addusage(argv0));
     59 		break;
     60 	case 'f':
     61 		flags = EARGF(addusage(argv0));
     62 		break;
     63 	case 'm':
     64 		folder = EARGF(addusage(argv0));
     65 		break;
     66 	case 'q':
     67 		status |= BEQUIET;
     68 		break;
     69 	default:
     70 		addusage(argv0);
     71 	} ARGEND;
     72 
     73 	filelen = 0;
     74 	if (argc < 1) {
     75 		filec = readtoeoffd(0, &filelen);
     76 		if (filec == NULL)
     77 			edie("readtoeoffd");
     78 	} else {
     79 		filec = readfile(argv[0], &filelen);
     80 	}
     81 
     82 	if (filec == NULL || filelen < 1)
     83 		die("Given file or input was too short or invalid.\n");
     84 
     85 	flagl = NULL;
     86 	if (flags != NULL) {
     87 		flagl = flag_sanitize(flags);
     88 		if (flagl == NULL)
     89 			die("Flag parameter seems to be invalid.\n");
     90 	}
     91 
     92 	cfg = config_init(cfgn);
     93 
     94 	user = config_checkgetstr(cfg, "imapuser");
     95 	pass = config_checkgetstr(cfg, "imappass");
     96 	netspec = config_checkgetstr(cfg, "imapnet");
     97 	if (folder == NULL) {
     98 		folder = config_checkgetstr(cfg, "selected");
     99 	} else {
    100 		folder = memdups(folder);
    101 	}
    102 	config_free(cfg);
    103 
    104 	imap = imap_new(netspec, user, pass);
    105 	free(user);
    106 	free(pass);
    107 	free(netspec);
    108 
    109 	if (imap_init(imap))
    110 		imap_die(imap, "imap_init");
    111 
    112 	if (imap_append(imap, folder, flagl, tdate, filec))
    113 		imap_die(imap, "imap_append");
    114 
    115 	incs = inc_init(cfgn);
    116 	inc_updatefolder(imap, folder, incs);
    117 	if (!(status & BEQUIET)) {
    118 		free(filec);
    119 		filec = inc_getstatuselem(incs, folder, "messages");
    120 		printf("%s\n", filec);
    121 	}
    122 	inc_stop(incs);
    123 
    124 	if (flagl != NULL)
    125 		llist_free(flagl);
    126 	free(filec);
    127 	free(folder);
    128 	imap_close(imap);
    129 	imap_free(imap);
    130 	return 0;
    131 }
    132