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;
80 static void add_visibility(gboolean val)
83 if (maxsize < stacksize) {
85 visible = g_realloc(visible, maxsize * sizeof(gboolean));
90 visible[stacksize - 1] = val;
93 static void remove_visibility(void)
97 g_warning("Error: visibility stack underflow\n");
102 static void add_buffer(const gchar *s)
110 if (current->bufsize + len + 1 > current->bufmax) {
111 if (current->bufmax == 0)
112 current->bufmax = 128;
113 while (current->bufsize + len + 1 > current->bufmax)
114 current->bufmax *= 2;
115 current->buffer = g_realloc(current->buffer, current->bufmax);
117 strcpy(current->buffer + current->bufsize, s);
118 current->bufsize += len;
121 static void clear_buffer(void)
124 *current->buffer = '\0';
126 /* force to an empty string, as buffer should not be left unallocated */
128 current->bufsize = 0;
131 gchar *quote_fmt_get_buffer(void)
133 if (current != &main_expr)
134 g_warning("Error: parser still in sub-expr mode\n");
139 return current->buffer;
142 GList *quote_fmt_get_attachments_list(void)
147 gint quote_fmt_get_line(void)
152 gint quote_fmt_get_cursor_pos(void)
157 #define INSERT(buf) \
158 if (stacksize != 0 && visible[stacksize - 1])\
161 #define INSERT_CHARACTER(chr) \
162 if (stacksize != 0 && visible[stacksize - 1]) { \
169 void quote_fmt_reset_vartable(void)
172 g_hash_table_destroy(var_table);
176 GList *cur = attachments;
179 cur = g_list_next(cur);
181 g_list_free(attachments);
187 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
188 const gchar *my_body, gboolean my_dry_run,
189 PrefsAccount *compose_account,
190 GtkAspell *compose_gtkaspell)
192 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
193 const gchar *my_body, gboolean my_dry_run,
194 PrefsAccount *compose_account)
197 quote_str = my_quote_str;
200 account = compose_account;
202 gchar *dict = gtkaspell_get_default_dictionary(compose_gtkaspell);
204 strncpy2(default_dictionary, dict, sizeof(default_dictionary));
206 *default_dictionary = '\0';
208 dry_run = my_dry_run;
210 add_visibility(TRUE);
211 main_expr.bufmax = 0;
213 current = &main_expr;
219 var_table = g_hash_table_new_full(g_str_hash, g_str_equal,
223 * force LEX initialization
225 quote_fmt_firsttime = 1;
229 void quote_fmterror(char *str)
231 g_warning("Error: %s at line %d\n", str, line);
235 int quote_fmtwrap(void)
240 static int isseparator(int ch)
242 return g_ascii_isspace(ch) || ch == '.' || ch == '-';
245 static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
258 * ALF - GNU C's strftime() has a nice format specifier
259 * for time zone offset (%z). Non-standard however, so
263 #define RLEFT (sizeof result) - (rptr - result)
264 #define STR_SIZE(x) (sizeof (x) - 1)
268 if (procheader_date_parse_to_tm(msginfo->date, <, zone)) {
270 * break up format string in tiny bits delimited by valid %z's and
271 * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
273 for (rptr = result, fptr = format; fptr && *fptr && rptr < &result[sizeof result - 1];) {
278 if (NULL != (zptr = strstr(fptr, "%z"))) {
280 * count nr. of prepended percent chars
282 for (perc = 0, p = zptr; p && p >= format && *p == '%'; p--, perc++)
287 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
289 rptr += strftime(rptr, RLEFT, tmp, <);
293 * append time zone offset
295 if (zone[0] && perc % 2)
296 rptr += g_snprintf(rptr, RLEFT, "%s", zone);
297 fptr = zptr + STR_SIZE("%z");
299 rptr += strftime(rptr, RLEFT, fptr, <);
304 if (g_utf8_validate(result, -1, NULL)) {
307 gchar *utf = conv_codeset_strdup(result,
308 conv_get_locale_charset_str_no_utf8(),
311 !g_utf8_validate(utf, -1, NULL)) {
313 utf = g_malloc(strlen(result)*2+1);
314 conv_localetodisp(utf,
315 strlen(result)*2+1, result);
317 if (g_utf8_validate(utf, -1, NULL)) {
327 static void quote_fmt_show_first_name(const MsgInfo *msginfo)
332 if (!msginfo->fromname)
335 p = (guchar*)strchr(msginfo->fromname, ',');
337 /* fromname is like "Duck, Donald" */
339 while (*p && isspace(*p)) p++;
340 str = alloca(strlen((char *)p) + 1);
342 strcpy(str, (char *)p);
346 /* fromname is like "Donald Duck" */
347 str = alloca(strlen(msginfo->fromname) + 1);
349 strcpy(str, msginfo->fromname);
351 while (*p && !isspace(*p)) p++;
358 static void quote_fmt_show_last_name(const MsgInfo *msginfo)
363 /* This probably won't work together very well with Middle
364 names and the like - thth */
365 if (!msginfo->fromname)
368 str = alloca(strlen(msginfo->fromname) + 1);
370 strcpy(str, msginfo->fromname);
371 p = strchr(str, ',');
373 /* fromname is like "Duck, Donald" */
377 /* fromname is like "Donald Duck" */
379 while (*p && !isspace(*p)) p++;
381 /* We found a space. Get first
382 none-space char and insert
383 rest of string from there. */
384 while (*p && isspace(*p)) p++;
388 /* If there is no none-space
389 char, just insert whole
394 /* If there is no space, just
395 insert whole fromname. */
402 static void quote_fmt_show_sender_initial(const MsgInfo *msginfo)
404 #define MAX_SENDER_INITIAL 20
405 gchar tmp[MAX_SENDER_INITIAL];
410 if (!msginfo->fromname)
413 p = (guchar *)msginfo->fromname;
416 if (*p && g_utf8_validate((gchar *)p, 1, NULL)) {
420 if (len >= MAX_SENDER_INITIAL - 1)
424 while (*p && !isseparator(*p)) p++;
425 while (*p && isseparator(*p)) p++;
431 static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
432 gboolean quoted, gboolean signature,
433 const gchar *quote_str)
438 if (!(msginfo->folder || body))
442 fp = str_open_as_stream(body);
444 if (MSG_IS_ENCRYPTED(msginfo->flags))
445 fp = procmime_get_first_encrypted_text_content(msginfo);
447 fp = procmime_get_first_text_content(msginfo);
451 g_warning("Can't get text part\n");
453 while (fgets(buf, sizeof(buf), fp) != NULL) {
456 if (!signature && strncmp(buf, "-- \n", 4) == 0)
459 if (quoted && quote_str)
468 static void quote_fmt_insert_file(const gchar *filename)
473 if ((file = g_fopen(filename, "rb")) != NULL) {
474 while (fgets(buffer, sizeof(buffer), file)) {
482 static void quote_fmt_insert_program_output(const gchar *progname)
487 if ((file = popen(progname, "r")) != NULL) {
488 while (fgets(buffer, sizeof(buffer), file)) {
495 static void quote_fmt_insert_user_input(const gchar *varname)
503 if ((text = g_hash_table_lookup(var_table, varname)) == NULL) {
504 buf = g_strdup_printf(_("Enter text to replace '%s'"), varname);
505 text = input_dialog(_("Enter variable"), buf, "");
509 g_hash_table_insert(var_table, g_strdup(varname), g_strdup(text));
511 /* don't free the one in hashtable at the end */
512 text = g_strdup(text);
521 static void quote_fmt_attach_file(const gchar *filename)
523 attachments = g_list_append(attachments, g_strdup(filename));
526 static gchar *quote_fmt_complete_address(const gchar *addr)
529 gchar *res, *tmp, *email_addr;
532 debug_print("quote_fmt_complete_address: %s\n", addr);
536 /* if addr is a list of message, try the 1st element only */
537 split = g_strsplit(addr, ",", -1);
538 if (!split || !split[0] || *split[0] == '\0') {
543 Xstrdup_a(email_addr, split[0], return NULL);
544 extract_address(email_addr);
551 start_address_completion(NULL);
552 if (1 < (count = complete_address(email_addr))) {
553 tmp = get_complete_address(1);
554 res = procheader_get_fromname(tmp);
557 end_address_completion();
560 debug_print("quote_fmt_complete_address: matched %s\n", res);
572 %token SHOW_NEWSGROUPS
573 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
574 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
575 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
576 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB SHOW_MAIL_ADDRESS
577 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
578 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_EXCLAMATION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
579 %token SHOW_ACCOUNT_FULL_NAME SHOW_ACCOUNT_MAIL_ADDRESS SHOW_ACCOUNT_NAME SHOW_ACCOUNT_ORGANIZATION
580 %token SHOW_ACCOUNT_DICT
581 %token SHOW_DICT SHOW_TAGS
582 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_CC
583 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_FROM
584 %token SHOW_ADDRESSBOOK_COMPLETION_FOR_TO
586 %token QUERY_DATE QUERY_FROM
587 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
588 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
589 %token QUERY_ACCOUNT_FULL_NAME QUERY_ACCOUNT_ORGANIZATION QUERY_ACCOUNT_DICT
591 %token QUERY_CC_FOUND_IN_ADDRESSBOOK
592 %token QUERY_FROM_FOUND_IN_ADDRESSBOOK
593 %token QUERY_TO_FOUND_IN_ADDRESSBOOK
594 /* tokens QUERY_NOT */
595 %token QUERY_NOT_DATE QUERY_NOT_FROM
596 %token QUERY_NOT_FULLNAME QUERY_NOT_SUBJECT QUERY_NOT_TO QUERY_NOT_NEWSGROUPS
597 %token QUERY_NOT_MESSAGEID QUERY_NOT_CC QUERY_NOT_REFERENCES
598 %token QUERY_NOT_ACCOUNT_FULL_NAME QUERY_NOT_ACCOUNT_ORGANIZATION QUERY_NOT_ACCOUNT_DICT
599 %token QUERY_NOT_DICT
600 %token QUERY_NOT_CC_FOUND_IN_ADDRESSBOOK
601 %token QUERY_NOT_FROM_FOUND_IN_ADDRESSBOOK
602 %token QUERY_NOT_TO_FOUND_IN_ADDRESSBOOK
604 %token INSERT_FILE INSERT_PROGRAMOUTPUT INSERT_USERINPUT
606 %token OPARENT CPARENT
608 %token SHOW_DATE_EXPR
609 %token SET_CURSOR_POS
613 %token <chr> CHARACTER
614 %type <chr> character
620 character_or_special_or_insert_or_query_list ;
623 character_or_special_list ;
625 character_or_special_or_insert_or_query_list:
626 character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
627 | character_or_special_or_insert_or_query ;
629 character_or_special_list:
630 character_or_special character_or_special_list
631 | character_or_special ;
633 character_or_special_or_insert_or_query:
640 character_or_special:
644 INSERT_CHARACTER($1);
661 strncpy($$, $1, sizeof($$));
662 $$[sizeof($$) - 1] = '\0';
664 if (len + 1 < sizeof($$)) {
673 if (msginfo->newsgroups)
674 INSERT(msginfo->newsgroups);
676 | SHOW_DATE_EXPR OPARENT string CPARENT
678 quote_fmt_show_date(msginfo, $3);
683 INSERT(msginfo->date);
688 INSERT(msginfo->from);
693 gchar *stripped_address = g_strdup(msginfo->from);
694 extract_address(stripped_address);
695 INSERT(stripped_address);
696 g_free(stripped_address);
701 if (msginfo->fromname)
702 INSERT(msginfo->fromname);
706 quote_fmt_show_first_name(msginfo);
710 quote_fmt_show_last_name(msginfo);
712 | SHOW_SENDER_INITIAL
714 quote_fmt_show_sender_initial(msginfo);
718 if (msginfo->subject)
719 INSERT(msginfo->subject);
729 INSERT(msginfo->msgid);
744 INSERT(msginfo->inreplyto);
745 for (item = msginfo->references; item != NULL; item = g_slist_next(item))
751 quote_fmt_show_msg(msginfo, body, FALSE, TRUE, quote_str);
753 | SHOW_QUOTED_MESSAGE
755 quote_fmt_show_msg(msginfo, body, TRUE, TRUE, quote_str);
757 | SHOW_MESSAGE_NO_SIGNATURE
759 quote_fmt_show_msg(msginfo, body, FALSE, FALSE, quote_str);
761 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
763 quote_fmt_show_msg(msginfo, body, TRUE, FALSE, quote_str);
765 | SHOW_ACCOUNT_FULL_NAME
767 if (account && account->name)
768 INSERT(account->name);
770 | SHOW_ACCOUNT_MAIL_ADDRESS
772 if (account && account->address)
773 INSERT(account->address);
777 if (account && account->account_name)
778 INSERT(account->account_name);
780 | SHOW_ACCOUNT_ORGANIZATION
782 if (account && account->organization)
783 INSERT(account->organization);
788 if (account && account->enable_default_dictionary) {
789 gchar *dictname = g_path_get_basename(account->default_dictionary);
798 INSERT(default_dictionary);
803 gchar *tags = procmsg_msginfo_get_tags_str(msginfo);
825 | SHOW_EXCLAMATION_MARK
844 cursor_pos = g_utf8_strlen(current->buffer, -1);
848 | SHOW_ADDRESSBOOK_COMPLETION_FOR_CC
850 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
856 | SHOW_ADDRESSBOOK_COMPLETION_FOR_FROM
858 gchar *tmp = quote_fmt_complete_address(msginfo->from);
864 | SHOW_ADDRESSBOOK_COMPLETION_FOR_TO
866 gchar *tmp = quote_fmt_complete_address(msginfo->to);
876 add_visibility(msginfo->date != NULL);
878 OPARENT quote_fmt CPARENT
884 add_visibility(msginfo->from != NULL);
886 OPARENT quote_fmt CPARENT
892 add_visibility(msginfo->fromname != NULL);
894 OPARENT quote_fmt CPARENT
900 add_visibility(msginfo->subject != NULL);
902 OPARENT quote_fmt CPARENT
908 add_visibility(msginfo->to != NULL);
910 OPARENT quote_fmt CPARENT
916 add_visibility(msginfo->newsgroups != NULL);
918 OPARENT quote_fmt CPARENT
924 add_visibility(msginfo->msgid != NULL);
926 OPARENT quote_fmt CPARENT
932 add_visibility(msginfo->cc != NULL);
934 OPARENT quote_fmt CPARENT
943 found = (msginfo->inreplyto != NULL);
944 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
947 add_visibility(found == TRUE);
949 OPARENT quote_fmt CPARENT
953 | QUERY_ACCOUNT_FULL_NAME
955 add_visibility(account != NULL && account->name != NULL);
957 OPARENT quote_fmt CPARENT
961 | QUERY_ACCOUNT_ORGANIZATION
963 add_visibility(account != NULL && account->organization != NULL);
965 OPARENT quote_fmt CPARENT
972 add_visibility(account != NULL && account->enable_default_dictionary == TRUE &&
973 account->default_dictionary != NULL && *account->default_dictionary != '\0');
975 add_visibility(FALSE);
978 OPARENT quote_fmt CPARENT
985 add_visibility(*default_dictionary != '\0');
987 add_visibility(FALSE);
990 OPARENT quote_fmt CPARENT
994 | QUERY_CC_FOUND_IN_ADDRESSBOOK
996 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
997 add_visibility(tmp != NULL && *tmp != '\0');
1000 OPARENT quote_fmt CPARENT
1002 remove_visibility();
1004 | QUERY_FROM_FOUND_IN_ADDRESSBOOK
1006 gchar *tmp = quote_fmt_complete_address(msginfo->from);
1007 add_visibility(tmp != NULL && *tmp != '\0');
1010 OPARENT quote_fmt CPARENT
1012 remove_visibility();
1014 | QUERY_TO_FOUND_IN_ADDRESSBOOK
1016 gchar *tmp = quote_fmt_complete_address(msginfo->to);
1017 add_visibility(tmp != NULL && *tmp != '\0');
1020 OPARENT quote_fmt CPARENT
1022 remove_visibility();
1028 add_visibility(msginfo->date == NULL);
1030 OPARENT quote_fmt CPARENT
1032 remove_visibility();
1036 add_visibility(msginfo->from == NULL);
1038 OPARENT quote_fmt CPARENT
1040 remove_visibility();
1042 | QUERY_NOT_FULLNAME
1044 add_visibility(msginfo->fromname == NULL);
1046 OPARENT quote_fmt CPARENT
1048 remove_visibility();
1052 add_visibility(msginfo->subject == NULL);
1054 OPARENT quote_fmt CPARENT
1056 remove_visibility();
1060 add_visibility(msginfo->to == NULL);
1062 OPARENT quote_fmt CPARENT
1064 remove_visibility();
1066 | QUERY_NOT_NEWSGROUPS
1068 add_visibility(msginfo->newsgroups == NULL);
1070 OPARENT quote_fmt CPARENT
1072 remove_visibility();
1074 | QUERY_NOT_MESSAGEID
1076 add_visibility(msginfo->msgid == NULL);
1078 OPARENT quote_fmt CPARENT
1080 remove_visibility();
1084 add_visibility(msginfo->cc == NULL);
1086 OPARENT quote_fmt CPARENT
1088 remove_visibility();
1090 | QUERY_NOT_REFERENCES
1095 found = (msginfo->inreplyto != NULL);
1096 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
1099 add_visibility(found == FALSE);
1101 OPARENT quote_fmt CPARENT
1103 remove_visibility();
1105 | QUERY_NOT_ACCOUNT_FULL_NAME
1107 add_visibility(account == NULL || account->name == NULL);
1109 OPARENT quote_fmt CPARENT
1111 remove_visibility();
1113 | QUERY_NOT_ACCOUNT_ORGANIZATION
1115 add_visibility(account == NULL || account->organization == NULL);
1117 OPARENT quote_fmt CPARENT
1119 remove_visibility();
1121 | QUERY_NOT_ACCOUNT_DICT
1124 add_visibility(account == NULL || account->enable_default_dictionary == FALSE
1125 || *account->default_dictionary == '\0');
1127 add_visibility(FALSE);
1130 OPARENT quote_fmt CPARENT
1132 remove_visibility();
1137 add_visibility(*default_dictionary == '\0');
1139 add_visibility(FALSE);
1142 OPARENT quote_fmt CPARENT
1144 remove_visibility();
1146 | QUERY_NOT_CC_FOUND_IN_ADDRESSBOOK
1148 gchar *tmp = quote_fmt_complete_address(msginfo->cc);
1149 add_visibility(tmp == NULL || *tmp == '\0');
1152 OPARENT quote_fmt CPARENT
1154 remove_visibility();
1156 | QUERY_NOT_FROM_FOUND_IN_ADDRESSBOOK
1158 gchar *tmp = quote_fmt_complete_address(msginfo->from);
1159 add_visibility(tmp == NULL || *tmp == '\0');
1162 OPARENT quote_fmt CPARENT
1164 remove_visibility();
1166 | QUERY_NOT_TO_FOUND_IN_ADDRESSBOOK
1168 gchar *tmp = quote_fmt_complete_address(msginfo->to);
1169 add_visibility(tmp == NULL || *tmp == '\0');
1172 OPARENT quote_fmt CPARENT
1174 remove_visibility();
1180 current = &sub_expr;
1183 OPARENT sub_expr CPARENT
1185 current = &main_expr;
1187 quote_fmt_insert_file(sub_expr.buffer);
1190 | INSERT_PROGRAMOUTPUT
1192 current = &sub_expr;
1195 OPARENT sub_expr CPARENT
1197 current = &main_expr;
1199 quote_fmt_insert_program_output(sub_expr.buffer);
1204 current = &sub_expr;
1207 OPARENT sub_expr CPARENT
1209 current = &main_expr;
1211 quote_fmt_insert_user_input(sub_expr.buffer);
1218 current = &sub_expr;
1221 OPARENT sub_expr CPARENT
1223 current = &main_expr;
1225 quote_fmt_attach_file(sub_expr.buffer);