2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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 gboolean *visible = NULL;
49 static gboolean dry_run = FALSE;
50 static gint maxsize = 0;
51 static gint stacksize = 0;
52 static GHashTable *var_table = NULL;
54 typedef struct st_buffer
61 static struct st_buffer main_expr = { NULL, 0, 0 };
62 static struct st_buffer sub_expr = { NULL, 0, 0 };
63 static struct st_buffer* current = NULL;
65 static const gchar *quote_str = NULL;
66 static const gchar *body = NULL;
67 static gint error = 0;
69 static gint cursor_pos = -1;
71 extern int quote_fmt_firsttime;
73 static void add_visibility(gboolean val)
76 if (maxsize < stacksize) {
78 visible = g_realloc(visible, maxsize * sizeof(gboolean));
83 visible[stacksize - 1] = val;
86 static void remove_visibility(void)
90 g_warning("Error: visibility stack underflow\n");
95 static void add_buffer(const gchar *s)
103 if (current->bufsize + len + 1 > current->bufmax) {
104 if (current->bufmax == 0)
105 current->bufmax = 128;
106 while (current->bufsize + len + 1 > current->bufmax)
107 current->bufmax *= 2;
108 current->buffer = g_realloc(current->buffer, current->bufmax);
110 strcpy(current->buffer + current->bufsize, s);
111 current->bufsize += len;
114 static void clear_buffer(void)
117 *current->buffer = '\0';
118 current->bufsize = 0;
121 gchar *quote_fmt_get_buffer(void)
123 if (current != &main_expr)
124 g_warning("Error: parser still in sub-expr mode\n");
129 return current->buffer;
132 gint quote_fmt_get_cursor_pos(void)
137 #define INSERT(buf) \
138 if (stacksize != 0 && visible[stacksize - 1])\
141 #define INSERT_CHARACTER(chr) \
142 if (stacksize != 0 && visible[stacksize - 1]) { \
149 void quote_fmt_reset_vartable(void)
152 g_hash_table_destroy(var_table);
157 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
158 const gchar *my_body, gboolean my_dry_run)
160 quote_str = my_quote_str;
163 dry_run = my_dry_run;
165 add_visibility(TRUE);
166 main_expr.bufmax = 0;
168 current = &main_expr;
173 var_table = g_hash_table_new_full(g_str_hash, g_str_equal,
177 * force LEX initialization
179 quote_fmt_firsttime = 1;
183 void quote_fmterror(char *str)
185 g_warning("Error: %s\n", str);
189 int quote_fmtwrap(void)
194 static int isseparator(int ch)
196 return g_ascii_isspace(ch) || ch == '.' || ch == '-';
199 static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
212 * ALF - GNU C's strftime() has a nice format specifier
213 * for time zone offset (%z). Non-standard however, so
217 #define RLEFT (sizeof result) - (rptr - result)
218 #define STR_SIZE(x) (sizeof (x) - 1)
222 if (procheader_date_parse_to_tm(msginfo->date, <, zone)) {
224 * break up format string in tiny bits delimited by valid %z's and
225 * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
227 for (rptr = result, fptr = format; fptr && *fptr && rptr < &result[sizeof result - 1];) {
232 if (NULL != (zptr = strstr(fptr, "%z"))) {
234 * count nr. of prepended percent chars
236 for (perc = 0, p = zptr; p && p >= format && *p == '%'; p--, perc++)
241 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
243 rptr += strftime(rptr, RLEFT, tmp, <);
247 * append time zone offset
249 if (zone[0] && perc % 2)
250 rptr += g_snprintf(rptr, RLEFT, "%s", zone);
251 fptr = zptr + STR_SIZE("%z");
253 rptr += strftime(rptr, RLEFT, fptr, <);
258 if (g_utf8_validate(result, -1, NULL)) {
261 gchar *utf = conv_codeset_strdup(result,
262 conv_get_locale_charset_str_no_utf8(),
265 !g_utf8_validate(utf, -1, NULL)) {
267 utf = g_malloc(strlen(result)*2+1);
268 conv_localetodisp(utf,
269 strlen(result)*2+1, result);
271 if (g_utf8_validate(utf, -1, NULL)) {
281 static void quote_fmt_show_first_name(const MsgInfo *msginfo)
286 if (!msginfo->fromname)
289 p = (guchar*)strchr(msginfo->fromname, ',');
291 /* fromname is like "Duck, Donald" */
293 while (*p && isspace(*p)) p++;
294 str = alloca(strlen((char *)p) + 1);
296 strcpy(str, (char *)p);
300 /* fromname is like "Donald Duck" */
301 str = alloca(strlen(msginfo->fromname) + 1);
303 strcpy(str, msginfo->fromname);
305 while (*p && !isspace(*p)) p++;
312 static void quote_fmt_show_last_name(const MsgInfo *msginfo)
317 /* This probably won't work together very well with Middle
318 names and the like - thth */
319 if (!msginfo->fromname)
322 str = alloca(strlen(msginfo->fromname) + 1);
324 strcpy(str, msginfo->fromname);
325 p = strchr(str, ',');
327 /* fromname is like "Duck, Donald" */
331 /* fromname is like "Donald Duck" */
333 while (*p && !isspace(*p)) p++;
335 /* We found a space. Get first
336 none-space char and insert
337 rest of string from there. */
338 while (*p && isspace(*p)) p++;
342 /* If there is no none-space
343 char, just insert whole
348 /* If there is no space, just
349 insert whole fromname. */
356 static void quote_fmt_show_sender_initial(const MsgInfo *msginfo)
358 #define MAX_SENDER_INITIAL 20
359 gchar tmp[MAX_SENDER_INITIAL];
364 if (!msginfo->fromname)
367 p = (guchar *)msginfo->fromname;
370 if (*p && g_utf8_validate((gchar *)p, 1, NULL)) {
374 if (len >= MAX_SENDER_INITIAL - 1)
378 while (*p && !isseparator(*p)) p++;
379 while (*p && isseparator(*p)) p++;
385 static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
386 gboolean quoted, gboolean signature,
387 const gchar *quote_str)
392 if (!(msginfo->folder || body))
396 fp = str_open_as_stream(body);
398 if (procmime_msginfo_is_encrypted(msginfo))
399 fp = procmime_get_first_encrypted_text_content(msginfo);
401 fp = procmime_get_first_text_content(msginfo);
405 g_warning("Can't get text part\n");
407 while (fgets(buf, sizeof(buf), fp) != NULL) {
410 if (!signature && strncmp(buf, "-- \n", 4) == 0)
413 if (quoted && quote_str)
422 static void quote_fmt_insert_file(const gchar *filename)
427 if ((file = g_fopen(filename, "rb")) != NULL) {
428 while (fgets(buffer, sizeof(buffer), file)) {
436 static void quote_fmt_insert_program_output(const gchar *progname)
441 if ((file = popen(progname, "r")) != NULL) {
442 while (fgets(buffer, sizeof(buffer), file)) {
449 static void quote_fmt_insert_user_input(const gchar *varname)
457 if ((text = g_hash_table_lookup(var_table, varname)) == NULL) {
458 buf = g_strdup_printf(_("Enter text to replace '%s'"), varname);
459 text = input_dialog(_("Enter variable"), buf, "");
463 g_hash_table_insert(var_table, g_strdup(varname), g_strdup(text));
465 /* don't free the one in hashtable at the end */
466 text = g_strdup(text);
482 %token SHOW_NEWSGROUPS
483 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
484 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
485 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
486 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB
487 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
488 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_EXCLAMATION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
489 %token QUERY_DATE QUERY_FROM
490 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
491 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
492 %token QUERY_NOT_DATE QUERY_NOT_FROM
493 %token QUERY_NOT_FULLNAME QUERY_NOT_SUBJECT QUERY_NOT_TO QUERY_NOT_NEWSGROUPS
494 %token QUERY_NOT_MESSAGEID QUERY_NOT_CC QUERY_NOT_REFERENCES
495 %token INSERT_FILE INSERT_PROGRAMOUTPUT INSERT_USERINPUT
496 %token OPARENT CPARENT
498 %token SHOW_DATE_EXPR
499 %token SET_CURSOR_POS
503 %token <chr> CHARACTER
504 %type <chr> character
510 character_or_special_or_insert_or_query_list ;
513 character_or_special_list ;
515 character_or_special_or_insert_or_query_list:
516 character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
517 | character_or_special_or_insert_or_query ;
519 character_or_special_list:
520 character_or_special character_or_special_list
521 | character_or_special ;
523 character_or_special_or_insert_or_query:
529 character_or_special:
533 INSERT_CHARACTER($1);
550 strncpy($$, $1, sizeof($$));
551 $$[sizeof($$) - 1] = '\0';
553 if (len + 1 < sizeof($$)) {
562 if (msginfo->newsgroups)
563 INSERT(msginfo->newsgroups);
565 | SHOW_DATE_EXPR OPARENT string CPARENT
567 quote_fmt_show_date(msginfo, $3);
572 INSERT(msginfo->date);
577 INSERT(msginfo->from);
581 if (msginfo->fromname)
582 INSERT(msginfo->fromname);
586 quote_fmt_show_first_name(msginfo);
590 quote_fmt_show_last_name(msginfo);
592 | SHOW_SENDER_INITIAL
594 quote_fmt_show_sender_initial(msginfo);
598 if (msginfo->subject)
599 INSERT(msginfo->subject);
609 INSERT(msginfo->msgid);
624 INSERT(msginfo->inreplyto);
625 for (item = msginfo->references; item != NULL; item = g_slist_next(item))
631 quote_fmt_show_msg(msginfo, body, FALSE, TRUE, quote_str);
633 | SHOW_QUOTED_MESSAGE
635 quote_fmt_show_msg(msginfo, body, TRUE, TRUE, quote_str);
637 | SHOW_MESSAGE_NO_SIGNATURE
639 quote_fmt_show_msg(msginfo, body, FALSE, FALSE, quote_str);
641 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
643 quote_fmt_show_msg(msginfo, body, TRUE, FALSE, quote_str);
661 | SHOW_EXCLAMATION_MARK
679 cursor_pos = current->bufsize;
685 add_visibility(msginfo->date != NULL);
687 OPARENT quote_fmt CPARENT
693 add_visibility(msginfo->from != NULL);
695 OPARENT quote_fmt CPARENT
701 add_visibility(msginfo->fromname != NULL);
703 OPARENT quote_fmt CPARENT
709 add_visibility(msginfo->subject != NULL);
711 OPARENT quote_fmt CPARENT
717 add_visibility(msginfo->to != NULL);
719 OPARENT quote_fmt CPARENT
725 add_visibility(msginfo->newsgroups != NULL);
727 OPARENT quote_fmt CPARENT
733 add_visibility(msginfo->msgid != NULL);
735 OPARENT quote_fmt CPARENT
741 add_visibility(msginfo->cc != NULL);
743 OPARENT quote_fmt CPARENT
752 found = (msginfo->inreplyto != NULL);
753 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
756 add_visibility(found == TRUE);
758 OPARENT quote_fmt CPARENT
766 add_visibility(msginfo->date == NULL);
768 OPARENT quote_fmt CPARENT
774 add_visibility(msginfo->from == NULL);
776 OPARENT quote_fmt CPARENT
782 add_visibility(msginfo->fromname == NULL);
784 OPARENT quote_fmt CPARENT
790 add_visibility(msginfo->subject == NULL);
792 OPARENT quote_fmt CPARENT
798 add_visibility(msginfo->to == NULL);
800 OPARENT quote_fmt CPARENT
804 | QUERY_NOT_NEWSGROUPS
806 add_visibility(msginfo->newsgroups == NULL);
808 OPARENT quote_fmt CPARENT
812 | QUERY_NOT_MESSAGEID
814 add_visibility(msginfo->msgid == NULL);
816 OPARENT quote_fmt CPARENT
822 add_visibility(msginfo->cc == NULL);
824 OPARENT quote_fmt CPARENT
828 | QUERY_NOT_REFERENCES
833 found = (msginfo->inreplyto != NULL);
834 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
837 add_visibility(found == FALSE);
839 OPARENT quote_fmt CPARENT
850 OPARENT sub_expr CPARENT
852 current = &main_expr;
854 quote_fmt_insert_file(sub_expr.buffer);
857 | INSERT_PROGRAMOUTPUT
862 OPARENT sub_expr CPARENT
864 current = &main_expr;
866 quote_fmt_insert_program_output(sub_expr.buffer);
874 OPARENT sub_expr CPARENT
876 current = &main_expr;
878 quote_fmt_insert_user_input(sub_expr.buffer);