shithub: vcardfs

Download patch

ref: 2f487c06c35d1a8b325f03fe640dd6e46d29b35f
parent: d5d0e163bf6d6df97ac6d991f8fd5a7c9a40d936
author: sirjofri <[email protected]>
date: Fri Oct 18 06:55:27 EDT 2024

edit property value and param value

--- a/john.vcf
+++ b/john.vcf
@@ -1,6 +1,6 @@
 BEGIN:VCARD
 VERSION:4.0
-N;TYPE=ABC;type="A;B;C":Doe;John;;
+N;TYPE=ABC;value="A;B;C":Doe;John;;
 FN:John 
  Doe
 END:VCARD
--- a/vcardfs.c
+++ b/vcardfs.c
@@ -73,6 +73,8 @@
 	Vcard *card;
 	Vline *line;
 	Vparam *param;
+	Vfile *cardfile;
+	File *file;
 	char *serialized;
 };
 
@@ -83,8 +85,10 @@
 char *file = nil;
 Vcard *cards = nil;
 
+static char* getcardname(Vcard*);
+
 static Vfile*
-emkvfile(int level, Vcard *c, Vline *l, Vparam *p)
+emkvfile(int level, Vcard *c, Vline *l, Vparam *p, Vfile *cfile)
 {
 	Vfile *f;
 	f = mallocz(sizeof(Vfile), 1);
@@ -94,6 +98,7 @@
 	f->card = c;
 	f->line = l;
 	f->param = p;
+	f->cardfile = cfile;
 	return f;
 }
 
@@ -109,16 +114,16 @@
 	
 	tmp = f->card->next;
 	f->card->next = nil;
-	f->serialized = vcmserialize(f->card);
+	f->cardfile->serialized = vcmserialize(f->card);
 	f->card->next = tmp;
 	
-	if (!f->serialized) {
+	if (!f->cardfile->serialized) {
 		respond(r, "unable to serialize");
 		return;
 	}
 
 Readin:
-	s = f->serialized + r->ifcall.offset;
+	s = f->cardfile->serialized + r->ifcall.offset;
 	n = strlen(s);
 	n = n < r->ifcall.count ? n : r->ifcall.count;
 	readbuf(r, s, n);
@@ -161,6 +166,90 @@
 }
 
 static void
+condrenamecard(Vfile *f)
+{
+	char *s;
+	
+	if (strcmp(f->line->name, "n") != 0
+	 && strcmp(f->line->name, "fn") != 0)
+		return;
+	
+	if (!f->cardfile->file)
+		return;
+	
+	s = getcardname(f->card);
+	if (!s)
+		return;
+	
+	if (f->cardfile->file->name)
+		free(f->cardfile->file->name);
+	f->cardfile->file->name = s;
+}
+
+static char*
+getsanitized(char *s)
+{
+	return s;
+}
+
+static void
+writedata(Req *r, Vfile *f)
+{
+	char *s;
+	
+	switch (f->level) {
+	case Qparamdata:
+		if (!f->param) goto Err;
+		s = mallocz(r->ifcall.count + 1, 1);
+		if (!s) {
+			responderror(r);
+			return;
+		}
+		memcpy(s, r->ifcall.data, r->ifcall.count);
+		
+		/* chop off last \n, if exists */
+		if (s[r->ifcall.count - 1] == '\n')
+			s[r->ifcall.count - 1] = 0;
+		
+		if (f->param->value)
+			free(f->param->value);
+		f->param->value = s;
+		break;
+	case Qdata:
+		if (!f->line) goto Err;
+		s = mallocz(r->ifcall.count + 1, 1);
+		if (!s) {
+			responderror(r);
+			return;
+		}
+		memcpy(s, r->ifcall.data, r->ifcall.count);
+		
+		if (s[r->ifcall.count - 1] == '\n')
+			s[r->ifcall.count - 1] = 0;
+		
+		if (f->line->value)
+			free(f->line->value);
+		f->line->value = getsanitized(s);
+		condrenamecard(f);
+		break;
+	case Qgroup:
+		break;
+	default:
+		respond(r, "file not found");
+		return;
+	}
+	
+	if (f->cardfile->serialized)
+		free(f->cardfile->serialized);
+	f->cardfile->serialized = nil;
+	respond(r, nil);
+	return;
+	
+Err:
+	respond(r, "error");
+}
+
+static void
 fswrite(Req *r)
 {
 	Vfile *f;
@@ -168,6 +257,14 @@
 	switch (f->level) {
 	case Qctl:
 		break;
+	case Qexport:
+		respond(r, "not a function");
+		return;
+	case Qparamdata:
+	case Qdata:
+	case Qgroup:
+		writedata(r, f);
+		return;
 	}
 	respond(r, "not implemented");
 }
@@ -231,7 +328,7 @@
 	Vcard *c;
 	Vline *l;
 	Vparam *p;
-	Vfile *vf;
+	Vfile *vf, *cf;
 	char *s;
 	
 	for (c = chain; c; c = c->next) {
@@ -238,28 +335,29 @@
 		s = getcardname(c);
 		if (!s)
 			continue;
-		vf = emkvfile(Qcard, c, nil, nil);
-		fc = createfile(fs.tree->root, s, user, DMDIR|0555, vf);
+		cf = emkvfile(Qcard, c, nil, nil, nil);
+		fc = createfile(fs.tree->root, s, user, DMDIR|0555, cf);
+		cf->file = fc;
 		free(s);
 		if (!fc)
 			sysfatal("%r");
-		vf = emkvfile(Qexport, c, nil, nil);
+		vf = emkvfile(Qexport, c, nil, nil, cf);
 		f = createfile(fc, "export", user, 0444, vf);
 		
 		for (l = c->content; l; l = l->next) {
-			vf = emkvfile(Qline, c, l, nil);
+			vf = emkvfile(Qline, c, l, nil, cf);
 			fl = createfile(fc, l->name, user, DMDIR|0555, vf);
-			vf = emkvfile(Qdata, c, l, nil);
+			vf = emkvfile(Qdata, c, l, nil, cf);
 			f = createfile(fl, "data", user, 0666, vf);
 			if (l->group) {
-				vf = emkvfile(Qgroup, c, l, nil);
+				vf = emkvfile(Qgroup, c, l, nil, cf);
 				f = createfile(fl, "group", user, 0666, vf);
 			}
-			vf = emkvfile(Qparams, c, l, nil);
+			vf = emkvfile(Qparams, c, l, nil, cf);
 			f = createfile(fl, "params", user, DMDIR|0555, vf);
 			
 			for (p = l->params; p; p = p->next) {
-				vf = emkvfile(Qparamdata, c, l, p);
+				vf = emkvfile(Qparamdata, c, l, p, cf);
 				fp = createfile(f, p->name, user, 0666, vf);
 			}
 		}
@@ -303,9 +401,9 @@
 	
 	fs.tree = alloctree("vcf", "vcf", DMDIR|0555, nil);
 	createfile(fs.tree->root, "ctl", user, 0666,
-		emkvfile(Qctl, nil, nil, nil));
+		emkvfile(Qctl, nil, nil, nil, nil));
 	createfile(fs.tree->root, "import", user, 0666,
-		emkvfile(Qimport, nil, nil, nil));
+		emkvfile(Qimport, nil, nil, nil, nil));
 	initcardfiles(cards);
 	
 	postmountsrv(&fs, service, mtpt, MREPL);