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 static void add_visibility(gboolean val)
60 if (maxsize < stacksize) {
62 visible = g_realloc(visible, maxsize * sizeof(gboolean));
67 visible[stacksize - 1] = val;
70 static void remove_visibility(void)
75 static void add_buffer(const gchar *s)
80 if (bufsize + len + 1 > bufmax) {
83 while (bufsize + len + 1 > bufmax)
85 buffer = g_realloc(buffer, bufmax);
87 strcpy(buffer + bufsize, s);
91 gchar *quote_fmt_get_buffer(void)
99 gint quote_fmt_get_cursor_pos(void)
104 #define INSERT(buf) \
105 if (stacksize != 0 && visible[stacksize - 1]) \
108 #define INSERT_CHARACTER(chr) \
109 if (stacksize != 0 && visible[stacksize - 1]) { \
116 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
117 const gchar *my_body)
119 quote_str = my_quote_str;
123 add_visibility(TRUE);
131 void quote_fmterror(char *str)
133 g_warning("Error: %s\n", str);
137 int quote_fmtwrap(void)
142 static int isseparator(int ch)
144 return isspace(ch) || ch == '.' || ch == '-';
147 static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
160 * ALF - GNU C's strftime() has a nice format specifier
161 * for time zone offset (%z). Non-standard however, so
165 #define RLEFT (sizeof result) - (rptr - result)
166 #define STR_SIZE(x) (sizeof (x) - 1)
170 if (procheader_date_parse_to_tm(msginfo->date, <, zone)) {
172 * break up format string in tiny bits delimited by valid %z's and
173 * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
175 for (rptr = result, fptr = format; fptr && *fptr && rptr < &result[sizeof result - 1];) {
180 if (NULL != (zptr = strstr(fptr, "%z"))) {
182 * count nr. of prepended percent chars
184 for (perc = 0, p = zptr; p && p >= format && *p == '%'; p--, perc++)
189 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
191 rptr += strftime(rptr, RLEFT, tmp, <);
195 * append time zone offset
197 if (zone[0] && perc % 2)
198 rptr += g_snprintf(rptr, RLEFT, "%s", zone);
199 fptr = zptr + STR_SIZE("%z");
201 rptr += strftime(rptr, RLEFT, fptr, <);
212 static void quote_fmt_show_first_name(const MsgInfo *msginfo)
217 if (!msginfo->fromname)
220 p = strchr(msginfo->fromname, ',');
222 /* fromname is like "Duck, Donald" */
224 while (*p && isspace(*p)) p++;
225 str = alloca(strlen(p) + 1);
231 /* fromname is like "Donald Duck" */
232 str = alloca(strlen(msginfo->fromname) + 1);
234 strcpy(str, msginfo->fromname);
236 while (*p && !isspace(*p)) p++;
243 static void quote_fmt_show_last_name(const MsgInfo *msginfo)
248 /* This probably won't work together very well with Middle
249 names and the like - thth */
250 if (!msginfo->fromname)
253 str = alloca(strlen(msginfo->fromname) + 1);
255 strcpy(str, msginfo->fromname);
256 p = strchr(str, ',');
258 /* fromname is like "Duck, Donald" */
262 /* fromname is like "Donald Duck" */
264 while (*p && !isspace(*p)) p++;
266 /* We found a space. Get first
267 none-space char and insert
268 rest of string from there. */
269 while (*p && isspace(*p)) p++;
273 /* If there is no none-space
274 char, just insert whole
279 /* If there is no space, just
280 insert whole fromname. */
287 static void quote_fmt_show_sender_initial(const MsgInfo *msginfo)
289 #define MAX_SENDER_INITIAL 20
290 gchar tmp[MAX_SENDER_INITIAL];
295 if (!msginfo->fromname)
298 p = msginfo->fromname;
301 if (*p && isalnum(*p)) {
305 if (len >= MAX_SENDER_INITIAL - 1)
309 while (*p && !isseparator(*p)) p++;
310 while (*p && isseparator(*p)) p++;
316 static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
317 gboolean quoted, gboolean signature,
318 const gchar *quote_str)
323 if (!(msginfo->folder || body))
327 fp = str_open_as_stream(body);
329 fp = procmime_get_first_text_content(msginfo);
332 g_warning("Can't get text part\n");
334 while (fgets(buf, sizeof(buf), fp) != NULL) {
337 if (!signature && strncmp(buf, "-- \n", 4) == 0)
340 if (quoted && quote_str)
349 static void quote_fmt_insert_file(const gchar *filename)
354 if ((file = fopen(filename, "rb")) != NULL) {
355 while (fgets(buffer, sizeof(buffer), file)) {
363 static void quote_fmt_insert_program_output(const gchar *progname)
368 if ((file = popen(progname, "r")) != NULL) {
369 while (fgets(buffer, sizeof(buffer), file)) {
383 %token SHOW_NEWSGROUPS
384 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
385 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
386 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
387 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB
388 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
389 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
390 %token QUERY_DATE QUERY_FROM
391 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
392 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
393 %token INSERT_FILE INSERT_PROGRAMOUTPUT
394 %token OPARENT CPARENT
396 %token SHOW_DATE_EXPR
397 %token SET_CURSOR_POS
401 %token <chr> CHARACTER
402 %type <chr> character
408 character_or_special_or_insert_or_query_list;
410 character_or_special_or_insert_or_query_list:
411 character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
412 | character_or_special_or_insert_or_query ;
414 character_or_special_or_insert_or_query:
418 INSERT_CHARACTER($1);
437 strncpy($$, $1, sizeof($$));
438 $$[sizeof($$) - 1] = '\0';
440 if (len + 1 < sizeof($$)) {
449 if (msginfo->newsgroups)
450 INSERT(msginfo->newsgroups);
452 | SHOW_DATE_EXPR OPARENT string CPARENT
454 quote_fmt_show_date(msginfo, $3);
459 INSERT(msginfo->date);
464 INSERT(msginfo->from);
468 if (msginfo->fromname)
469 INSERT(msginfo->fromname);
473 quote_fmt_show_first_name(msginfo);
477 quote_fmt_show_last_name(msginfo);
479 | SHOW_SENDER_INITIAL
481 quote_fmt_show_sender_initial(msginfo);
485 if (msginfo->subject)
486 INSERT(msginfo->subject);
496 INSERT(msginfo->msgid);
509 /* if (msginfo->references)
510 INSERT(msginfo->references); */
514 quote_fmt_show_msg(msginfo, body, FALSE, TRUE, quote_str);
516 | SHOW_QUOTED_MESSAGE
518 quote_fmt_show_msg(msginfo, body, TRUE, TRUE, quote_str);
520 | SHOW_MESSAGE_NO_SIGNATURE
522 quote_fmt_show_msg(msginfo, body, FALSE, FALSE, quote_str);
524 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
526 quote_fmt_show_msg(msginfo, body, TRUE, FALSE, quote_str);
558 cursor_pos = bufsize;
564 add_visibility(msginfo->date != NULL);
566 OPARENT quote_fmt CPARENT
572 add_visibility(msginfo->from != NULL);
574 OPARENT quote_fmt CPARENT
580 add_visibility(msginfo->fromname != NULL);
582 OPARENT quote_fmt CPARENT
588 add_visibility(msginfo->subject != NULL);
590 OPARENT quote_fmt CPARENT
596 add_visibility(msginfo->to != NULL);
598 OPARENT quote_fmt CPARENT
604 add_visibility(msginfo->newsgroups != NULL);
606 OPARENT quote_fmt CPARENT
612 add_visibility(msginfo->msgid != NULL);
614 OPARENT quote_fmt CPARENT
620 add_visibility(msginfo->cc != NULL);
622 OPARENT quote_fmt CPARENT
628 /* add_visibility(msginfo->references != NULL); */
630 OPARENT quote_fmt CPARENT
636 INSERT_FILE OPARENT string CPARENT
638 quote_fmt_insert_file($3);
640 | INSERT_PROGRAMOUTPUT OPARENT string CPARENT
642 quote_fmt_insert_program_output($3);