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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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 gint maxsize = 0;
46 static gint stacksize = 0;
48 static gchar *buffer = NULL;
49 static gint bufmax = 0;
50 static gint bufsize = 0;
51 static const gchar *quote_str = NULL;
52 static const gchar *body = NULL;
53 static gint error = 0;
55 static gint cursor_pos = 0;
57 extern int quote_fmt_firsttime;
59 static void add_visibility(gboolean val)
62 if (maxsize < stacksize) {
64 visible = g_realloc(visible, maxsize * sizeof(gboolean));
69 visible[stacksize - 1] = val;
72 static void remove_visibility(void)
77 static void add_buffer(const gchar *s)
82 if (bufsize + len + 1 > bufmax) {
85 while (bufsize + len + 1 > bufmax)
87 buffer = g_realloc(buffer, bufmax);
89 strcpy(buffer + bufsize, s);
93 gchar *quote_fmt_get_buffer(void)
101 gint quote_fmt_get_cursor_pos(void)
106 #define INSERT(buf) \
107 if (stacksize != 0 && visible[stacksize - 1]) \
110 #define INSERT_CHARACTER(chr) \
111 if (stacksize != 0 && visible[stacksize - 1]) { \
118 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
119 const gchar *my_body)
121 quote_str = my_quote_str;
125 add_visibility(TRUE);
131 * force LEX initialization
133 quote_fmt_firsttime = 1;
137 void quote_fmterror(char *str)
139 g_warning("Error: %s\n", str);
143 int quote_fmtwrap(void)
148 static int isseparator(int ch)
150 return g_ascii_isspace(ch) || ch == '.' || ch == '-';
153 static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
166 * ALF - GNU C's strftime() has a nice format specifier
167 * for time zone offset (%z). Non-standard however, so
171 #define RLEFT (sizeof result) - (rptr - result)
172 #define STR_SIZE(x) (sizeof (x) - 1)
176 if (procheader_date_parse_to_tm(msginfo->date, <, zone)) {
178 * break up format string in tiny bits delimited by valid %z's and
179 * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
181 for (rptr = result, fptr = format; fptr && *fptr && rptr < &result[sizeof result - 1];) {
186 if (NULL != (zptr = strstr(fptr, "%z"))) {
188 * count nr. of prepended percent chars
190 for (perc = 0, p = zptr; p && p >= format && *p == '%'; p--, perc++)
195 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
197 rptr += strftime(rptr, RLEFT, tmp, <);
201 * append time zone offset
203 if (zone[0] && perc % 2)
204 rptr += g_snprintf(rptr, RLEFT, "%s", zone);
205 fptr = zptr + STR_SIZE("%z");
207 rptr += strftime(rptr, RLEFT, fptr, <);
218 static void quote_fmt_show_first_name(const MsgInfo *msginfo)
223 if (!msginfo->fromname)
226 p = strchr(msginfo->fromname, ',');
228 /* fromname is like "Duck, Donald" */
230 while (*p && isspace(*p)) p++;
231 str = alloca(strlen(p) + 1);
237 /* fromname is like "Donald Duck" */
238 str = alloca(strlen(msginfo->fromname) + 1);
240 strcpy(str, msginfo->fromname);
242 while (*p && !isspace(*p)) p++;
249 static void quote_fmt_show_last_name(const MsgInfo *msginfo)
254 /* This probably won't work together very well with Middle
255 names and the like - thth */
256 if (!msginfo->fromname)
259 str = alloca(strlen(msginfo->fromname) + 1);
261 strcpy(str, msginfo->fromname);
262 p = strchr(str, ',');
264 /* fromname is like "Duck, Donald" */
268 /* fromname is like "Donald Duck" */
270 while (*p && !isspace(*p)) p++;
272 /* We found a space. Get first
273 none-space char and insert
274 rest of string from there. */
275 while (*p && isspace(*p)) p++;
279 /* If there is no none-space
280 char, just insert whole
285 /* If there is no space, just
286 insert whole fromname. */
293 static void quote_fmt_show_sender_initial(const MsgInfo *msginfo)
295 #define MAX_SENDER_INITIAL 20
296 gchar tmp[MAX_SENDER_INITIAL];
301 if (!msginfo->fromname)
304 p = msginfo->fromname;
307 if (*p && isalnum(*p)) {
311 if (len >= MAX_SENDER_INITIAL - 1)
315 while (*p && !isseparator(*p)) p++;
316 while (*p && isseparator(*p)) p++;
322 static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
323 gboolean quoted, gboolean signature,
324 const gchar *quote_str)
329 if (!(msginfo->folder || body))
333 fp = str_open_as_stream(body);
335 if (procmime_msginfo_is_encrypted(msginfo))
336 fp = procmime_get_first_encrypted_text_content(msginfo);
338 fp = procmime_get_first_text_content(msginfo);
342 g_warning("Can't get text part\n");
344 while (fgets(buf, sizeof(buf), fp) != NULL) {
347 if (!signature && strncmp(buf, "-- \n", 4) == 0)
350 if (quoted && quote_str)
359 static void quote_fmt_insert_file(const gchar *filename)
364 if ((file = g_fopen(filename, "rb")) != NULL) {
365 while (fgets(buffer, sizeof(buffer), file)) {
373 static void quote_fmt_insert_program_output(const gchar *progname)
378 if ((file = popen(progname, "r")) != NULL) {
379 while (fgets(buffer, sizeof(buffer), file)) {
393 %token SHOW_NEWSGROUPS
394 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
395 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
396 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
397 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB
398 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
399 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
400 %token QUERY_DATE QUERY_FROM
401 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
402 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
403 %token INSERT_FILE INSERT_PROGRAMOUTPUT
404 %token OPARENT CPARENT
406 %token SHOW_DATE_EXPR
407 %token SET_CURSOR_POS
411 %token <chr> CHARACTER
412 %type <chr> character
418 character_or_special_or_insert_or_query_list;
420 character_or_special_or_insert_or_query_list:
421 character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
422 | character_or_special_or_insert_or_query ;
424 character_or_special_or_insert_or_query:
428 INSERT_CHARACTER($1);
447 strncpy($$, $1, sizeof($$));
448 $$[sizeof($$) - 1] = '\0';
450 if (len + 1 < sizeof($$)) {
459 if (msginfo->newsgroups)
460 INSERT(msginfo->newsgroups);
462 | SHOW_DATE_EXPR OPARENT string CPARENT
464 quote_fmt_show_date(msginfo, $3);
469 INSERT(msginfo->date);
474 INSERT(msginfo->from);
478 if (msginfo->fromname)
479 INSERT(msginfo->fromname);
483 quote_fmt_show_first_name(msginfo);
487 quote_fmt_show_last_name(msginfo);
489 | SHOW_SENDER_INITIAL
491 quote_fmt_show_sender_initial(msginfo);
495 if (msginfo->subject)
496 INSERT(msginfo->subject);
506 INSERT(msginfo->msgid);
519 /* CLAWS: use in reply to header */
520 /* if (msginfo->references)
521 INSERT(msginfo->references); */
525 quote_fmt_show_msg(msginfo, body, FALSE, TRUE, quote_str);
527 | SHOW_QUOTED_MESSAGE
529 quote_fmt_show_msg(msginfo, body, TRUE, TRUE, quote_str);
531 | SHOW_MESSAGE_NO_SIGNATURE
533 quote_fmt_show_msg(msginfo, body, FALSE, FALSE, quote_str);
535 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
537 quote_fmt_show_msg(msginfo, body, TRUE, FALSE, quote_str);
569 cursor_pos = bufsize;
575 add_visibility(msginfo->date != NULL);
577 OPARENT quote_fmt CPARENT
583 add_visibility(msginfo->from != NULL);
585 OPARENT quote_fmt CPARENT
591 add_visibility(msginfo->fromname != NULL);
593 OPARENT quote_fmt CPARENT
599 add_visibility(msginfo->subject != NULL);
601 OPARENT quote_fmt CPARENT
607 add_visibility(msginfo->to != NULL);
609 OPARENT quote_fmt CPARENT
615 add_visibility(msginfo->newsgroups != NULL);
617 OPARENT quote_fmt CPARENT
623 add_visibility(msginfo->msgid != NULL);
625 OPARENT quote_fmt CPARENT
631 add_visibility(msginfo->cc != NULL);
633 OPARENT quote_fmt CPARENT
639 /* CLAWS: use in-reply-to header */
640 /* add_visibility(msginfo->references != NULL); */
642 OPARENT quote_fmt CPARENT
648 INSERT_FILE OPARENT string CPARENT
650 quote_fmt_insert_file($3);
652 | INSERT_PROGRAMOUTPUT OPARENT string CPARENT
654 quote_fmt_insert_program_output($3);