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_init(MsgInfo *info, const gchar *my_quote_str,
150 const gchar *my_body, gboolean my_dry_run)
152 quote_str = my_quote_str;
155 dry_run = my_dry_run;
157 add_visibility(TRUE);
158 main_expr.bufmax = 0;
160 current = &main_expr;
164 g_hash_table_destroy(var_table);
167 var_table = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
170 * force LEX initialization
172 quote_fmt_firsttime = 1;
176 void quote_fmterror(char *str)
178 g_warning("Error: %s\n", str);
182 int quote_fmtwrap(void)
187 static int isseparator(int ch)
189 return g_ascii_isspace(ch) || ch == '.' || ch == '-';
192 static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
205 * ALF - GNU C's strftime() has a nice format specifier
206 * for time zone offset (%z). Non-standard however, so
210 #define RLEFT (sizeof result) - (rptr - result)
211 #define STR_SIZE(x) (sizeof (x) - 1)
215 if (procheader_date_parse_to_tm(msginfo->date, <, zone)) {
217 * break up format string in tiny bits delimited by valid %z's and
218 * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
220 for (rptr = result, fptr = format; fptr && *fptr && rptr < &result[sizeof result - 1];) {
225 if (NULL != (zptr = strstr(fptr, "%z"))) {
227 * count nr. of prepended percent chars
229 for (perc = 0, p = zptr; p && p >= format && *p == '%'; p--, perc++)
234 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
236 rptr += strftime(rptr, RLEFT, tmp, <);
240 * append time zone offset
242 if (zone[0] && perc % 2)
243 rptr += g_snprintf(rptr, RLEFT, "%s", zone);
244 fptr = zptr + STR_SIZE("%z");
246 rptr += strftime(rptr, RLEFT, fptr, <);
251 if (g_utf8_validate(result, -1, NULL)) {
254 gchar *utf = conv_codeset_strdup(result,
255 conv_get_locale_charset_str_no_utf8(),
258 !g_utf8_validate(utf, -1, NULL)) {
260 utf = g_malloc(strlen(result)*2+1);
261 conv_localetodisp(utf,
262 strlen(result)*2+1, result);
264 if (g_utf8_validate(utf, -1, NULL)) {
274 static void quote_fmt_show_first_name(const MsgInfo *msginfo)
279 if (!msginfo->fromname)
282 p = (guchar*)strchr(msginfo->fromname, ',');
284 /* fromname is like "Duck, Donald" */
286 while (*p && isspace(*p)) p++;
287 str = alloca(strlen((char *)p) + 1);
289 strcpy(str, (char *)p);
293 /* fromname is like "Donald Duck" */
294 str = alloca(strlen(msginfo->fromname) + 1);
296 strcpy(str, msginfo->fromname);
298 while (*p && !isspace(*p)) p++;
305 static void quote_fmt_show_last_name(const MsgInfo *msginfo)
310 /* This probably won't work together very well with Middle
311 names and the like - thth */
312 if (!msginfo->fromname)
315 str = alloca(strlen(msginfo->fromname) + 1);
317 strcpy(str, msginfo->fromname);
318 p = strchr(str, ',');
320 /* fromname is like "Duck, Donald" */
324 /* fromname is like "Donald Duck" */
326 while (*p && !isspace(*p)) p++;
328 /* We found a space. Get first
329 none-space char and insert
330 rest of string from there. */
331 while (*p && isspace(*p)) p++;
335 /* If there is no none-space
336 char, just insert whole
341 /* If there is no space, just
342 insert whole fromname. */
349 static void quote_fmt_show_sender_initial(const MsgInfo *msginfo)
351 #define MAX_SENDER_INITIAL 20
352 gchar tmp[MAX_SENDER_INITIAL];
357 if (!msginfo->fromname)
360 p = (guchar *)msginfo->fromname;
363 if (*p && g_utf8_validate((gchar *)p, 1, NULL)) {
367 if (len >= MAX_SENDER_INITIAL - 1)
371 while (*p && !isseparator(*p)) p++;
372 while (*p && isseparator(*p)) p++;
378 static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
379 gboolean quoted, gboolean signature,
380 const gchar *quote_str)
385 if (!(msginfo->folder || body))
389 fp = str_open_as_stream(body);
391 if (procmime_msginfo_is_encrypted(msginfo))
392 fp = procmime_get_first_encrypted_text_content(msginfo);
394 fp = procmime_get_first_text_content(msginfo);
398 g_warning("Can't get text part\n");
400 while (fgets(buf, sizeof(buf), fp) != NULL) {
403 if (!signature && strncmp(buf, "-- \n", 4) == 0)
406 if (quoted && quote_str)
415 static void quote_fmt_insert_file(const gchar *filename)
420 if ((file = g_fopen(filename, "rb")) != NULL) {
421 while (fgets(buffer, sizeof(buffer), file)) {
429 static void quote_fmt_insert_program_output(const gchar *progname)
434 if ((file = popen(progname, "r")) != NULL) {
435 while (fgets(buffer, sizeof(buffer), file)) {
442 static void quote_fmt_insert_user_input(const gchar *varname)
450 if ((text = g_hash_table_lookup(var_table, varname)) == NULL) {
451 buf = g_strdup_printf(_("Enter text to replace '%s'"), varname);
452 text = input_dialog(_("Enter variable"), buf, "");
456 g_hash_table_insert(var_table, g_strdup(varname), g_strdup(text));
458 /* don't free the one in hashtable at the end */
459 text = g_strdup(text);
475 %token SHOW_NEWSGROUPS
476 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
477 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
478 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
479 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB
480 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
481 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_EXCLAMATION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
482 %token QUERY_DATE QUERY_FROM
483 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
484 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
485 %token QUERY_NOT_DATE QUERY_NOT_FROM
486 %token QUERY_NOT_FULLNAME QUERY_NOT_SUBJECT QUERY_NOT_TO QUERY_NOT_NEWSGROUPS
487 %token QUERY_NOT_MESSAGEID QUERY_NOT_CC QUERY_NOT_REFERENCES
488 %token INSERT_FILE INSERT_PROGRAMOUTPUT INSERT_USERINPUT
489 %token OPARENT CPARENT
491 %token SHOW_DATE_EXPR
492 %token SET_CURSOR_POS
496 %token <chr> CHARACTER
497 %type <chr> character
503 character_or_special_or_insert_or_query_list ;
506 character_or_special_list ;
508 character_or_special_or_insert_or_query_list:
509 character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
510 | character_or_special_or_insert_or_query ;
512 character_or_special_list:
513 character_or_special character_or_special_list
514 | character_or_special ;
516 character_or_special_or_insert_or_query:
522 character_or_special:
526 INSERT_CHARACTER($1);
543 strncpy($$, $1, sizeof($$));
544 $$[sizeof($$) - 1] = '\0';
546 if (len + 1 < sizeof($$)) {
555 if (msginfo->newsgroups)
556 INSERT(msginfo->newsgroups);
558 | SHOW_DATE_EXPR OPARENT string CPARENT
560 quote_fmt_show_date(msginfo, $3);
565 INSERT(msginfo->date);
570 INSERT(msginfo->from);
574 if (msginfo->fromname)
575 INSERT(msginfo->fromname);
579 quote_fmt_show_first_name(msginfo);
583 quote_fmt_show_last_name(msginfo);
585 | SHOW_SENDER_INITIAL
587 quote_fmt_show_sender_initial(msginfo);
591 if (msginfo->subject)
592 INSERT(msginfo->subject);
602 INSERT(msginfo->msgid);
617 INSERT(msginfo->inreplyto);
618 for (item = msginfo->references; item != NULL; item = g_slist_next(item))
624 quote_fmt_show_msg(msginfo, body, FALSE, TRUE, quote_str);
626 | SHOW_QUOTED_MESSAGE
628 quote_fmt_show_msg(msginfo, body, TRUE, TRUE, quote_str);
630 | SHOW_MESSAGE_NO_SIGNATURE
632 quote_fmt_show_msg(msginfo, body, FALSE, FALSE, quote_str);
634 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
636 quote_fmt_show_msg(msginfo, body, TRUE, FALSE, quote_str);
654 | SHOW_EXCLAMATION_MARK
672 cursor_pos = current->bufsize;
678 add_visibility(msginfo->date != NULL);
680 OPARENT quote_fmt CPARENT
686 add_visibility(msginfo->from != NULL);
688 OPARENT quote_fmt CPARENT
694 add_visibility(msginfo->fromname != NULL);
696 OPARENT quote_fmt CPARENT
702 add_visibility(msginfo->subject != NULL);
704 OPARENT quote_fmt CPARENT
710 add_visibility(msginfo->to != NULL);
712 OPARENT quote_fmt CPARENT
718 add_visibility(msginfo->newsgroups != NULL);
720 OPARENT quote_fmt CPARENT
726 add_visibility(msginfo->msgid != NULL);
728 OPARENT quote_fmt CPARENT
734 add_visibility(msginfo->cc != NULL);
736 OPARENT quote_fmt CPARENT
745 found = (msginfo->inreplyto != NULL);
746 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
749 add_visibility(found == TRUE);
751 OPARENT quote_fmt CPARENT
759 add_visibility(msginfo->date == NULL);
761 OPARENT quote_fmt CPARENT
767 add_visibility(msginfo->from == NULL);
769 OPARENT quote_fmt CPARENT
775 add_visibility(msginfo->fromname == NULL);
777 OPARENT quote_fmt CPARENT
783 add_visibility(msginfo->subject == NULL);
785 OPARENT quote_fmt CPARENT
791 add_visibility(msginfo->to == NULL);
793 OPARENT quote_fmt CPARENT
797 | QUERY_NOT_NEWSGROUPS
799 add_visibility(msginfo->newsgroups == NULL);
801 OPARENT quote_fmt CPARENT
805 | QUERY_NOT_MESSAGEID
807 add_visibility(msginfo->msgid == NULL);
809 OPARENT quote_fmt CPARENT
815 add_visibility(msginfo->cc == NULL);
817 OPARENT quote_fmt CPARENT
821 | QUERY_NOT_REFERENCES
826 found = (msginfo->inreplyto != NULL);
827 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
830 add_visibility(found == FALSE);
832 OPARENT quote_fmt CPARENT
843 OPARENT sub_expr CPARENT
845 current = &main_expr;
847 quote_fmt_insert_file(sub_expr.buffer);
850 | INSERT_PROGRAMOUTPUT
855 OPARENT sub_expr CPARENT
857 current = &main_expr;
859 quote_fmt_insert_program_output(sub_expr.buffer);
867 OPARENT sub_expr CPARENT
869 current = &main_expr;
871 quote_fmt_insert_user_input(sub_expr.buffer);