2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail Team
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.
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.
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/>.
25 #include <glib/gi18n.h>
33 #include "procheader.h"
34 #include "addr_compl.h"
35 #include "gtk/inputdialog.h"
37 #include "quote_fmt.h"
38 #include "quote_fmt_lex.h"
43 bison -p quote_fmt quote_fmt.y
48 static MsgInfo *msginfo = NULL;
49 static PrefsAccount *account = NULL;
51 static gchar default_dictionary[BUFFSIZE];
53 static gboolean *visible = NULL;
54 static gboolean dry_run = FALSE;
55 static gint maxsize = 0;
56 static gint stacksize = 0;
57 static GHashTable *var_table = NULL;
59 typedef struct st_buffer
66 static struct st_buffer main_expr = { NULL, 0, 0 };
67 static struct st_buffer sub_expr = { NULL, 0, 0 };
68 static struct st_buffer* current = NULL;
70 static const gchar *quote_str = NULL;
71 static const gchar *body = NULL;
72 static gint error = 0;
74 static gint cursor_pos = -1;
76 extern int quote_fmt_firsttime;
79 static void add_visibility(gboolean val)
82 if (maxsize < stacksize) {
84 visible = g_realloc(visible, maxsize * sizeof(gboolean));
89 visible[stacksize - 1] = val;
92 static void remove_visibility(void)
96 g_warning("Error: visibility stack underflow\n");
101 static void add_buffer(const gchar *s)
109 if (current->bufsize + len + 1 > current->bufmax) {
110 if (current->bufmax == 0)
111 current->bufmax = 128;
112 while (current->bufsize + len + 1 > current->bufmax)
113 current->bufmax *= 2;
114 current->buffer = g_realloc(current->buffer, current->bufmax);
116 strcpy(current->buffer + current->bufsize, s);
117 current->bufsize += len;
120 static void clear_buffer(void)
123 *current->buffer = '\0';
125 /* force to an empty string, as buffer should not be left unallocated */
127 current->bufsize = 0;
130 gchar *quote_fmt_get_buffer(void)
132 if (current != &main_expr)
133 g_warning("Error: parser still in sub-expr mode\n");
138 return current->buffer;
141 gint quote_fmt_get_line(void)
146 gint quote_fmt_get_cursor_pos(void)
151 #define INSERT(buf) \
152 if (stacksize != 0 && visible[stacksize - 1])\
155 #define INSERT_CHARACTER(chr) \
156 if (stacksize != 0 && visible[stacksize - 1]) { \
163 void quote_fmt_reset_vartable(void)
166 g_hash_table_destroy(var_table);
172 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
173 const gchar *my_body, gboolean my_dry_run,
174 PrefsAccount *compose_account,
175 GtkAspell *compose_gtkaspell)
177 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
178 const gchar *my_body, gboolean my_dry_run,
179 PrefsAccount *compose_account)
182 quote_str = my_quote_str;
185 account = compose_account;
187 gchar *dict = gtkaspell_get_default_dictionary(compose_gtkaspell);
189 strncpy2(default_dictionary, dict, sizeof(default_dictionary));
191 *default_dictionary = '\0';
193 dry_run = my_dry_run;
195 add_visibility(TRUE);
196 main_expr.bufmax = 0;
198 current = &main_expr;
204 var_table = g_hash_table_new_full(g_str_hash, g_str_equal,
208 * force LEX initialization
210 quote_fmt_firsttime = 1;
214 void quote_fmterror(char *str)
216 g_warning("Error: %s at line %d\n", str, line);
220 int quote_fmtwrap(void)
225 static int isseparator(int ch)
227 return g_ascii_isspace(ch) || ch == '.' || ch == '-';
230 static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
243 * ALF - GNU C's strftime() has a nice format specifier
244 * for time zone offset (%z). Non-standard however, so
248 #define RLEFT (sizeof result) - (rptr - result)
249 #define STR_SIZE(x) (sizeof (x) - 1)
253 if (procheader_date_parse_to_tm(msginfo->date, <, zone)) {
255 * break up format string in tiny bits delimited by valid %z's and
256 * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
258 for (rptr = result, fptr = format; fptr && *fptr && rptr < &result[sizeof result - 1];) {
263 if (NULL != (zptr = strstr(fptr, "%z"))) {
265 * count nr. of prepended percent chars
267 for (perc = 0, p = zptr; p && p >= format && *p == '%'; p--, perc++)
272 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
274 rptr += strftime(rptr, RLEFT, tmp, <);
278 * append time zone offset
280 if (zone[0] && perc % 2)
281 rptr += g_snprintf(rptr, RLEFT, "%s", zone);
282 fptr = zptr + STR_SIZE("%z");
284 rptr += strftime(rptr, RLEFT, fptr, <);
289 if (g_utf8_validate(result, -1, NULL)) {
292 gchar *utf = conv_codeset_strdup(result,
293 conv_get_locale_charset_str_no_utf8(),
296 !g_utf8_validate(utf, -1, NULL)) {
298 utf = g_malloc(strlen(result)*2+1);
299 conv_localetodisp(utf,
300 strlen(result)*2+1, result);
302 if (g_utf8_validate(utf, -1, NULL)) {
312 static void quote_fmt_show_first_name(const MsgInfo *msginfo)
317 if (!msginfo->fromname)
320 p = (guchar*)strchr(msginfo->fromname, ',');
322 /* fromname is like "Duck, Donald" */
324 while (*p && isspace(*p)) p++;
325 str = alloca(strlen((char *)p) + 1);
327 strcpy(str, (char *)p);
331 /* fromname is like "Donald Duck" */
332 str = alloca(strlen(msginfo->fromname) + 1);
334 strcpy(str, msginfo->fromname);
336 while (*p && !isspace(*p)) p++;
343 static void quote_fmt_show_last_name(const MsgInfo *msginfo)
348 /* This probably won't work together very well with Middle
349 names and the like - thth */
350 if (!msginfo->fromname)
353 str = alloca(strlen(msginfo->fromname) + 1);
355 strcpy(str, msginfo->fromname);
356 p = strchr(str, ',');
358 /* fromname is like "Duck, Donald" */
362 /* fromname is like "Donald Duck" */
364 while (*p && !isspace(*p)) p++;
366 /* We found a space. Get first
367 none-space char and insert
368 rest of string from there. */
369 while (*p && isspace(*p)) p++;
373 /* If there is no none-space
374 char, just insert whole
379 /* If there is no space, just
380 insert whole fromname. */
387 static void quote_fmt_show_sender_initial(const MsgInfo *msginfo)
389 #define MAX_SENDER_INITIAL 20
390 gchar tmp[MAX_SENDER_INITIAL];
395 if (!msginfo->fromname)
398 p = (guchar *)msginfo->fromname;
401 if (*p && g_utf8_validate((gchar *)p, 1, NULL)) {
405 if (len >= MAX_SENDER_INITIAL - 1)
409 while (*p && !isseparator(*p)) p++;
410 while (*p && isseparator(*p)) p++;
416 static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
417 gboolean quoted, gboolean signature,
418 const gchar *quote_str)
423 if (!(msginfo->folder || body))
427 fp = str_open_as_stream(body);
429 if (MSG_IS_ENCRYPTED(msginfo->flags))
430 fp = procmime_get_first_encrypted_text_content(msginfo);
432 fp = procmime_get_first_text_content(msginfo);
436 g_warning("Can't get text part\n");
438 while (fgets(buf, sizeof(buf), fp) != NULL) {
441 if (!signature && strncmp(buf, "-- \n", 4) == 0)
444 if (quoted && quote_str)
453 static void quote_fmt_insert_file(const gchar *filename)
458 if ((file = g_fopen(filename, "rb")) != NULL) {
459 while (fgets(buffer, sizeof(buffer), file)) {
467 static void quote_fmt_insert_program_output(const gchar *progname)
472 if ((file = popen(progname, "r")) != NULL) {
473 while (fgets(buffer, sizeof(buffer), file)) {
480 static void quote_fmt_insert_user_input(const gchar *varname)
488 if ((text = g_hash_table_lookup(var_table, varname)) == NULL) {
489 buf = g_strdup_printf(_("Enter text to replace '%s'"), varname);
490 text = input_dialog(_("Enter variable"), buf, "");
494 g_hash_table_insert(var_table, g_strdup(varname), g_strdup(text));
496 /* don't free the one in hashtable at the end */
497 text = g_strdup(text);
506 static gchar *quote_fmt_complete_address(const gchar *addr)
509 gchar *res, *tmp, *email_addr;
512 debug_print("quote_fmt_complete_address: %s\n", addr);
516 /* if addr is a list of message, try the 1st element only */
517 split = g_strsplit(addr, ",", -1);
518 if (!split || !split[0] || *split[0] == '\0') {
523 Xstrdup_a(email_addr, split[0], return NULL);
524 extract_address(email_addr);
531 start_address_completion(NULL);
532 if (1 < (count = complete_address(email_addr))) {
533 tmp = get_complete_address(1);
534 res = procheader_get_fromname(tmp);
537 end_address_completion();
540 debug_print("quote_fmt_complete_address: matched %s\n", res);
552 %token SHOW_NEWSGROUPS
553 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
554 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
555 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
556 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB SHOW_MAIL_ADDRESS
557 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
558 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_EXCLAMATION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
559 %token SHOW_ACCOUNT_FULL_NAME SHOW_ACCOUNT_MAIL_ADDRESS SHOW_ACCOUNT_NAME SHOW_ACCOUNT_ORGANIZATION
560 %token SHOW_ACCOUNT_DICT
561 %token SHOW_DICT SHOW_TAGS
562 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_CC
563 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_FROM
564 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_TO
566 %token QUERY_DATE QUERY_FROM
567 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
568 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
569 %token QUERY_ACCOUNT_FULL_NAME QUERY_ACCOUNT_ORGANIZATION QUERY_ACCOUNT_DICT
571 %token QUERY_CC_FOUND_IN_ADDRESSBOOK
572 %token QUERY_FROM_FOUND_IN_ADDRESSBOOK
573 %token QUERY_TO_FOUND_IN_ADDRESSBOOK
574 /* tokens QUERY_NOT */
575 %token QUERY_NOT_DATE QUERY_NOT_FROM
576 %token QUERY_NOT_FULLNAME QUERY_NOT_SUBJECT QUERY_NOT_TO QUERY_NOT_NEWSGROUPS
577 %token QUERY_NOT_MESSAGEID QUERY_NOT_CC QUERY_NOT_REFERENCES
578 %token QUERY_NOT_ACCOUNT_FULL_NAME QUERY_NOT_ACCOUNT_ORGANIZATION QUERY_NOT_ACCOUNT_DICT
579 %token QUERY_NOT_DICT
580 %token QUERY_NOT_CC_FOUND_IN_ADDRESSBOOK
581 %token QUERY_NOT_FROM_FOUND_IN_ADDRESSBOOK
582 %token QUERY_NOT_TO_FOUND_IN_ADDRESSBOOK
584 %token INSERT_FILE INSERT_PROGRAMOUTPUT INSERT_USERINPUT
585 %token OPARENT CPARENT
587 %token SHOW_DATE_EXPR
588 %token SET_CURSOR_POS
592 %token <chr> CHARACTER
593 %type <chr> character
599 character_or_special_or_insert_or_query_list ;
602 character_or_special_list ;
604 character_or_special_or_insert_or_query_list:
605 character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
606 | character_or_special_or_insert_or_query ;
608 character_or_special_list:
609 character_or_special character_or_special_list
610 | character_or_special ;
612 character_or_special_or_insert_or_query:
618 character_or_special:
622 INSERT_CHARACTER($1);
639 strncpy($$, $1, sizeof($$));
640 $$[sizeof($$) - 1] = '\0';
642 if (len + 1 < sizeof($$)) {
651 if (msginfo->newsgroups)
652 INSERT(msginfo->newsgroups);
654 | SHOW_DATE_EXPR OPARENT string CPARENT
656 quote_fmt_show_date(msginfo, $3);
661 INSERT(msginfo->date);
666 INSERT(msginfo->from);
671 gchar *stripped_address = g_strdup(msginfo->from);
672 extract_address(stripped_address);
673 INSERT(stripped_address);
674 g_free(stripped_address);
679 if (msginfo->fromname)
680 INSERT(msginfo->fromname);
684 quote_fmt_show_first_name(msginfo);
688 quote_fmt_show_last_name(msginfo);
690 | SHOW_SENDER_INITIAL
692 quote_fmt_show_sender_initial(msginfo);
696 if (msginfo->subject)
697 INSERT(msginfo->subject);
707 INSERT(msginfo->msgid);
722 INSERT(msginfo->inreplyto);
723 for (item = msginfo->references; item != NULL; item = g_slist_next(item))
729 quote_fmt_show_msg(msginfo, body, FALSE, TRUE, quote_str);
731 | SHOW_QUOTED_MESSAGE
733 quote_fmt_show_msg(msginfo, body, TRUE, TRUE, quote_str);
735 | SHOW_MESSAGE_NO_SIGNATURE
737 quote_fmt_show_msg(msginfo, body, FALSE, FALSE, quote_str);
739 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
741 quote_fmt_show_msg(msginfo, body, TRUE, FALSE, quote_str);
743 | SHOW_ACCOUNT_FULL_NAME
745 if (account && account->name)
746 INSERT(account->name);
748 | SHOW_ACCOUNT_MAIL_ADDRESS
750 if (account && account->address)
751 INSERT(account->address);
755 if (account && account->account_name)
756 INSERT(account->account_name);
758 | SHOW_ACCOUNT_ORGANIZATION
760 if (account && account->organization)
761 INSERT(account->organization);
766 if (account && account->enable_default_dictionary) {
767 gchar *dictname = g_path_get_basename(account->default_dictionary);
776 INSERT(default_dictionary);
781 gchar *tags = procmsg_msginfo_get_tags_str(msginfo);
803 | SHOW_EXCLAMATION_MARK
822 cursor_pos = g_utf8_strlen(current->buffer, -1);
826 | SHOW_ADDRESSBOOK_COMPLETION_FOR_CC
828 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
834 | SHOW_ADDRESSBOOK_COMPLETION_FOR_FROM
836 gchar *tmp = quote_fmt_complete_address(msginfo->from);
842 | SHOW_ADDRESSBOOK_COMPLETION_FOR_TO
844 gchar *tmp = quote_fmt_complete_address(msginfo->to);
854 add_visibility(msginfo->date != NULL);
856 OPARENT quote_fmt CPARENT
862 add_visibility(msginfo->from != NULL);
864 OPARENT quote_fmt CPARENT
870 add_visibility(msginfo->fromname != NULL);
872 OPARENT quote_fmt CPARENT
878 add_visibility(msginfo->subject != NULL);
880 OPARENT quote_fmt CPARENT
886 add_visibility(msginfo->to != NULL);
888 OPARENT quote_fmt CPARENT
894 add_visibility(msginfo->newsgroups != NULL);
896 OPARENT quote_fmt CPARENT
902 add_visibility(msginfo->msgid != NULL);
904 OPARENT quote_fmt CPARENT
910 add_visibility(msginfo->cc != NULL);
912 OPARENT quote_fmt CPARENT
921 found = (msginfo->inreplyto != NULL);
922 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
925 add_visibility(found == TRUE);
927 OPARENT quote_fmt CPARENT
931 | QUERY_ACCOUNT_FULL_NAME
933 add_visibility(account != NULL && account->name != NULL);
935 OPARENT quote_fmt CPARENT
939 | QUERY_ACCOUNT_ORGANIZATION
941 add_visibility(account != NULL && account->organization != NULL);
943 OPARENT quote_fmt CPARENT
950 add_visibility(account != NULL && account->enable_default_dictionary == TRUE &&
951 account->default_dictionary != NULL && *account->default_dictionary != '\0');
953 add_visibility(FALSE);
956 OPARENT quote_fmt CPARENT
963 add_visibility(*default_dictionary != '\0');
965 add_visibility(FALSE);
968 OPARENT quote_fmt CPARENT
972 | QUERY_CC_FOUND_IN_ADDRESSBOOK
974 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
975 add_visibility(tmp != NULL && *tmp != '\0');
978 OPARENT quote_fmt CPARENT
982 | QUERY_FROM_FOUND_IN_ADDRESSBOOK
984 gchar *tmp = quote_fmt_complete_address(msginfo->from);
985 add_visibility(tmp != NULL && *tmp != '\0');
988 OPARENT quote_fmt CPARENT
992 | QUERY_TO_FOUND_IN_ADDRESSBOOK
994 gchar *tmp = quote_fmt_complete_address(msginfo->to);
995 add_visibility(tmp != NULL && *tmp != '\0');
998 OPARENT quote_fmt CPARENT
1000 remove_visibility();
1006 add_visibility(msginfo->date == NULL);
1008 OPARENT quote_fmt CPARENT
1010 remove_visibility();
1014 add_visibility(msginfo->from == NULL);
1016 OPARENT quote_fmt CPARENT
1018 remove_visibility();
1020 | QUERY_NOT_FULLNAME
1022 add_visibility(msginfo->fromname == NULL);
1024 OPARENT quote_fmt CPARENT
1026 remove_visibility();
1030 add_visibility(msginfo->subject == NULL);
1032 OPARENT quote_fmt CPARENT
1034 remove_visibility();
1038 add_visibility(msginfo->to == NULL);
1040 OPARENT quote_fmt CPARENT
1042 remove_visibility();
1044 | QUERY_NOT_NEWSGROUPS
1046 add_visibility(msginfo->newsgroups == NULL);
1048 OPARENT quote_fmt CPARENT
1050 remove_visibility();
1052 | QUERY_NOT_MESSAGEID
1054 add_visibility(msginfo->msgid == NULL);
1056 OPARENT quote_fmt CPARENT
1058 remove_visibility();
1062 add_visibility(msginfo->cc == NULL);
1064 OPARENT quote_fmt CPARENT
1066 remove_visibility();
1068 | QUERY_NOT_REFERENCES
1073 found = (msginfo->inreplyto != NULL);
1074 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
1077 add_visibility(found == FALSE);
1079 OPARENT quote_fmt CPARENT
1081 remove_visibility();
1083 | QUERY_NOT_ACCOUNT_FULL_NAME
1085 add_visibility(account == NULL || account->name == NULL);
1087 OPARENT quote_fmt CPARENT
1089 remove_visibility();
1091 | QUERY_NOT_ACCOUNT_ORGANIZATION
1093 add_visibility(account == NULL || account->organization == NULL);
1095 OPARENT quote_fmt CPARENT
1097 remove_visibility();
1099 | QUERY_NOT_ACCOUNT_DICT
1102 add_visibility(account == NULL || account->enable_default_dictionary == FALSE
1103 || *account->default_dictionary == '\0');
1105 add_visibility(FALSE);
1108 OPARENT quote_fmt CPARENT
1110 remove_visibility();
1115 add_visibility(*default_dictionary == '\0');
1117 add_visibility(FALSE);
1120 OPARENT quote_fmt CPARENT
1122 remove_visibility();
1124 | QUERY_NOT_CC_FOUND_IN_ADDRESSBOOK
1126 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
1127 add_visibility(tmp == NULL || *tmp == '\0');
1130 OPARENT quote_fmt CPARENT
1132 remove_visibility();
1134 | QUERY_NOT_FROM_FOUND_IN_ADDRESSBOOK
1136 gchar *tmp = quote_fmt_complete_address(msginfo->from);
1137 add_visibility(tmp == NULL || *tmp == '\0');
1140 OPARENT quote_fmt CPARENT
1142 remove_visibility();
1144 | QUERY_NOT_TO_FOUND_IN_ADDRESSBOOK
1146 gchar *tmp = quote_fmt_complete_address(msginfo->to);
1147 add_visibility(tmp == NULL || *tmp == '\0');
1150 OPARENT quote_fmt CPARENT
1152 remove_visibility();
1158 current = &sub_expr;
1161 OPARENT sub_expr CPARENT
1163 current = &main_expr;
1165 quote_fmt_insert_file(sub_expr.buffer);
1168 | INSERT_PROGRAMOUTPUT
1170 current = &sub_expr;
1173 OPARENT sub_expr CPARENT
1175 current = &main_expr;
1177 quote_fmt_insert_program_output(sub_expr.buffer);
1182 current = &sub_expr;
1185 OPARENT sub_expr CPARENT
1187 current = &main_expr;
1189 quote_fmt_insert_user_input(sub_expr.buffer);