sync with 0.8.10cvs11
[claws.git] / src / quote_fmt_parse.y
index 1989a56f2ff8934d9f613dbb5f8dc403830614de..13cb1aacae80926e211097fa1bc35ef14ba8bb87 100644 (file)
@@ -1,3 +1,22 @@
+/*
+ * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2002 Hiroyuki Yamamoto and the Sylpheed-Claws Team
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ */
+
 %{
 
 #include "defs.h"
@@ -8,7 +27,7 @@
 #include "procmsg.h"
 #include "procmime.h"
 #include "utils.h"
-#include "intl.h"
+#include "procheader.h"
 
 #include "quote_fmt.h"
 #include "quote_fmt_lex.h"
@@ -67,13 +86,6 @@ static void add_buffer(const gchar *s)
        bufsize += len;
 }
 
-static void flush_buffer(void)
-{
-       if (buffer != NULL)
-               *buffer = '\0';
-       bufsize = 0;
-}
-
 gchar *quote_fmt_get_buffer(void)
 {
        if (error != 0)
@@ -110,7 +122,7 @@ void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
 
 void quote_fmterror(char *str)
 {
-       g_warning(_("Error: %s\n"), str);
+       g_warning("Error: %s\n", str);
        error = 1;
 }
 
@@ -131,7 +143,7 @@ static int isseparator(char ch)
 }
 
 %token SHOW_NEWSGROUPS
-%token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME
+%token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB
@@ -143,6 +155,7 @@ static int isseparator(char ch)
 %token INSERT_FILE INSERT_PROGRAMOUTPUT
 %token OPARENT CPARENT
 %token CHARACTER
+%token SHOW_DATE_EXPR
 
 %start quote_fmt
 
@@ -165,7 +178,7 @@ character_or_special_or_insert_or_query:
        {
                INSERT_CHARACTER($1);
        }
-       | query ;
+       | query
        | insert ;
 
 character:
@@ -191,6 +204,68 @@ special:
                if (msginfo->newsgroups)
                        INSERT(msginfo->newsgroups);
        }
+       | SHOW_DATE_EXPR OPARENT string CPARENT
+       {
+               /* 
+                * ALF - GNU C's strftime() has a nice format specifier 
+                * for time zone offset (%z). Non-standard however, so 
+                * emulate it.
+                */
+               if (msginfo->date) {
+                       char  result[100];
+                       char *rptr;
+                       char  zone[6];
+                       struct tm lt;
+                       const char *fptr;
+                       const char *zptr;
+
+#define RLEFT (sizeof result) - (rptr - result)        
+#define STR_SIZE(x) (sizeof (x) - 1)
+
+                       zone[0] = 0;
+
+                       if (procheader_date_parse_to_tm(msginfo->date, &lt, zone)) {
+                               /*
+                                * break up format string in tiny bits delimited by valid %z's and 
+                                * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
+                                */
+                               for (rptr = result, fptr = $3; fptr && *fptr && rptr < &result[sizeof result - 1];) {
+                                       int         perc;
+                                       const char *p;
+                                       char       *tmp;
+                                       
+                                       if (NULL != (zptr = strstr(fptr, "%z"))) {
+                                               /*
+                                                * count nr. of prepended percent chars
+                                                */
+                                               for (perc = 0, p = zptr; p && p >= $3 && *p == '%'; p--, perc++)
+                                                       ;
+                                               /*
+                                                * feed to strftime()
+                                                */
+                                               tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
+                                               if (tmp) {
+                                                       rptr += strftime(rptr, RLEFT, tmp, &lt);
+                                                       g_free(tmp);
+                                               }
+                                               /*
+                                                * append time zone offset
+                                                */
+                                               if (zone[0] && perc % 2) 
+                                                       rptr += g_snprintf(rptr, RLEFT, "%s", zone);
+                                               fptr = zptr + STR_SIZE("%z");
+                                       } else {
+                                               rptr += strftime(rptr, RLEFT, fptr, &lt);
+                                               fptr  = NULL;
+                                       }
+                               }
+                               
+                               INSERT(result);
+                       }
+#undef STR_SIZE                        
+#undef RLEFT                   
+               }
+       }
        | SHOW_DATE
        {
                if (msginfo->date)
@@ -222,6 +297,37 @@ special:
                        }
                }
        }
+       | SHOW_LAST_NAME
+        {
+                /* This probably won't work together very well with Middle
+                 names and the like - thth */
+               if (msginfo->fromname) {
+                       gchar *p;
+                       gchar *str;
+
+                       str = alloca(strlen(msginfo->fromname) + 1);
+                       if (str != NULL) {
+                               strcpy(str, msginfo->fromname);
+                                p = str;
+                                while (*p && !isspace(*p)) p++;
+                                if (*p) {
+                                   /* We found a space. Get first none-space char and
+                                    insert rest of string from there. */
+                                   while (*p && isspace(*p)) p++;
+                                    if (*p) {
+                                       INSERT(p);
+                                   } else {
+                                       /* If there is no none-space char, just insert
+                                        whole fromname. */
+                                       INSERT(str);
+                                   }
+                               } else {
+                                   /* If there is no space, just insert whole fromname. */
+                                   INSERT(str);
+                               }
+                       }
+               }
+       }
        | SHOW_SENDER_INITIAL
        {
 #define MAX_SENDER_INITIAL 20
@@ -280,7 +386,7 @@ special:
        }
        | SHOW_MESSAGE
        {
-               if (msginfo->folder) {
+               if (msginfo->folder || body) {
                        gchar buf[BUFFSIZE];
                        FILE *fp;
 
@@ -290,7 +396,7 @@ special:
                                fp = procmime_get_first_text_content(msginfo);
 
                        if (fp == NULL)
-                               g_warning(_("Can't get text part\n"));
+                               g_warning("Can't get text part\n");
                        else {
                                while (fgets(buf, sizeof(buf), fp) != NULL) {
                                        strcrchomp(buf);
@@ -302,7 +408,7 @@ special:
        }
        | SHOW_QUOTED_MESSAGE
        {
-               if (msginfo->folder) {
+               if (msginfo->folder || body) {
                        gchar buf[BUFFSIZE];
                        FILE *fp;
 
@@ -312,7 +418,7 @@ special:
                                fp = procmime_get_first_text_content(msginfo);
 
                        if (fp == NULL)
-                               g_warning(_("Can't get text part\n"));
+                               g_warning("Can't get text part\n");
                        else {
                                while (fgets(buf, sizeof(buf), fp) != NULL) {
                                        strcrchomp(buf);
@@ -326,7 +432,7 @@ special:
        }
        | SHOW_MESSAGE_NO_SIGNATURE
        {
-               if (msginfo->folder) {
+               if (msginfo->folder || body) {
                        gchar buf[BUFFSIZE];
                        FILE *fp;
 
@@ -336,11 +442,11 @@ special:
                                fp = procmime_get_first_text_content(msginfo);
 
                        if (fp == NULL)
-                               g_warning(_("Can't get text part\n"));
+                               g_warning("Can't get text part\n");
                        else {
                                while (fgets(buf, sizeof(buf), fp) != NULL) {
                                        strcrchomp(buf);
-                                       if (strncmp(buf, "-- ", 3) == 0)
+                                       if (strncmp(buf, "-- \n", 4) == 0)
                                                break;
                                        INSERT(buf);
                                }
@@ -350,7 +456,7 @@ special:
        }
        | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
        {
-               if (msginfo->folder) {
+               if (msginfo->folder || body) {
                        gchar buf[BUFFSIZE];
                        FILE *fp;
 
@@ -360,11 +466,11 @@ special:
                                fp = procmime_get_first_text_content(msginfo);
 
                        if (fp == NULL)
-                               g_warning(_("Can't get text part\n"));
+                               g_warning("Can't get text part\n");
                        else {
                                while (fgets(buf, sizeof(buf), fp) != NULL) {
                                        strcrchomp(buf);
-                                       if (strncmp(buf, "-- ", 3) == 0)
+                                       if (strncmp(buf, "-- \n", 4) == 0)
                                                break;
                                        if (quote_str)
                                                INSERT(quote_str);
@@ -484,10 +590,11 @@ insert:
                        FILE *file;
                        char buffer[256];
                        
-                       if(file = fopen($3, "r")) {
+                       if((file = fopen($3, "rb")) != NULL) {
                                while(fgets(buffer, sizeof(buffer), file)) {
                                        INSERT(buffer);
                                }
+                               fclose(file);
                        }
                }
        }
@@ -497,10 +604,11 @@ insert:
                        FILE *file;
                        char buffer[256];
 
-                       if(file = popen($3, "r")) {
+                       if((file = popen($3, "r")) != NULL) {
                                while(fgets(buffer, sizeof(buffer), file)) {
                                        INSERT(buffer);
                                }
+                               fclose(file);
                        }
                }
        };