Index: functions.c
===================================================================
--- functions.c	(revision 8bba1aed9c99b38e86fab76c25d3b415be31afdb)
+++ functions.c	(revision 8bba1aed9c99b38e86fab76c25d3b415be31afdb)
@@ -222,17 +222,11 @@
  * that.
  */
-owl_message *owl_function_make_outgoing_zephyr(const char *body, const char *zwriteline, const char *zsig)
+owl_message *owl_function_make_outgoing_zephyr(const owl_zwrite *z)
 {
   owl_message *m;
-  owl_zwrite zw;
-
-  owl_zwrite_create_from_line(&zw, zwriteline);
-  owl_zwrite_set_zsig(&zw, zsig);
 
   /* create the message */
   m=owl_malloc(sizeof(owl_message));
-  
-  owl_message_create_from_zwrite(m, &zw, body);
-  owl_zwrite_cleanup(&zw);
+  owl_message_create_from_zwrite(m, z, owl_zwrite_get_message(z));
 
   return(m);
@@ -295,5 +289,5 @@
 }
 
-static void owl_function_write_setup(const char *line, const char *noun, void (*callback)(owl_editwin *))
+static void owl_function_write_setup(const char *noun)
 {
 
@@ -306,46 +300,41 @@
 			 "End with a dot on a line by itself.  ^C will quit.",
 			 noun);
-
-  owl_function_start_edit_win(line, callback,
+}
+
+void owl_function_zwrite_setup(owl_zwrite *z)
+{
+  /* send a ping if necessary */
+  if (owl_global_is_txping(&g)) {
+    owl_zwrite_send_ping(z);
+  }
+
+
+  owl_function_write_setup("zephyr");
+  owl_function_start_edit_win(z->zwriteline,
+                              &owl_callback_zwrite,
+                              z, (void(*)(void*))owl_zwrite_delete);
+}
+
+void owl_function_aimwrite_setup(const char *line)
+{
+  owl_function_write_setup("message");
+  owl_function_start_edit_win(line,
+                              &owl_callback_aimwrite,
                               owl_strdup(line),
                               owl_free);
-}
-
-void owl_function_zwrite_setup(const char *line)
-{
-  owl_zwrite z;
-  int ret;
-
-  /* check the arguments */
-  ret=owl_zwrite_create_from_line(&z, line);
-  if (ret) {
-    owl_function_error("Error in zwrite arguments");
-    owl_zwrite_cleanup(&z);
-    return;
-  }
-
-  /* send a ping if necessary */
-  if (owl_global_is_txping(&g)) {
-    owl_zwrite_send_ping(&z);
-  }
-  owl_zwrite_cleanup(&z);
-
-  owl_function_write_setup(line, "zephyr", &owl_callback_zwrite);
-}
-
-void owl_function_aimwrite_setup(const char *line)
-{
-  owl_function_write_setup(line, "message", &owl_callback_aimwrite);
+
 }
 
 void owl_function_loopwrite_setup(void)
 {
-  owl_function_write_setup("loopwrite", "message", owl_callback_loopwrite);
+  owl_function_write_setup("message");
+  owl_function_start_edit_win("loopwrite",
+                              &owl_callback_loopwrite,
+                              "loopwrite", NULL);
 }
 
 void owl_callback_zwrite(owl_editwin *e) {
-  char *command = owl_editwin_get_cbdata(e);
-  owl_function_zwrite(command,
-                      owl_editwin_get_text(e));
+  owl_zwrite *z = owl_editwin_get_cbdata(e);
+  owl_function_zwrite(z, owl_editwin_get_text(e));
 }
 
@@ -353,29 +342,25 @@
  * the message is expected to be set from the zwrite line itself
  */
-void owl_function_zwrite(const char *line, const char *msg)
-{
-  owl_zwrite z;
-  const char *mymsg;
+void owl_function_zwrite(owl_zwrite *z, const char *msg)
+{
   owl_message *m;
 
-  if(!strncmp(line, "zcrypt", strlen("zcrypt"))) {
-    owl_function_zcrypt(line, msg);
+  if(strcmp(z->cmd, "zcrypt") == 0) {
+    owl_function_zcrypt(z, msg);
     return;
   }
 
   /* create the zwrite and send the message */
-  owl_zwrite_create_from_line(&z, line);
-  owl_zwrite_populate_zsig(&z);
+  owl_zwrite_populate_zsig(z);
   if (msg) {
-    owl_zwrite_set_message(&z, msg);
-  }
-  owl_zwrite_send_message(&z);
+    owl_zwrite_set_message(z, msg);
+  }
+  owl_zwrite_send_message(z);
   owl_function_makemsg("Waiting for ack...");
 
   /* If it's personal */
-  if (owl_zwrite_is_personal(&z)) {
+  if (owl_zwrite_is_personal(z)) {
     /* create the outgoing message */
-    mymsg=owl_zwrite_get_message(&z);
-    m=owl_function_make_outgoing_zephyr(mymsg, line, owl_zwrite_get_zsig(&z));
+    m=owl_function_make_outgoing_zephyr(z);
 
     if (m) {
@@ -385,7 +370,4 @@
     }
   }
-
-  /* free the zwrite */
-  owl_zwrite_cleanup(&z);
 }
 
@@ -393,8 +375,6 @@
  * the message is expected to be set from the zwrite line itself
  */
-void owl_function_zcrypt(const char *line, const char *msg)
-{
-  owl_zwrite z;
-  const char *mymsg;
+void owl_function_zcrypt(owl_zwrite *z, const char *msg)
+{
   char *cryptmsg;
   owl_message *m;
@@ -402,22 +382,21 @@
   char *zcrypt;
   int rv, status;
+  char *old_msg;
 
   /* create the zwrite and send the message */
-  owl_zwrite_create_from_line(&z, line);
-  owl_zwrite_populate_zsig(&z);
+  owl_zwrite_populate_zsig(z);
   if (msg) {
-    owl_zwrite_set_message(&z, msg);
-  }
-
-  mymsg=owl_zwrite_get_message(&z);
+    owl_zwrite_set_message(z, msg);
+  }
+  old_msg = owl_strdup(owl_zwrite_get_message(z));
 
   zcrypt = owl_sprintf("%s/zcrypt", owl_get_bindir());
   argv[0] = "zcrypt";
   argv[1] = "-E";
-  argv[2] = "-c"; argv[3] = owl_zwrite_get_class(&z);
-  argv[4] = "-i"; argv[5] = owl_zwrite_get_instance(&z);
+  argv[2] = "-c"; argv[3] = owl_zwrite_get_class(z);
+  argv[4] = "-i"; argv[5] = owl_zwrite_get_instance(z);
   argv[6] = NULL;
 
-  rv = call_filter(zcrypt, argv, mymsg, &cryptmsg, &status);
+  rv = call_filter(zcrypt, argv, owl_zwrite_get_message(z), &cryptmsg, &status);
 
   owl_free(zcrypt);
@@ -425,21 +404,21 @@
   if (rv || status) {
     if(cryptmsg) owl_free(cryptmsg);
+    owl_free(old_msg);
     owl_function_error("Error in zcrypt, possibly no key found.  Message not sent.");
     owl_function_beep();
-    owl_zwrite_cleanup(&z);
     return;
   }
 
-  owl_zwrite_set_message(&z, cryptmsg);
-  owl_zwrite_set_opcode(&z, "crypt");
-    
-  owl_zwrite_send_message(&z);
+  owl_zwrite_set_message_raw(z, cryptmsg);
+  owl_zwrite_set_opcode(z, "crypt");
+
+  owl_zwrite_send_message(z);
   owl_function_makemsg("Waiting for ack...");
 
   /* If it's personal */
-  if (owl_zwrite_is_personal(&z)) {
-    /* create the outgoing message */
-    mymsg=owl_zwrite_get_message(&z);
-    m=owl_function_make_outgoing_zephyr(mymsg, line, owl_zwrite_get_zsig(&z));
+  if (owl_zwrite_is_personal(z)) {
+    /* Create the outgoing message. Restore the un-crypted message for display. */
+    owl_zwrite_set_message_raw(z, old_msg);
+    m=owl_function_make_outgoing_zephyr(z);
     if (m) {
       owl_global_messagequeue_addmsg(&g, m);
@@ -451,5 +430,4 @@
   /* free the zwrite */
   owl_free(cryptmsg);
-  owl_zwrite_cleanup(&z);
 }
 
