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 static gchar *buffer = NULL;
51 static gint bufmax = 0;
52 static gint bufsize = 0;
53 static const gchar *quote_str = NULL;
54 static const gchar *body = NULL;
55 static gint error = 0;
57 static gint cursor_pos = -1;
59 extern int quote_fmt_firsttime;
61 static void add_visibility(gboolean val)
64 if (maxsize < stacksize) {
66 visible = g_realloc(visible, maxsize * sizeof(gboolean));
71 visible[stacksize - 1] = val;
74 static void remove_visibility(void)
79 static void add_buffer(const gchar *s)
84 if (bufsize + len + 1 > bufmax) {
87 while (bufsize + len + 1 > bufmax)
89 buffer = g_realloc(buffer, bufmax);
91 strcpy(buffer + bufsize, s);
95 gchar *quote_fmt_get_buffer(void)
103 gint quote_fmt_get_cursor_pos(void)
108 #define INSERT(buf) \
109 if (stacksize != 0 && visible[stacksize - 1]) \
112 #define INSERT_CHARACTER(chr) \
113 if (stacksize != 0 && visible[stacksize - 1]) { \
120 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
121 const gchar *my_body, gboolean my_dry_run)
123 quote_str = my_quote_str;
126 dry_run = my_dry_run;
128 add_visibility(TRUE);
134 * force LEX initialization
136 quote_fmt_firsttime = 1;
140 void quote_fmterror(char *str)
142 g_warning("Error: %s\n", str);
146 int quote_fmtwrap(void)
151 static int isseparator(int ch)
153 return g_ascii_isspace(ch) || ch == '.' || ch == '-';
156 static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
169 * ALF - GNU C's strftime() has a nice format specifier
170 * for time zone offset (%z). Non-standard however, so
174 #define RLEFT (sizeof result) - (rptr - result)
175 #define STR_SIZE(x) (sizeof (x) - 1)
179 if (procheader_date_parse_to_tm(msginfo->date, <, zone)) {
181 * break up format string in tiny bits delimited by valid %z's and
182 * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
184 for (rptr = result, fptr = format; fptr && *fptr && rptr < &result[sizeof result - 1];) {
189 if (NULL != (zptr = strstr(fptr, "%z"))) {
191 * count nr. of prepended percent chars
193 for (perc = 0, p = zptr; p && p >= format && *p == '%'; p--, perc++)
198 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
200 rptr += strftime(rptr, RLEFT, tmp, <);
204 * append time zone offset
206 if (zone[0] && perc % 2)
207 rptr += g_snprintf(rptr, RLEFT, "%s", zone);
208 fptr = zptr + STR_SIZE("%z");
210 rptr += strftime(rptr, RLEFT, fptr, <);
215 if (g_utf8_validate(result, -1, NULL)) {
218 gchar *utf = conv_codeset_strdup(result,
219 conv_get_locale_charset_str_no_utf8(),
222 !g_utf8_validate(utf, -1, NULL)) {
224 utf = g_malloc(strlen(result)*2+1);
225 conv_localetodisp(utf,
226 strlen(result)*2+1, result);
228 if (g_utf8_validate(utf, -1, NULL)) {
238 static void quote_fmt_show_first_name(const MsgInfo *msginfo)
243 if (!msginfo->fromname)
246 p = strchr(msginfo->fromname, ',');
248 /* fromname is like "Duck, Donald" */
250 while (*p && isspace(*p)) p++;
251 str = alloca(strlen(p) + 1);
257 /* fromname is like "Donald Duck" */
258 str = alloca(strlen(msginfo->fromname) + 1);
260 strcpy(str, msginfo->fromname);
262 while (*p && !isspace(*p)) p++;
269 static void quote_fmt_show_last_name(const MsgInfo *msginfo)
274 /* This probably won't work together very well with Middle
275 names and the like - thth */
276 if (!msginfo->fromname)
279 str = alloca(strlen(msginfo->fromname) + 1);
281 strcpy(str, msginfo->fromname);
282 p = strchr(str, ',');
284 /* fromname is like "Duck, Donald" */
288 /* fromname is like "Donald Duck" */
290 while (*p && !isspace(*p)) p++;
292 /* We found a space. Get first
293 none-space char and insert
294 rest of string from there. */
295 while (*p && isspace(*p)) p++;
299 /* If there is no none-space
300 char, just insert whole
305 /* If there is no space, just
306 insert whole fromname. */
313 static void quote_fmt_show_sender_initial(const MsgInfo *msginfo)
315 #define MAX_SENDER_INITIAL 20
316 gchar tmp[MAX_SENDER_INITIAL];
321 if (!msginfo->fromname)
324 p = msginfo->fromname;
327 if (*p && g_utf8_validate(p, 1, NULL)) {
331 if (len >= MAX_SENDER_INITIAL - 1)
335 while (*p && !isseparator(*p)) p++;
336 while (*p && isseparator(*p)) p++;
342 static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
343 gboolean quoted, gboolean signature,
344 const gchar *quote_str)
349 if (!(msginfo->folder || body))
353 fp = str_open_as_stream(body);
355 if (procmime_msginfo_is_encrypted(msginfo))
356 fp = procmime_get_first_encrypted_text_content(msginfo);
358 fp = procmime_get_first_text_content(msginfo);
362 g_warning("Can't get text part\n");
364 while (fgets(buf, sizeof(buf), fp) != NULL) {
367 if (!signature && strncmp(buf, "-- \n", 4) == 0)
370 if (quoted && quote_str)
379 static void quote_fmt_insert_file(const gchar *filename)
384 if ((file = g_fopen(filename, "rb")) != NULL) {
385 while (fgets(buffer, sizeof(buffer), file)) {
393 static void quote_fmt_insert_program_output(const gchar *progname)
398 if ((file = popen(progname, "r")) != NULL) {
399 while (fgets(buffer, sizeof(buffer), file)) {
413 %token SHOW_NEWSGROUPS
414 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
415 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
416 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
417 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB
418 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
419 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
420 %token QUERY_DATE QUERY_FROM
421 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
422 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
423 %token INSERT_FILE INSERT_PROGRAMOUTPUT
424 %token OPARENT CPARENT
426 %token SHOW_DATE_EXPR
427 %token SET_CURSOR_POS
431 %token <chr> CHARACTER
432 %type <chr> character
438 character_or_special_or_insert_or_query_list;
440 character_or_special_or_insert_or_query_list:
441 character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
442 | character_or_special_or_insert_or_query ;
444 character_or_special_or_insert_or_query:
448 INSERT_CHARACTER($1);
467 strncpy($$, $1, sizeof($$));
468 $$[sizeof($$) - 1] = '\0';
470 if (len + 1 < sizeof($$)) {
479 if (msginfo->newsgroups)
480 INSERT(msginfo->newsgroups);
482 | SHOW_DATE_EXPR OPARENT string CPARENT
484 quote_fmt_show_date(msginfo, $3);
489 INSERT(msginfo->date);
494 INSERT(msginfo->from);
498 if (msginfo->fromname)
499 INSERT(msginfo->fromname);
503 quote_fmt_show_first_name(msginfo);
507 quote_fmt_show_last_name(msginfo);
509 | SHOW_SENDER_INITIAL
511 quote_fmt_show_sender_initial(msginfo);
515 if (msginfo->subject)
516 INSERT(msginfo->subject);
526 INSERT(msginfo->msgid);
539 /* CLAWS: use in reply to header */
540 /* if (msginfo->references)
541 INSERT(msginfo->references); */
545 quote_fmt_show_msg(msginfo, body, FALSE, TRUE, quote_str);
547 | SHOW_QUOTED_MESSAGE
549 quote_fmt_show_msg(msginfo, body, TRUE, TRUE, quote_str);
551 | SHOW_MESSAGE_NO_SIGNATURE
553 quote_fmt_show_msg(msginfo, body, FALSE, FALSE, quote_str);
555 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
557 quote_fmt_show_msg(msginfo, body, TRUE, FALSE, quote_str);
589 cursor_pos = bufsize;
595 add_visibility(msginfo->date != NULL);
597 OPARENT quote_fmt CPARENT
603 add_visibility(msginfo->from != NULL);
605 OPARENT quote_fmt CPARENT
611 add_visibility(msginfo->fromname != NULL);
613 OPARENT quote_fmt CPARENT
619 add_visibility(msginfo->subject != NULL);
621 OPARENT quote_fmt CPARENT
627 add_visibility(msginfo->to != NULL);
629 OPARENT quote_fmt CPARENT
635 add_visibility(msginfo->newsgroups != NULL);
637 OPARENT quote_fmt CPARENT
643 add_visibility(msginfo->msgid != NULL);
645 OPARENT quote_fmt CPARENT
651 add_visibility(msginfo->cc != NULL);
653 OPARENT quote_fmt CPARENT
659 /* CLAWS: use in-reply-to header */
660 /* add_visibility(msginfo->references != NULL); */
662 OPARENT quote_fmt CPARENT
668 INSERT_FILE OPARENT string CPARENT
671 quote_fmt_insert_file($3);
674 | INSERT_PROGRAMOUTPUT OPARENT string CPARENT
677 quote_fmt_insert_program_output($3);