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

stats.c (2694B)


      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 
     10 #include "ind.h"
     11 #include "arg.h"
     12 #include "cfg.h"
     13 #include "llist.h"
     14 #include "folder.h"
     15 #include "imap.h"
     16 #include "stats.h"
     17 
     18 void
     19 statsusage(char *argv0)
     20 {
     21 	die("usage: %s [-haru] [-c cfg] [-m folder]\n", argv0);
     22 }
     23 
     24 int
     25 statsmain(int argc, char *argv[])
     26 {
     27 	config_t *cfg;
     28 	imap_t *imap;
     29 	int cmsgs, crecent, cunseen, cfolders, domsgs, dorecent, dounseen;
     30 	char *user, *pass, *netspec, *msgs, *recent, *unseen, *selected,
     31 	     *cfgn, *argv0;
     32 	llist_t *folders, *ffolders, *statusl;
     33 	llistelem_t *folder, *elem;
     34 
     35 	domsgs = 0;
     36 	dorecent = 0;
     37 	dounseen = 0;
     38 	selected = NULL;
     39 	cfgn = NULL;
     40 
     41 	ARGBEGIN(argv0) {
     42 	case 'a':
     43 		domsgs = 1;
     44 		break;
     45 	case 'c':
     46 		cfgn = EARGF(statsusage(argv0));
     47 		break;
     48 	case 'm':
     49 		selected = EARGF(statsusage(argv0));
     50 		break;
     51 	case 'r':
     52 		dorecent = 1;
     53 		break;
     54 	case 'u':
     55 		dounseen = 1;
     56 		break;
     57 	default:
     58 		statsusage(argv0);
     59 	} ARGEND;
     60 
     61 	cfg = config_init(cfgn);
     62 	user = (config_checkget(cfg, "imapuser"))->data;
     63 	pass = (config_checkget(cfg, "imappass"))->data;
     64 	netspec = (config_checkget(cfg, "imapnet"))->data;
     65 	imap = imap_new(netspec, user, pass);
     66 	config_free(cfg);
     67 
     68 	if (imap_init(imap))
     69 		imap_die(imap, "imap_init");
     70 
     71 	folders = imap_listfolders(imap);
     72 	if (folders == NULL)
     73 		imap_die(imap, "imap_subscribed");
     74 
     75 	if (selected != NULL) {
     76 		folder = llist_get(folders, selected);
     77 		if (folder != NULL) {
     78 			ffolders = llist_new();
     79 			llist_add(ffolders, folder->key, folder->data,
     80 					folder->datalen);
     81 		} else {
     82 			die("Could not find folder %s.\n", selected);
     83 		}
     84 	} else {
     85 		ffolders = folders;
     86 	}
     87 
     88 	cmsgs = 0;
     89 	crecent = 0;
     90 	cunseen = 0;
     91 	cfolders = ffolders->len;
     92 	forllist(ffolders, folder) {
     93 		if (folder->key == NULL)
     94 			continue;
     95 		statusl = imap_status(imap, folder->key);
     96 		if (statusl == NULL)
     97 			continue;
     98 
     99 		msgs = "";
    100 		recent = "";
    101 		unseen = "";
    102 		elem = llist_ciget(statusl, "messages");
    103 		if (elem != NULL)
    104 			msgs = (char *)elem->data;
    105 		elem = llist_ciget(statusl, "recent");
    106 		if (elem != NULL)
    107 			recent = (char *)elem->data;
    108 		elem = llist_ciget(statusl, "unseen");
    109 		if (elem != NULL)
    110 			unseen = (char *)elem->data;
    111 
    112 		cmsgs += atoi(msgs);
    113 		crecent += atoi(recent);
    114 		cunseen += atoi(unseen);
    115 	}
    116 
    117 	if (domsgs)
    118 		printf("%d\n", cmsgs);
    119 	if (dounseen)
    120 		printf("%d\n", cunseen);
    121 	if (dorecent)
    122 		printf("%d\n", crecent);
    123 
    124 	if (!domsgs && !dounseen && !dorecent) {
    125 		printf("%d folders containing %d messages, with %d unseen and %d "
    126 				"recent messages.\n", cfolders, cmsgs, cunseen,
    127 				crecent);
    128 	}
    129 	if (folder != NULL)
    130 		llist_free(ffolders);
    131 	llist_free(folders);
    132 
    133 	imap_close(imap);
    134 	imap_free(imap);
    135 	return 0;
    136 }
    137