2007-05-23 [wwp] 2.9.2cvs12
[claws.git] / src / quote_fmt_parse.y
index ab0427e8f29b8ca78fb7068df39cf87d01a72751..6d0556af3b4b3d613c1ae8f5442ad3f576000af4 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws Team
+ * Copyright (C) 1999-2007 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
@@ -22,6 +22,8 @@
 #include "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
+
 #include <ctype.h>
 
 #include "procmsg.h"
@@ -29,6 +31,8 @@
 #include "utils.h"
 #include "codeconv.h"
 #include "procheader.h"
+#include "addr_compl.h"
+#include "gtk/inputdialog.h"
 
 #include "quote_fmt.h"
 #include "quote_fmt_lex.h"
@@ -42,10 +46,15 @@ bison -p quote_fmt quote_fmt.y
 int yylex(void);
 
 static MsgInfo *msginfo = NULL;
+static PrefsAccount *account = NULL;
+#ifdef USE_ASPELL
+static gchar default_dictionary[BUFFSIZE];
+#endif
 static gboolean *visible = NULL;
 static gboolean dry_run = FALSE;
 static gint maxsize = 0;
 static gint stacksize = 0;
+static GHashTable *var_table = NULL;
 
 typedef struct st_buffer
 {
@@ -65,6 +74,7 @@ static gint error = 0;
 static gint cursor_pos = -1;
 
 extern int quote_fmt_firsttime;
+extern int line;
 
 static void add_visibility(gboolean val)
 {
@@ -111,6 +121,9 @@ 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;
 }
 
@@ -142,12 +155,36 @@ 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;
+       }
+}
+
+#ifdef USE_ASPELL
+void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
+                   const gchar *my_body, gboolean my_dry_run,
+                       PrefsAccount *compose_account,
+                       GtkAspell *compose_gtkaspell)
+#else
 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)
+#endif
 {
        quote_str = my_quote_str;
        body = my_body;
        msginfo = info;
+       account = compose_account;
+#ifdef USE_ASPELL
+       gchar *dict = gtkaspell_get_default_dictionary(compose_gtkaspell);
+       if (dict)
+               strncpy2(default_dictionary, dict, sizeof(default_dictionary));
+       else
+               *default_dictionary = '\0';
+#endif
        dry_run = my_dry_run;
        stacksize = 0;
        add_visibility(TRUE);
@@ -156,6 +193,12 @@ void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
        current = &main_expr;
        clear_buffer();
        error = 0;
+       line = 1;
+
+       if (!var_table)
+               var_table = g_hash_table_new_full(g_str_hash, g_str_equal, 
+                               g_free, g_free);
+
         /*
          * force LEX initialization
          */
@@ -165,7 +208,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 at line %d\n", str, line);
        error = 1;
 }
 
@@ -429,6 +472,70 @@ static void quote_fmt_insert_program_output(const gchar *progname)
        }
 }
 
+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);
+}
+
+static gchar *quote_fmt_complete_address(const gchar *addr)
+{
+       gint count;
+       gchar *res, *tmp, *email_addr;
+       gchar **split;
+
+       debug_print("quote_fmt_complete_address: %s\n", addr);
+       if (addr == NULL)
+               return NULL;
+
+       /* if addr is a list of message, try the 1st element only */
+       split = g_strsplit(addr, ",", -1);
+       if (!split || !split[0] || *split[0] == '\0') {
+               g_strfreev(split);
+               return NULL;
+       }
+
+       Xstrdup_a(email_addr, split[0], return NULL);
+       extract_address(email_addr);
+       if (!*email_addr) {
+               g_strfreev(split);
+               return NULL;
+       }
+
+       res = NULL;
+       start_address_completion(NULL);
+       if (1 < (count = complete_address(email_addr))) {
+               tmp = get_complete_address(1);
+               res = procheader_get_fromname(tmp);
+               g_free(tmp);
+       }
+       end_address_completion();
+       g_strfreev(split);
+
+       debug_print("quote_fmt_complete_address: matched %s\n", res);
+       return res;
+}
+
 %}
 
 %union {
@@ -436,20 +543,40 @@ 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 SHOW_BACKSLASH SHOW_TAB SHOW_MAIL_ADDRESS
 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
 %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
+%token SHOW_ACCOUNT_DICT
+%token SHOW_DICT
+%token SHOW_ADDRESSBOOK_COMPLETION_FOR_CC
+%token SHOW_ADDRESSBOOK_COMPLETION_FOR_FROM
+%token SHOW_ADDRESSBOOK_COMPLETION_FOR_TO
+/* tokens QUERY */
 %token QUERY_DATE QUERY_FROM
 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
+%token QUERY_ACCOUNT_FULL_NAME QUERY_ACCOUNT_ORGANIZATION QUERY_ACCOUNT_DICT
+%token QUERY_DICT
+%token QUERY_CC_FOUND_IN_ADDRESSBOOK
+%token QUERY_FROM_FOUND_IN_ADDRESSBOOK
+%token QUERY_TO_FOUND_IN_ADDRESSBOOK
+/* 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 INSERT_FILE INSERT_PROGRAMOUTPUT
+%token QUERY_NOT_ACCOUNT_FULL_NAME QUERY_NOT_ACCOUNT_ORGANIZATION QUERY_NOT_ACCOUNT_DICT
+%token QUERY_NOT_DICT
+%token QUERY_NOT_CC_FOUND_IN_ADDRESSBOOK
+%token QUERY_NOT_FROM_FOUND_IN_ADDRESSBOOK
+%token QUERY_NOT_TO_FOUND_IN_ADDRESSBOOK
+/* other tokens */
+%token INSERT_FILE INSERT_PROGRAMOUTPUT INSERT_USERINPUT
 %token OPARENT CPARENT
 %token CHARACTER
 %token SHOW_DATE_EXPR
@@ -533,6 +660,15 @@ special:
                if (msginfo->from)
                        INSERT(msginfo->from);
        }
+       | SHOW_MAIL_ADDRESS
+       {
+               if (msginfo->from) {
+                       gchar *stripped_address = g_strdup(msginfo->from);
+                       extract_address(stripped_address);
+                       INSERT(stripped_address);
+                       g_free(stripped_address);
+               }
+       }
        | SHOW_FULLNAME
        {
                if (msginfo->fromname)
@@ -599,6 +735,42 @@ 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_ACCOUNT_DICT
+       {
+#ifdef USE_ASPELL
+               if (account && account->enable_default_dictionary) {
+                       gchar *dictname = g_path_get_basename(account->default_dictionary);
+                       INSERT(dictname);
+                       g_free(dictname);
+               }
+#endif
+       }
+       | SHOW_DICT
+       {
+#ifdef USE_ASPELL
+               INSERT(default_dictionary);
+#endif
+       }
        | SHOW_BACKSLASH
        {
                INSERT("\\");
@@ -634,6 +806,30 @@ special:
        | SET_CURSOR_POS
        {
                cursor_pos = current->bufsize;
+       }
+       | SHOW_ADDRESSBOOK_COMPLETION_FOR_CC
+       {
+               gchar *tmp = quote_fmt_complete_address(msginfo->cc);
+               if (tmp) {
+                       INSERT(tmp);
+                       g_free(tmp);
+               }
+       }
+       | SHOW_ADDRESSBOOK_COMPLETION_FOR_FROM
+       {
+               gchar *tmp = quote_fmt_complete_address(msginfo->from);
+               if (tmp) {
+                       INSERT(tmp);
+                       g_free(tmp);
+               }
+       }
+       | SHOW_ADDRESSBOOK_COMPLETION_FOR_TO
+       {
+               gchar *tmp = quote_fmt_complete_address(msginfo->to);
+               if (tmp) {
+                       INSERT(tmp);
+                       g_free(tmp);
+               }
        };
 
 query:
@@ -713,6 +909,77 @@ query:
                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_ACCOUNT_DICT
+       {
+#ifdef USE_ASPELL
+               add_visibility(account != NULL && account->enable_default_dictionary == TRUE &&
+                               account->default_dictionary != NULL && *account->default_dictionary != '\0');
+#else
+               add_visibility(FALSE);
+#endif
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_DICT
+       {
+#ifdef USE_ASPELL
+               add_visibility(*default_dictionary != '\0');
+#else
+               add_visibility(FALSE);
+#endif
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_CC_FOUND_IN_ADDRESSBOOK
+       {
+               gchar *tmp = quote_fmt_complete_address(msginfo->cc);
+               add_visibility(tmp != NULL && *tmp != '\0');
+               g_free(tmp);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_FROM_FOUND_IN_ADDRESSBOOK
+       {
+               gchar *tmp = quote_fmt_complete_address(msginfo->from);
+               add_visibility(tmp != NULL && *tmp != '\0');
+               g_free(tmp);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_TO_FOUND_IN_ADDRESSBOOK
+       {
+               gchar *tmp = quote_fmt_complete_address(msginfo->to);
+               add_visibility(tmp != NULL && *tmp != '\0');
+               g_free(tmp);
+       }
+       OPARENT quote_fmt CPARENT
        {
                remove_visibility();
        };
@@ -794,6 +1061,77 @@ query_not:
                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
+       {
+               remove_visibility();
+       }
+       | QUERY_NOT_ACCOUNT_DICT
+       {
+#ifdef USE_ASPELL
+               add_visibility(account == NULL || account->enable_default_dictionary == FALSE
+                               || *account->default_dictionary == '\0');
+#else
+               add_visibility(FALSE);
+#endif
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_NOT_DICT
+       {
+#ifdef USE_ASPELL
+               add_visibility(*default_dictionary == '\0');
+#else
+               add_visibility(FALSE);
+#endif
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_NOT_CC_FOUND_IN_ADDRESSBOOK
+       {
+               gchar *tmp = quote_fmt_complete_address(msginfo->cc);
+               add_visibility(tmp == NULL || *tmp == '\0');
+               g_free(tmp);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_NOT_FROM_FOUND_IN_ADDRESSBOOK
+       {
+               gchar *tmp = quote_fmt_complete_address(msginfo->from);
+               add_visibility(tmp == NULL || *tmp == '\0');
+               g_free(tmp);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_NOT_TO_FOUND_IN_ADDRESSBOOK
+       {
+               gchar *tmp = quote_fmt_complete_address(msginfo->to);
+               add_visibility(tmp == NULL || *tmp == '\0');
+               g_free(tmp);
+       }
+       OPARENT quote_fmt CPARENT
        {
                remove_visibility();
        };
@@ -822,4 +1160,16 @@ insert:
                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_user_input(sub_expr.buffer);
+               }
        };