tttm

git clone https://orangeshoelaces.net/git/tttm.git

bd02c5b3a5777f6c1b6817a257d548af2913c9e4

Author: Vasily Kolobkov on 05/30/2016

Committer: Vasily Kolobkov on 05/30/2016

Take arguments

Stats

tttm.c | 46 +++++++-
1 file changed, 42 insertions(+), 4 deletions(-)

Patch

diff --git a/tttm.c b/tttm.c
index 0b37c7f..1f1bef5 100644
--- a/tttm.c
+++ b/tttm.c
@@ -32,6 +32,7 @@ static int	sink_free(pid_t, int);
 static void	fin(struct imapctx *, int, const char *);
 static void	terr(int, const char *);
 static void	twarn(int, const char *);
+static void	usage(void);
 
 extern char **environ;
 
@@ -43,12 +44,35 @@ main(int argc, char **argv)
 {
 	int e;
 	struct imapctx con;
+	char *namekey, *passkey;
 	char *name, *pass, *sink;
 	int stor;
-	int refetchc, batchsz, mpiped;
+	int arg, refetchc, batchsz, mpiped;
 	struct msgd bag[BATCHSZ], *m, *bend;
 	long sv;
 
+	namekey = passkey = 0;
+
+	while ((arg = getopt(argc, argv, "n:p:")) != -1) {
+		switch (arg) {
+		case 'n':
+			namekey = optarg;
+			break;
+		case 'p':
+			passkey = optarg;
+			break;
+		default:
+			usage();
+		}
+	}
+	argc -= optind;
+	argv += optind;
+	if (argc == 1) {
+		sink = *argv;
+	} else {
+		usage();
+	}
+
 	if ((sv = sysconf(_SC_PAGESIZE)) == -1)
 		err(1, "failed to find out page size");
 	page = sv;
@@ -63,11 +87,19 @@ main(int argc, char **argv)
 	}
 	if (con.state == IS_AUTH) {
 		goto select;
+	} else if (!(namekey && passkey)) {
+		warnx("authentication credentials are needed");
+		goto logout;
 	}
 
-	name = argv[1];
-	pass = argv[2];
-	sink = argv[3];
+	if (!(name = getenv(namekey))) {
+		warnx("cannot find name within environment");
+		goto logout;
+	}
+	if (!(pass = getenv(passkey))) {
+		warnx("cannot find password within environment");
+		goto logout;
+	}
 
 	e = imap_login(&con, name, pass);
 	if (e == TE_NO) {
@@ -300,3 +332,9 @@ twarn(int e, const char *msg)
 		warnx("%s: %s", msg, errmsgs[e]);
 	}
 }
+
+void
+usage(void)
+{
+	errx(1, "usage: tttm [-n namekey] [-p passkey] sink");
+}