03c872a67679aab4b05d41cd9712cc67ec9d8b99
[claws.git] / src / quote_fmt_parse.y
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail Team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 %{
21
22 #include "defs.h"
23
24 #include <glib.h>
25 #include <glib/gi18n.h>
26
27 #include <ctype.h>
28
29 #include "procmsg.h"
30 #include "procmime.h"
31 #include "utils.h"
32 #include "codeconv.h"
33 #include "procheader.h"
34 #include "addr_compl.h"
35 #include "gtk/inputdialog.h"
36
37 #include "quote_fmt.h"
38 #include "quote_fmt_lex.h"
39 #include "account.h"
40
41 /* decl */
42 /*
43 flex quote_fmt.l
44 bison -p quote_fmt quote_fmt.y
45 */
46
47 int yylex(void);
48
49 static MsgInfo *msginfo = NULL;
50 static PrefsAccount *account = NULL;
51 #ifdef USE_ENCHANT
52 static gchar default_dictionary[BUFFSIZE];
53 #endif
54 static gboolean *visible = NULL;
55 static gboolean dry_run = FALSE;
56 static gint maxsize = 0;
57 static gint stacksize = 0;
58 static GHashTable *var_table = NULL;
59 static GList *attachments = NULL;
60
61 typedef struct st_buffer
62 {
63         gchar *buffer;
64         gint bufsize;
65         gint bufmax;
66 } st_buffer;
67
68 static struct st_buffer main_expr = { NULL, 0, 0 };
69 static struct st_buffer sub_expr = { NULL, 0, 0 };
70 static struct st_buffer* current = NULL;
71
72 static const gchar *quote_str = NULL;
73 static const gchar *body = NULL;
74 static gint error = 0;
75
76 static gint cursor_pos = -1;
77
78 extern int quote_fmt_firsttime;
79 extern int line;
80 extern int escaped_string;
81
82 static void add_visibility(gboolean val)
83 {
84         stacksize++;
85         if (maxsize < stacksize) {
86                 maxsize += 128;
87                 visible = g_realloc(visible, maxsize * sizeof(gboolean));
88                 if (visible == NULL)
89                         maxsize = 0;
90         }
91         if (visible != NULL)
92                 visible[stacksize - 1] = val;
93 }
94
95 static void remove_visibility(void)
96 {
97         stacksize--;
98         if (stacksize < 0) {
99                 g_warning("Error: visibility stack underflow");
100                 stacksize = 0;
101         }
102 }
103
104 static void add_buffer(const gchar *s)
105 {
106         gint len;
107
108         if (s == NULL)
109                 return;
110
111         len = strlen(s);
112         if (current->bufsize + len + 1 > current->bufmax) {
113                 if (current->bufmax == 0)
114                         current->bufmax = 128;
115                 while (current->bufsize + len + 1 > current->bufmax)
116                         current->bufmax *= 2;
117                 current->buffer = g_realloc(current->buffer, current->bufmax);
118         }
119         strcpy(current->buffer + current->bufsize, s);
120         current->bufsize += len;
121 }
122
123 static void clear_buffer(void)
124 {
125         if (current->buffer)
126                 *current->buffer = '\0';
127         else
128                 /* force to an empty string, as buffer should not be left unallocated */
129                 add_buffer("");
130         current->bufsize = 0;
131 }
132
133 gchar *quote_fmt_get_buffer(void)
134 {
135         if (current != &main_expr)
136                 g_warning("Error: parser still in sub-expr mode");
137
138         if (error != 0)
139                 return NULL;
140         else
141                 return current->buffer;
142 }
143
144 GList *quote_fmt_get_attachments_list(void)
145 {
146         return attachments;
147 }
148
149 gint quote_fmt_get_line(void)
150 {
151         return line;
152 }
153
154 gint quote_fmt_get_cursor_pos(void)
155 {
156         return cursor_pos;      
157 }
158
159 #define INSERT(buf) \
160         if (stacksize != 0 && visible[stacksize - 1])\
161                 add_buffer(buf); \
162
163 #define INSERT_CHARACTER(chr) \
164         if (stacksize != 0 && visible[stacksize - 1]) { \
165                 gchar tmp[2]; \
166                 tmp[0] = (chr); \
167                 tmp[1] = '\0'; \
168                 add_buffer(tmp); \
169         }
170
171 void quote_fmt_reset_vartable(void)
172 {
173         if (var_table) {
174                 g_hash_table_destroy(var_table);
175                 var_table = NULL;
176         }
177         if (attachments) {
178                 GList *cur = attachments;
179                 while (cur) {
180                         g_free(cur->data);
181                         cur = g_list_next(cur);
182                 }
183                 g_list_free(attachments);
184                 attachments = NULL;
185         }
186 }
187
188 #ifdef USE_ENCHANT
189 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
190                     const gchar *my_body, gboolean my_dry_run,
191                         PrefsAccount *compose_account,
192                         gboolean string_is_escaped,
193                         GtkAspell *compose_gtkaspell)
194 #else
195 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
196                     const gchar *my_body, gboolean my_dry_run,
197                         PrefsAccount *compose_account,
198                         gboolean string_is_escaped)
199 #endif
200 {
201         quote_str = my_quote_str;
202         body = my_body;
203         msginfo = info;
204         account = compose_account;
205 #ifdef USE_ENCHANT
206         gchar *dict = gtkaspell_get_default_dictionary(compose_gtkaspell);
207         if (dict)
208                 strncpy2(default_dictionary, dict, sizeof(default_dictionary));
209         else
210                 *default_dictionary = '\0';
211 #endif
212         dry_run = my_dry_run;
213         stacksize = 0;
214         add_visibility(TRUE);
215         main_expr.bufmax = 0;
216         sub_expr.bufmax = 0;
217         current = &main_expr;
218         clear_buffer();
219         error = 0;
220         line = 1;
221         escaped_string = string_is_escaped;
222
223         if (!var_table)
224                 var_table = g_hash_table_new_full(g_str_hash, g_str_equal, 
225                                 g_free, g_free);
226
227         /*
228          * force LEX initialization
229          */
230         quote_fmt_firsttime = 1;
231         cursor_pos = -1;
232 }
233
234 void quote_fmterror(char *str)
235 {
236         g_warning("Error: %s at line %d", str, line);
237         error = 1;
238 }
239
240 int quote_fmtwrap(void)
241 {
242         return 1;
243 }
244
245 static int isseparator(int ch)
246 {
247         return g_ascii_isspace(ch) || ch == '.' || ch == '-';
248 }
249
250 /*
251  * Search for glibc extended strftime timezone specs within haystack.
252  * If not found NULL is returned and the integer pointed by tzspeclen is
253  * not changed.
254  * If found a pointer to the start of the specification within haystack
255  * is returned and the integer pointed by tzspeclen is set to the lenght
256  * of specification.
257  */
258 static const char* strtzspec(const char *haystack, int *tzspeclen)
259 {
260         const char *p = NULL;
261         const char *q = NULL;
262         const char *r = NULL;
263
264         p = strstr(haystack, "%");
265         while (p != NULL) {
266                 q = p + 1;
267                 if (!*q) return NULL;
268                 r = strchr("_-0^#", *q); /* skip flags */
269                 if (r != NULL) {
270                         ++q;
271                         if (!*q) return NULL;
272                 }
273                 while (*q >= '0' && *q <= '9') ++q; /* skip width */
274                 if (!*q) return NULL;
275                 if (*q == 'z' || *q == 'Z') { /* numeric or name */
276                         *tzspeclen = 1 + (q - p);
277                         return p;
278                 }
279                 p = strstr(q, "%");
280         }
281         return NULL;
282 }
283
284 static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
285 {
286         char  result[100];
287         char *rptr;
288         char  zone[6];
289         struct tm lt;
290         const char *fptr;
291         const char *zptr;
292
293         if (!msginfo->date)
294                 return;
295         
296         /* 
297          * ALF - GNU C's strftime() has a nice format specifier 
298          * for time zone offset (%z). Non-standard however, so 
299          * emulate it.
300          */
301
302 #define RLEFT (sizeof result) - (rptr - result) 
303
304         zone[0] = 0;
305
306         if (procheader_date_parse_to_tm(msginfo->date, &lt, zone)) {
307                 /*
308                  * break up format string in tiny bits delimited by valid %z's and 
309                  * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
310                  */
311                 for (rptr = result, fptr = format; fptr && *fptr && rptr < &result[sizeof result - 1];) {
312                         int         perc, zlen;
313                         const char *p;
314                         char       *tmp;
315                         
316                         if (NULL != (zptr = strtzspec(fptr, &zlen))) {
317                                 /*
318                                  * count nr. of prepended percent chars
319                                  */
320                                 for (perc = 0, p = zptr; p && p >= format && *p == '%'; p--, perc++)
321                                         ;
322                                 /*
323                                  * feed to strftime()
324                                  */
325                                 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : zlen));
326                                 if (tmp) {
327                                         rptr += strftime(rptr, RLEFT, tmp, &lt);
328                                         g_free(tmp);
329                                 }
330                                 /*
331                                  * append time zone offset
332                                  */
333                                 if (zone[0] && perc % 2) 
334                                         rptr += g_snprintf(rptr, RLEFT, "%s", zone);
335                                 fptr = zptr + zlen;
336                         } else {
337                                 rptr += strftime(rptr, RLEFT, fptr, &lt);
338                                 fptr  = NULL;
339                         }
340                 }
341                 
342                 if (g_utf8_validate(result, -1, NULL)) {
343                         INSERT(result);
344                 } else {
345                         gchar *utf = conv_codeset_strdup(result, 
346                                 conv_get_locale_charset_str_no_utf8(),
347                                 CS_INTERNAL);
348                         if (utf == NULL || 
349                             !g_utf8_validate(utf, -1, NULL)) {
350                                 g_free(utf);
351                                 utf = g_malloc(strlen(result)*2+1);
352                                 conv_localetodisp(utf, 
353                                         strlen(result)*2+1, result);
354                         }
355                         if (g_utf8_validate(utf, -1, NULL)) {
356                                 INSERT(utf);
357                         }
358                         g_free(utf);
359                 }
360         }
361 #undef RLEFT                    
362 }               
363
364 static void quote_fmt_show_first_name(const MsgInfo *msginfo)
365 {
366         guchar *p;
367         gchar *str;
368
369         if (!msginfo->fromname)
370                 return; 
371         
372         p = (guchar*)strchr(msginfo->fromname, ',');
373         if (p != NULL) {
374                 /* fromname is like "Duck, Donald" */
375                 p++;
376                 while (*p && isspace(*p)) p++;
377                 str = alloca(strlen((char *)p) + 1);
378                 if (str != NULL) {
379                         strcpy(str, (char *)p);
380                         INSERT(str);
381                 }
382         } else {
383                 /* fromname is like "Donald Duck" */
384                 str = alloca(strlen(msginfo->fromname) + 1);
385                 if (str != NULL) {
386                         strcpy(str, msginfo->fromname);
387                         p = (guchar *)str;
388                         while (*p && !isspace(*p)) p++;
389                         *p = '\0';
390                         INSERT(str);
391                 }
392         }
393 }
394
395 static void quote_fmt_show_last_name(const MsgInfo *msginfo)
396 {
397         gchar *p;
398         gchar *str;
399
400         /* This probably won't work together very well with Middle
401            names and the like - thth */
402         if (!msginfo->fromname) 
403                 return;
404
405         str = alloca(strlen(msginfo->fromname) + 1);
406         if (str != NULL) {
407                 strcpy(str, msginfo->fromname);
408                 p = strchr(str, ',');
409                 if (p != NULL) {
410                         /* fromname is like "Duck, Donald" */
411                         *p = '\0';
412                         INSERT(str);
413                 } else {
414                         /* fromname is like "Donald Duck" */
415                         p = str;
416                         while (*p && !isspace(*p)) p++;
417                         if (*p) {
418                             /* We found a space. Get first 
419                              none-space char and insert
420                              rest of string from there. */
421                             while (*p && isspace(*p)) p++;
422                             if (*p) {
423                                 INSERT(p);
424                             } else {
425                                 /* If there is no none-space 
426                                  char, just insert whole 
427                                  fromname. */
428                                 INSERT(str);
429                             }
430                         } else {
431                             /* If there is no space, just 
432                              insert whole fromname. */
433                             INSERT(str);
434                         }
435                 }
436         }
437 }
438
439 static void quote_fmt_show_sender_initial(const MsgInfo *msginfo)
440 {
441 #define MAX_SENDER_INITIAL 20
442         gchar tmp[MAX_SENDER_INITIAL];
443         guchar *p;
444         gchar *cur;
445         gint len = 0;
446
447         if (!msginfo->fromname) 
448                 return;
449
450         p = (guchar *)msginfo->fromname;
451         cur = tmp;
452         while (*p) {
453                 if (*p && g_utf8_validate((gchar *)p, 1, NULL)) {
454                         *cur = toupper(*p);
455                                 cur++;
456                         len++;
457                         if (len >= MAX_SENDER_INITIAL - 1)
458                                 break;
459                 } else
460                         break;
461                 while (*p && !isseparator(*p)) p++;
462                 while (*p && isseparator(*p)) p++;
463         }
464         *cur = '\0';
465         INSERT(tmp);
466 }
467
468 static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
469                                gboolean quoted, gboolean signature,
470                                const gchar *quote_str)
471 {
472         gchar buf[BUFFSIZE];
473         FILE *fp;
474
475         if (!(msginfo->folder || body))
476                 return;
477
478         if (body)
479                 fp = str_open_as_stream(body);
480         else {
481                 if (MSG_IS_ENCRYPTED(msginfo->flags))
482                         fp = procmime_get_first_encrypted_text_content(msginfo);
483                 else
484                         fp = procmime_get_first_text_content(msginfo);
485         }
486
487         if (fp == NULL)
488                 g_warning("Can't get text part");
489         else {
490                 account_signatures_matchlist_create();
491                 while (fgets(buf, sizeof(buf), fp) != NULL) {
492                         strcrchomp(buf);
493
494                         if (!signature && account_signatures_matchlist_nchar_found(buf, "%\n"))
495                                 break;
496                 
497                         if (quoted && quote_str)
498                                 INSERT(quote_str);
499                         
500                         INSERT(buf);
501                 }
502                 account_signatures_matchlist_delete();
503                 fclose(fp);
504         }
505 }
506
507 static void quote_fmt_insert_file(const gchar *filename)
508 {
509         FILE *file;
510         char buffer[256];
511         
512         if ((file = g_fopen(filename, "rb")) != NULL) {
513                 while (fgets(buffer, sizeof(buffer), file)) {
514                         INSERT(buffer);
515                 }
516                 fclose(file);
517         }
518
519 }
520
521 static void quote_fmt_insert_program_output(const gchar *progname)
522 {
523         FILE *file;
524         char buffer[256];
525
526         if ((file = popen(progname, "r")) != NULL) {
527                 while (fgets(buffer, sizeof(buffer), file)) {
528                         INSERT(buffer);
529                 }
530                 pclose(file);
531         }
532 }
533
534 static void quote_fmt_insert_user_input(const gchar *varname)
535 {
536         gchar *buf = NULL;
537         gchar *text = NULL;
538         
539         if (dry_run) 
540                 return;
541
542         if ((text = g_hash_table_lookup(var_table, varname)) == NULL) {
543                 buf = g_strdup_printf(_("Enter text to replace '%s'"), varname);
544                 text = input_dialog(_("Enter variable"), buf, "");
545                 g_free(buf);
546                 if (!text)
547                         return;
548                 g_hash_table_insert(var_table, g_strdup(varname), g_strdup(text));
549         } else {
550                 /* don't free the one in hashtable at the end */
551                 text = g_strdup(text);
552         }
553
554         if (!text)
555                 return;
556         INSERT(text);
557         g_free(text);
558 }
559
560 static void quote_fmt_attach_file(const gchar *filename)
561 {
562         attachments = g_list_append(attachments, g_strdup(filename));
563 }
564
565 static void quote_fmt_attach_file_program_output(const gchar *progname)
566 {
567         FILE *file;
568         char buffer[PATH_MAX];
569
570         if ((file = popen(progname, "r")) != NULL) {
571                 /* get first line only */
572                 if (fgets(buffer, sizeof(buffer), file)) {
573                         /* trim trailing CR/LF */
574                         strretchomp(buffer);
575                         attachments = g_list_append(attachments, g_strdup(buffer));
576                 }
577                 pclose(file);
578         }
579 }
580
581 static gchar *quote_fmt_complete_address(const gchar *addr)
582 {
583         gint count;
584         gchar *res, *tmp, *email_addr;
585         gchar **split;
586
587         debug_print("quote_fmt_complete_address: %s\n", addr);
588         if (addr == NULL)
589                 return NULL;
590
591         /* if addr is a list of message, try the 1st element only */
592         split = g_strsplit(addr, ",", -1);
593         if (!split || !split[0] || *split[0] == '\0') {
594                 g_strfreev(split);
595                 return NULL;
596         }
597
598         Xstrdup_a(email_addr, split[0], return NULL);
599         extract_address(email_addr);
600         if (!*email_addr) {
601                 g_strfreev(split);
602                 return NULL;
603         }
604
605         res = NULL;
606         start_address_completion(NULL);
607         if (1 < (count = complete_address(email_addr))) {
608                 tmp = get_complete_address(1);
609                 res = procheader_get_fromname(tmp);
610                 g_free(tmp);
611         }
612         end_address_completion();
613         g_strfreev(split);
614
615         debug_print("quote_fmt_complete_address: matched %s\n", res);
616         return res;
617 }
618
619 %}
620
621 %union {
622         char chr;
623         char str[256];
624 }
625
626 /* tokens SHOW */
627 %token SHOW_NEWSGROUPS
628 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
629 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
630 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
631 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB SHOW_MAIL_ADDRESS
632 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
633 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_EXCLAMATION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
634 %token SHOW_ACCOUNT_FULL_NAME SHOW_ACCOUNT_MAIL_ADDRESS SHOW_ACCOUNT_NAME SHOW_ACCOUNT_ORGANIZATION
635 %token SHOW_ACCOUNT_DICT SHOW_ACCOUNT_SIG SHOW_ACCOUNT_SIGPATH
636 %token SHOW_DICT SHOW_TAGS
637 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_CC
638 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_FROM
639 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_TO
640 /* tokens QUERY */
641 %token QUERY_DATE QUERY_FROM
642 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
643 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
644 %token QUERY_ACCOUNT_FULL_NAME QUERY_ACCOUNT_ORGANIZATION QUERY_ACCOUNT_DICT
645 %token QUERY_ACCOUNT_SIG QUERY_ACCOUNT_SIGPATH
646 %token QUERY_DICT
647 %token QUERY_CC_FOUND_IN_ADDRESSBOOK
648 %token QUERY_FROM_FOUND_IN_ADDRESSBOOK
649 %token QUERY_TO_FOUND_IN_ADDRESSBOOK
650 /* tokens QUERY_NOT */
651 %token QUERY_NOT_DATE QUERY_NOT_FROM
652 %token QUERY_NOT_FULLNAME QUERY_NOT_SUBJECT QUERY_NOT_TO QUERY_NOT_NEWSGROUPS
653 %token QUERY_NOT_MESSAGEID QUERY_NOT_CC QUERY_NOT_REFERENCES
654 %token QUERY_NOT_ACCOUNT_FULL_NAME QUERY_NOT_ACCOUNT_ORGANIZATION QUERY_NOT_ACCOUNT_DICT
655 %token QUERY_NOT_ACCOUNT_SIG QUERY_NOT_ACCOUNT_SIGPATH
656 %token QUERY_NOT_DICT
657 %token QUERY_NOT_CC_FOUND_IN_ADDRESSBOOK
658 %token QUERY_NOT_FROM_FOUND_IN_ADDRESSBOOK
659 %token QUERY_NOT_TO_FOUND_IN_ADDRESSBOOK
660 /* other tokens */
661 %token INSERT_FILE INSERT_PROGRAMOUTPUT INSERT_USERINPUT
662 %token ATTACH_FILE ATTACH_PROGRAMOUTPUT
663 %token OPARENT CPARENT
664 %token CHARACTER
665 %token SHOW_DATE_EXPR
666 %token SET_CURSOR_POS
667
668 %start quote_fmt
669
670 %type <chr> CHARACTER
671 %type <chr> character
672 %type <str> string
673
674 %%
675
676 quote_fmt:
677         character_or_special_or_insert_or_query_list ;
678
679 sub_expr:
680         character_or_special_list ;
681
682 character_or_special_or_insert_or_query_list:
683         character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
684         | character_or_special_or_insert_or_query ;
685
686 character_or_special_list:
687         character_or_special character_or_special_list
688         | character_or_special ;
689
690 character_or_special_or_insert_or_query:
691         character_or_special
692         | query
693         | query_not
694         | insert
695         | attach ;
696
697 character_or_special:
698         special
699         | character
700         {
701                 INSERT_CHARACTER($1);
702         };
703
704 character:
705         CHARACTER
706         ;
707
708 string:
709         CHARACTER
710         {
711                 $$[0] = $1;
712                 $$[1] = '\0';
713         }
714         | string CHARACTER
715         {
716                 size_t len;
717                 
718                 strncpy($$, $1, sizeof($$));
719                 $$[sizeof($$) - 1] = '\0';
720                 len = strlen($$);
721                 if (len + 1 < sizeof($$)) {
722                         $$[len + 1] = '\0';
723                         $$[len] = $2;
724                 }
725         };
726
727 special:
728         SHOW_NEWSGROUPS
729         {
730                 if (msginfo->newsgroups)
731                         INSERT(msginfo->newsgroups);
732         }
733         | SHOW_DATE_EXPR OPARENT string CPARENT
734         {
735                 quote_fmt_show_date(msginfo, $3);
736         }
737         | SHOW_DATE
738         {
739                 if (msginfo->date)
740                         INSERT(msginfo->date);
741         }
742         | SHOW_FROM
743         {
744                 if (msginfo->from)
745                         INSERT(msginfo->from);
746         }
747         | SHOW_MAIL_ADDRESS
748         {
749                 if (msginfo->from) {
750                         gchar *stripped_address = g_strdup(msginfo->from);
751                         extract_address(stripped_address);
752                         INSERT(stripped_address);
753                         g_free(stripped_address);
754                 }
755         }
756         | SHOW_FULLNAME
757         {
758                 if (msginfo->fromname)
759                         INSERT(msginfo->fromname);
760         }
761         | SHOW_FIRST_NAME
762         {
763                 quote_fmt_show_first_name(msginfo);
764         }
765         | SHOW_LAST_NAME
766     {
767                 quote_fmt_show_last_name(msginfo);
768         }
769         | SHOW_SENDER_INITIAL
770         {
771                 quote_fmt_show_sender_initial(msginfo);
772         }
773         | SHOW_SUBJECT
774         {
775                 if (msginfo->subject)
776                         INSERT(msginfo->subject);
777         }
778         | SHOW_TO
779         {
780                 if (msginfo->to)
781                         INSERT(msginfo->to);
782         }
783         | SHOW_MESSAGEID
784         {
785                 if (msginfo->msgid)
786                         INSERT(msginfo->msgid);
787         }
788         | SHOW_PERCENT
789         {
790                 INSERT("%");
791         }
792         | SHOW_CC
793         {
794                 if (msginfo->cc)
795                         INSERT(msginfo->cc);
796         }
797         | SHOW_REFERENCES
798         {
799                 GSList *item;
800
801                 INSERT(msginfo->inreplyto);
802                 for (item = msginfo->references; item != NULL; item = g_slist_next(item))
803                         if (item->data)
804                                 INSERT(item->data);
805         }
806         | SHOW_MESSAGE
807         {
808                 quote_fmt_show_msg(msginfo, body, FALSE, TRUE, quote_str);
809         }
810         | SHOW_QUOTED_MESSAGE
811         {
812                 quote_fmt_show_msg(msginfo, body, TRUE, TRUE, quote_str);
813         }
814         | SHOW_MESSAGE_NO_SIGNATURE
815         {
816                 quote_fmt_show_msg(msginfo, body, FALSE, FALSE, quote_str);
817         }
818         | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
819         {
820                 quote_fmt_show_msg(msginfo, body, TRUE, FALSE, quote_str);
821         }
822         | SHOW_ACCOUNT_FULL_NAME
823         {
824                 if (account && account->name)
825                         INSERT(account->name);
826         }
827         | SHOW_ACCOUNT_MAIL_ADDRESS
828         {
829                 if (account && account->address)
830                         INSERT(account->address);
831         }
832         | SHOW_ACCOUNT_NAME
833         {
834                 if (account && account->account_name)
835                         INSERT(account->account_name);
836         }
837         | SHOW_ACCOUNT_ORGANIZATION
838         {
839                 if (account && account->organization)
840                         INSERT(account->organization);
841         }
842         | SHOW_ACCOUNT_SIG
843         {
844                 gchar *str = account_get_signature_str(account);
845                 INSERT(str);
846                 g_free(str);
847         }
848         | SHOW_ACCOUNT_SIGPATH
849         {
850                 if (account && account->sig_path)
851                         INSERT(account->sig_path);
852         }
853         | SHOW_ACCOUNT_DICT
854         {
855 #ifdef USE_ENCHANT
856                 if (account && account->enable_default_dictionary) {
857                         gchar *dictname = g_path_get_basename(account->default_dictionary);
858                         INSERT(dictname);
859                         g_free(dictname);
860                 }
861 #endif
862         }
863         | SHOW_DICT
864         {
865 #ifdef USE_ENCHANT
866                 INSERT(default_dictionary);
867 #endif
868         }
869         | SHOW_TAGS
870         {
871                 gchar *tags = procmsg_msginfo_get_tags_str(msginfo);
872                 if (tags) {
873                         INSERT(tags);
874                 }
875                 g_free(tags);
876         }
877         | SHOW_BACKSLASH
878         {
879                 INSERT("\\");
880         }
881         | SHOW_TAB
882         {
883                 INSERT("\t");
884         }
885         | SHOW_EOL
886         {
887                 INSERT("\n");
888         }
889         | SHOW_QUESTION_MARK
890         {
891                 INSERT("?");
892         }
893         | SHOW_EXCLAMATION_MARK
894         {
895                 INSERT("!");
896         }
897         | SHOW_PIPE
898         {
899                 INSERT("|");
900         }
901         | SHOW_OPARENT
902         {
903                 INSERT("{");
904         }
905         | SHOW_CPARENT
906         {
907                 INSERT("}");
908         }
909         | SET_CURSOR_POS
910         {
911                 if (current->buffer)
912                         cursor_pos = g_utf8_strlen(current->buffer, -1);
913                 else
914                         cursor_pos = 0;
915         }
916         | SHOW_ADDRESSBOOK_COMPLETION_FOR_CC
917         {
918                 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
919                 if (tmp) {
920                         INSERT(tmp);
921                         g_free(tmp);
922                 }
923         }
924         | SHOW_ADDRESSBOOK_COMPLETION_FOR_FROM
925         {
926                 gchar *tmp = quote_fmt_complete_address(msginfo->from);
927                 if (tmp) {
928                         INSERT(tmp);
929                         g_free(tmp);
930                 }
931         }
932         | SHOW_ADDRESSBOOK_COMPLETION_FOR_TO
933         {
934                 gchar *tmp = quote_fmt_complete_address(msginfo->to);
935                 if (tmp) {
936                         INSERT(tmp);
937                         g_free(tmp);
938                 }
939         };
940
941 query:
942         QUERY_DATE
943         {
944                 add_visibility(msginfo->date != NULL);
945         }
946         OPARENT quote_fmt CPARENT
947         {
948                 remove_visibility();
949         }
950         | QUERY_FROM
951         {
952                 add_visibility(msginfo->from != NULL);
953         }
954         OPARENT quote_fmt CPARENT
955         {
956                 remove_visibility();
957         }
958         | QUERY_FULLNAME
959         {
960                 add_visibility(msginfo->fromname != NULL);
961         }
962         OPARENT quote_fmt CPARENT
963         {
964                 remove_visibility();
965         }
966         | QUERY_SUBJECT
967         {
968                 add_visibility(msginfo->subject != NULL);
969         }
970         OPARENT quote_fmt CPARENT
971         {
972                 remove_visibility();
973         }
974         | QUERY_TO
975         {
976                 add_visibility(msginfo->to != NULL);
977         }
978         OPARENT quote_fmt CPARENT
979         {
980                 remove_visibility();
981         }
982         | QUERY_NEWSGROUPS
983         {
984                 add_visibility(msginfo->newsgroups != NULL);
985         }
986         OPARENT quote_fmt CPARENT
987         {
988                 remove_visibility();
989         }
990         | QUERY_MESSAGEID
991         {
992                 add_visibility(msginfo->msgid != NULL);
993         }
994         OPARENT quote_fmt CPARENT
995         {
996                 remove_visibility();
997         }
998         | QUERY_CC
999         {
1000                 add_visibility(msginfo->cc != NULL);
1001         }
1002         OPARENT quote_fmt CPARENT
1003         {
1004                 remove_visibility();
1005         }
1006         | QUERY_REFERENCES
1007         {
1008                 gboolean found;
1009                 GSList *item;
1010
1011                 found = (msginfo->inreplyto != NULL);
1012                 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
1013                         if (item->data)
1014                                 found = TRUE;
1015                 add_visibility(found == TRUE);
1016         }
1017         OPARENT quote_fmt CPARENT
1018         {
1019                 remove_visibility();
1020         }
1021         | QUERY_ACCOUNT_FULL_NAME
1022         {
1023                 add_visibility(account != NULL && account->name != NULL);
1024         }
1025         OPARENT quote_fmt CPARENT
1026         {
1027                 remove_visibility();
1028         }
1029         | QUERY_ACCOUNT_ORGANIZATION
1030         {
1031                 add_visibility(account != NULL && account->organization != NULL);
1032         }
1033         OPARENT quote_fmt CPARENT
1034         {
1035                 remove_visibility();
1036         }
1037         | QUERY_ACCOUNT_SIG
1038         {
1039                 gchar *str = account_get_signature_str(account);
1040                 add_visibility(str != NULL && * str != '\0');
1041                 g_free(str);
1042         }
1043         OPARENT quote_fmt CPARENT
1044         {
1045                 remove_visibility();
1046         }
1047         | QUERY_ACCOUNT_SIGPATH
1048         {
1049                 add_visibility(account != NULL && account->sig_path != NULL
1050                                 && *account->sig_path != '\0');
1051         }
1052         OPARENT quote_fmt CPARENT
1053         {
1054                 remove_visibility();
1055         }
1056         | QUERY_ACCOUNT_DICT
1057         {
1058 #ifdef USE_ENCHANT
1059                 add_visibility(account != NULL && account->enable_default_dictionary == TRUE &&
1060                                 account->default_dictionary != NULL && *account->default_dictionary != '\0');
1061 #else
1062                 add_visibility(FALSE);
1063 #endif
1064         }
1065         OPARENT quote_fmt CPARENT
1066         {
1067                 remove_visibility();
1068         }
1069         | QUERY_DICT
1070         {
1071 #ifdef USE_ENCHANT
1072                 add_visibility(*default_dictionary != '\0');
1073 #else
1074                 add_visibility(FALSE);
1075 #endif
1076         }
1077         OPARENT quote_fmt CPARENT
1078         {
1079                 remove_visibility();
1080         }
1081         | QUERY_CC_FOUND_IN_ADDRESSBOOK
1082         {
1083                 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
1084                 add_visibility(tmp != NULL && *tmp != '\0');
1085                 g_free(tmp);
1086         }
1087         OPARENT quote_fmt CPARENT
1088         {
1089                 remove_visibility();
1090         }
1091         | QUERY_FROM_FOUND_IN_ADDRESSBOOK
1092         {
1093                 gchar *tmp = quote_fmt_complete_address(msginfo->from);
1094                 add_visibility(tmp != NULL && *tmp != '\0');
1095                 g_free(tmp);
1096         }
1097         OPARENT quote_fmt CPARENT
1098         {
1099                 remove_visibility();
1100         }
1101         | QUERY_TO_FOUND_IN_ADDRESSBOOK
1102         {
1103                 gchar *tmp = quote_fmt_complete_address(msginfo->to);
1104                 add_visibility(tmp != NULL && *tmp != '\0');
1105                 g_free(tmp);
1106         }
1107         OPARENT quote_fmt CPARENT
1108         {
1109                 remove_visibility();
1110         };
1111
1112 query_not:
1113         QUERY_NOT_DATE
1114         {
1115                 add_visibility(msginfo->date == NULL);
1116         }
1117         OPARENT quote_fmt CPARENT
1118         {
1119                 remove_visibility();
1120         }
1121         | QUERY_NOT_FROM
1122         {
1123                 add_visibility(msginfo->from == NULL);
1124         }
1125         OPARENT quote_fmt CPARENT
1126         {
1127                 remove_visibility();
1128         }
1129         | QUERY_NOT_FULLNAME
1130         {
1131                 add_visibility(msginfo->fromname == NULL);
1132         }
1133         OPARENT quote_fmt CPARENT
1134         {
1135                 remove_visibility();
1136         }
1137         | QUERY_NOT_SUBJECT
1138         {
1139                 add_visibility(msginfo->subject == NULL);
1140         }
1141         OPARENT quote_fmt CPARENT
1142         {
1143                 remove_visibility();
1144         }
1145         | QUERY_NOT_TO
1146         {
1147                 add_visibility(msginfo->to == NULL);
1148         }
1149         OPARENT quote_fmt CPARENT
1150         {
1151                 remove_visibility();
1152         }
1153         | QUERY_NOT_NEWSGROUPS
1154         {
1155                 add_visibility(msginfo->newsgroups == NULL);
1156         }
1157         OPARENT quote_fmt CPARENT
1158         {
1159                 remove_visibility();
1160         }
1161         | QUERY_NOT_MESSAGEID
1162         {
1163                 add_visibility(msginfo->msgid == NULL);
1164         }
1165         OPARENT quote_fmt CPARENT
1166         {
1167                 remove_visibility();
1168         }
1169         | QUERY_NOT_CC
1170         {
1171                 add_visibility(msginfo->cc == NULL);
1172         }
1173         OPARENT quote_fmt CPARENT
1174         {
1175                 remove_visibility();
1176         }
1177         | QUERY_NOT_REFERENCES
1178         {
1179                 gboolean found;
1180                 GSList *item;
1181
1182                 found = (msginfo->inreplyto != NULL);
1183                 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
1184                         if (item->data)
1185                                 found = TRUE;
1186                 add_visibility(found == FALSE);
1187         }
1188         OPARENT quote_fmt CPARENT
1189         {
1190                 remove_visibility();
1191         }
1192         | QUERY_NOT_ACCOUNT_FULL_NAME
1193         {
1194                 add_visibility(account == NULL || account->name == NULL);
1195         }
1196         OPARENT quote_fmt CPARENT
1197         {
1198                 remove_visibility();
1199         }
1200         | QUERY_NOT_ACCOUNT_ORGANIZATION
1201         {
1202                 add_visibility(account == NULL || account->organization == NULL);
1203         }
1204         OPARENT quote_fmt CPARENT
1205         {
1206                 remove_visibility();
1207         }
1208         | QUERY_NOT_ACCOUNT_SIG
1209         {
1210                 gchar *str = account_get_signature_str(account);
1211                 add_visibility(str == NULL || *str == '\0');
1212                 g_free(str);
1213         }
1214         OPARENT quote_fmt CPARENT
1215         {
1216                 remove_visibility();
1217         }
1218         | QUERY_NOT_ACCOUNT_SIGPATH
1219         {
1220                 add_visibility(account == NULL || account->sig_path == NULL
1221                                 || *account->sig_path == '\0');
1222         }
1223         OPARENT quote_fmt CPARENT
1224         {
1225                 remove_visibility();
1226         }
1227         | QUERY_NOT_ACCOUNT_DICT
1228         {
1229 #ifdef USE_ENCHANT
1230                 add_visibility(account == NULL || account->enable_default_dictionary == FALSE
1231                                 || *account->default_dictionary == '\0');
1232 #else
1233                 add_visibility(FALSE);
1234 #endif
1235         }
1236         OPARENT quote_fmt CPARENT
1237         {
1238                 remove_visibility();
1239         }
1240         | QUERY_NOT_DICT
1241         {
1242 #ifdef USE_ENCHANT
1243                 add_visibility(*default_dictionary == '\0');
1244 #else
1245                 add_visibility(FALSE);
1246 #endif
1247         }
1248         OPARENT quote_fmt CPARENT
1249         {
1250                 remove_visibility();
1251         }
1252         | QUERY_NOT_CC_FOUND_IN_ADDRESSBOOK
1253         {
1254                 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
1255                 add_visibility(tmp == NULL || *tmp == '\0');
1256                 g_free(tmp);
1257         }
1258         OPARENT quote_fmt CPARENT
1259         {
1260                 remove_visibility();
1261         }
1262         | QUERY_NOT_FROM_FOUND_IN_ADDRESSBOOK
1263         {
1264                 gchar *tmp = quote_fmt_complete_address(msginfo->from);
1265                 add_visibility(tmp == NULL || *tmp == '\0');
1266                 g_free(tmp);
1267         }
1268         OPARENT quote_fmt CPARENT
1269         {
1270                 remove_visibility();
1271         }
1272         | QUERY_NOT_TO_FOUND_IN_ADDRESSBOOK
1273         {
1274                 gchar *tmp = quote_fmt_complete_address(msginfo->to);
1275                 add_visibility(tmp == NULL || *tmp == '\0');
1276                 g_free(tmp);
1277         }
1278         OPARENT quote_fmt CPARENT
1279         {
1280                 remove_visibility();
1281         };
1282
1283 insert:
1284         INSERT_FILE
1285         {
1286                 current = &sub_expr;
1287                 clear_buffer();
1288         }
1289         OPARENT sub_expr CPARENT
1290         {
1291                 current = &main_expr;
1292                 if (!dry_run) {
1293                         quote_fmt_insert_file(sub_expr.buffer);
1294                 }
1295         }
1296         | INSERT_PROGRAMOUTPUT
1297         {
1298                 current = &sub_expr;
1299                 clear_buffer();
1300         }
1301         OPARENT sub_expr CPARENT
1302         {
1303                 current = &main_expr;
1304                 if (!dry_run) {
1305                         quote_fmt_insert_program_output(sub_expr.buffer);
1306                 }
1307         }
1308         | INSERT_USERINPUT
1309         {
1310                 current = &sub_expr;
1311                 clear_buffer();
1312         }
1313         OPARENT sub_expr CPARENT
1314         {
1315                 current = &main_expr;
1316                 if (!dry_run) {
1317                         quote_fmt_insert_user_input(sub_expr.buffer);
1318                 }
1319         };
1320
1321 attach:
1322         ATTACH_FILE
1323         {
1324                 current = &sub_expr;
1325                 clear_buffer();
1326         }
1327         OPARENT sub_expr CPARENT
1328         {
1329                 current = &main_expr;
1330                 if (!dry_run) {
1331                         quote_fmt_attach_file(sub_expr.buffer);
1332                 }
1333         }
1334         | ATTACH_PROGRAMOUTPUT
1335         {
1336                 current = &sub_expr;
1337                 clear_buffer();
1338         }
1339         OPARENT sub_expr CPARENT
1340         {
1341                 current = &main_expr;
1342                 if (!dry_run) {
1343                         quote_fmt_attach_file_program_output(sub_expr.buffer);
1344                 }
1345         };
1346 ;