]> diplodocus.org Git - nmh/blobdiff - uip/mhical.c
Added warning if the profile contains a post entry.
[nmh] / uip / mhical.c
index a25b08bf854ba9acc29c9c04afbddda6062ef899..078ad37ec66d11c8bad3d27a6807b689ab6584cd 100644 (file)
@@ -6,9 +6,19 @@
  */
 
 #include "h/mh.h"
+#include "sbr/fmt_new.h"
+#include "sbr/getarguments.h"
+#include "sbr/concat.h"
+#include "sbr/smatch.h"
+#include "sbr/ambigsw.h"
+#include "sbr/path.h"
+#include "sbr/print_version.h"
+#include "sbr/print_help.h"
+#include "sbr/error.h"
 #include "h/icalendar.h"
+#include "sbr/datetime.h"
 #include "sbr/icalparse.h"
-#include <h/fmt_scan.h>
+#include "h/fmt_scan.h"
 #include "h/addrsbr.h"
 #include "h/mts.h"
 #include "h/done.h"
@@ -24,7 +34,7 @@ typedef enum act {
     ACT_CANCEL
 } act;
 
-static void convert_to_reply (contentline *, act);
+static int convert_to_reply (contentline *, act, char *);
 static void convert_to_cancellation (contentline *);
 static void convert_common (contentline *, act);
 static void dump_unfolded (FILE *, contentline *);
@@ -41,6 +51,7 @@ static char *fold (char *, int);
     X("format string", 5, FMTSW) \
     X("infile", 0, INFILESW) \
     X("outfile", 0, OUTFILESW) \
+    X("attendee", 0, ATTENDEESW) \
     X("contenttype", 0, CONTENTTYPESW) \
     X("nocontenttype", 0, NCONTENTTYPESW) \
     X("unfold", 0, UNFOLDSW) \
@@ -60,7 +71,8 @@ vevent vevents = { NULL, NULL, NULL};
 int parser_status = 0;
 
 int
-main (int argc, char *argv[]) {
+main (int argc, char *argv[])
+{
     /* RFC 5322 § 3.3 date-time format, including the optional
        day-of-week and not including the optional seconds.  The
        zone is required by the RFC but not always output by this
@@ -70,6 +82,7 @@ main (int argc, char *argv[]) {
     act action = ACT_NONE;
     char *infile = NULL, *outfile = NULL;
     FILE *inputfile = NULL, *outputfile = NULL;
+    char *attendee = NULL;
     bool contenttype = false;
     bool unfold = false;
     vevent *v, *nextvevent;
@@ -150,6 +163,12 @@ main (int argc, char *argv[]) {
                 outfile = *cp == '-'  ?  mh_xstrdup(cp)  :  path (cp, TFILE);
                 continue;
 
+            case ATTENDEESW:
+                if (! (cp = *argp++) || (*cp == '-' && cp[1]))
+                    die("missing argument to %s", argp[-2]);
+                attendee = cp;
+                continue;
+
             case CONTENTTYPESW:
                 contenttype = true;
                 continue;
@@ -214,9 +233,12 @@ main (int argc, char *argv[]) {
             if (action == ACT_CANCEL) {
                 convert_to_cancellation (v->contentlines);
             } else {
-                convert_to_reply (v->contentlines, action);
+                parser_status +=
+                    convert_to_reply (v->contentlines, action, attendee);
+            }
+            if (parser_status == 0) {
+                output (outputfile, v->contentlines, contenttype);
             }
-            output (outputfile, v->contentlines, contenttype);
         }
 
         free_contentlines (v->contentlines);
@@ -239,13 +261,15 @@ main (int argc, char *argv[]) {
         free (outfile);
     }
 
-    return parser_status;
+    return parser_status > 0 ? 1 : 0;
 }
 
 /*
  * - Change METHOD from REQUEST to REPLY.
  * - Change PRODID.
- * - Remove all ATTENDEE lines for other users (based on ismymbox ()).
+ * - Remove all ATTENDEE lines for other users (based on ismymbox()).
+ *   If more than one address matches ismymbox(), the attendee argument,
+ *   from the -attendee switch, must be used to select one.
  * - For the user's ATTENDEE line:
  *   - Remove ROLE and RSVP parameters.
  *   - Change PARTSTAT value to indicate reply action, e.g., ACCEPTED,
@@ -253,13 +277,13 @@ main (int argc, char *argv[]) {
  * - Insert action at beginning of SUMMARY value.
  * - Remove all X- lines.
  * - Update DTSTAMP with current timestamp.
- * - Remove all DESCRIPTION lines.
  * - Excise VALARM sections.
  */
-static void
-convert_to_reply (contentline *clines, act action) {
+static int
+convert_to_reply (contentline *clines, act action, char *attendee)
+{
     char *partstat = NULL;
-    bool found_my_attendee_line = false;
+    unsigned int found_my_attendee_line = 0;
     contentline *node;
 
     convert_common (clines, action);
@@ -302,8 +326,9 @@ convert_to_reply (contentline *clines, act action) {
             /* Need to flush getname after use. */
             while (getname ("")) { continue; }
 
-            if (ismymbox (mn)) {
-                found_my_attendee_line = true;
+            if (ismymbox (mn)  &&
+                (attendee == NULL || ! strcasecmp(mn->m_text, attendee))) {
+                ++found_my_attendee_line;
                 for (p = node->params; p && p->param_name; p = p->next) {
                     value_list *v;
 
@@ -325,7 +350,7 @@ convert_to_reply (contentline *clines, act action) {
         }
     }
 
-    if (! found_my_attendee_line) {
+    if (found_my_attendee_line == 0) {
         /* Generate and attach an ATTENDEE line for me. */
         contentline *node;
 
@@ -340,16 +365,13 @@ convert_to_reply (contentline *clines, act action) {
             add_param_value (new_node, mh_xstrdup (getfullname ()));
             new_node->value = concat ("MAILTO:", getlocalmbox (), NULL);
         }
+    } else if (found_my_attendee_line > 1) {
+        inform("Multiple attendees match your address, "
+               "re-run with -attendee switch");
+        return 1;
     }
 
-    /* Call find_contentline () with node as argument to find multiple
-       matching contentlines. */
-    for (node = clines;
-         (node = find_contentline (node, "DESCRIPTION", 0));
-         node = node->next) {
-        /* ACCEPT, at least, replies don't seem to have DESCRIPTIONS. */
-        remove_contentline (node);
-    }
+    return 0;
 }
 
 /*
@@ -363,7 +385,8 @@ convert_to_reply (contentline *clines, act action) {
  * - Excise VALARM sections.
  */
 static void
-convert_to_cancellation (contentline *clines) {
+convert_to_cancellation (contentline *clines)
+{
     contentline *node;
 
     convert_common (clines, ACT_CANCEL);
@@ -385,7 +408,8 @@ convert_to_cancellation (contentline *clines) {
 }
 
 static void
-convert_common (contentline *clines, act action) {
+convert_common (contentline *clines, act action)
+{
     contentline *node;
     bool in_valarm;
 
@@ -396,7 +420,7 @@ convert_common (contentline *clines, act action) {
 
     if ((node = find_contentline (clines, "PRODID", 0))) {
         free (node->value);
-        node->value = mh_xstrdup ("nmh mhical v0.1");
+        node->value = mh_xstrdup ("nmh mhical v0.5");
     }
 
     if ((node = find_contentline (clines, "VERSION", 0))) {
@@ -407,8 +431,8 @@ convert_common (contentline *clines, act action) {
 
         if (strcmp (node->value, "2.0")) {
             inform("supports the Version 2.0 specified by RFC 5545 "
-               "but iCalendar object has Version %s, continuing...",
-               node->value);
+                "but iCalendar object has Version %s, continuing...",
+                node->value);
             node->value = mh_xstrdup ("2.0");
         }
     }
@@ -496,7 +520,8 @@ convert_common (contentline *clines, act action) {
 
 /* Echo the input, but with unfolded lines. */
 static void
-dump_unfolded (FILE *file, contentline *clines) {
+dump_unfolded (FILE *file, contentline *clines)
+{
     contentline *node;
 
     for (node = clines; node; node = node->next) {
@@ -505,7 +530,8 @@ dump_unfolded (FILE *file, contentline *clines) {
 }
 
 static void
-output (FILE *file, contentline *clines, int contenttype) {
+output (FILE *file, contentline *clines, int contenttype)
+{
     contentline *node;
 
     if (contenttype) {
@@ -558,7 +584,8 @@ output (FILE *file, contentline *clines, int contenttype) {
  *   - attendees (limited to number specified in initialization)
  */
 static void
-display (FILE *file, contentline *clines, char *nfs) {
+display (FILE *file, contentline *clines, char *nfs)
+{
     tzdesc_t timezones = load_timezones (clines);
     bool in_vtimezone;
     bool in_valarm;
@@ -727,7 +754,8 @@ display (FILE *file, contentline *clines, char *nfs) {
 }
 
 static const char *
-identity (const contentline *node) {
+identity (const contentline *node)
+{
     /* According to RFC 5545 § 3.3.3, an email address in the value
        must be a mailto URI. */
     if (! strncasecmp (node->value, "mailto:", 7)) {
@@ -757,7 +785,8 @@ identity (const contentline *node) {
 }
 
 static char *
-format_params (char *line, param_list *p) {
+format_params (char *line, param_list *p)
+{
     for ( ; p && p->param_name; p = p->next) {
         value_list *v;
         size_t num_values = 0;
@@ -790,7 +819,8 @@ format_params (char *line, param_list *p) {
 }
 
 static char *
-fold (char *line, int uses_cr) {
+fold (char *line, int uses_cr)
+{
     size_t remaining = strlen (line);
     size_t current_line_len = 0;
     charstring_t folded_line = charstring_create (2 * remaining);