2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail 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.
25 #include <glib/gi18n.h>
33 #include "procheader.h"
34 #include "gtk/inputdialog.h"
36 #include "quote_fmt.h"
37 #include "quote_fmt_lex.h"
42 bison -p quote_fmt quote_fmt.y
47 static MsgInfo *msginfo = NULL;
48 static PrefsAccount *account = NULL;
49 static gboolean *visible = NULL;
50 static gboolean dry_run = FALSE;
51 static gint maxsize = 0;
52 static gint stacksize = 0;
53 static GHashTable *var_table = NULL;
55 typedef struct st_buffer
62 static struct st_buffer main_expr = { NULL, 0, 0 };
63 static struct st_buffer sub_expr = { NULL, 0, 0 };
64 static struct st_buffer* current = NULL;
66 static const gchar *quote_str = NULL;
67 static const gchar *body = NULL;
68 static gint error = 0;
70 static gint cursor_pos = -1;
72 extern int quote_fmt_firsttime;
74 static void add_visibility(gboolean val)
77 if (maxsize < stacksize) {
79 visible = g_realloc(visible, maxsize * sizeof(gboolean));
84 visible[stacksize - 1] = val;
87 static void remove_visibility(void)
91 g_warning("Error: visibility stack underflow\n");
96 static void add_buffer(const gchar *s)
104 if (current->bufsize + len + 1 > current->bufmax) {
105 if (current->bufmax == 0)
106 current->bufmax = 128;
107 while (current->bufsize + len + 1 > current->bufmax)
108 current->bufmax *= 2;
109 current->buffer = g_realloc(current->buffer, current->bufmax);
111 strcpy(current->buffer + current->bufsize, s);
112 current->bufsize += len;
115 static void clear_buffer(void)
118 *current->buffer = '\0';
119 current->bufsize = 0;
122 gchar *quote_fmt_get_buffer(void)
124 if (current != &main_expr)
125 g_warning("Error: parser still in sub-expr mode\n");
130 return current->buffer;
133 gint quote_fmt_get_cursor_pos(void)
138 #define INSERT(buf) \
139 if (stacksize != 0 && visible[stacksize - 1])\
142 #define INSERT_CHARACTER(chr) \
143 if (stacksize != 0 && visible[stacksize - 1]) { \
150 void quote_fmt_reset_vartable(void)
153 g_hash_table_destroy(var_table);
158 void quote_fmt_init(MsgInfo *info, const gchar *my_quote_str,
159 const gchar *my_body, gboolean my_dry_run,
160 PrefsAccount *compose_account)
162 quote_str = my_quote_str;
165 account = compose_account;
166 dry_run = my_dry_run;
168 add_visibility(TRUE);
169 main_expr.bufmax = 0;
171 current = &main_expr;
176 var_table = g_hash_table_new_full(g_str_hash, g_str_equal,
180 * force LEX initialization
182 quote_fmt_firsttime = 1;
186 void quote_fmterror(char *str)
188 g_warning("Error: %s\n", str);
192 int quote_fmtwrap(void)
197 static int isseparator(int ch)
199 return g_ascii_isspace(ch) || ch == '.' || ch == '-';
202 static void quote_fmt_show_date(const MsgInfo *msginfo, const gchar *format)
215 * ALF - GNU C's strftime() has a nice format specifier
216 * for time zone offset (%z). Non-standard however, so
220 #define RLEFT (sizeof result) - (rptr - result)
221 #define STR_SIZE(x) (sizeof (x) - 1)
225 if (procheader_date_parse_to_tm(msginfo->date, <, zone)) {
227 * break up format string in tiny bits delimited by valid %z's and
228 * feed it to strftime(). don't forget that '%%z' mean literal '%z'.
230 for (rptr = result, fptr = format; fptr && *fptr && rptr < &result[sizeof result - 1];) {
235 if (NULL != (zptr = strstr(fptr, "%z"))) {
237 * count nr. of prepended percent chars
239 for (perc = 0, p = zptr; p && p >= format && *p == '%'; p--, perc++)
244 tmp = g_strndup(fptr, zptr - fptr + (perc % 2 ? 0 : STR_SIZE("%z")));
246 rptr += strftime(rptr, RLEFT, tmp, <);
250 * append time zone offset
252 if (zone[0] && perc % 2)
253 rptr += g_snprintf(rptr, RLEFT, "%s", zone);
254 fptr = zptr + STR_SIZE("%z");
256 rptr += strftime(rptr, RLEFT, fptr, <);
261 if (g_utf8_validate(result, -1, NULL)) {
264 gchar *utf = conv_codeset_strdup(result,
265 conv_get_locale_charset_str_no_utf8(),
268 !g_utf8_validate(utf, -1, NULL)) {
270 utf = g_malloc(strlen(result)*2+1);
271 conv_localetodisp(utf,
272 strlen(result)*2+1, result);
274 if (g_utf8_validate(utf, -1, NULL)) {
284 static void quote_fmt_show_first_name(const MsgInfo *msginfo)
289 if (!msginfo->fromname)
292 p = (guchar*)strchr(msginfo->fromname, ',');
294 /* fromname is like "Duck, Donald" */
296 while (*p && isspace(*p)) p++;
297 str = alloca(strlen((char *)p) + 1);
299 strcpy(str, (char *)p);
303 /* fromname is like "Donald Duck" */
304 str = alloca(strlen(msginfo->fromname) + 1);
306 strcpy(str, msginfo->fromname);
308 while (*p && !isspace(*p)) p++;
315 static void quote_fmt_show_last_name(const MsgInfo *msginfo)
320 /* This probably won't work together very well with Middle
321 names and the like - thth */
322 if (!msginfo->fromname)
325 str = alloca(strlen(msginfo->fromname) + 1);
327 strcpy(str, msginfo->fromname);
328 p = strchr(str, ',');
330 /* fromname is like "Duck, Donald" */
334 /* fromname is like "Donald Duck" */
336 while (*p && !isspace(*p)) p++;
338 /* We found a space. Get first
339 none-space char and insert
340 rest of string from there. */
341 while (*p && isspace(*p)) p++;
345 /* If there is no none-space
346 char, just insert whole
351 /* If there is no space, just
352 insert whole fromname. */
359 static void quote_fmt_show_sender_initial(const MsgInfo *msginfo)
361 #define MAX_SENDER_INITIAL 20
362 gchar tmp[MAX_SENDER_INITIAL];
367 if (!msginfo->fromname)
370 p = (guchar *)msginfo->fromname;
373 if (*p && g_utf8_validate((gchar *)p, 1, NULL)) {
377 if (len >= MAX_SENDER_INITIAL - 1)
381 while (*p && !isseparator(*p)) p++;
382 while (*p && isseparator(*p)) p++;
388 static void quote_fmt_show_msg(MsgInfo *msginfo, const gchar *body,
389 gboolean quoted, gboolean signature,
390 const gchar *quote_str)
395 if (!(msginfo->folder || body))
399 fp = str_open_as_stream(body);
401 if (procmime_msginfo_is_encrypted(msginfo))
402 fp = procmime_get_first_encrypted_text_content(msginfo);
404 fp = procmime_get_first_text_content(msginfo);
408 g_warning("Can't get text part\n");
410 while (fgets(buf, sizeof(buf), fp) != NULL) {
413 if (!signature && strncmp(buf, "-- \n", 4) == 0)
416 if (quoted && quote_str)
425 static void quote_fmt_insert_file(const gchar *filename)
430 if ((file = g_fopen(filename, "rb")) != NULL) {
431 while (fgets(buffer, sizeof(buffer), file)) {
439 static void quote_fmt_insert_program_output(const gchar *progname)
444 if ((file = popen(progname, "r")) != NULL) {
445 while (fgets(buffer, sizeof(buffer), file)) {
452 static void quote_fmt_insert_user_input(const gchar *varname)
460 if ((text = g_hash_table_lookup(var_table, varname)) == NULL) {
461 buf = g_strdup_printf(_("Enter text to replace '%s'"), varname);
462 text = input_dialog(_("Enter variable"), buf, "");
466 g_hash_table_insert(var_table, g_strdup(varname), g_strdup(text));
468 /* don't free the one in hashtable at the end */
469 text = g_strdup(text);
486 %token SHOW_NEWSGROUPS
487 %token SHOW_DATE SHOW_FROM SHOW_FULLNAME SHOW_FIRST_NAME SHOW_LAST_NAME
488 %token SHOW_SENDER_INITIAL SHOW_SUBJECT SHOW_TO SHOW_MESSAGEID
489 %token SHOW_PERCENT SHOW_CC SHOW_REFERENCES SHOW_MESSAGE
490 %token SHOW_QUOTED_MESSAGE SHOW_BACKSLASH SHOW_TAB
491 %token SHOW_QUOTED_MESSAGE_NO_SIGNATURE SHOW_MESSAGE_NO_SIGNATURE
492 %token SHOW_EOL SHOW_QUESTION_MARK SHOW_EXCLAMATION_MARK SHOW_PIPE SHOW_OPARENT SHOW_CPARENT
493 %token SHOW_ACCOUNT_FULL_NAME SHOW_ACCOUNT_MAIL_ADDRESS SHOW_ACCOUNT_NAME SHOW_ACCOUNT_ORGANIZATION
495 %token QUERY_DATE QUERY_FROM
496 %token QUERY_FULLNAME QUERY_SUBJECT QUERY_TO QUERY_NEWSGROUPS
497 %token QUERY_MESSAGEID QUERY_CC QUERY_REFERENCES
498 %token QUERY_ACCOUNT_FULL_NAME QUERY_ACCOUNT_ORGANIZATION
499 /* tokens QUERY_NOT */
500 %token QUERY_NOT_DATE QUERY_NOT_FROM
501 %token QUERY_NOT_FULLNAME QUERY_NOT_SUBJECT QUERY_NOT_TO QUERY_NOT_NEWSGROUPS
502 %token QUERY_NOT_MESSAGEID QUERY_NOT_CC QUERY_NOT_REFERENCES
503 %token QUERY_NOT_ACCOUNT_FULL_NAME QUERY_NOT_ACCOUNT_ORGANIZATION
505 %token INSERT_FILE INSERT_PROGRAMOUTPUT INSERT_USERINPUT
506 %token OPARENT CPARENT
508 %token SHOW_DATE_EXPR
509 %token SET_CURSOR_POS
513 %token <chr> CHARACTER
514 %type <chr> character
520 character_or_special_or_insert_or_query_list ;
523 character_or_special_list ;
525 character_or_special_or_insert_or_query_list:
526 character_or_special_or_insert_or_query character_or_special_or_insert_or_query_list
527 | character_or_special_or_insert_or_query ;
529 character_or_special_list:
530 character_or_special character_or_special_list
531 | character_or_special ;
533 character_or_special_or_insert_or_query:
539 character_or_special:
543 INSERT_CHARACTER($1);
560 strncpy($$, $1, sizeof($$));
561 $$[sizeof($$) - 1] = '\0';
563 if (len + 1 < sizeof($$)) {
572 if (msginfo->newsgroups)
573 INSERT(msginfo->newsgroups);
575 | SHOW_DATE_EXPR OPARENT string CPARENT
577 quote_fmt_show_date(msginfo, $3);
582 INSERT(msginfo->date);
587 INSERT(msginfo->from);
591 if (msginfo->fromname)
592 INSERT(msginfo->fromname);
596 quote_fmt_show_first_name(msginfo);
600 quote_fmt_show_last_name(msginfo);
602 | SHOW_SENDER_INITIAL
604 quote_fmt_show_sender_initial(msginfo);
608 if (msginfo->subject)
609 INSERT(msginfo->subject);
619 INSERT(msginfo->msgid);
634 INSERT(msginfo->inreplyto);
635 for (item = msginfo->references; item != NULL; item = g_slist_next(item))
641 quote_fmt_show_msg(msginfo, body, FALSE, TRUE, quote_str);
643 | SHOW_QUOTED_MESSAGE
645 quote_fmt_show_msg(msginfo, body, TRUE, TRUE, quote_str);
647 | SHOW_MESSAGE_NO_SIGNATURE
649 quote_fmt_show_msg(msginfo, body, FALSE, FALSE, quote_str);
651 | SHOW_QUOTED_MESSAGE_NO_SIGNATURE
653 quote_fmt_show_msg(msginfo, body, TRUE, FALSE, quote_str);
655 | SHOW_ACCOUNT_FULL_NAME
657 if (account && account->name)
658 INSERT(account->name);
660 | SHOW_ACCOUNT_MAIL_ADDRESS
662 if (account && account->address)
663 INSERT(account->address);
667 if (account && account->account_name)
668 INSERT(account->account_name);
670 | SHOW_ACCOUNT_ORGANIZATION
672 if (account && account->organization)
673 INSERT(account->organization);
691 | SHOW_EXCLAMATION_MARK
709 cursor_pos = current->bufsize;
715 add_visibility(msginfo->date != NULL);
717 OPARENT quote_fmt CPARENT
723 add_visibility(msginfo->from != NULL);
725 OPARENT quote_fmt CPARENT
731 add_visibility(msginfo->fromname != NULL);
733 OPARENT quote_fmt CPARENT
739 add_visibility(msginfo->subject != NULL);
741 OPARENT quote_fmt CPARENT
747 add_visibility(msginfo->to != NULL);
749 OPARENT quote_fmt CPARENT
755 add_visibility(msginfo->newsgroups != NULL);
757 OPARENT quote_fmt CPARENT
763 add_visibility(msginfo->msgid != NULL);
765 OPARENT quote_fmt CPARENT
771 add_visibility(msginfo->cc != NULL);
773 OPARENT quote_fmt CPARENT
782 found = (msginfo->inreplyto != NULL);
783 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
786 add_visibility(found == TRUE);
788 OPARENT quote_fmt CPARENT
792 | QUERY_ACCOUNT_FULL_NAME
794 add_visibility(account != NULL && account->name != NULL);
796 OPARENT quote_fmt CPARENT
800 | QUERY_ACCOUNT_ORGANIZATION
802 add_visibility(account != NULL && account->organization != NULL);
804 OPARENT quote_fmt CPARENT
812 add_visibility(msginfo->date == NULL);
814 OPARENT quote_fmt CPARENT
820 add_visibility(msginfo->from == NULL);
822 OPARENT quote_fmt CPARENT
828 add_visibility(msginfo->fromname == NULL);
830 OPARENT quote_fmt CPARENT
836 add_visibility(msginfo->subject == NULL);
838 OPARENT quote_fmt CPARENT
844 add_visibility(msginfo->to == NULL);
846 OPARENT quote_fmt CPARENT
850 | QUERY_NOT_NEWSGROUPS
852 add_visibility(msginfo->newsgroups == NULL);
854 OPARENT quote_fmt CPARENT
858 | QUERY_NOT_MESSAGEID
860 add_visibility(msginfo->msgid == NULL);
862 OPARENT quote_fmt CPARENT
868 add_visibility(msginfo->cc == NULL);
870 OPARENT quote_fmt CPARENT
874 | QUERY_NOT_REFERENCES
879 found = (msginfo->inreplyto != NULL);
880 for (item = msginfo->references; found == FALSE && item != NULL; item = g_slist_next(item))
883 add_visibility(found == FALSE);
885 OPARENT quote_fmt CPARENT
889 | QUERY_NOT_ACCOUNT_FULL_NAME
891 add_visibility(account == NULL || account->name == NULL);
893 OPARENT quote_fmt CPARENT
897 | QUERY_NOT_ACCOUNT_ORGANIZATION
899 add_visibility(account == NULL || account->organization == NULL);
901 OPARENT quote_fmt CPARENT
912 OPARENT sub_expr CPARENT
914 current = &main_expr;
916 quote_fmt_insert_file(sub_expr.buffer);
919 | INSERT_PROGRAMOUTPUT
924 OPARENT sub_expr CPARENT
926 current = &main_expr;
928 quote_fmt_insert_program_output(sub_expr.buffer);
936 OPARENT sub_expr CPARENT
938 current = &main_expr;
940 quote_fmt_insert_user_input(sub_expr.buffer);