2006-11-13 [wwp] 2.6.0cvs39
[claws.git] / src / quote_fmt_parse.y
index 18e5f67ecdee3c6100e0b4db036d0b7aeda86e16..265aac601e071076fb061837e5c0e6d86d558b01 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2002 Hiroyuki Yamamoto and the Sylpheed-Claws Team
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail 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
 #include "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
+
 #include <ctype.h>
 
 #include "procmsg.h"
 #include "procmime.h"
 #include "utils.h"
+#include "codeconv.h"
 #include "procheader.h"
+#include "gtk/inputdialog.h"
 
 #include "quote_fmt.h"
 #include "quote_fmt_lex.h"
@@ -41,19 +45,29 @@ bison -p quote_fmt quote_fmt.y
 int yylex(void);
 
 static MsgInfo *msginfo = NULL;
+static PrefsAccount *account = NULL;
 static gboolean *visible = NULL;
-static gboolean *dry_run = NULL;
+static gboolean dry_run = FALSE;
 static gint maxsize = 0;
 static gint stacksize = 0;
+static GHashTable *var_table = NULL;
+
+typedef struct st_buffer
+{
+       gchar *buffer;
+       gint bufsize;
+       gint bufmax;
+} st_buffer;
+
+static struct st_buffer main_expr = { NULL, 0, 0 };
+static struct st_buffer sub_expr = { NULL, 0, 0 };
+static struct st_buffer* current = NULL;
 
-static gchar *buffer = NULL;
-static gint bufmax = 0;
-static gint bufsize = 0;
 static const gchar *quote_str = NULL;
 static const gchar *body = NULL;
 static gint error = 0;
 
-static gint cursor_pos  = 0;
+static gint cursor_pos = -1;
 
 extern int quote_fmt_firsttime;
 
@@ -73,30 +87,50 @@ static void add_visibility(gboolean val)
 static void remove_visibility(void)
 {
        stacksize--;
+       if (stacksize < 0) {
+               g_warning("Error: visibility stack underflow\n");
+               stacksize = 0;
+       }
 }
 
 static void add_buffer(const gchar *s)
 {
        gint len;
 
+       if (s == NULL)
+               return;
+
        len = strlen(s);
-       if (bufsize + len + 1 > bufmax) {
-               if (bufmax == 0)
-                       bufmax = 128;
-               while (bufsize + len + 1 > bufmax)
-                       bufmax *= 2;
-               buffer = g_realloc(buffer, bufmax);
-       }
-       strcpy(buffer + bufsize, s);
-       bufsize += len;
+       if (current->bufsize + len + 1 > current->bufmax) {
+               if (current->bufmax == 0)
+                       current->bufmax = 128;
+               while (current->bufsize + len + 1 > current->bufmax)
+                       current->bufmax *= 2;
+               current->buffer = g_realloc(current->buffer, current->bufmax);
+       }
+       strcpy(current->buffer + current->bufsize, s);
+       current->bufsize += len;
+}
+
+static void clear_buffer(void)
+{
+       if (current->buffer)
+               *current->buffer = '\0';
+       else
+               /* force to an empty string, as buffer should not be left unallocated */
+               add_buffer("");
+       current->bufsize = 0;
 }
 
 gchar *quote_fmt_get_buffer(void)
 {
+       if (current != &main_expr)
+               g_warning("Error: parser still in sub-expr mode\n");
+
        if (error != 0)
                return NULL;
        else
-               return buffer;
+               return current->buffer;
 }
 
 gint quote_fmt_get_cursor_pos(void)
@@ -105,8 +139,8 @@ gint quote_fmt_get_cursor_pos(void)
 }
 
 #define INSERT(buf) \
-       if (stacksize != 0 && visible[stacksize - 1]) \
-               add_buffer(buf)
+       if (stacksize != 0 && visible[stacksize - 1])\
+               add_buffer(buf); \
 
 #define INSERT_CHARACTER(chr) \
        if (stacksize != 0 && visible[stacksize - 1]) { \
@@ -116,24 +150,40 @@ gint quote_fmt_get_cursor_pos(void)
                add_buffer(tmp); \
        }
 
+void quote_fmt_reset_vartable(void)
+{
+       if (var_table) {
+               g_hash_table_destroy(var_table);
+               var_table = NULL;
+       }
+}
+
 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
-                   const gchar *my_body, gboolean* my_dry_run)
+                   const gchar *my_body, gboolean my_dry_run,
+                       PrefsAccount *compose_account)
 {
        quote_str = my_quote_str;
        body = my_body;
        msginfo = info;
+       account = compose_account;
        dry_run = my_dry_run;
        stacksize = 0;
        add_visibility(TRUE);
-       if (buffer != NULL)
-               *buffer = 0;
-       bufsize = 0;
+       main_expr.bufmax = 0;
+       sub_expr.bufmax = 0;
+       current = &main_expr;
+       clear_buffer();
        error = 0;
+
+       if (!var_table)
+               var_table = g_hash_table_new_full(g_str_hash, g_str_equal, 
+                               g_free, g_free);
+
         /*
          * force LEX initialization
          */
        quote_fmt_firsttime = 1;
-       cursor_pos = 0;
+       cursor_pos = -1;
 }
 
 void quote_fmterror(char *str)
@@ -211,7 +261,24 @@ static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
                        }
                }
                
-               INSERT(result);
+               if (g_utf8_validate(result, -1, NULL)) {
+                       INSERT(result);
+               } else {
+                       gchar *utf = conv_codeset_strdup(result, 
+                               conv_get_locale_charset_str_no_utf8(),
+                               CS_INTERNAL);
+                       if (utf == NULL || 
+                           !g_utf8_validate(utf, -1, NULL)) {
+                               g_free(utf);
+                               utf = g_malloc(strlen(result)*2+1);
+                               conv_localetodisp(utf, 
+                                       strlen(result)*2+1, result);
+                       }
+                       if (g_utf8_validate(utf, -1, NULL)) {
+                               INSERT(utf);
+                       }
+                       g_free(utf);
+               }
        }
 #undef STR_SIZE                        
 #undef RLEFT                   
@@ -225,14 +292,14 @@ static void quote_fmt_show_first_name(const MsgInfo *msginfo)
        if (!msginfo->fromname)
                return; 
        
-       p = strchr(msginfo->fromname, ',');
+       p = (guchar*)strchr(msginfo->fromname, ',');
        if (p != NULL) {
                /* fromname is like "Duck, Donald" */
                p++;
                while (*p && isspace(*p)) p++;
-               str = alloca(strlen(p) + 1);
+               str = alloca(strlen((char *)p) + 1);
                if (str != NULL) {
-                       strcpy(str, p);
+                       strcpy(str, (char *)p);
                        INSERT(str);
                }
        } else {
@@ -240,7 +307,7 @@ static void quote_fmt_show_first_name(const MsgInfo *msginfo)
                str = alloca(strlen(msginfo->fromname) + 1);
                if (str != NULL) {
                        strcpy(str, msginfo->fromname);
-                       p = str;
+                       p = (guchar *)str;
                        while (*p && !isspace(*p)) p++;
                        *p = '\0';
                        INSERT(str);
@@ -303,10 +370,10 @@ static void quote_fmt_show_sender_initial(const MsgInfo *msginfo)
        if (!msginfo->fromname) 
                return;
 
-       p = msginfo->fromname;
+       p = (guchar *)msginfo->fromname;
        cur = tmp;
        while (*p) {
-               if (*p && g_utf8_validate(p, 1, NULL)) {
+               if (*p && g_utf8_validate((gchar *)p, 1, NULL)) {
                        *cur = toupper(*p);
                                cur++;
                        len++;
@@ -381,8 +448,34 @@ static void quote_fmt_insert_program_output(const gchar *progname)
                while (fgets(buffer, sizeof(buffer), file)) {
                        INSERT(buffer);
                }
-               fclose(file);
+               pclose(file);
+       }
+}
+
+static void quote_fmt_insert_user_input(const gchar *varname)
+{
+       gchar *buf = NULL;
+       gchar *text = NULL;
+       
+       if (dry_run) 
+               return;
+
+       if ((text = g_hash_table_lookup(var_table, varname)) == NULL) {
+               buf = g_strdup_printf(_("Enter text to replace '%s'"), varname);
+               text = input_dialog(_("Enter variable"), buf, "");
+               g_free(buf);
+               if (!text)
+                       return;
+               g_hash_table_insert(var_table, g_strdup(varname), g_strdup(text));
+       } else {
+               /* don't free the one in hashtable at the end */
+               text = g_strdup(text);
        }
+
+       if (!text)
+               return;
+       INSERT(text);
+       g_free(text);
 }
 
 %}
@@ -392,17 +485,27 @@ static void quote_fmt_insert_program_output(const gchar *progname)
        char str[256];
 }
 
+/* tokens SHOW */
 %token SHOW_NEWSGROUPS
 %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
 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
-%token SHOW_EOL SHOW_QUESTION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
+%token SHOW_EOL SHOW_QUESTION_MARK SHOW_EXCLAMATION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
+%token SHOW_ACCOUNT_FULL_NAME SHOW_ACCOUNT_MAIL_ADDRESS SHOW_ACCOUNT_NAME SHOW_ACCOUNT_ORGANIZATION
+/* tokens QUERY */
 %token QUERY_DATE QUERY_FROM
 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
-%token INSERT_FILE INSERT_PROGRAMOUTPUT
+%token QUERY_ACCOUNT_FULL_NAME QUERY_ACCOUNT_ORGANIZATION
+/* tokens QUERY_NOT */
+%token QUERY_NOT_DATE QUERY_NOT_FROM
+%token QUERY_NOT_FULLNAME QUERY_NOT_SUBJECT QUERY_NOT_TO QUERY_NOT_NEWSGROUPS
+%token QUERY_NOT_MESSAGEID QUERY_NOT_CC QUERY_NOT_REFERENCES
+%token QUERY_NOT_ACCOUNT_FULL_NAME QUERY_NOT_ACCOUNT_ORGANIZATION
+/* other tokens */
+%token INSERT_FILE INSERT_PROGRAMOUTPUT INSERT_USERINPUT
 %token OPARENT CPARENT
 %token CHARACTER
 %token SHOW_DATE_EXPR
@@ -417,20 +520,31 @@ static void quote_fmt_insert_program_output(const gchar *progname)
 %%
 
 quote_fmt:
-       character_or_special_or_insert_or_query_list;
+       character_or_special_or_insert_or_query_list ;
+
+sub_expr:
+       character_or_special_list ;
 
 character_or_special_or_insert_or_query_list:
        character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
        | character_or_special_or_insert_or_query ;
 
+character_or_special_list:
+       character_or_special character_or_special_list
+       | character_or_special ;
+
 character_or_special_or_insert_or_query:
+       character_or_special
+       | query
+       | query_not
+       | insert ;
+
+character_or_special:
        special
        | character
        {
                INSERT_CHARACTER($1);
-       }
-       | query
-       | insert ;
+       };
 
 character:
        CHARACTER
@@ -485,7 +599,7 @@ special:
                quote_fmt_show_first_name(msginfo);
        }
        | SHOW_LAST_NAME
-        {
+    {
                quote_fmt_show_last_name(msginfo);
        }
        | SHOW_SENDER_INITIAL
@@ -518,9 +632,12 @@ special:
        }
        | SHOW_REFERENCES
        {
-                /* CLAWS: use in reply to header */
-               /* if (msginfo->references)
-                       INSERT(msginfo->references); */
+               GSList *item;
+
+               INSERT(msginfo->inreplyto);
+               for (item = msginfo->references; item != NULL; item = g_slist_next(item))
+                       if (item->data)
+                               INSERT(item->data);
        }
        | SHOW_MESSAGE
        {
@@ -538,6 +655,26 @@ special:
        {
                quote_fmt_show_msg(msginfo, body, TRUE, FALSE, quote_str);
        }
+       | SHOW_ACCOUNT_FULL_NAME
+       {
+               if (account && account->name)
+                       INSERT(account->name);
+       }
+       | SHOW_ACCOUNT_MAIL_ADDRESS
+       {
+               if (account && account->address)
+                       INSERT(account->address);
+       }
+       | SHOW_ACCOUNT_NAME
+       {
+               if (account && account->account_name)
+                       INSERT(account->account_name);
+       }
+       | SHOW_ACCOUNT_ORGANIZATION
+       {
+               if (account && account->organization)
+                       INSERT(account->organization);
+       }
        | SHOW_BACKSLASH
        {
                INSERT("\\");
@@ -554,6 +691,10 @@ special:
        {
                INSERT("?");
        }
+       | SHOW_EXCLAMATION_MARK
+       {
+               INSERT("!");
+       }
        | SHOW_PIPE
        {
                INSERT("|");
@@ -568,7 +709,7 @@ special:
        }
        | SET_CURSOR_POS
        {
-               cursor_pos = bufsize;
+               cursor_pos = current->bufsize;
        };
 
 query:
@@ -638,8 +779,127 @@ query:
        }
        | QUERY_REFERENCES
        {
-                /* CLAWS: use in-reply-to header */
-               /* add_visibility(msginfo->references != NULL); */
+               gboolean found;
+               GSList *item;
+
+               found = (msginfo->inreplyto != NULL);
+               for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
+                       if (item->data)
+                               found = TRUE;
+               add_visibility(found == TRUE);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_ACCOUNT_FULL_NAME
+       {
+               add_visibility(account != NULL && account->name != NULL);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_ACCOUNT_ORGANIZATION
+       {
+               add_visibility(account != NULL && account->organization != NULL);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       };
+
+query_not:
+       QUERY_NOT_DATE
+       {
+               add_visibility(msginfo->date == NULL);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_NOT_FROM
+       {
+               add_visibility(msginfo->from == NULL);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_NOT_FULLNAME
+       {
+               add_visibility(msginfo->fromname == NULL);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_NOT_SUBJECT
+       {
+               add_visibility(msginfo->subject == NULL);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_NOT_TO
+       {
+               add_visibility(msginfo->to == NULL);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_NOT_NEWSGROUPS
+       {
+               add_visibility(msginfo->newsgroups == NULL);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_NOT_MESSAGEID
+       {
+               add_visibility(msginfo->msgid == NULL);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_NOT_CC
+       {
+               add_visibility(msginfo->cc == NULL);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_NOT_REFERENCES
+       {
+               gboolean found;
+               GSList *item;
+
+               found = (msginfo->inreplyto != NULL);
+               for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
+                       if (item->data)
+                               found = TRUE;
+               add_visibility(found == FALSE);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_NOT_ACCOUNT_FULL_NAME
+       {
+               add_visibility(account == NULL || account->name == NULL);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_NOT_ACCOUNT_ORGANIZATION
+       {
+               add_visibility(account == NULL || account->organization == NULL);
        }
        OPARENT quote_fmt CPARENT
        {
@@ -647,15 +907,39 @@ query:
        };
 
 insert:
-       INSERT_FILE OPARENT string CPARENT
+       INSERT_FILE
+       {
+               current = &sub_expr;
+               clear_buffer();
+       }
+       OPARENT sub_expr CPARENT
        {
+               current = &main_expr;
                if (!dry_run) {
-                       quote_fmt_insert_file($3);
+                       quote_fmt_insert_file(sub_expr.buffer);
                }
        }
-       | INSERT_PROGRAMOUTPUT OPARENT string CPARENT
+       | INSERT_PROGRAMOUTPUT
+       {
+               current = &sub_expr;
+               clear_buffer();
+       }
+       OPARENT sub_expr CPARENT
+       {
+               current = &main_expr;
+               if (!dry_run) {
+                       quote_fmt_insert_program_output(sub_expr.buffer);
+               }
+       }
+       | INSERT_USERINPUT
+       {
+               current = &sub_expr;
+               clear_buffer();
+       }
+       OPARENT sub_expr CPARENT
        {
+               current = &main_expr;
                if (!dry_run) {
-                       quote_fmt_insert_program_output($3);
+                       quote_fmt_insert_user_input(sub_expr.buffer);
                }
        };