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 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 fp = procmime_get_first_text_content(msginfo);
338 g_warning("Can't get text part\n");
340 while (fgets(buf, sizeof(buf), fp) != NULL) {
343 if (!signature && strncmp(buf, "-- \n", 4) == 0)
346 if (quoted && quote_str)
355 static void quote_fmt_insert_file(const gchar *filename)
360 if ((file = fopen(filename, "rb")) != NULL) {
361 while (fgets(buffer, sizeof(buffer), file)) {
369 static void quote_fmt_insert_program_output(const gchar *progname)
374 if ((file = popen(progname, "r")) != NULL) {
375 while (fgets(buffer, sizeof(buffer), file)) {
389 %token SHOW_NEWSGROUPS
390 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
391 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
392 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
393 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB
394 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
395 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
396 %token QUERY_DATE QUERY_FROM
397 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
398 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
399 %token INSERT_FILE INSERT_PROGRAMOUTPUT
400 %token OPARENT CPARENT
402 %token SHOW_DATE_EXPR
403 %token SET_CURSOR_POS
407 %token <chr> CHARACTER
408 %type <chr> character
414 character_or_special_or_insert_or_query_list;
416 character_or_special_or_insert_or_query_list:
417 character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
418 | character_or_special_or_insert_or_query ;
420 character_or_special_or_insert_or_query:
424 INSERT_CHARACTER($1);
443 strncpy($$, $1, sizeof($$));
444 $$[sizeof($$) - 1] = '\0';
446 if (len + 1 < sizeof($$)) {
455 if (msginfo->newsgroups)
456 INSERT(msginfo->newsgroups);
458 | SHOW_DATE_EXPR OPARENT string CPARENT
460 quote_fmt_show_date(msginfo, $3);
465 INSERT(msginfo->date);
470 INSERT(msginfo->from);
474 if (msginfo->fromname)
475 INSERT(msginfo->fromname);
479 quote_fmt_show_first_name(msginfo);
483 quote_fmt_show_last_name(msginfo);
485 | SHOW_SENDER_INITIAL
487 quote_fmt_show_sender_initial(msginfo);
491 if (msginfo->subject)
492 INSERT(msginfo->subject);
502 INSERT(msginfo->msgid);
515 /* if (msginfo->references)
516 INSERT(msginfo->references); */
520 quote_fmt_show_msg(msginfo, body, FALSE, TRUE, quote_str);
522 | SHOW_QUOTED_MESSAGE
524 quote_fmt_show_msg(msginfo, body, TRUE, TRUE, quote_str);
526 | SHOW_MESSAGE_NO_SIGNATURE
528 quote_fmt_show_msg(msginfo, body, FALSE, FALSE, quote_str);
530 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
532 quote_fmt_show_msg(msginfo, body, TRUE, FALSE, quote_str);
564 cursor_pos = bufsize;
570 add_visibility(msginfo->date != NULL);
572 OPARENT quote_fmt CPARENT
578 add_visibility(msginfo->from != NULL);
580 OPARENT quote_fmt CPARENT
586 add_visibility(msginfo->fromname != NULL);
588 OPARENT quote_fmt CPARENT
594 add_visibility(msginfo->subject != NULL);
596 OPARENT quote_fmt CPARENT
602 add_visibility(msginfo->to != NULL);
604 OPARENT quote_fmt CPARENT
610 add_visibility(msginfo->newsgroups != NULL);
612 OPARENT quote_fmt CPARENT
618 add_visibility(msginfo->msgid != NULL);
620 OPARENT quote_fmt CPARENT
626 add_visibility(msginfo->cc != NULL);
628 OPARENT quote_fmt CPARENT
634 /* add_visibility(msginfo->references != NULL); */
636 OPARENT quote_fmt CPARENT
642 INSERT_FILE OPARENT string CPARENT
644 quote_fmt_insert_file($3);
646 | INSERT_PROGRAMOUTPUT OPARENT string CPARENT
648 quote_fmt_insert_program_output($3);