Index: trunk/lib/header_pack.ml
===================================================================
--- trunk/lib/header_pack.ml	(revision 22)
+++ trunk/lib/header_pack.ml	(revision 42)
@@ -11,5 +11,6 @@
 let str = Msgpck.of_string
 let str_list ls = Msgpck.of_list @@ List.map str ls
-let to_str_list x = List.map Msgpck.to_string (Msgpck.to_list x)
+let to_str_list x = List.map Msgpck.to_string
+	(try Msgpck.to_list x with e -> prerr_endline "to_str_list"; raise e)
 
 let of_set field t =
@@ -20,5 +21,8 @@
 let to_sec = function Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i | x -> Msgpck.to_uint32 x
 
-let fields = Msgpck.(List [String "id"; String "time";  String "title"; String "authors"; String "topics"])
+let fields = Msgpck.(List [
+	String "id"; String "time";  String "title"; String "authors"; String "topics";
+	String "references"; String "replies";
+	])
 let to_fields fieldpack = List.map Msgpck.to_string (Msgpck.to_list fieldpack)
 
@@ -36,6 +40,11 @@
 	let open Text in
 	Msgpck.(List [
-		of_id t.id; of_uint32 (date (Date.listing t.date));
-		String t.title; persons t.authors; List (of_set "topics" t)
+		of_id t.id;
+		of_uint32 (date (Date.listing t.date));
+		String t.title;
+		persons t.authors;
+		List (of_set "topics" t);
+		List (of_set "references" t);
+		List (of_set "in-reply-to" t);
 	]) :: a
 
@@ -82,6 +91,6 @@
 let numof_texts pack = List.length (Msgpck.to_list pack.texts)
 
-let iteri fn pack =
-	let of_pck i = function Msgpck.List (id::time::title::authors::topics::[]) ->
+let txt_iter_apply fn i = function
+	| Msgpck.List (id::time::title::authors::topics::extra) ->
 		let t = match time with Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i
 			| x -> Msgpck.to_uint32 x in
@@ -90,19 +99,33 @@
 		let topics = to_str_list topics in
 		let authors = to_str_list authors in
-		fn i id t title authors topics
+		let references, replies =
+			try begin match extra with [] -> [], []
+			| refs::[] -> to_str_list refs, []
+			| refs::replies::_xs -> to_str_list refs, to_str_list replies
+			end with e -> prerr_endline "iter ref reps"; raise e
+		in
+		fn i id t title authors topics references replies
 	| _  -> prerr_endline ("\n\nInvalid record structure\n\n")
-	in List.iteri of_pck (Msgpck.to_list pack.texts);
 
-(*let pack_filename ?(filename="index.pck") archive =*)
-(*	let dir = Store.KV.find "Export-Dir" archive.File_store.kv in (*raises Not_found*)*)
-(*	dir ^ "/" ^ filename*)
+let txt_fold_apply fn i m =
+(*	Printf.eprintf "%s\n%!" @@ Msgpck.show m;*)
+	match m with
+	| Msgpck.List (id::time::title::authors::topics::extra) ->
+		let t = match time with Msgpck.Int i -> Int32.of_int i | Msgpck.Uint32 i -> i
+			| x -> Msgpck.to_uint32 x in
+		let id = to_id id in
+		let title = Msgpck.to_string title in
+		let topics = try to_str_list topics with _e -> Printf.eprintf "topics %s" title; [] in
+		let authors = try to_str_list authors with _e -> Printf.eprintf "authors %s" title; [] in
+		let references, replies = begin match extra with
+			| [] -> [], []
+			| refs::[] -> (try to_str_list refs, [] with e -> prerr_endline "fold ref"; raise e)
+			| refs::replies::_xs -> to_str_list refs, to_str_list replies
+			end
+		in
+		fn i id t title authors topics references replies
+	| x  -> Printf.eprintf "Invalid record structure: %s\n%!" (Msgpck.show x); i
 
-(*let add archive records =*)
-(*	let fname = pack_filename archive in*)
-(*	let append published (t, _f) = if List.exists (contains t) published then published else to_pack published t in*)
-(*	match list fname with Error e -> prerr_endline e | Ok published_list ->*)
-(*		let header_pack = List.fold_left append published_list records in*)
-(*		let archive = Msgpck.(List [*)
-(*			Int 0; String archive.File_store.name; persons archive.people]) in*)
-(*		File_store.file fname @@ Bytes.to_string*)
-(*			@@ Msgpck.Bytes.to_string (List [archive; fields; Msgpck.List header_pack])*)
+let iteri fn pack = List.iteri (txt_iter_apply fn) (Msgpck.to_list pack.texts)
+let fold fn init pack = List.fold_left (txt_fold_apply fn) init
+	(try Msgpck.to_list pack.texts with e -> prerr_string "Pack.fold"; raise e)
