2008-09-12 [colin] 3.5.0cvs103
[claws.git] / src / quote_fmt_parse.y
index b9829c9f8a5096c96eee91ef0e7e61c950b0fbac..762120c23ed0a5add218ea81ac3775a09041d50a 100644 (file)
@@ -4,7 +4,7 @@
  *
  * 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
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 %{
@@ -31,6 +31,7 @@
 #include "utils.h"
 #include "codeconv.h"
 #include "procheader.h"
+#include "addr_compl.h"
 #include "gtk/inputdialog.h"
 
 #include "quote_fmt.h"
@@ -46,11 +47,15 @@ int yylex(void);
 
 static MsgInfo *msginfo = NULL;
 static PrefsAccount *account = NULL;
+#ifdef USE_ENCHANT
+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;
+static GList *attachments = NULL;
 
 typedef struct st_buffer
 {
@@ -70,6 +75,8 @@ static gint error = 0;
 static gint cursor_pos = -1;
 
 extern int quote_fmt_firsttime;
+extern int line;
+extern int escaped_string;
 
 static void add_visibility(gboolean val)
 {
@@ -133,6 +140,16 @@ gchar *quote_fmt_get_buffer(void)
                return current->buffer;
 }
 
+GList *quote_fmt_get_attachments_list(void)
+{
+       return attachments;
+}
+
+gint quote_fmt_get_line(void)
+{
+       return line;
+}
+
 gint quote_fmt_get_cursor_pos(void)
 {
        return cursor_pos;      
@@ -156,16 +173,41 @@ void quote_fmt_reset_vartable(void)
                g_hash_table_destroy(var_table);
                var_table = NULL;
        }
+       if (attachments) {
+               GList *cur = attachments;
+               while (cur) {
+                       g_free(cur->data);
+                       cur = g_list_next(cur);
+               }
+               g_list_free(attachments);
+               attachments = NULL;
+       }
 }
 
+#ifdef USE_ENCHANT
 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
                    const gchar *my_body, gboolean my_dry_run,
-                       PrefsAccount *compose_account)
+                       PrefsAccount *compose_account,
+                       gboolean string_is_escaped,
+                       GtkAspell *compose_gtkaspell)
+#else
+void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
+                   const gchar *my_body, gboolean my_dry_run,
+                       PrefsAccount *compose_account,
+                       gboolean string_is_escaped)
+#endif
 {
        quote_str = my_quote_str;
        body = my_body;
        msginfo = info;
        account = compose_account;
+#ifdef USE_ENCHANT
+       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);
@@ -174,6 +216,8 @@ void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
        current = &main_expr;
        clear_buffer();
        error = 0;
+       line = 1;
+       escaped_string = string_is_escaped;
 
        if (!var_table)
                var_table = g_hash_table_new_full(g_str_hash, g_str_equal, 
@@ -188,7 +232,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;
 }
 
@@ -401,7 +445,7 @@ static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
        if (body)
                fp = str_open_as_stream(body);
        else {
-               if (procmime_msginfo_is_encrypted(msginfo))
+               if (MSG_IS_ENCRYPTED(msginfo->flags))
                        fp = procmime_get_first_encrypted_text_content(msginfo);
                else
                        fp = procmime_get_first_text_content(msginfo);
@@ -478,6 +522,49 @@ static void quote_fmt_insert_user_input(const gchar *varname)
        g_free(text);
 }
 
+static void quote_fmt_attach_file(const gchar *filename)
+{
+       attachments = g_list_append(attachments, g_strdup(filename));
+}
+
+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 {
@@ -490,22 +577,36 @@ static void quote_fmt_insert_user_input(const gchar *varname)
 %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 SHOW_TAGS
+%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
+%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 QUERY_NOT_ACCOUNT_FULL_NAME QUERY_NOT_ACCOUNT_ORGANIZATION
+%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 ATTACH_FILE
 %token OPARENT CPARENT
 %token CHARACTER
 %token SHOW_DATE_EXPR
@@ -537,7 +638,8 @@ character_or_special_or_insert_or_query:
        character_or_special
        | query
        | query_not
-       | insert ;
+       | insert
+       | attach ;
 
 character_or_special:
        special
@@ -558,7 +660,7 @@ string:
        }
        | string CHARACTER
        {
-               int len;
+               size_t len;
                
                strncpy($$, $1, sizeof($$));
                $$[sizeof($$) - 1] = '\0';
@@ -589,6 +691,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)
@@ -675,6 +786,30 @@ special:
                if (account && account->organization)
                        INSERT(account->organization);
        }
+       | SHOW_ACCOUNT_DICT
+       {
+#ifdef USE_ENCHANT
+               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_ENCHANT
+               INSERT(default_dictionary);
+#endif
+       }
+       | SHOW_TAGS
+       {
+               gchar *tags = procmsg_msginfo_get_tags_str(msginfo);
+               if (tags) {
+                       INSERT(tags);
+               }
+               g_free(tags);
+       }
        | SHOW_BACKSLASH
        {
                INSERT("\\");
@@ -709,7 +844,34 @@ special:
        }
        | SET_CURSOR_POS
        {
-               cursor_pos = current->bufsize;
+               if (current->buffer)
+                       cursor_pos = g_utf8_strlen(current->buffer, -1);
+               else
+                       cursor_pos = 0;
+       }
+       | 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:
@@ -805,6 +967,61 @@ query:
                add_visibility(account != NULL && account->organization != NULL);
        }
        OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_ACCOUNT_DICT
+       {
+#ifdef USE_ENCHANT
+               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_ENCHANT
+               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();
        };
@@ -902,6 +1119,61 @@ query_not:
                add_visibility(account == NULL || account->organization == NULL);
        }
        OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_NOT_ACCOUNT_DICT
+       {
+#ifdef USE_ENCHANT
+               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_ENCHANT
+               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();
        };
@@ -943,3 +1215,17 @@ insert:
                        quote_fmt_insert_user_input(sub_expr.buffer);
                }
        };
+
+attach:
+       ATTACH_FILE
+       {
+               current = &sub_expr;
+               clear_buffer();
+       }
+       OPARENT sub_expr CPARENT
+       {
+               current = &main_expr;
+               if (!dry_run) {
+                       quote_fmt_attach_file(sub_expr.buffer);
+               }
+       };