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;
58 static GList *attachments = NULL;
60 typedef struct st_buffer
67 static struct st_buffer main_expr = { NULL, 0, 0 };
68 static struct st_buffer sub_expr = { NULL, 0, 0 };
69 static struct st_buffer* current = NULL;
71 static const gchar *quote_str = NULL;
72 static const gchar *body = NULL;
73 static gint error = 0;
75 static gint cursor_pos = -1;
77 extern int quote_fmt_firsttime;
79 extern int escaped_string;
81 static void add_visibility(gboolean val)
84 if (maxsize < stacksize) {
86 visible = g_realloc(visible, maxsize * sizeof(gboolean));
91 visible[stacksize - 1] = val;
94 static void remove_visibility(void)
98 g_warning("Error: visibility stack underflow\n");
103 static void add_buffer(const gchar *s)
111 if (current->bufsize + len + 1 > current->bufmax) {
112 if (current->bufmax == 0)
113 current->bufmax = 128;
114 while (current->bufsize + len + 1 > current->bufmax)
115 current->bufmax *= 2;
116 current->buffer = g_realloc(current->buffer, current->bufmax);
118 strcpy(current->buffer + current->bufsize, s);
119 current->bufsize += len;
122 static void clear_buffer(void)
125 *current->buffer = '\0';
127 /* force to an empty string, as buffer should not be left unallocated */
129 current->bufsize = 0;
132 gchar *quote_fmt_get_buffer(void)
134 if (current != &main_expr)
135 g_warning("Error: parser still in sub-expr mode\n");
140 return current->buffer;
143 GList *quote_fmt_get_attachments_list(void)
148 gint quote_fmt_get_line(void)
153 gint quote_fmt_get_cursor_pos(void)
158 #define INSERT(buf) \
159 if (stacksize != 0 && visible[stacksize - 1])\
162 #define INSERT_CHARACTER(chr) \
163 if (stacksize != 0 && visible[stacksize - 1]) { \
170 void quote_fmt_reset_vartable(void)
173 g_hash_table_destroy(var_table);
177 GList *cur = attachments;
180 cur = g_list_next(cur);
182 g_list_free(attachments);
188 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
189 const gchar *my_body, gboolean my_dry_run,
190 PrefsAccount *compose_account,
191 gboolean string_is_escaped,
192 GtkAspell *compose_gtkaspell)
194 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
195 const gchar *my_body, gboolean my_dry_run,
196 PrefsAccount *compose_account,
197 gboolean string_is_escaped)
200 quote_str = my_quote_str;
203 account = compose_account;
205 gchar *dict = gtkaspell_get_default_dictionary(compose_gtkaspell);
207 strncpy2(default_dictionary, dict, sizeof(default_dictionary));
209 *default_dictionary = '\0';
211 dry_run = my_dry_run;
213 add_visibility(TRUE);
214 main_expr.bufmax = 0;
216 current = &main_expr;
220 escaped_string = string_is_escaped;
223 var_table = g_hash_table_new_full(g_str_hash, g_str_equal,
227 * force LEX initialization
229 quote_fmt_firsttime = 1;
233 void quote_fmterror(char *str)
235 g_warning("Error: %s at line %d\n", str, line);
239 int quote_fmtwrap(void)
244 static int isseparator(int ch)
246 return g_ascii_isspace(ch) || ch == '.' || ch == '-';
249 static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
262 * ALF - GNU C's strftime() has a nice format specifier
263 * for time zone offset (%z). Non-standard however, so
267 #define RLEFT (sizeof result) - (rptr - result)
268 #define STR_SIZE(x) (sizeof (x) - 1)
272 if (procheader_date_parse_to_tm(msginfo->date, <, zone)) {
274 * break up format string in tiny bits delimited by valid %z's and
275 * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
277 for (rptr = result, fptr = format; fptr && *fptr && rptr < &result[sizeof result - 1];) {
282 if (NULL != (zptr = strstr(fptr, "%z"))) {
284 * count nr. of prepended percent chars
286 for (perc = 0, p = zptr; p && p >= format && *p == '%'; p--, perc++)
291 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
293 rptr += strftime(rptr, RLEFT, tmp, <);
297 * append time zone offset
299 if (zone[0] && perc % 2)
300 rptr += g_snprintf(rptr, RLEFT, "%s", zone);
301 fptr = zptr + STR_SIZE("%z");
303 rptr += strftime(rptr, RLEFT, fptr, <);
308 if (g_utf8_validate(result, -1, NULL)) {
311 gchar *utf = conv_codeset_strdup(result,
312 conv_get_locale_charset_str_no_utf8(),
315 !g_utf8_validate(utf, -1, NULL)) {
317 utf = g_malloc(strlen(result)*2+1);
318 conv_localetodisp(utf,
319 strlen(result)*2+1, result);
321 if (g_utf8_validate(utf, -1, NULL)) {
331 static void quote_fmt_show_first_name(const MsgInfo *msginfo)
336 if (!msginfo->fromname)
339 p = (guchar*)strchr(msginfo->fromname, ',');
341 /* fromname is like "Duck, Donald" */
343 while (*p && isspace(*p)) p++;
344 str = alloca(strlen((char *)p) + 1);
346 strcpy(str, (char *)p);
350 /* fromname is like "Donald Duck" */
351 str = alloca(strlen(msginfo->fromname) + 1);
353 strcpy(str, msginfo->fromname);
355 while (*p && !isspace(*p)) p++;
362 static void quote_fmt_show_last_name(const MsgInfo *msginfo)
367 /* This probably won't work together very well with Middle
368 names and the like - thth */
369 if (!msginfo->fromname)
372 str = alloca(strlen(msginfo->fromname) + 1);
374 strcpy(str, msginfo->fromname);
375 p = strchr(str, ',');
377 /* fromname is like "Duck, Donald" */
381 /* fromname is like "Donald Duck" */
383 while (*p && !isspace(*p)) p++;
385 /* We found a space. Get first
386 none-space char and insert
387 rest of string from there. */
388 while (*p && isspace(*p)) p++;
392 /* If there is no none-space
393 char, just insert whole
398 /* If there is no space, just
399 insert whole fromname. */
406 static void quote_fmt_show_sender_initial(const MsgInfo *msginfo)
408 #define MAX_SENDER_INITIAL 20
409 gchar tmp[MAX_SENDER_INITIAL];
414 if (!msginfo->fromname)
417 p = (guchar *)msginfo->fromname;
420 if (*p && g_utf8_validate((gchar *)p, 1, NULL)) {
424 if (len >= MAX_SENDER_INITIAL - 1)
428 while (*p && !isseparator(*p)) p++;
429 while (*p && isseparator(*p)) p++;
435 static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
436 gboolean quoted, gboolean signature,
437 const gchar *quote_str)
442 if (!(msginfo->folder || body))
446 fp = str_open_as_stream(body);
448 if (MSG_IS_ENCRYPTED(msginfo->flags))
449 fp = procmime_get_first_encrypted_text_content(msginfo);
451 fp = procmime_get_first_text_content(msginfo);
455 g_warning("Can't get text part\n");
457 while (fgets(buf, sizeof(buf), fp) != NULL) {
460 if (!signature && strncmp(buf, "-- \n", 4) == 0)
463 if (quoted && quote_str)
472 static void quote_fmt_insert_file(const gchar *filename)
477 if ((file = g_fopen(filename, "rb")) != NULL) {
478 while (fgets(buffer, sizeof(buffer), file)) {
486 static void quote_fmt_insert_program_output(const gchar *progname)
491 if ((file = popen(progname, "r")) != NULL) {
492 while (fgets(buffer, sizeof(buffer), file)) {
499 static void quote_fmt_insert_user_input(const gchar *varname)
507 if ((text = g_hash_table_lookup(var_table, varname)) == NULL) {
508 buf = g_strdup_printf(_("Enter text to replace '%s'"), varname);
509 text = input_dialog(_("Enter variable"), buf, "");
513 g_hash_table_insert(var_table, g_strdup(varname), g_strdup(text));
515 /* don't free the one in hashtable at the end */
516 text = g_strdup(text);
525 static void quote_fmt_attach_file(const gchar *filename)
527 attachments = g_list_append(attachments, g_strdup(filename));
530 static gchar *quote_fmt_complete_address(const gchar *addr)
533 gchar *res, *tmp, *email_addr;
536 debug_print("quote_fmt_complete_address: %s\n", addr);
540 /* if addr is a list of message, try the 1st element only */
541 split = g_strsplit(addr, ",", -1);
542 if (!split || !split[0] || *split[0] == '\0') {
547 Xstrdup_a(email_addr, split[0], return NULL);
548 extract_address(email_addr);
555 start_address_completion(NULL);
556 if (1 < (count = complete_address(email_addr))) {
557 tmp = get_complete_address(1);
558 res = procheader_get_fromname(tmp);
561 end_address_completion();
564 debug_print("quote_fmt_complete_address: matched %s\n", res);
576 %token SHOW_NEWSGROUPS
577 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
578 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
579 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
580 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB SHOW_MAIL_ADDRESS
581 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
582 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_EXCLAMATION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
583 %token SHOW_ACCOUNT_FULL_NAME SHOW_ACCOUNT_MAIL_ADDRESS SHOW_ACCOUNT_NAME SHOW_ACCOUNT_ORGANIZATION
584 %token SHOW_ACCOUNT_DICT
585 %token SHOW_DICT SHOW_TAGS
586 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_CC
587 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_FROM
588 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_TO
590 %token QUERY_DATE QUERY_FROM
591 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
592 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
593 %token QUERY_ACCOUNT_FULL_NAME QUERY_ACCOUNT_ORGANIZATION QUERY_ACCOUNT_DICT
595 %token QUERY_CC_FOUND_IN_ADDRESSBOOK
596 %token QUERY_FROM_FOUND_IN_ADDRESSBOOK
597 %token QUERY_TO_FOUND_IN_ADDRESSBOOK
598 /* tokens QUERY_NOT */
599 %token QUERY_NOT_DATE QUERY_NOT_FROM
600 %token QUERY_NOT_FULLNAME QUERY_NOT_SUBJECT QUERY_NOT_TO QUERY_NOT_NEWSGROUPS
601 %token QUERY_NOT_MESSAGEID QUERY_NOT_CC QUERY_NOT_REFERENCES
602 %token QUERY_NOT_ACCOUNT_FULL_NAME QUERY_NOT_ACCOUNT_ORGANIZATION QUERY_NOT_ACCOUNT_DICT
603 %token QUERY_NOT_DICT
604 %token QUERY_NOT_CC_FOUND_IN_ADDRESSBOOK
605 %token QUERY_NOT_FROM_FOUND_IN_ADDRESSBOOK
606 %token QUERY_NOT_TO_FOUND_IN_ADDRESSBOOK
608 %token INSERT_FILE INSERT_PROGRAMOUTPUT INSERT_USERINPUT
610 %token OPARENT CPARENT
612 %token SHOW_DATE_EXPR
613 %token SET_CURSOR_POS
617 %token <chr> CHARACTER
618 %type <chr> character
624 character_or_special_or_insert_or_query_list ;
627 character_or_special_list ;
629 character_or_special_or_insert_or_query_list:
630 character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
631 | character_or_special_or_insert_or_query ;
633 character_or_special_list:
634 character_or_special character_or_special_list
635 | character_or_special ;
637 character_or_special_or_insert_or_query:
644 character_or_special:
648 INSERT_CHARACTER($1);
665 strncpy($$, $1, sizeof($$));
666 $$[sizeof($$) - 1] = '\0';
668 if (len + 1 < sizeof($$)) {
677 if (msginfo->newsgroups)
678 INSERT(msginfo->newsgroups);
680 | SHOW_DATE_EXPR OPARENT string CPARENT
682 quote_fmt_show_date(msginfo, $3);
687 INSERT(msginfo->date);
692 INSERT(msginfo->from);
697 gchar *stripped_address = g_strdup(msginfo->from);
698 extract_address(stripped_address);
699 INSERT(stripped_address);
700 g_free(stripped_address);
705 if (msginfo->fromname)
706 INSERT(msginfo->fromname);
710 quote_fmt_show_first_name(msginfo);
714 quote_fmt_show_last_name(msginfo);
716 | SHOW_SENDER_INITIAL
718 quote_fmt_show_sender_initial(msginfo);
722 if (msginfo->subject)
723 INSERT(msginfo->subject);
733 INSERT(msginfo->msgid);
748 INSERT(msginfo->inreplyto);
749 for (item = msginfo->references; item != NULL; item = g_slist_next(item))
755 quote_fmt_show_msg(msginfo, body, FALSE, TRUE, quote_str);
757 | SHOW_QUOTED_MESSAGE
759 quote_fmt_show_msg(msginfo, body, TRUE, TRUE, quote_str);
761 | SHOW_MESSAGE_NO_SIGNATURE
763 quote_fmt_show_msg(msginfo, body, FALSE, FALSE, quote_str);
765 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
767 quote_fmt_show_msg(msginfo, body, TRUE, FALSE, quote_str);
769 | SHOW_ACCOUNT_FULL_NAME
771 if (account && account->name)
772 INSERT(account->name);
774 | SHOW_ACCOUNT_MAIL_ADDRESS
776 if (account && account->address)
777 INSERT(account->address);
781 if (account && account->account_name)
782 INSERT(account->account_name);
784 | SHOW_ACCOUNT_ORGANIZATION
786 if (account && account->organization)
787 INSERT(account->organization);
792 if (account && account->enable_default_dictionary) {
793 gchar *dictname = g_path_get_basename(account->default_dictionary);
802 INSERT(default_dictionary);
807 gchar *tags = procmsg_msginfo_get_tags_str(msginfo);
829 | SHOW_EXCLAMATION_MARK
848 cursor_pos = g_utf8_strlen(current->buffer, -1);
852 | SHOW_ADDRESSBOOK_COMPLETION_FOR_CC
854 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
860 | SHOW_ADDRESSBOOK_COMPLETION_FOR_FROM
862 gchar *tmp = quote_fmt_complete_address(msginfo->from);
868 | SHOW_ADDRESSBOOK_COMPLETION_FOR_TO
870 gchar *tmp = quote_fmt_complete_address(msginfo->to);
880 add_visibility(msginfo->date != NULL);
882 OPARENT quote_fmt CPARENT
888 add_visibility(msginfo->from != NULL);
890 OPARENT quote_fmt CPARENT
896 add_visibility(msginfo->fromname != NULL);
898 OPARENT quote_fmt CPARENT
904 add_visibility(msginfo->subject != NULL);
906 OPARENT quote_fmt CPARENT
912 add_visibility(msginfo->to != NULL);
914 OPARENT quote_fmt CPARENT
920 add_visibility(msginfo->newsgroups != NULL);
922 OPARENT quote_fmt CPARENT
928 add_visibility(msginfo->msgid != NULL);
930 OPARENT quote_fmt CPARENT
936 add_visibility(msginfo->cc != NULL);
938 OPARENT quote_fmt CPARENT
947 found = (msginfo->inreplyto != NULL);
948 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
951 add_visibility(found == TRUE);
953 OPARENT quote_fmt CPARENT
957 | QUERY_ACCOUNT_FULL_NAME
959 add_visibility(account != NULL && account->name != NULL);
961 OPARENT quote_fmt CPARENT
965 | QUERY_ACCOUNT_ORGANIZATION
967 add_visibility(account != NULL && account->organization != NULL);
969 OPARENT quote_fmt CPARENT
976 add_visibility(account != NULL && account->enable_default_dictionary == TRUE &&
977 account->default_dictionary != NULL && *account->default_dictionary != '\0');
979 add_visibility(FALSE);
982 OPARENT quote_fmt CPARENT
989 add_visibility(*default_dictionary != '\0');
991 add_visibility(FALSE);
994 OPARENT quote_fmt CPARENT
998 | QUERY_CC_FOUND_IN_ADDRESSBOOK
1000 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
1001 add_visibility(tmp != NULL && *tmp != '\0');
1004 OPARENT quote_fmt CPARENT
1006 remove_visibility();
1008 | QUERY_FROM_FOUND_IN_ADDRESSBOOK
1010 gchar *tmp = quote_fmt_complete_address(msginfo->from);
1011 add_visibility(tmp != NULL && *tmp != '\0');
1014 OPARENT quote_fmt CPARENT
1016 remove_visibility();
1018 | QUERY_TO_FOUND_IN_ADDRESSBOOK
1020 gchar *tmp = quote_fmt_complete_address(msginfo->to);
1021 add_visibility(tmp != NULL && *tmp != '\0');
1024 OPARENT quote_fmt CPARENT
1026 remove_visibility();
1032 add_visibility(msginfo->date == NULL);
1034 OPARENT quote_fmt CPARENT
1036 remove_visibility();
1040 add_visibility(msginfo->from == NULL);
1042 OPARENT quote_fmt CPARENT
1044 remove_visibility();
1046 | QUERY_NOT_FULLNAME
1048 add_visibility(msginfo->fromname == NULL);
1050 OPARENT quote_fmt CPARENT
1052 remove_visibility();
1056 add_visibility(msginfo->subject == NULL);
1058 OPARENT quote_fmt CPARENT
1060 remove_visibility();
1064 add_visibility(msginfo->to == NULL);
1066 OPARENT quote_fmt CPARENT
1068 remove_visibility();
1070 | QUERY_NOT_NEWSGROUPS
1072 add_visibility(msginfo->newsgroups == NULL);
1074 OPARENT quote_fmt CPARENT
1076 remove_visibility();
1078 | QUERY_NOT_MESSAGEID
1080 add_visibility(msginfo->msgid == NULL);
1082 OPARENT quote_fmt CPARENT
1084 remove_visibility();
1088 add_visibility(msginfo->cc == NULL);
1090 OPARENT quote_fmt CPARENT
1092 remove_visibility();
1094 | QUERY_NOT_REFERENCES
1099 found = (msginfo->inreplyto != NULL);
1100 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
1103 add_visibility(found == FALSE);
1105 OPARENT quote_fmt CPARENT
1107 remove_visibility();
1109 | QUERY_NOT_ACCOUNT_FULL_NAME
1111 add_visibility(account == NULL || account->name == NULL);
1113 OPARENT quote_fmt CPARENT
1115 remove_visibility();
1117 | QUERY_NOT_ACCOUNT_ORGANIZATION
1119 add_visibility(account == NULL || account->organization == NULL);
1121 OPARENT quote_fmt CPARENT
1123 remove_visibility();
1125 | QUERY_NOT_ACCOUNT_DICT
1128 add_visibility(account == NULL || account->enable_default_dictionary == FALSE
1129 || *account->default_dictionary == '\0');
1131 add_visibility(FALSE);
1134 OPARENT quote_fmt CPARENT
1136 remove_visibility();
1141 add_visibility(*default_dictionary == '\0');
1143 add_visibility(FALSE);
1146 OPARENT quote_fmt CPARENT
1148 remove_visibility();
1150 | QUERY_NOT_CC_FOUND_IN_ADDRESSBOOK
1152 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
1153 add_visibility(tmp == NULL || *tmp == '\0');
1156 OPARENT quote_fmt CPARENT
1158 remove_visibility();
1160 | QUERY_NOT_FROM_FOUND_IN_ADDRESSBOOK
1162 gchar *tmp = quote_fmt_complete_address(msginfo->from);
1163 add_visibility(tmp == NULL || *tmp == '\0');
1166 OPARENT quote_fmt CPARENT
1168 remove_visibility();
1170 | QUERY_NOT_TO_FOUND_IN_ADDRESSBOOK
1172 gchar *tmp = quote_fmt_complete_address(msginfo->to);
1173 add_visibility(tmp == NULL || *tmp == '\0');
1176 OPARENT quote_fmt CPARENT
1178 remove_visibility();
1184 current = &sub_expr;
1187 OPARENT sub_expr CPARENT
1189 current = &main_expr;
1191 quote_fmt_insert_file(sub_expr.buffer);
1194 | INSERT_PROGRAMOUTPUT
1196 current = &sub_expr;
1199 OPARENT sub_expr CPARENT
1201 current = &main_expr;
1203 quote_fmt_insert_program_output(sub_expr.buffer);
1208 current = &sub_expr;
1211 OPARENT sub_expr CPARENT
1213 current = &main_expr;
1215 quote_fmt_insert_user_input(sub_expr.buffer);
1222 current = &sub_expr;
1225 OPARENT sub_expr CPARENT
1227 current = &main_expr;
1229 quote_fmt_attach_file(sub_expr.buffer);