2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 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.
30 #include "procheader.h"
32 #include "quote_fmt.h"
33 #include "quote_fmt_lex.h"
38 bison -p quote_fmt quote_fmt.y
43 static MsgInfo *msginfo = NULL;
44 static gboolean *visible = NULL;
45 static gboolean dry_run = FALSE;
46 static gint maxsize = 0;
47 static gint stacksize = 0;
49 static gchar *buffer = NULL;
50 static gint bufmax = 0;
51 static gint bufsize = 0;
52 static const gchar *quote_str = NULL;
53 static const gchar *body = NULL;
54 static gint error = 0;
56 static gint cursor_pos = 0;
58 extern int quote_fmt_firsttime;
60 static void add_visibility(gboolean val)
63 if (maxsize < stacksize) {
65 visible = g_realloc(visible, maxsize * sizeof(gboolean));
70 visible[stacksize - 1] = val;
73 static void remove_visibility(void)
78 static void add_buffer(const gchar *s)
83 if (bufsize + len + 1 > bufmax) {
86 while (bufsize + len + 1 > bufmax)
88 buffer = g_realloc(buffer, bufmax);
90 strcpy(buffer + bufsize, s);
94 gchar *quote_fmt_get_buffer(void)
102 gint quote_fmt_get_cursor_pos(void)
107 #define INSERT(buf) \
108 if (stacksize != 0 && visible[stacksize - 1]) \
111 #define INSERT_CHARACTER(chr) \
112 if (stacksize != 0 && visible[stacksize - 1]) { \
119 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
120 const gchar *my_body, gboolean my_dry_run)
122 quote_str = my_quote_str;
125 dry_run = my_dry_run;
127 add_visibility(TRUE);
133 * force LEX initialization
135 quote_fmt_firsttime = 1;
139 void quote_fmterror(char *str)
141 g_warning("Error: %s\n", str);
145 int quote_fmtwrap(void)
150 static int isseparator(int ch)
152 return g_ascii_isspace(ch) || ch == '.' || ch == '-';
155 static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
168 * ALF - GNU C's strftime() has a nice format specifier
169 * for time zone offset (%z). Non-standard however, so
173 #define RLEFT (sizeof result) - (rptr - result)
174 #define STR_SIZE(x) (sizeof (x) - 1)
178 if (procheader_date_parse_to_tm(msginfo->date, <, zone)) {
180 * break up format string in tiny bits delimited by valid %z's and
181 * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
183 for (rptr = result, fptr = format; fptr && *fptr && rptr < &result[sizeof result - 1];) {
188 if (NULL != (zptr = strstr(fptr, "%z"))) {
190 * count nr. of prepended percent chars
192 for (perc = 0, p = zptr; p && p >= format && *p == '%'; p--, perc++)
197 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
199 rptr += strftime(rptr, RLEFT, tmp, <);
203 * append time zone offset
205 if (zone[0] && perc % 2)
206 rptr += g_snprintf(rptr, RLEFT, "%s", zone);
207 fptr = zptr + STR_SIZE("%z");
209 rptr += strftime(rptr, RLEFT, fptr, <);
220 static void quote_fmt_show_first_name(const MsgInfo *msginfo)
225 if (!msginfo->fromname)
228 p = strchr(msginfo->fromname, ',');
230 /* fromname is like "Duck, Donald" */
232 while (*p && isspace(*p)) p++;
233 str = alloca(strlen(p) + 1);
239 /* fromname is like "Donald Duck" */
240 str = alloca(strlen(msginfo->fromname) + 1);
242 strcpy(str, msginfo->fromname);
244 while (*p && !isspace(*p)) p++;
251 static void quote_fmt_show_last_name(const MsgInfo *msginfo)
256 /* This probably won't work together very well with Middle
257 names and the like - thth */
258 if (!msginfo->fromname)
261 str = alloca(strlen(msginfo->fromname) + 1);
263 strcpy(str, msginfo->fromname);
264 p = strchr(str, ',');
266 /* fromname is like "Duck, Donald" */
270 /* fromname is like "Donald Duck" */
272 while (*p && !isspace(*p)) p++;
274 /* We found a space. Get first
275 none-space char and insert
276 rest of string from there. */
277 while (*p && isspace(*p)) p++;
281 /* If there is no none-space
282 char, just insert whole
287 /* If there is no space, just
288 insert whole fromname. */
295 static void quote_fmt_show_sender_initial(const MsgInfo *msginfo)
297 #define MAX_SENDER_INITIAL 20
298 gchar tmp[MAX_SENDER_INITIAL];
303 if (!msginfo->fromname)
306 p = msginfo->fromname;
309 if (*p && g_utf8_validate(p, 1, NULL)) {
313 if (len >= MAX_SENDER_INITIAL - 1)
317 while (*p && !isseparator(*p)) p++;
318 while (*p && isseparator(*p)) p++;
324 static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
325 gboolean quoted, gboolean signature,
326 const gchar *quote_str)
331 if (!(msginfo->folder || body))
335 fp = str_open_as_stream(body);
337 if (procmime_msginfo_is_encrypted(msginfo))
338 fp = procmime_get_first_encrypted_text_content(msginfo);
340 fp = procmime_get_first_text_content(msginfo);
344 g_warning("Can't get text part\n");
346 while (fgets(buf, sizeof(buf), fp) != NULL) {
349 if (!signature && strncmp(buf, "-- \n", 4) == 0)
352 if (quoted && quote_str)
361 static void quote_fmt_insert_file(const gchar *filename)
366 if ((file = g_fopen(filename, "rb")) != NULL) {
367 while (fgets(buffer, sizeof(buffer), file)) {
375 static void quote_fmt_insert_program_output(const gchar *progname)
380 if ((file = popen(progname, "r")) != NULL) {
381 while (fgets(buffer, sizeof(buffer), file)) {
395 %token SHOW_NEWSGROUPS
396 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
397 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
398 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
399 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB
400 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
401 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
402 %token QUERY_DATE QUERY_FROM
403 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
404 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
405 %token INSERT_FILE INSERT_PROGRAMOUTPUT
406 %token OPARENT CPARENT
408 %token SHOW_DATE_EXPR
409 %token SET_CURSOR_POS
413 %token <chr> CHARACTER
414 %type <chr> character
420 character_or_special_or_insert_or_query_list;
422 character_or_special_or_insert_or_query_list:
423 character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
424 | character_or_special_or_insert_or_query ;
426 character_or_special_or_insert_or_query:
430 INSERT_CHARACTER($1);
449 strncpy($$, $1, sizeof($$));
450 $$[sizeof($$) - 1] = '\0';
452 if (len + 1 < sizeof($$)) {
461 if (msginfo->newsgroups)
462 INSERT(msginfo->newsgroups);
464 | SHOW_DATE_EXPR OPARENT string CPARENT
466 quote_fmt_show_date(msginfo, $3);
471 INSERT(msginfo->date);
476 INSERT(msginfo->from);
480 if (msginfo->fromname)
481 INSERT(msginfo->fromname);
485 quote_fmt_show_first_name(msginfo);
489 quote_fmt_show_last_name(msginfo);
491 | SHOW_SENDER_INITIAL
493 quote_fmt_show_sender_initial(msginfo);
497 if (msginfo->subject)
498 INSERT(msginfo->subject);
508 INSERT(msginfo->msgid);
521 /* CLAWS: use in reply to header */
522 /* if (msginfo->references)
523 INSERT(msginfo->references); */
527 quote_fmt_show_msg(msginfo, body, FALSE, TRUE, quote_str);
529 | SHOW_QUOTED_MESSAGE
531 quote_fmt_show_msg(msginfo, body, TRUE, TRUE, quote_str);
533 | SHOW_MESSAGE_NO_SIGNATURE
535 quote_fmt_show_msg(msginfo, body, FALSE, FALSE, quote_str);
537 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
539 quote_fmt_show_msg(msginfo, body, TRUE, FALSE, quote_str);
571 cursor_pos = bufsize;
577 add_visibility(msginfo->date != NULL);
579 OPARENT quote_fmt CPARENT
585 add_visibility(msginfo->from != NULL);
587 OPARENT quote_fmt CPARENT
593 add_visibility(msginfo->fromname != NULL);
595 OPARENT quote_fmt CPARENT
601 add_visibility(msginfo->subject != NULL);
603 OPARENT quote_fmt CPARENT
609 add_visibility(msginfo->to != NULL);
611 OPARENT quote_fmt CPARENT
617 add_visibility(msginfo->newsgroups != NULL);
619 OPARENT quote_fmt CPARENT
625 add_visibility(msginfo->msgid != NULL);
627 OPARENT quote_fmt CPARENT
633 add_visibility(msginfo->cc != NULL);
635 OPARENT quote_fmt CPARENT
641 /* CLAWS: use in-reply-to header */
642 /* add_visibility(msginfo->references != NULL); */
644 OPARENT quote_fmt CPARENT
650 INSERT_FILE OPARENT string CPARENT
653 quote_fmt_insert_file($3);
656 | INSERT_PROGRAMOUTPUT OPARENT string CPARENT
659 quote_fmt_insert_program_output($3);