summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--notemap.c41
1 files changed, 33 insertions, 8 deletions
diff --git a/notemap.c b/notemap.c
index 1a5cef2..5f35794 100644
--- a/notemap.c
+++ b/notemap.c
@@ -306,6 +306,8 @@ int main(int argc, char *argv[]) {
 		}
 	}
 	if (!user) errx(EX_USAGE, "username required");
+	argv += optind;
+	argc -= optind;
 	
 	if (!host) {
 		const char *domain = strchr(user, '@');
@@ -323,7 +325,7 @@ int main(int argc, char *argv[]) {
 	if (add) {
 		FILE *map = fopen(path, "a");
 		if (!map) err(EX_CANTCREAT, "%s", path);
-		for (int i = optind; i < argc; ++i) {
+		for (int i = 0; i < argc; ++i) {
 			if (access(argv[i], R_OK)) err(EX_NOINPUT, "%s", argv[i]);
 			fprintf(map, "%s %s\n", uuidGen(), argv[i]);
 			if (ferror(map)) err(EX_IOERR, "%s", path);
@@ -398,11 +400,7 @@ int main(int argc, char *argv[]) {
 				ssize_t len;
 				len = getline(&entry, &entryCap, map);
 				if (ferror(map)) err(EX_IOERR, "%s", path);
-				if (len < 1) {
-					fprintf(imap, "ayy LOGOUT\r\n");
-					fclose(imap);
-					return EX_OK;
-				}
+				if (len < 1) goto done;
 				if (entry[len - 1] == '\n') entry[len - 1] = '\0';
 
 				note = entry;
@@ -411,7 +409,17 @@ int main(int argc, char *argv[]) {
 					errx(EX_CONFIG, "invalid map entry: %s", entry);
 				}
 
-				// TODO: Skip note if not in argv.
+				if (argc) {
+					int i;
+					for (i = 0; i < argc; ++i) {
+						if (!argv[i]) continue;
+						if (strcmp(argv[i], note)) continue;
+						argv[i] = NULL;
+						break;
+					}
+					if (i == argc) goto Next;
+				}
+
 				fprintf(
 					imap,
 					"%s SEARCH HEADER X-Universally-Unique-Identifier %s\r\n",
@@ -429,6 +437,7 @@ int main(int argc, char *argv[]) {
 			}
 
 			break; case Append: {
+				printf("%c %s\n", (seq ? '~' : '+'), note);
 				if (!seq) goto Next;
 				fprintf(
 					imap, "%s STORE %d +FLAGS.SILENT (\\Deleted)\r\n",
@@ -464,10 +473,26 @@ int main(int argc, char *argv[]) {
 			if (error) err(EX_NOINPUT, "%s", note);
 
 			if (!force && status.st_mtime < mktime(&date)) {
-				errx(EX_TEMPFAIL, "%s: note modified in mailbox", note);
+				errx(
+					EX_TEMPFAIL,
+					"%s: note modified in mailbox; use -f to overwrite",
+					note
+				);
 			} else if (status.st_mtime == mktime(&date)) {
 				goto Next;
 			}
 		}
 	}
+
+done:
+	fprintf(imap, "ayy LOGOUT\r\n");
+	fclose(imap);
+
+	int ret = EX_OK;
+	for (int i = 0; i < argc; ++i) {
+		if (!argv[i]) continue;
+		warnx("%s: unmapped note; use -a to add", argv[i]);
+		ret = EX_CONFIG;
+	}
+	return ret;
 }