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.
31 #include "procheader.h"
33 #include "quote_fmt.h"
34 #include "quote_fmt_lex.h"
39 bison -p quote_fmt quote_fmt.y
44 static MsgInfo *msginfo = NULL;
45 static gboolean *visible = NULL;
46 static gboolean dry_run = FALSE;
47 static gint maxsize = 0;
48 static gint stacksize = 0;
50 typedef struct st_buffer
57 static struct st_buffer main_expr = { NULL, 0, 0 };
58 static struct st_buffer sub_expr = { NULL, 0, 0 };
59 static struct st_buffer* current = NULL;
61 static const gchar *quote_str = NULL;
62 static const gchar *body = NULL;
63 static gint error = 0;
65 static gint cursor_pos = -1;
67 extern int quote_fmt_firsttime;
69 static void add_visibility(gboolean val)
72 if (maxsize < stacksize) {
74 visible = g_realloc(visible, maxsize * sizeof(gboolean));
79 visible[stacksize - 1] = val;
82 static void remove_visibility(void)
86 g_warning("Error: visibility stack underflow\n");
91 static void add_buffer(const gchar *s)
99 if (current->bufsize + len + 1 > current->bufmax) {
100 if (current->bufmax == 0)
101 current->bufmax = 128;
102 while (current->bufsize + len + 1 > current->bufmax)
103 current->bufmax *= 2;
104 current->buffer = g_realloc(current->buffer, current->bufmax);
106 strcpy(current->buffer + current->bufsize, s);
107 current->bufsize += len;
110 static void clear_buffer(void)
113 *current->buffer = '\0';
114 current->bufsize = 0;
117 gchar *quote_fmt_get_buffer(void)
119 if (current != &main_expr)
120 g_warning("Error: parser still in sub-expr mode\n");
125 return current->buffer;
128 gint quote_fmt_get_cursor_pos(void)
133 #define INSERT(buf) \
134 if (stacksize != 0 && visible[stacksize - 1])\
137 #define INSERT_CHARACTER(chr) \
138 if (stacksize != 0 && visible[stacksize - 1]) { \
145 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
146 const gchar *my_body, gboolean my_dry_run)
148 quote_str = my_quote_str;
151 dry_run = my_dry_run;
153 add_visibility(TRUE);
154 main_expr.bufmax = 0;
156 current = &main_expr;
160 * force LEX initialization
162 quote_fmt_firsttime = 1;
166 void quote_fmterror(char *str)
168 g_warning("Error: %s\n", str);
172 int quote_fmtwrap(void)
177 static int isseparator(int ch)
179 return g_ascii_isspace(ch) || ch == '.' || ch == '-';
182 static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
195 * ALF - GNU C's strftime() has a nice format specifier
196 * for time zone offset (%z). Non-standard however, so
200 #define RLEFT (sizeof result) - (rptr - result)
201 #define STR_SIZE(x) (sizeof (x) - 1)
205 if (procheader_date_parse_to_tm(msginfo->date, <, zone)) {
207 * break up format string in tiny bits delimited by valid %z's and
208 * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
210 for (rptr = result, fptr = format; fptr && *fptr && rptr < &result[sizeof result - 1];) {
215 if (NULL != (zptr = strstr(fptr, "%z"))) {
217 * count nr. of prepended percent chars
219 for (perc = 0, p = zptr; p && p >= format && *p == '%'; p--, perc++)
224 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
226 rptr += strftime(rptr, RLEFT, tmp, <);
230 * append time zone offset
232 if (zone[0] && perc % 2)
233 rptr += g_snprintf(rptr, RLEFT, "%s", zone);
234 fptr = zptr + STR_SIZE("%z");
236 rptr += strftime(rptr, RLEFT, fptr, <);
241 if (g_utf8_validate(result, -1, NULL)) {
244 gchar *utf = conv_codeset_strdup(result,
245 conv_get_locale_charset_str_no_utf8(),
248 !g_utf8_validate(utf, -1, NULL)) {
250 utf = g_malloc(strlen(result)*2+1);
251 conv_localetodisp(utf,
252 strlen(result)*2+1, result);
254 if (g_utf8_validate(utf, -1, NULL)) {
264 static void quote_fmt_show_first_name(const MsgInfo *msginfo)
269 if (!msginfo->fromname)
272 p = (guchar*)strchr(msginfo->fromname, ',');
274 /* fromname is like "Duck, Donald" */
276 while (*p && isspace(*p)) p++;
277 str = alloca(strlen((char *)p) + 1);
279 strcpy(str, (char *)p);
283 /* fromname is like "Donald Duck" */
284 str = alloca(strlen(msginfo->fromname) + 1);
286 strcpy(str, msginfo->fromname);
288 while (*p && !isspace(*p)) p++;
295 static void quote_fmt_show_last_name(const MsgInfo *msginfo)
300 /* This probably won't work together very well with Middle
301 names and the like - thth */
302 if (!msginfo->fromname)
305 str = alloca(strlen(msginfo->fromname) + 1);
307 strcpy(str, msginfo->fromname);
308 p = strchr(str, ',');
310 /* fromname is like "Duck, Donald" */
314 /* fromname is like "Donald Duck" */
316 while (*p && !isspace(*p)) p++;
318 /* We found a space. Get first
319 none-space char and insert
320 rest of string from there. */
321 while (*p && isspace(*p)) p++;
325 /* If there is no none-space
326 char, just insert whole
331 /* If there is no space, just
332 insert whole fromname. */
339 static void quote_fmt_show_sender_initial(const MsgInfo *msginfo)
341 #define MAX_SENDER_INITIAL 20
342 gchar tmp[MAX_SENDER_INITIAL];
347 if (!msginfo->fromname)
350 p = (guchar *)msginfo->fromname;
353 if (*p && g_utf8_validate((gchar *)p, 1, NULL)) {
357 if (len >= MAX_SENDER_INITIAL - 1)
361 while (*p && !isseparator(*p)) p++;
362 while (*p && isseparator(*p)) p++;
368 static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
369 gboolean quoted, gboolean signature,
370 const gchar *quote_str)
375 if (!(msginfo->folder || body))
379 fp = str_open_as_stream(body);
381 if (procmime_msginfo_is_encrypted(msginfo))
382 fp = procmime_get_first_encrypted_text_content(msginfo);
384 fp = procmime_get_first_text_content(msginfo);
388 g_warning("Can't get text part\n");
390 while (fgets(buf, sizeof(buf), fp) != NULL) {
393 if (!signature && strncmp(buf, "-- \n", 4) == 0)
396 if (quoted && quote_str)
405 static void quote_fmt_insert_file(const gchar *filename)
410 if ((file = g_fopen(filename, "rb")) != NULL) {
411 while (fgets(buffer, sizeof(buffer), file)) {
419 static void quote_fmt_insert_program_output(const gchar *progname)
424 if ((file = popen(progname, "r")) != NULL) {
425 while (fgets(buffer, sizeof(buffer), file)) {
439 %token SHOW_NEWSGROUPS
440 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
441 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
442 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
443 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB
444 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
445 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_EXCLAMATION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
446 %token QUERY_DATE QUERY_FROM
447 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
448 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
449 %token QUERY_NOT_DATE QUERY_NOT_FROM
450 %token QUERY_NOT_FULLNAME QUERY_NOT_SUBJECT QUERY_NOT_TO QUERY_NOT_NEWSGROUPS
451 %token QUERY_NOT_MESSAGEID QUERY_NOT_CC QUERY_NOT_REFERENCES
452 %token INSERT_FILE INSERT_PROGRAMOUTPUT
453 %token OPARENT CPARENT
455 %token SHOW_DATE_EXPR
456 %token SET_CURSOR_POS
460 %token <chr> CHARACTER
461 %type <chr> character
467 character_or_special_or_insert_or_query_list ;
470 character_or_special_list ;
472 character_or_special_or_insert_or_query_list:
473 character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
474 | character_or_special_or_insert_or_query ;
476 character_or_special_list:
477 character_or_special character_or_special_list
478 | character_or_special ;
480 character_or_special_or_insert_or_query:
486 character_or_special:
490 INSERT_CHARACTER($1);
507 strncpy($$, $1, sizeof($$));
508 $$[sizeof($$) - 1] = '\0';
510 if (len + 1 < sizeof($$)) {
519 if (msginfo->newsgroups)
520 INSERT(msginfo->newsgroups);
522 | SHOW_DATE_EXPR OPARENT string CPARENT
524 quote_fmt_show_date(msginfo, $3);
529 INSERT(msginfo->date);
534 INSERT(msginfo->from);
538 if (msginfo->fromname)
539 INSERT(msginfo->fromname);
543 quote_fmt_show_first_name(msginfo);
547 quote_fmt_show_last_name(msginfo);
549 | SHOW_SENDER_INITIAL
551 quote_fmt_show_sender_initial(msginfo);
555 if (msginfo->subject)
556 INSERT(msginfo->subject);
566 INSERT(msginfo->msgid);
581 INSERT(msginfo->inreplyto);
582 for (item = msginfo->references; item != NULL; item = g_slist_next(item))
588 quote_fmt_show_msg(msginfo, body, FALSE, TRUE, quote_str);
590 | SHOW_QUOTED_MESSAGE
592 quote_fmt_show_msg(msginfo, body, TRUE, TRUE, quote_str);
594 | SHOW_MESSAGE_NO_SIGNATURE
596 quote_fmt_show_msg(msginfo, body, FALSE, FALSE, quote_str);
598 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
600 quote_fmt_show_msg(msginfo, body, TRUE, FALSE, quote_str);
618 | SHOW_EXCLAMATION_MARK
636 cursor_pos = current->bufsize;
642 add_visibility(msginfo->date != NULL);
644 OPARENT quote_fmt CPARENT
650 add_visibility(msginfo->from != NULL);
652 OPARENT quote_fmt CPARENT
658 add_visibility(msginfo->fromname != NULL);
660 OPARENT quote_fmt CPARENT
666 add_visibility(msginfo->subject != NULL);
668 OPARENT quote_fmt CPARENT
674 add_visibility(msginfo->to != NULL);
676 OPARENT quote_fmt CPARENT
682 add_visibility(msginfo->newsgroups != NULL);
684 OPARENT quote_fmt CPARENT
690 add_visibility(msginfo->msgid != NULL);
692 OPARENT quote_fmt CPARENT
698 add_visibility(msginfo->cc != NULL);
700 OPARENT quote_fmt CPARENT
709 found = (msginfo->inreplyto != NULL);
710 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
713 add_visibility(found == TRUE);
715 OPARENT quote_fmt CPARENT
723 add_visibility(msginfo->date == NULL);
725 OPARENT quote_fmt CPARENT
731 add_visibility(msginfo->from == NULL);
733 OPARENT quote_fmt CPARENT
739 add_visibility(msginfo->fromname == NULL);
741 OPARENT quote_fmt CPARENT
747 add_visibility(msginfo->subject == NULL);
749 OPARENT quote_fmt CPARENT
755 add_visibility(msginfo->to == NULL);
757 OPARENT quote_fmt CPARENT
761 | QUERY_NOT_NEWSGROUPS
763 add_visibility(msginfo->newsgroups == NULL);
765 OPARENT quote_fmt CPARENT
769 | QUERY_NOT_MESSAGEID
771 add_visibility(msginfo->msgid == NULL);
773 OPARENT quote_fmt CPARENT
779 add_visibility(msginfo->cc == NULL);
781 OPARENT quote_fmt CPARENT
785 | QUERY_NOT_REFERENCES
790 found = (msginfo->inreplyto != NULL);
791 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
794 add_visibility(found == FALSE);
796 OPARENT quote_fmt CPARENT
807 OPARENT sub_expr CPARENT
809 current = &main_expr;
811 quote_fmt_insert_file(sub_expr.buffer);
814 | INSERT_PROGRAMOUTPUT
819 OPARENT sub_expr CPARENT
821 current = &main_expr;
823 quote_fmt_insert_program_output(sub_expr.buffer);