#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <newmail.h>
int query = NM_QUERY_NCUR|NM_QUERY_NNEW;
int count = 0;
oop_source *oop = NULL;
struct spool_ll{
struct nm_spool *spool;
struct spool_ll *next;
} *spools = NULL;
char *b2s(int b) {
if (b < 0) return "fail";
if (b > 0) return "yes";
return "no";
}
void* _finish(oop_source *source, struct timeval tv, void *user) {
return OOP_HALT;
}
void finish(void) {
oop->on_time(oop, OOP_TIME_NOW, _finish, NULL);
}
void cb_check(struct nm_spool *s, struct nm_status *status, void *user) {
struct nm_info i;
static char txt[256];
if (!status)
snprintf(txt, sizeof(txt), "%s", nm_strerror(nm_errno, errno, nm_explanation));
if (nm_info(s, &i) < 0) {
fprintf(stderr, "nm_open(): %s\n", nm_strerror(nm_errno, errno, nm_explanation));
goto finish;
}
printf("\n%s:\n\tName: %s\n\tType: %s\n\tText: %s\n\tFlags: ", i.path, i.name, i.type, i.text);
if (i.flags & NM_FLAG_SYNCHRONOUS) printf("SYNCHRONOUS ");
if (i.flags & NM_FLAG_ASYNCHRONOUS) printf("ASYNCHRONOUS ");
printf("\n");
if (status) {
if (query & NM_QUERY_NCUR)
printf("\tCurrent: %i\n", status->cur);
else if (query & NM_QUERY_CUR)
printf("\tCurrent: %s\n", b2s(status->cur));
if (query & NM_QUERY_NNEW)
printf("\tNew: %i\n", status->new);
else if (query & NM_QUERY_NEW)
printf("\tNew: %s\n", b2s(status->new));
} else
fprintf(stderr, "\n\tFAILURE: %s\n", txt);
finish:
count--;
if (count <= 0)
finish();
}
void cb_list(const char *spool, void *user) {
struct nm_spool* s = NULL;
struct spool_ll* l = 0;
if (!(s = nm_open(spool))) {
fprintf(stderr, "nm_open(\"%s\"): %s\n", spool, nm_strerror(nm_errno, errno, nm_explanation));
goto finish;
}
if (!(l = malloc(sizeof(struct spool_ll)))) {
fprintf(stderr, "Memory\n");
goto finish;
}
if (nm_query_submit(s, query, oop, cb_check, NULL) < 0) {
fprintf(stderr, "nm_check_submit(\"%s\"): %s\n", spool, nm_strerror(nm_errno, errno, nm_explanation));
goto finish;
}
l->spool = s;
l->next = spools;
spools = l;
count++;
return;
finish:
free(l);
if (s)
nm_close(s);
}
int main(int argc, char *argv[]) {
oop_source_sys *sys;
count = 1;
if (!(sys = oop_sys_new())) {
fprintf(stderr, "Could not reate OOP system source.\n");
return 1;
}
if (!(oop = oop_sys_source(sys))) {
fprintf(stderr, "Could not reate OOP system source.\n");
oop_sys_delete(sys);
return 1;
}
if (nm_list(cb_list, NULL) < 0)
nm_perror("nm_list()");
count--;
while (count > 0)
oop_sys_run(sys);
oop_sys_delete(sys);
while (spools) {
struct spool_ll *l = spools;
spools = spools->next;
nm_close(l->spool);
free(l);
}
return 0;
}