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 2 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include <glib/gi18n.h>
33 #include "procheader.h"
34 #include "gtk/inputdialog.h"
36 #include "quote_fmt.h"
37 #include "quote_fmt_lex.h"
42 bison -p quote_fmt quote_fmt.y
47 static MsgInfo *msginfo = NULL;
48 static PrefsAccount *account = NULL;
49 static gboolean *visible = NULL;
50 static gboolean dry_run = FALSE;
51 static gint maxsize = 0;
52 static gint stacksize = 0;
53 static GHashTable *var_table = NULL;
55 typedef struct st_buffer
62 static struct st_buffer main_expr = { NULL, 0, 0 };
63 static struct st_buffer sub_expr = { NULL, 0, 0 };
64 static struct st_buffer* current = NULL;
66 static const gchar *quote_str = NULL;
67 static const gchar *body = NULL;
68 static gint error = 0;
70 static gint cursor_pos = -1;
72 extern int quote_fmt_firsttime;
74 static void add_visibility(gboolean val)
77 if (maxsize < stacksize) {
79 visible = g_realloc(visible, maxsize * sizeof(gboolean));
84 visible[stacksize - 1] = val;
87 static void remove_visibility(void)
91 g_warning("Error: visibility stack underflow\n");
96 static void add_buffer(const gchar *s)
104 if (current->bufsize + len + 1 > current->bufmax) {
105 if (current->bufmax == 0)
106 current->bufmax = 128;
107 while (current->bufsize + len + 1 > current->bufmax)
108 current->bufmax *= 2;
109 current->buffer = g_realloc(current->buffer, current->bufmax);
111 strcpy(current->buffer + current->bufsize, s);
112 current->bufsize += len;
115 static void clear_buffer(void)
118 *current->buffer = '\0';
120 /* force to an empty string, as buffer should not be left unallocated */
122 current->bufsize = 0;
125 gchar *quote_fmt_get_buffer(void)
127 if (current != &main_expr)
128 g_warning("Error: parser still in sub-expr mode\n");
133 return current->buffer;
136 gint quote_fmt_get_cursor_pos(void)
141 #define INSERT(buf) \
142 if (stacksize != 0 && visible[stacksize - 1])\
145 #define INSERT_CHARACTER(chr) \
146 if (stacksize != 0 && visible[stacksize - 1]) { \
153 void quote_fmt_reset_vartable(void)
156 g_hash_table_destroy(var_table);
161 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
162 const gchar *my_body, gboolean my_dry_run,
163 PrefsAccount *compose_account)
165 quote_str = my_quote_str;
168 account = compose_account;
169 dry_run = my_dry_run;
171 add_visibility(TRUE);
172 main_expr.bufmax = 0;
174 current = &main_expr;
179 var_table = g_hash_table_new_full(g_str_hash, g_str_equal,
183 * force LEX initialization
185 quote_fmt_firsttime = 1;
189 void quote_fmterror(char *str)
191 g_warning("Error: %s\n", str);
195 int quote_fmtwrap(void)
200 static int isseparator(int ch)
202 return g_ascii_isspace(ch) || ch == '.' || ch == '-';
205 static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
218 * ALF - GNU C's strftime() has a nice format specifier
219 * for time zone offset (%z). Non-standard however, so
223 #define RLEFT (sizeof result) - (rptr - result)
224 #define STR_SIZE(x) (sizeof (x) - 1)
228 if (procheader_date_parse_to_tm(msginfo->date, <, zone)) {
230 * break up format string in tiny bits delimited by valid %z's and
231 * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
233 for (rptr = result, fptr = format; fptr && *fptr && rptr < &result[sizeof result - 1];) {
238 if (NULL != (zptr = strstr(fptr, "%z"))) {
240 * count nr. of prepended percent chars
242 for (perc = 0, p = zptr; p && p >= format && *p == '%'; p--, perc++)
247 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
249 rptr += strftime(rptr, RLEFT, tmp, <);
253 * append time zone offset
255 if (zone[0] && perc % 2)
256 rptr += g_snprintf(rptr, RLEFT, "%s", zone);
257 fptr = zptr + STR_SIZE("%z");
259 rptr += strftime(rptr, RLEFT, fptr, <);
264 if (g_utf8_validate(result, -1, NULL)) {
267 gchar *utf = conv_codeset_strdup(result,
268 conv_get_locale_charset_str_no_utf8(),
271 !g_utf8_validate(utf, -1, NULL)) {
273 utf = g_malloc(strlen(result)*2+1);
274 conv_localetodisp(utf,
275 strlen(result)*2+1, result);
277 if (g_utf8_validate(utf, -1, NULL)) {
287 static void quote_fmt_show_first_name(const MsgInfo *msginfo)
292 if (!msginfo->fromname)
295 p = (guchar*)strchr(msginfo->fromname, ',');
297 /* fromname is like "Duck, Donald" */
299 while (*p && isspace(*p)) p++;
300 str = alloca(strlen((char *)p) + 1);
302 strcpy(str, (char *)p);
306 /* fromname is like "Donald Duck" */
307 str = alloca(strlen(msginfo->fromname) + 1);
309 strcpy(str, msginfo->fromname);
311 while (*p && !isspace(*p)) p++;
318 static void quote_fmt_show_last_name(const MsgInfo *msginfo)
323 /* This probably won't work together very well with Middle
324 names and the like - thth */
325 if (!msginfo->fromname)
328 str = alloca(strlen(msginfo->fromname) + 1);
330 strcpy(str, msginfo->fromname);
331 p = strchr(str, ',');
333 /* fromname is like "Duck, Donald" */
337 /* fromname is like "Donald Duck" */
339 while (*p && !isspace(*p)) p++;
341 /* We found a space. Get first
342 none-space char and insert
343 rest of string from there. */
344 while (*p && isspace(*p)) p++;
348 /* If there is no none-space
349 char, just insert whole
354 /* If there is no space, just
355 insert whole fromname. */
362 static void quote_fmt_show_sender_initial(const MsgInfo *msginfo)
364 #define MAX_SENDER_INITIAL 20
365 gchar tmp[MAX_SENDER_INITIAL];
370 if (!msginfo->fromname)
373 p = (guchar *)msginfo->fromname;
376 if (*p && g_utf8_validate((gchar *)p, 1, NULL)) {
380 if (len >= MAX_SENDER_INITIAL - 1)
384 while (*p && !isseparator(*p)) p++;
385 while (*p && isseparator(*p)) p++;
391 static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
392 gboolean quoted, gboolean signature,
393 const gchar *quote_str)
398 if (!(msginfo->folder || body))
402 fp = str_open_as_stream(body);
404 if (procmime_msginfo_is_encrypted(msginfo))
405 fp = procmime_get_first_encrypted_text_content(msginfo);
407 fp = procmime_get_first_text_content(msginfo);
411 g_warning("Can't get text part\n");
413 while (fgets(buf, sizeof(buf), fp) != NULL) {
416 if (!signature && strncmp(buf, "-- \n", 4) == 0)
419 if (quoted && quote_str)
428 static void quote_fmt_insert_file(const gchar *filename)
433 if ((file = g_fopen(filename, "rb")) != NULL) {
434 while (fgets(buffer, sizeof(buffer), file)) {
442 static void quote_fmt_insert_program_output(const gchar *progname)
447 if ((file = popen(progname, "r")) != NULL) {
448 while (fgets(buffer, sizeof(buffer), file)) {
455 static void quote_fmt_insert_user_input(const gchar *varname)
463 if ((text = g_hash_table_lookup(var_table, varname)) == NULL) {
464 buf = g_strdup_printf(_("Enter text to replace '%s'"), varname);
465 text = input_dialog(_("Enter variable"), buf, "");
469 g_hash_table_insert(var_table, g_strdup(varname), g_strdup(text));
471 /* don't free the one in hashtable at the end */
472 text = g_strdup(text);
489 %token SHOW_NEWSGROUPS
490 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
491 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
492 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
493 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB SHOW_MAIL_ADDRESS
494 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
495 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_EXCLAMATION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
496 %token SHOW_ACCOUNT_FULL_NAME SHOW_ACCOUNT_MAIL_ADDRESS SHOW_ACCOUNT_NAME SHOW_ACCOUNT_ORGANIZATION
498 %token QUERY_DATE QUERY_FROM
499 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
500 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
501 %token QUERY_ACCOUNT_FULL_NAME QUERY_ACCOUNT_ORGANIZATION
502 /* tokens QUERY_NOT */
503 %token QUERY_NOT_DATE QUERY_NOT_FROM
504 %token QUERY_NOT_FULLNAME QUERY_NOT_SUBJECT QUERY_NOT_TO QUERY_NOT_NEWSGROUPS
505 %token QUERY_NOT_MESSAGEID QUERY_NOT_CC QUERY_NOT_REFERENCES
506 %token QUERY_NOT_ACCOUNT_FULL_NAME QUERY_NOT_ACCOUNT_ORGANIZATION
508 %token INSERT_FILE INSERT_PROGRAMOUTPUT INSERT_USERINPUT
509 %token OPARENT CPARENT
511 %token SHOW_DATE_EXPR
512 %token SET_CURSOR_POS
516 %token <chr> CHARACTER
517 %type <chr> character
523 character_or_special_or_insert_or_query_list ;
526 character_or_special_list ;
528 character_or_special_or_insert_or_query_list:
529 character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
530 | character_or_special_or_insert_or_query ;
532 character_or_special_list:
533 character_or_special character_or_special_list
534 | character_or_special ;
536 character_or_special_or_insert_or_query:
542 character_or_special:
546 INSERT_CHARACTER($1);
563 strncpy($$, $1, sizeof($$));
564 $$[sizeof($$) - 1] = '\0';
566 if (len + 1 < sizeof($$)) {
575 if (msginfo->newsgroups)
576 INSERT(msginfo->newsgroups);
578 | SHOW_DATE_EXPR OPARENT string CPARENT
580 quote_fmt_show_date(msginfo, $3);
585 INSERT(msginfo->date);
590 INSERT(msginfo->from);
595 gchar *stripped_address = g_strdup(msginfo->from);
596 extract_address(stripped_address);
597 INSERT(stripped_address);
598 g_free(stripped_address);
603 if (msginfo->fromname)
604 INSERT(msginfo->fromname);
608 quote_fmt_show_first_name(msginfo);
612 quote_fmt_show_last_name(msginfo);
614 | SHOW_SENDER_INITIAL
616 quote_fmt_show_sender_initial(msginfo);
620 if (msginfo->subject)
621 INSERT(msginfo->subject);
631 INSERT(msginfo->msgid);
646 INSERT(msginfo->inreplyto);
647 for (item = msginfo->references; item != NULL; item = g_slist_next(item))
653 quote_fmt_show_msg(msginfo, body, FALSE, TRUE, quote_str);
655 | SHOW_QUOTED_MESSAGE
657 quote_fmt_show_msg(msginfo, body, TRUE, TRUE, quote_str);
659 | SHOW_MESSAGE_NO_SIGNATURE
661 quote_fmt_show_msg(msginfo, body, FALSE, FALSE, quote_str);
663 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
665 quote_fmt_show_msg(msginfo, body, TRUE, FALSE, quote_str);
667 | SHOW_ACCOUNT_FULL_NAME
669 if (account && account->name)
670 INSERT(account->name);
672 | SHOW_ACCOUNT_MAIL_ADDRESS
674 if (account && account->address)
675 INSERT(account->address);
679 if (account && account->account_name)
680 INSERT(account->account_name);
682 | SHOW_ACCOUNT_ORGANIZATION
684 if (account && account->organization)
685 INSERT(account->organization);
703 | SHOW_EXCLAMATION_MARK
721 cursor_pos = current->bufsize;
727 add_visibility(msginfo->date != NULL);
729 OPARENT quote_fmt CPARENT
735 add_visibility(msginfo->from != NULL);
737 OPARENT quote_fmt CPARENT
743 add_visibility(msginfo->fromname != NULL);
745 OPARENT quote_fmt CPARENT
751 add_visibility(msginfo->subject != NULL);
753 OPARENT quote_fmt CPARENT
759 add_visibility(msginfo->to != NULL);
761 OPARENT quote_fmt CPARENT
767 add_visibility(msginfo->newsgroups != NULL);
769 OPARENT quote_fmt CPARENT
775 add_visibility(msginfo->msgid != NULL);
777 OPARENT quote_fmt CPARENT
783 add_visibility(msginfo->cc != NULL);
785 OPARENT quote_fmt CPARENT
794 found = (msginfo->inreplyto != NULL);
795 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
798 add_visibility(found == TRUE);
800 OPARENT quote_fmt CPARENT
804 | QUERY_ACCOUNT_FULL_NAME
806 add_visibility(account != NULL && account->name != NULL);
808 OPARENT quote_fmt CPARENT
812 | QUERY_ACCOUNT_ORGANIZATION
814 add_visibility(account != NULL && account->organization != NULL);
816 OPARENT quote_fmt CPARENT
824 add_visibility(msginfo->date == NULL);
826 OPARENT quote_fmt CPARENT
832 add_visibility(msginfo->from == NULL);
834 OPARENT quote_fmt CPARENT
840 add_visibility(msginfo->fromname == NULL);
842 OPARENT quote_fmt CPARENT
848 add_visibility(msginfo->subject == NULL);
850 OPARENT quote_fmt CPARENT
856 add_visibility(msginfo->to == NULL);
858 OPARENT quote_fmt CPARENT
862 | QUERY_NOT_NEWSGROUPS
864 add_visibility(msginfo->newsgroups == NULL);
866 OPARENT quote_fmt CPARENT
870 | QUERY_NOT_MESSAGEID
872 add_visibility(msginfo->msgid == NULL);
874 OPARENT quote_fmt CPARENT
880 add_visibility(msginfo->cc == NULL);
882 OPARENT quote_fmt CPARENT
886 | QUERY_NOT_REFERENCES
891 found = (msginfo->inreplyto != NULL);
892 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
895 add_visibility(found == FALSE);
897 OPARENT quote_fmt CPARENT
901 | QUERY_NOT_ACCOUNT_FULL_NAME
903 add_visibility(account == NULL || account->name == NULL);
905 OPARENT quote_fmt CPARENT
909 | QUERY_NOT_ACCOUNT_ORGANIZATION
911 add_visibility(account == NULL || account->organization == NULL);
913 OPARENT quote_fmt CPARENT
924 OPARENT sub_expr CPARENT
926 current = &main_expr;
928 quote_fmt_insert_file(sub_expr.buffer);
931 | INSERT_PROGRAMOUTPUT
936 OPARENT sub_expr CPARENT
938 current = &main_expr;
940 quote_fmt_insert_program_output(sub_expr.buffer);
948 OPARENT sub_expr CPARENT
950 current = &main_expr;
952 quote_fmt_insert_user_input(sub_expr.buffer);