Fix bug #3263 ‘Wrong timezone while using date_fmt function’
[claws.git] / src / quote_fmt_parse.y
index fa72cd9318b97cc3ecaa91bbb6e91a43b0a1192d..af7f421462c7409410c97b0bd563757c1e9be644 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/>.
+ * 
  */
 
 %{
@@ -36,6 +36,7 @@
 
 #include "quote_fmt.h"
 #include "quote_fmt_lex.h"
+#include "account.h"
 
 /* decl */
 /*
@@ -47,7 +48,7 @@ int yylex(void);
 
 static MsgInfo *msginfo = NULL;
 static PrefsAccount *account = NULL;
-#ifdef USE_ASPELL
+#ifdef USE_ENCHANT
 static gchar default_dictionary[BUFFSIZE];
 #endif
 static gboolean *visible = NULL;
@@ -55,6 +56,7 @@ 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
 {
@@ -75,6 +77,7 @@ static gint cursor_pos = -1;
 
 extern int quote_fmt_firsttime;
 extern int line;
+extern int escaped_string;
 
 static void add_visibility(gboolean val)
 {
@@ -85,15 +88,15 @@ static void add_visibility(gboolean val)
                if (visible == NULL)
                        maxsize = 0;
        }
-
-       visible[stacksize - 1] = val;
+       if (visible != NULL)
+               visible[stacksize - 1] = val;
 }
 
 static void remove_visibility(void)
 {
        stacksize--;
        if (stacksize < 0) {
-               g_warning("Error: visibility stack underflow\n");
+               g_warning("Error: visibility stack underflow");
                stacksize = 0;
        }
 }
@@ -130,7 +133,7 @@ static void clear_buffer(void)
 gchar *quote_fmt_get_buffer(void)
 {
        if (current != &main_expr)
-               g_warning("Error: parser still in sub-expr mode\n");
+               g_warning("Error: parser still in sub-expr mode");
 
        if (error != 0)
                return NULL;
@@ -138,6 +141,11 @@ 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;
@@ -166,24 +174,35 @@ 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_ASPELL
+#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,
+                       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)
+                       PrefsAccount *compose_account,
+                       gboolean string_is_escaped)
 #endif
 {
        quote_str = my_quote_str;
        body = my_body;
        msginfo = info;
        account = compose_account;
-#ifdef USE_ASPELL
+#ifdef USE_ENCHANT
        gchar *dict = gtkaspell_get_default_dictionary(compose_gtkaspell);
        if (dict)
                strncpy2(default_dictionary, dict, sizeof(default_dictionary));
@@ -199,6 +218,7 @@ void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
        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, 
@@ -213,7 +233,7 @@ void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
 
 void quote_fmterror(char *str)
 {
-       g_warning("Error: %s at line %d\n", str, line);
+       g_warning("Error: %s at line %d", str, line);
        error = 1;
 }
 
@@ -227,6 +247,40 @@ static int isseparator(int ch)
        return g_ascii_isspace(ch) || ch == '.' || ch == '-';
 }
 
+/*
+ * Search for glibc extended strftime timezone specs within haystack.
+ * If not found NULL is returned and the integer pointed by tzspeclen is
+ * not changed.
+ * If found a pointer to the start of the specification within haystack
+ * is returned and the integer pointed by tzspeclen is set to the lenght
+ * of specification.
+ */
+static const char* strtzspec(const char *haystack, int *tzspeclen)
+{
+       const char *p = NULL;
+       const char *q = NULL;
+       const char *r = NULL;
+
+       p = strstr(haystack, "%");
+       while (p != NULL) {
+               q = p + 1;
+               if (!*q) return NULL;
+               r = strchr("_-0^#", *q); /* skip flags */
+               if (r != NULL) {
+                       ++q;
+                       if (!*q) return NULL;
+               }
+               while (*q >= '0' && *q <= '9') ++q; /* skip width */
+               if (!*q) return NULL;
+               if (*q == 'z' || *q == 'Z') { /* numeric or name */
+                       *tzspeclen = 1 + (q - p);
+                       return p;
+               }
+               p = strstr(q, "%");
+       }
+       return NULL;
+}
+
 static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
 {
        char  result[100];
@@ -246,7 +300,6 @@ static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
         */
 
 #define RLEFT (sizeof result) - (rptr - result)        
-#define STR_SIZE(x) (sizeof (x) - 1)
 
        zone[0] = 0;
 
@@ -256,11 +309,11 @@ static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
                 * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
                 */
                for (rptr = result, fptr = format; fptr && *fptr && rptr < &result[sizeof result - 1];) {
-                       int         perc;
+                       int         perc, zlen;
                        const char *p;
                        char       *tmp;
                        
-                       if (NULL != (zptr = strstr(fptr, "%z"))) {
+                       if (NULL != (zptr = strtzspec(fptr, &zlen))) {
                                /*
                                 * count nr. of prepended percent chars
                                 */
@@ -269,7 +322,7 @@ static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
                                /*
                                 * feed to strftime()
                                 */
-                               tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
+                               tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : zlen));
                                if (tmp) {
                                        rptr += strftime(rptr, RLEFT, tmp, &lt);
                                        g_free(tmp);
@@ -279,7 +332,7 @@ static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
                                 */
                                if (zone[0] && perc % 2) 
                                        rptr += g_snprintf(rptr, RLEFT, "%s", zone);
-                               fptr = zptr + STR_SIZE("%z");
+                               fptr = zptr + zlen;
                        } else {
                                rptr += strftime(rptr, RLEFT, fptr, &lt);
                                fptr  = NULL;
@@ -305,7 +358,6 @@ static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
                        g_free(utf);
                }
        }
-#undef STR_SIZE                        
 #undef RLEFT                   
 }              
 
@@ -426,14 +478,14 @@ 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);
        }
 
        if (fp == NULL)
-               g_warning("Can't get text part\n");
+               g_warning("Can't get text part");
        else {
                while (fgets(buf, sizeof(buf), fp) != NULL) {
                        strcrchomp(buf);
@@ -503,6 +555,11 @@ 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;
@@ -557,8 +614,8 @@ static gchar *quote_fmt_complete_address(const gchar *addr)
 %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_ACCOUNT_DICT SHOW_ACCOUNT_SIG SHOW_ACCOUNT_SIGPATH
+%token SHOW_DICT SHOW_TAGS
 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_CC
 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_FROM
 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_TO
@@ -567,6 +624,7 @@ static gchar *quote_fmt_complete_address(const gchar *addr)
 %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_ACCOUNT_SIG QUERY_ACCOUNT_SIGPATH
 %token QUERY_DICT
 %token QUERY_CC_FOUND_IN_ADDRESSBOOK
 %token QUERY_FROM_FOUND_IN_ADDRESSBOOK
@@ -576,12 +634,14 @@ static gchar *quote_fmt_complete_address(const gchar *addr)
 %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 QUERY_NOT_ACCOUNT_DICT
+%token QUERY_NOT_ACCOUNT_SIG QUERY_NOT_ACCOUNT_SIGPATH
 %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
@@ -589,7 +649,7 @@ static gchar *quote_fmt_complete_address(const gchar *addr)
 
 %start quote_fmt
 
-%token <chr> CHARACTER
+%type <chr> CHARACTER
 %type <chr> character
 %type <str> string
 
@@ -613,7 +673,8 @@ character_or_special_or_insert_or_query:
        character_or_special
        | query
        | query_not
-       | insert ;
+       | insert
+       | attach ;
 
 character_or_special:
        special
@@ -634,7 +695,7 @@ string:
        }
        | string CHARACTER
        {
-               int len;
+               size_t len;
                
                strncpy($$, $1, sizeof($$));
                $$[sizeof($$) - 1] = '\0';
@@ -760,9 +821,20 @@ special:
                if (account && account->organization)
                        INSERT(account->organization);
        }
+       | SHOW_ACCOUNT_SIG
+       {
+               gchar *str = account_get_signature_str(account);
+               INSERT(str);
+               g_free(str);
+       }
+       | SHOW_ACCOUNT_SIGPATH
+       {
+               if (account && account->sig_path)
+                       INSERT(account->sig_path);
+       }
        | SHOW_ACCOUNT_DICT
        {
-#ifdef USE_ASPELL
+#ifdef USE_ENCHANT
                if (account && account->enable_default_dictionary) {
                        gchar *dictname = g_path_get_basename(account->default_dictionary);
                        INSERT(dictname);
@@ -772,10 +844,18 @@ special:
        }
        | SHOW_DICT
        {
-#ifdef USE_ASPELL
+#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("\\");
@@ -810,7 +890,10 @@ 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
        {
@@ -933,9 +1016,28 @@ query:
        {
                remove_visibility();
        }
+       | QUERY_ACCOUNT_SIG
+       {
+               gchar *str = account_get_signature_str(account);
+               add_visibility(str != NULL && * str != '\0');
+               g_free(str);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_ACCOUNT_SIGPATH
+       {
+               add_visibility(account != NULL && account->sig_path != NULL
+                               && *account->sig_path != '\0');
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
        | QUERY_ACCOUNT_DICT
        {
-#ifdef USE_ASPELL
+#ifdef USE_ENCHANT
                add_visibility(account != NULL && account->enable_default_dictionary == TRUE &&
                                account->default_dictionary != NULL && *account->default_dictionary != '\0');
 #else
@@ -948,7 +1050,7 @@ query:
        }
        | QUERY_DICT
        {
-#ifdef USE_ASPELL
+#ifdef USE_ENCHANT
                add_visibility(*default_dictionary != '\0');
 #else
                add_visibility(FALSE);
@@ -1085,9 +1187,28 @@ query_not:
        {
                remove_visibility();
        }
+       | QUERY_NOT_ACCOUNT_SIG
+       {
+               gchar *str = account_get_signature_str(account);
+               add_visibility(str == NULL || *str == '\0');
+               g_free(str);
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
+       | QUERY_NOT_ACCOUNT_SIGPATH
+       {
+               add_visibility(account == NULL || account->sig_path == NULL
+                               || *account->sig_path == '\0');
+       }
+       OPARENT quote_fmt CPARENT
+       {
+               remove_visibility();
+       }
        | QUERY_NOT_ACCOUNT_DICT
        {
-#ifdef USE_ASPELL
+#ifdef USE_ENCHANT
                add_visibility(account == NULL || account->enable_default_dictionary == FALSE
                                || *account->default_dictionary == '\0');
 #else
@@ -1100,7 +1221,7 @@ query_not:
        }
        | QUERY_NOT_DICT
        {
-#ifdef USE_ASPELL
+#ifdef USE_ENCHANT
                add_visibility(*default_dictionary == '\0');
 #else
                add_visibility(FALSE);
@@ -1178,3 +1299,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);
+               }
+       };