claws-mail/src/matcher.c

1674 lines
41 KiB
C
Raw Normal View History

2002-08-15 09:38:17 +02:00
/*
* Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
2004-05-10 12:22:28 +02:00
* Copyright (C) 2002-2004 by the Sylpheed Claws Team and Hiroyuki Yamamoto
2002-08-15 09:38:17 +02:00
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
2005-09-21 [paul] 1.9.14cvs47 * README * README.jp * configure.ac * config/config.rpath * doc/manual/en/sylpheed-20.html * doc/manual/es/sylpheed-20.html * po/bg.po * po/ca.po * po/de.po * po/es.po * po/fr.po * po/it.po * po/pt_BR.po * po/sk.po * po/zh_TW.po * src/account.c * src/account.h * src/action.c * src/action.h * src/adbookbase.h * src/addr_compl.c * src/addr_compl.h * src/addrbook.c * src/addrbook.h * src/addrcache.c * src/addrcache.h * src/addrcindex.c * src/addrcindex.h * src/addrclip.c * src/addrclip.h * src/addrdefs.h * src/addressadd.c * src/addressadd.h * src/addressbook.c * src/addressbook.h * src/addressitem.h * src/addrgather.c * src/addrgather.h * src/addrharvest.c * src/addrharvest.h * src/addrindex.c * src/addrindex.h * src/addritem.c * src/addritem.h * src/addrquery.c * src/addrquery.h * src/addrselect.c * src/addrselect.h * src/alertpanel.c * src/alertpanel.h * src/browseldap.c * src/browseldap.h * src/codeconv.c * src/codeconv.h * src/compose.c * src/compose.h * src/crash.c * src/crash.h * src/customheader.c * src/customheader.h * src/displayheader.c * src/displayheader.h * src/editaddress.c * src/editaddress.h * src/editbook.c * src/editbook.h * src/editgroup.c * src/editgroup.h * src/editjpilot.c * src/editjpilot.h * src/editldap.c * src/editldap.h * src/editldap_basedn.c * src/editldap_basedn.h * src/editvcard.c * src/editvcard.h * src/enriched.c * src/enriched.h * src/exphtmldlg.c * src/exphtmldlg.h * src/expldifdlg.c * src/expldifdlg.h * src/export.c * src/export.h * src/exporthtml.c * src/exporthtml.h * src/exportldif.c * src/exportldif.h * src/filtering.c * src/filtering.h * src/folder.c * src/folder.h * src/folder_item_prefs.c * src/folder_item_prefs.h * src/foldersel.c * src/foldersel.h * src/folderutils.c * src/folderutils.h * src/folderview.c * src/folderview.h * src/grouplistdialog.c * src/grouplistdialog.h * src/headerview.c * src/headerview.h * src/html.c * src/html.h * src/image_viewer.c * src/image_viewer.h * src/imap.c * src/imap.h * src/imap_gtk.c * src/imap_gtk.h * src/import.c * src/import.h * src/importldif.c * src/importldif.h * src/importmutt.c * src/importmutt.h * src/importpine.c * src/importpine.h * src/inc.c * src/inc.h * src/jpilot.c * src/jpilot.h * src/ldapctrl.c * src/ldapctrl.h * src/ldaplocate.c * src/ldaplocate.h * src/ldapquery.c * src/ldapquery.h * src/ldapserver.c * src/ldapserver.h * src/ldaputil.c * src/ldaputil.h * src/ldif.c * src/ldif.h * src/localfolder.c * src/localfolder.h * src/main.c * src/main.h * src/mainwindow.c * src/mainwindow.h * src/manual.c * src/manual.h * src/matcher.c * src/matcher.h * src/matcher_parser.h * src/matcher_parser_lex.l * src/matcher_parser_parse.y * src/mbox.c * src/mbox.h * src/message_search.c * src/message_search.h * src/messageview.c * src/messageview.h * src/mh.c * src/mh.h * src/mh_gtk.c * src/mh_gtk.h * src/mimeview.c * src/mimeview.h * src/msgcache.c * src/msgcache.h * src/mutt.c * src/mutt.h * src/news.c * src/news.h * src/news_gtk.c * src/news_gtk.h * src/noticeview.c * src/noticeview.h * src/partial_download.c * src/partial_download.h * src/pine.c * src/pine.h * src/pop.c * src/pop.h * src/prefs_account.c * src/prefs_account.h * src/prefs_actions.c * src/prefs_actions.h * src/prefs_common.c * src/prefs_common.h * src/prefs_compose_writing.c * src/prefs_compose_writing.h * src/prefs_customheader.c * src/prefs_customheader.h * src/prefs_display_header.c * src/prefs_display_header.h * src/prefs_ext_prog.c * src/prefs_ext_prog.h * src/prefs_filtering.c * src/prefs_filtering.h * src/prefs_filtering_action.c * src/prefs_filtering_action.h * src/prefs_folder_column.c * src/prefs_folder_column.h * src/prefs_folder_item.c * src/prefs_folder_item.h * src/prefs_fonts.c * src/prefs_fonts.h * src/prefs_gtk.c * src/prefs_gtk.h * src/prefs_image_viewer.c * src/prefs_image_viewer.h * src/prefs_matcher.c * src/prefs_matcher.h * src/prefs_message.c * src/prefs_message.h * src/prefs_msg_colors.c * src/prefs_msg_colors.h * src/prefs_other.c * src/prefs_other.h * src/prefs_quote.c * src/prefs_quote.h * src/prefs_receive.c * src/prefs_receive.h * src/prefs_send.c * src/prefs_send.h * src/prefs_spelling.c * src/prefs_spelling.h * src/prefs_summaries.c * src/prefs_summaries.h * src/prefs_summary_column.c * src/prefs_summary_column.h * src/prefs_template.c * src/prefs_template.h * src/prefs_themes.c * src/prefs_themes.h * src/prefs_toolbar.c * src/prefs_toolbar.h * src/prefs_wrapping.c * src/prefs_wrapping.h * src/privacy.c * src/privacy.h * src/procheader.c * src/procheader.h * src/procmime.c * src/procmime.h * src/procmsg.c * src/procmsg.h * src/quote_fmt.c * src/quote_fmt_parse.y * src/recv.c * src/recv.h * src/remotefolder.c * src/remotefolder.h * src/send_message.c * src/send_message.h * src/setup.c * src/setup.h * src/simple-gettext.c * src/sourcewindow.c * src/sourcewindow.h * src/ssl_manager.c * src/ssl_manager.h * src/statusbar.c * src/statusbar.h * src/stock_pixmap.c * src/stock_pixmap.h * src/summary_search.c * src/summary_search.h * src/summaryview.c * src/summaryview.h * src/syldap.c * src/syldap.h * src/textview.c * src/textview.h * src/toolbar.c * src/toolbar.h * src/undo.c * src/undo.h * src/unmime.c * src/unmime.h * src/vcard.c * src/vcard.h * src/wizard.c * src/wizard.h * src/common/base64.c * src/common/base64.h * src/common/defs.h * src/common/hooks.c * src/common/hooks.h * src/common/log.c * src/common/log.h * src/common/md5.c * src/common/md5.h * src/common/mgutils.c * src/common/mgutils.h * src/common/nntp.c * src/common/nntp.h * src/common/passcrypt.c * src/common/passcrypt.h.in * src/common/plugin.c * src/common/plugin.h * src/common/prefs.c * src/common/prefs.h * src/common/progressindicator.c * src/common/progressindicator.h * src/common/quoted-printable.c * src/common/quoted-printable.h * src/common/session.c * src/common/session.h * src/common/smtp.c * src/common/smtp.h * src/common/socket.c * src/common/socket.h * src/common/ssl.c * src/common/ssl.h * src/common/ssl_certificate.c * src/common/ssl_certificate.h * src/common/string_match.c * src/common/string_match.h * src/common/stringtable.c * src/common/sylpheed.c * src/common/sylpheed.h * src/common/template.c * src/common/template.h * src/common/timing.h * src/common/utils.c * src/common/utils.h * src/common/uuencode.c * src/common/uuencode.h * src/common/version.h.in * src/common/xml.c * src/common/xml.h * src/common/xmlprops.c * src/common/xmlprops.h * src/gtk/about.c * src/gtk/about.h * src/gtk/colorlabel.c * src/gtk/colorlabel.h * src/gtk/colorsel.c * src/gtk/colorsel.h * src/gtk/description_window.c * src/gtk/description_window.h * src/gtk/filesel.c * src/gtk/filesel.h * src/gtk/foldersort.c * src/gtk/foldersort.h * src/gtk/gtkutils.c * src/gtk/gtkutils.h * src/gtk/inputdialog.c * src/gtk/inputdialog.h * src/gtk/logwindow.c * src/gtk/logwindow.h * src/gtk/manage_window.c * src/gtk/manage_window.h * src/gtk/menu.c * src/gtk/menu.h * src/gtk/pluginwindow.c * src/gtk/pluginwindow.h * src/gtk/prefswindow.c * src/gtk/prefswindow.h * src/gtk/progressdialog.c * src/gtk/progressdialog.h * src/gtk/quicksearch.c * src/gtk/quicksearch.h * src/gtk/sslcertwindow.c * src/gtk/sslcertwindow.h * src/plugins/clamav/clamav_plugin.c * src/plugins/clamav/clamav_plugin.h * src/plugins/clamav/clamav_plugin_gtk.c * src/plugins/demo/demo.c * src/plugins/dillo_viewer/dillo_prefs.c * src/plugins/dillo_viewer/dillo_prefs.h * src/plugins/dillo_viewer/dillo_viewer.c * src/plugins/mathml_viewer/mathml_viewer.c * src/plugins/pgpcore/passphrase.c * src/plugins/pgpcore/passphrase.h * src/plugins/pgpcore/plugin.c * src/plugins/pgpcore/prefs_gpg.c * src/plugins/pgpcore/prefs_gpg.h * src/plugins/pgpcore/select-keys.c * src/plugins/pgpcore/select-keys.h * src/plugins/pgpcore/sgpgme.c * src/plugins/pgpcore/sgpgme.h * src/plugins/pgpinline/pgpinline.c * src/plugins/pgpinline/pgpinline.h * src/plugins/pgpinline/plugin.c * src/plugins/pgpmime/pgpmime.c * src/plugins/pgpmime/pgpmime.h * src/plugins/pgpmime/plugin.c * src/plugins/spamassassin/spamassassin.c * src/plugins/spamassassin/spamassassin.h * src/plugins/spamassassin/spamassassin_gtk.c * src/plugins/trayicon/trayicon.c * tools/OOo2sylpheed.pl * tools/acroread2sylpheed.pl * tools/asus_mailled.sh * tools/filter_conv.pl * tools/freshmeat_search.pl * tools/google_search.pl * tools/kmail2sylpheed.pl * tools/kmail2sylpheed_v2.pl * tools/maildir2sylpheed.pl * tools/multiwebsearch.pl * tools/newscache_clean.pl * tools/outlook2sylpheed.pl * tools/sylpheed-switcher * tools/update-po * tools/vcard2xml.py * tools/kdeservicemenu/sylpheed-kdeservicemenu.pl update FSF address
2005-09-21 20:22:51 +02:00
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2002-08-15 09:38:17 +02:00
*/
2005-02-10 [paul] 1.0.1cvs3.2 * AUTHORS * Makefile.am * autogen.sh * configure.ac * config/mkinstalldirs * intl/.cvsignore * intl/ChangeLog * intl/VERSION * intl/bindtextdom.c * intl/config.charset * intl/dcgettext.c * intl/dcigettext.c * intl/dcngettext.c * intl/dgettext.c * intl/dngettext.c * intl/eval-plural.h * intl/explodename.c * intl/finddomain.c * intl/gettext.c * intl/gettextP.h * intl/gmo.h * intl/hash-string.h * intl/intl-compat.c * intl/l10nflist.c * intl/libgnuintl.h.in * intl/loadinfo.h * intl/loadmsgcat.c * intl/localcharset.c * intl/localcharset.h * intl/locale.alias * intl/localealias.c * intl/localename.c * intl/log.c * intl/ngettext.c * intl/os2compat.c * intl/os2compat.h * intl/osdep.c * intl/plural-exp.c * intl/plural-exp.h * intl/plural.c * intl/plural.y * intl/ref-add.sin * intl/ref-del.sin * intl/relocatable.c * intl/relocatable.h * intl/textdomain.c * m4/Makefile.am * po/Makefile.in.in * src/Makefile.am * src/account.c * src/action.c * src/addr_compl.c * src/addressadd.c * src/addressbook.c * src/addrgather.c * src/addrindex.c * src/alertpanel.c * src/browseldap.c * src/codeconv.c * src/codeconv.h * src/compose.c * src/compose.h * src/crash.c * src/editaddress.c * src/editbook.c * src/editgroup.c * src/editjpilot.c * src/editldap.c * src/editldap_basedn.c * src/editvcard.c * src/exphtmldlg.c * src/expldifdlg.c * src/export.c * src/exporthtml.c * src/filtering.c * src/folder.c * src/folder_item_prefs.c * src/foldersel.c * src/folderview.c * src/grouplistdialog.c * src/headerview.c * src/imap.c * src/imap_gtk.c * src/import.c * src/importldif.c * src/importmutt.c * src/importpine.c * src/inc.c * src/ldif.c * src/main.c * src/mainwindow.c * src/matcher.c * src/mbox.c * src/message_search.c * src/messageview.c * src/mh.c * src/mh_gtk.c * src/mimeview.c * src/msgcache.c * src/news.c * src/news_gtk.c * src/noticeview.c * src/partial_download.c * src/pop.c * src/prefs_account.c * src/prefs_actions.c * src/prefs_common.c * src/prefs_customheader.c * src/prefs_display_header.c * src/prefs_ext_prog.c * src/prefs_filtering.c * src/prefs_filtering_action.c * src/prefs_folder_item.c * src/prefs_fonts.c * src/prefs_gtk.c * src/prefs_matcher.c * src/prefs_msg_colors.c * src/prefs_spelling.c * src/prefs_summary_column.c * src/prefs_template.c * src/prefs_themes.c * src/prefs_toolbar.c * src/prefs_wrapping.c * src/privacy.c * src/procheader.c * src/procmime.c * src/procmsg.c * src/quote_fmt.c * src/recv.c * src/send_message.c * src/setup.c * src/sourcewindow.c * src/ssl_manager.c * src/statusbar.c * src/summary_search.c * src/summaryview.c * src/textview.c * src/toolbar.c * src/wizard.c * src/common/nntp.c * src/common/plugin.c * src/common/smtp.c * src/common/ssl.c * src/common/ssl_certificate.c * src/common/string_match.c * src/common/sylpheed.c * src/common/template.c * src/common/utils.c * src/gtk/about.c * src/gtk/colorlabel.c * src/gtk/description_window.c * src/gtk/filesel.c * src/gtk/foldersort.c * src/gtk/gtkaspell.c * src/gtk/gtkutils.c * src/gtk/inputdialog.c * src/gtk/logwindow.c * src/gtk/menu.c * src/gtk/pluginwindow.c * src/gtk/prefswindow.c * src/gtk/progressdialog.c * src/gtk/quicksearch.c * src/gtk/sslcertwindow.c * src/plugins/clamav/clamav_plugin.c * src/plugins/clamav/clamav_plugin_gtk.c * src/plugins/demo/demo.c * src/plugins/dillo_viewer/dillo_prefs.c * src/plugins/dillo_viewer/dillo_viewer.c * src/plugins/image_viewer/plugin.c * src/plugins/image_viewer/viewer.c * src/plugins/image_viewer/viewerprefs.c * src/plugins/mathml_viewer/mathml_viewer.c * src/plugins/pgpmime/passphrase.c * src/plugins/pgpmime/plugin.c * src/plugins/pgpmime/prefs_gpg.c * src/plugins/pgpmime/select-keys.c * src/plugins/pgpmime/sgpgme.c * src/plugins/spamassassin/spamassassin.c * src/plugins/spamassassin/spamassassin_gtk.c * src/plugins/trayicon/trayicon.c sync with main (revision 104)
2005-02-10 13:06:07 +01:00
#include <glib.h>
#include <glib/gi18n.h>
2001-05-06 06:27:28 +02:00
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
2005-02-10 [paul] 1.0.1cvs3.2 * AUTHORS * Makefile.am * autogen.sh * configure.ac * config/mkinstalldirs * intl/.cvsignore * intl/ChangeLog * intl/VERSION * intl/bindtextdom.c * intl/config.charset * intl/dcgettext.c * intl/dcigettext.c * intl/dcngettext.c * intl/dgettext.c * intl/dngettext.c * intl/eval-plural.h * intl/explodename.c * intl/finddomain.c * intl/gettext.c * intl/gettextP.h * intl/gmo.h * intl/hash-string.h * intl/intl-compat.c * intl/l10nflist.c * intl/libgnuintl.h.in * intl/loadinfo.h * intl/loadmsgcat.c * intl/localcharset.c * intl/localcharset.h * intl/locale.alias * intl/localealias.c * intl/localename.c * intl/log.c * intl/ngettext.c * intl/os2compat.c * intl/os2compat.h * intl/osdep.c * intl/plural-exp.c * intl/plural-exp.h * intl/plural.c * intl/plural.y * intl/ref-add.sin * intl/ref-del.sin * intl/relocatable.c * intl/relocatable.h * intl/textdomain.c * m4/Makefile.am * po/Makefile.in.in * src/Makefile.am * src/account.c * src/action.c * src/addr_compl.c * src/addressadd.c * src/addressbook.c * src/addrgather.c * src/addrindex.c * src/alertpanel.c * src/browseldap.c * src/codeconv.c * src/codeconv.h * src/compose.c * src/compose.h * src/crash.c * src/editaddress.c * src/editbook.c * src/editgroup.c * src/editjpilot.c * src/editldap.c * src/editldap_basedn.c * src/editvcard.c * src/exphtmldlg.c * src/expldifdlg.c * src/export.c * src/exporthtml.c * src/filtering.c * src/folder.c * src/folder_item_prefs.c * src/foldersel.c * src/folderview.c * src/grouplistdialog.c * src/headerview.c * src/imap.c * src/imap_gtk.c * src/import.c * src/importldif.c * src/importmutt.c * src/importpine.c * src/inc.c * src/ldif.c * src/main.c * src/mainwindow.c * src/matcher.c * src/mbox.c * src/message_search.c * src/messageview.c * src/mh.c * src/mh_gtk.c * src/mimeview.c * src/msgcache.c * src/news.c * src/news_gtk.c * src/noticeview.c * src/partial_download.c * src/pop.c * src/prefs_account.c * src/prefs_actions.c * src/prefs_common.c * src/prefs_customheader.c * src/prefs_display_header.c * src/prefs_ext_prog.c * src/prefs_filtering.c * src/prefs_filtering_action.c * src/prefs_folder_item.c * src/prefs_fonts.c * src/prefs_gtk.c * src/prefs_matcher.c * src/prefs_msg_colors.c * src/prefs_spelling.c * src/prefs_summary_column.c * src/prefs_template.c * src/prefs_themes.c * src/prefs_toolbar.c * src/prefs_wrapping.c * src/privacy.c * src/procheader.c * src/procmime.c * src/procmsg.c * src/quote_fmt.c * src/recv.c * src/send_message.c * src/setup.c * src/sourcewindow.c * src/ssl_manager.c * src/statusbar.c * src/summary_search.c * src/summaryview.c * src/textview.c * src/toolbar.c * src/wizard.c * src/common/nntp.c * src/common/plugin.c * src/common/smtp.c * src/common/ssl.c * src/common/ssl_certificate.c * src/common/string_match.c * src/common/sylpheed.c * src/common/template.c * src/common/utils.c * src/gtk/about.c * src/gtk/colorlabel.c * src/gtk/description_window.c * src/gtk/filesel.c * src/gtk/foldersort.c * src/gtk/gtkaspell.c * src/gtk/gtkutils.c * src/gtk/inputdialog.c * src/gtk/logwindow.c * src/gtk/menu.c * src/gtk/pluginwindow.c * src/gtk/prefswindow.c * src/gtk/progressdialog.c * src/gtk/quicksearch.c * src/gtk/sslcertwindow.c * src/plugins/clamav/clamav_plugin.c * src/plugins/clamav/clamav_plugin_gtk.c * src/plugins/demo/demo.c * src/plugins/dillo_viewer/dillo_prefs.c * src/plugins/dillo_viewer/dillo_viewer.c * src/plugins/image_viewer/plugin.c * src/plugins/image_viewer/viewer.c * src/plugins/image_viewer/viewerprefs.c * src/plugins/mathml_viewer/mathml_viewer.c * src/plugins/pgpmime/passphrase.c * src/plugins/pgpmime/plugin.c * src/plugins/pgpmime/prefs_gpg.c * src/plugins/pgpmime/select-keys.c * src/plugins/pgpmime/sgpgme.c * src/plugins/spamassassin/spamassassin.c * src/plugins/spamassassin/spamassassin_gtk.c * src/plugins/trayicon/trayicon.c sync with main (revision 104)
2005-02-10 13:06:07 +01:00
2001-05-06 06:27:28 +02:00
#include "defs.h"
#include "utils.h"
2001-05-06 06:27:28 +02:00
#include "procheader.h"
#include "matcher.h"
#include "matcher_parser.h"
#include "prefs_gtk.h"
2004-05-10 12:22:28 +02:00
#include "addr_compl.h"
#include "codeconv.h"
#include "quoted-printable.h"
2004-01-12 22:28:31 +01:00
#include <ctype.h>
2001-05-06 06:27:28 +02:00
/*!
*\brief Keyword lookup element
*/
2001-05-06 06:27:28 +02:00
struct _MatchParser {
gint id; /*!< keyword id */
gchar *str; /*!< keyword */
2001-05-06 06:27:28 +02:00
};
typedef struct _MatchParser MatchParser;
/*!
*\brief Table with strings and ids used by the lexer and
* the parser. New keywords can be added here.
*/
static const MatchParser matchparser_tab[] = {
2001-05-11 01:00:27 +02:00
/* msginfo flags */
{MATCHCRITERIA_ALL, "all"},
{MATCHCRITERIA_UNREAD, "unread"},
{MATCHCRITERIA_NOT_UNREAD, "~unread"},
{MATCHCRITERIA_NEW, "new"},
{MATCHCRITERIA_NOT_NEW, "~new"},
{MATCHCRITERIA_MARKED, "marked"},
{MATCHCRITERIA_NOT_MARKED, "~marked"},
{MATCHCRITERIA_DELETED, "deleted"},
{MATCHCRITERIA_NOT_DELETED, "~deleted"},
{MATCHCRITERIA_REPLIED, "replied"},
{MATCHCRITERIA_NOT_REPLIED, "~replied"},
{MATCHCRITERIA_FORWARDED, "forwarded"},
{MATCHCRITERIA_NOT_FORWARDED, "~forwarded"},
{MATCHCRITERIA_LOCKED, "locked"},
{MATCHCRITERIA_NOT_LOCKED, "~locked"},
{MATCHCRITERIA_COLORLABEL, "colorlabel"},
{MATCHCRITERIA_NOT_COLORLABEL, "~colorlabel"},
{MATCHCRITERIA_IGNORE_THREAD, "ignore_thread"},
{MATCHCRITERIA_NOT_IGNORE_THREAD, "~ignore_thread"},
2001-05-11 01:00:27 +02:00
/* msginfo headers */
{MATCHCRITERIA_SUBJECT, "subject"},
{MATCHCRITERIA_NOT_SUBJECT, "~subject"},
{MATCHCRITERIA_FROM, "from"},
{MATCHCRITERIA_NOT_FROM, "~from"},
{MATCHCRITERIA_TO, "to"},
{MATCHCRITERIA_NOT_TO, "~to"},
{MATCHCRITERIA_CC, "cc"},
{MATCHCRITERIA_NOT_CC, "~cc"},
{MATCHCRITERIA_TO_OR_CC, "to_or_cc"},
{MATCHCRITERIA_NOT_TO_AND_NOT_CC, "~to_or_cc"},
{MATCHCRITERIA_AGE_GREATER, "age_greater"},
{MATCHCRITERIA_AGE_LOWER, "age_lower"},
{MATCHCRITERIA_NEWSGROUPS, "newsgroups"},
{MATCHCRITERIA_NOT_NEWSGROUPS, "~newsgroups"},
{MATCHCRITERIA_INREPLYTO, "inreplyto"},
{MATCHCRITERIA_NOT_INREPLYTO, "~inreplyto"},
{MATCHCRITERIA_REFERENCES, "references"},
{MATCHCRITERIA_NOT_REFERENCES, "~references"},
{MATCHCRITERIA_SCORE_GREATER, "score_greater"},
{MATCHCRITERIA_SCORE_LOWER, "score_lower"},
{MATCHCRITERIA_SCORE_EQUAL, "score_equal"},
{MATCHCRITERIA_PARTIAL, "partial"},
{MATCHCRITERIA_NOT_PARTIAL, "~partial"},
2001-05-06 06:27:28 +02:00
{MATCHCRITERIA_SIZE_GREATER, "size_greater"},
{MATCHCRITERIA_SIZE_SMALLER, "size_smaller"},
{MATCHCRITERIA_SIZE_EQUAL, "size_equal"},
2001-05-06 06:27:28 +02:00
/* content have to be read */
{MATCHCRITERIA_HEADER, "header"},
{MATCHCRITERIA_NOT_HEADER, "~header"},
{MATCHCRITERIA_HEADERS_PART, "headers_part"},
{MATCHCRITERIA_NOT_HEADERS_PART, "~headers_part"},
{MATCHCRITERIA_MESSAGE, "message"},
{MATCHCRITERIA_NOT_MESSAGE, "~message"},
{MATCHCRITERIA_BODY_PART, "body_part"},
{MATCHCRITERIA_NOT_BODY_PART, "~body_part"},
2004-01-12 22:28:31 +01:00
{MATCHCRITERIA_TEST, "test"},
{MATCHCRITERIA_NOT_TEST, "~test"},
2001-05-06 06:27:28 +02:00
/* match type */
{MATCHTYPE_MATCHCASE, "matchcase"},
{MATCHTYPE_MATCH, "match"},
{MATCHTYPE_REGEXPCASE, "regexpcase"},
{MATCHTYPE_REGEXP, "regexp"},
2004-05-10 12:22:28 +02:00
{MATCHTYPE_ANY_IN_ADDRESSBOOK, "any_in_addressbook"},
{MATCHTYPE_ALL_IN_ADDRESSBOOK, "all_in_addressbook"},
2001-05-06 06:27:28 +02:00
/* actions */
2004-01-12 22:28:31 +01:00
{MATCHACTION_SCORE, "score"}, /* for backward compatibility */
{MATCHACTION_MOVE, "move"},
{MATCHACTION_COPY, "copy"},
{MATCHACTION_DELETE, "delete"},
{MATCHACTION_MARK, "mark"},
{MATCHACTION_UNMARK, "unmark"},
{MATCHACTION_LOCK, "lock"},
{MATCHACTION_UNLOCK, "unlock"},
{MATCHACTION_MARK_AS_READ, "mark_as_read"},
{MATCHACTION_MARK_AS_UNREAD, "mark_as_unread"},
{MATCHACTION_FORWARD, "forward"},
{MATCHACTION_FORWARD_AS_ATTACHMENT, "forward_as_attachment"},
{MATCHACTION_EXECUTE, "execute"},
{MATCHACTION_COLOR, "color"},
2002-05-23 10:18:33 +02:00
{MATCHACTION_REDIRECT, "redirect"},
2004-01-12 22:28:31 +01:00
{MATCHACTION_CHANGE_SCORE, "change_score"},
{MATCHACTION_SET_SCORE, "set_score"},
{MATCHACTION_STOP, "stop"},
{MATCHACTION_HIDE, "hide"},
{MATCHACTION_IGNORE, "ignore"},
2001-05-06 06:27:28 +02:00
};
/*!
*\brief Look up table with keywords defined in \sa matchparser_tab
*/
static GHashTable *matchparser_hashtab;
/*!
*\brief Translate keyword id to keyword string
*
*\param id Id of keyword
*
*\return const gchar * Keyword
*/
const gchar *get_matchparser_tab_str(gint id)
2001-05-06 06:27:28 +02:00
{
gint i;
for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
if (matchparser_tab[i].id == id)
return matchparser_tab[i].str;
2001-05-06 06:27:28 +02:00
}
return NULL;
2001-05-06 06:27:28 +02:00
}
/*!
*\brief Create keyword lookup table
*/
static void create_matchparser_hashtab(void)
{
int i;
if (matchparser_hashtab) return;
matchparser_hashtab = g_hash_table_new(g_str_hash, g_str_equal);
for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++)
g_hash_table_insert(matchparser_hashtab,
matchparser_tab[i].str,
(gpointer) &matchparser_tab[i]);
}
/*!
*\brief Return a keyword id from a keyword string
*
*\param str Keyword string
*
*\return gint Keyword id
*/
gint get_matchparser_tab_id(const gchar *str)
{
MatchParser *res;
if (NULL != (res = g_hash_table_lookup(matchparser_hashtab, str))) {
return res->id;
} else
return -1;
}
2001-05-06 06:27:28 +02:00
/* **************** data structure allocation **************** */
/*!
*\brief Allocate a structure for a filtering / scoring
* "condition" (a matcher structure)
*
*\param criteria Criteria ID (MATCHCRITERIA_XXXX)
*\param header Header string (if criteria is MATCHCRITERIA_HEADER)
*\param matchtype Type of action (MATCHTYPE_XXX)
*\param expr String value or expression to check
*\param value Integer value to check
*
*\return MatcherProp * Pointer to newly allocated structure
2001-12-23 20:02:36 +01:00
*/
MatcherProp *matcherprop_new(gint criteria, const gchar *header,
gint matchtype, const gchar *expr,
2001-05-13 04:57:01 +02:00
int value)
2001-05-06 06:27:28 +02:00
{
MatcherProp *prop;
2001-05-06 06:27:28 +02:00
prop = g_new0(MatcherProp, 1);
prop->criteria = criteria;
prop->header = header != NULL ? g_strdup(header) : NULL;
prop->expr = expr != NULL ? g_strdup(expr) : NULL;
2001-05-06 06:27:28 +02:00
prop->matchtype = matchtype;
prop->preg = NULL;
2001-05-13 04:57:01 +02:00
prop->value = value;
2001-05-06 06:27:28 +02:00
prop->error = 0;
return prop;
}
/*!
*\brief Free a matcher structure
*
*\param prop Pointer to matcher structure allocated with
* #matcherprop_new
2001-12-23 20:02:36 +01:00
*/
void matcherprop_free(MatcherProp *prop)
2001-05-06 06:27:28 +02:00
{
g_free(prop->expr);
g_free(prop->header);
2001-05-06 06:27:28 +02:00
if (prop->preg != NULL) {
regfree(prop->preg);
g_free(prop->preg);
}
g_free(prop);
}
/*!
*\brief Copy a matcher structure
*
*\param src Matcher structure to copy
*
*\return MatcherProp * Pointer to newly allocated matcher structure
*/
MatcherProp *matcherprop_copy(const MatcherProp *src)
{
MatcherProp *prop = g_new0(MatcherProp, 1);
prop->criteria = src->criteria;
prop->header = src->header ? g_strdup(src->header) : NULL;
prop->expr = src->expr ? g_strdup(src->expr) : NULL;
prop->matchtype = src->matchtype;
prop->preg = NULL; /* will be re-evaluated */
prop->value = src->value;
prop->error = src->error;
return prop;
}
2001-05-06 06:27:28 +02:00
/* ************** match ******************************/
2004-05-10 12:22:28 +02:00
static gboolean match_with_addresses_in_addressbook
(MatcherProp *prop, const gchar *str, gint type)
{
GSList *address_list = NULL;
GSList *walk;
gboolean res = FALSE;
if (str == NULL || *str == 0)
return FALSE;
/* XXX: perhaps complete with comments too */
address_list = address_list_append(address_list, str);
if (!address_list)
return FALSE;
start_address_completion();
res = FALSE;
for (walk = address_list; walk != NULL; walk = walk->next) {
gboolean found = complete_address(walk->data) ? TRUE : FALSE;
g_free(walk->data);
if (!found && type == MATCHTYPE_ALL_IN_ADDRESSBOOK) {
res = FALSE;
break;
} else if (found)
res = TRUE;
}
g_slist_free(address_list);
end_address_completion();
return res;
}
/*!
*\brief Find out if a string matches a condition
*
*\param prop Matcher structure
*\param str String to check
*
*\return gboolean TRUE if str matches the condition in the
* matcher structure
*/
static gboolean matcherprop_string_match(MatcherProp *prop, const gchar *str)
2001-05-06 06:27:28 +02:00
{
gchar *str1;
gchar *str2;
2001-05-06 06:27:28 +02:00
if (str == NULL)
2001-05-11 01:00:27 +02:00
return FALSE;
2001-05-06 06:27:28 +02:00
switch (prop->matchtype) {
case MATCHTYPE_REGEXPCASE:
case MATCHTYPE_REGEXP:
2001-05-06 06:27:28 +02:00
if (!prop->preg && (prop->error == 0)) {
prop->preg = g_new0(regex_t, 1);
/* if regexp then don't use the escaped string */
if (regcomp(prop->preg, prop->expr,
2001-05-06 06:27:28 +02:00
REG_NOSUB | REG_EXTENDED
| ((prop->matchtype == MATCHTYPE_REGEXPCASE)
2001-05-06 06:27:28 +02:00
? REG_ICASE : 0)) != 0) {
prop->error = 1;
g_free(prop->preg);
prop->preg = NULL;
2001-05-06 06:27:28 +02:00
}
}
if (prop->preg == NULL)
2001-05-11 01:00:27 +02:00
return FALSE;
2001-05-06 06:27:28 +02:00
if (regexec(prop->preg, str, 0, NULL, 0) == 0)
2001-05-11 01:00:27 +02:00
return TRUE;
2001-05-06 06:27:28 +02:00
else
2001-05-11 01:00:27 +02:00
return FALSE;
2004-05-10 12:22:28 +02:00
case MATCHTYPE_ALL_IN_ADDRESSBOOK:
case MATCHTYPE_ANY_IN_ADDRESSBOOK:
return match_with_addresses_in_addressbook
(prop, str, prop->matchtype);
2001-05-06 06:27:28 +02:00
case MATCHTYPE_MATCH:
return (strstr(str, prop->expr) != NULL);
2001-05-06 06:27:28 +02:00
/* FIXME: put upper in unesc_str */
case MATCHTYPE_MATCHCASE:
str2 = alloca(strlen(prop->expr) + 1);
strcpy(str2, prop->expr);
2001-05-11 01:00:27 +02:00
g_strup(str2);
2001-05-06 06:27:28 +02:00
str1 = alloca(strlen(str) + 1);
strcpy(str1, str);
g_strup(str1);
2001-05-11 01:00:27 +02:00
return (strstr(str1, str2) != NULL);
2001-05-06 06:27:28 +02:00
default:
2001-05-11 01:00:27 +02:00
return FALSE;
2001-05-06 06:27:28 +02:00
}
}
/* FIXME body search is a hack. */
static gboolean matcherprop_string_decode_match(MatcherProp *prop, const gchar *str)
{
gchar *utf = NULL;
gchar tmp[BUFFSIZE];
gboolean res = FALSE;
if (str == NULL)
return FALSE;
/* we try to decode QP first, because it's faster than base64 */
qp_decode_const(tmp, BUFFSIZE-1, str);
if (!g_utf8_validate(tmp, -1, NULL)) {
utf = conv_codeset_strdup
(tmp, conv_get_locale_charset_str_no_utf8(),
CS_INTERNAL);
res = matcherprop_string_match(prop, utf);
g_free(utf);
} else {
res = matcherprop_string_match(prop, tmp);
}
if (res == FALSE && strchr(prop->expr, '=') || strchr(prop->expr, '_') ) {
/* if searching for something with an equal char, maybe
* we should try to match the non-decoded string.
* In case it was not qp-encoded. */
if (!g_utf8_validate(str, -1, NULL)) {
utf = conv_codeset_strdup
(str, conv_get_locale_charset_str_no_utf8(),
CS_INTERNAL);
res = matcherprop_string_match(prop, utf);
} else {
res = matcherprop_string_match(prop, str);
}
}
/* FIXME base64 decoding is too slow, especially since text can
* easily be handled as base64. Don't even try now. */
return res;
}
/*!
*\brief Execute a command defined in the matcher structure
*
*\param prop Pointer to matcher structure
*\param info Pointer to message info structure
*
*\return gboolean TRUE if command was executed succesfully
*/
2004-01-12 22:28:31 +01:00
static gboolean matcherprop_match_test(const MatcherProp *prop,
MsgInfo *info)
{
gchar *file;
gchar *cmd;
gint retval;
2001-11-25 14:47:45 +01:00
file = procmsg_get_message_file(info);
if (file == NULL)
return FALSE;
2002-08-18 16:23:06 +02:00
g_free(file);
2001-11-25 14:47:45 +01:00
cmd = matching_build_command(prop->expr, info);
if (cmd == NULL)
return FALSE;
retval = system(cmd);
2002-08-15 09:38:17 +02:00
debug_print("Command exit code: %d\n", retval);
g_free(cmd);
return (retval == 0);
}
/*!
*\brief Check if a message matches the condition in a matcher
* structure.
*
*\param prop Pointer to matcher structure
*\param info Pointer to message info
*
*\return gboolean TRUE if a match
*/
gboolean matcherprop_match(MatcherProp *prop,
MsgInfo *info)
2001-05-06 06:27:28 +02:00
{
time_t t;
switch(prop->criteria) {
case MATCHCRITERIA_ALL:
2001-05-06 06:27:28 +02:00
return 1;
case MATCHCRITERIA_UNREAD:
2001-05-11 01:00:27 +02:00
return MSG_IS_UNREAD(info->flags);
case MATCHCRITERIA_NOT_UNREAD:
2001-05-11 01:00:27 +02:00
return !MSG_IS_UNREAD(info->flags);
case MATCHCRITERIA_NEW:
2001-05-11 01:00:27 +02:00
return MSG_IS_NEW(info->flags);
case MATCHCRITERIA_NOT_NEW:
2001-05-11 01:00:27 +02:00
return !MSG_IS_NEW(info->flags);
case MATCHCRITERIA_MARKED:
2001-05-11 01:00:27 +02:00
return MSG_IS_MARKED(info->flags);
case MATCHCRITERIA_NOT_MARKED:
2001-05-11 01:00:27 +02:00
return !MSG_IS_MARKED(info->flags);
case MATCHCRITERIA_DELETED:
2001-05-11 01:00:27 +02:00
return MSG_IS_DELETED(info->flags);
case MATCHCRITERIA_NOT_DELETED:
2001-05-11 01:00:27 +02:00
return !MSG_IS_DELETED(info->flags);
case MATCHCRITERIA_REPLIED:
2001-05-11 01:00:27 +02:00
return MSG_IS_REPLIED(info->flags);
case MATCHCRITERIA_NOT_REPLIED:
2001-05-11 01:00:27 +02:00
return !MSG_IS_REPLIED(info->flags);
case MATCHCRITERIA_FORWARDED:
2001-05-11 01:00:27 +02:00
return MSG_IS_FORWARDED(info->flags);
case MATCHCRITERIA_NOT_FORWARDED:
2001-05-11 01:00:27 +02:00
return !MSG_IS_FORWARDED(info->flags);
case MATCHCRITERIA_LOCKED:
return MSG_IS_LOCKED(info->flags);
case MATCHCRITERIA_NOT_LOCKED:
return !MSG_IS_LOCKED(info->flags);
case MATCHCRITERIA_COLORLABEL:
return MSG_GET_COLORLABEL_VALUE(info->flags) == prop->value;
case MATCHCRITERIA_NOT_COLORLABEL:
return MSG_GET_COLORLABEL_VALUE(info->flags) != prop->value;
case MATCHCRITERIA_IGNORE_THREAD:
return MSG_IS_IGNORE_THREAD(info->flags);
case MATCHCRITERIA_NOT_IGNORE_THREAD:
return !MSG_IS_IGNORE_THREAD(info->flags);
case MATCHCRITERIA_SUBJECT:
2001-05-06 06:27:28 +02:00
return matcherprop_string_match(prop, info->subject);
case MATCHCRITERIA_NOT_SUBJECT:
2001-05-06 06:27:28 +02:00
return !matcherprop_string_match(prop, info->subject);
case MATCHCRITERIA_FROM:
2001-05-06 06:27:28 +02:00
return matcherprop_string_match(prop, info->from);
case MATCHCRITERIA_NOT_FROM:
2001-05-06 06:27:28 +02:00
return !matcherprop_string_match(prop, info->from);
case MATCHCRITERIA_TO:
2001-05-06 06:27:28 +02:00
return matcherprop_string_match(prop, info->to);
case MATCHCRITERIA_NOT_TO:
2001-05-06 06:27:28 +02:00
return !matcherprop_string_match(prop, info->to);
case MATCHCRITERIA_CC:
2001-05-06 06:27:28 +02:00
return matcherprop_string_match(prop, info->cc);
case MATCHCRITERIA_NOT_CC:
2001-05-06 06:27:28 +02:00
return !matcherprop_string_match(prop, info->cc);
case MATCHCRITERIA_TO_OR_CC:
2001-05-06 06:27:28 +02:00
return matcherprop_string_match(prop, info->to)
|| matcherprop_string_match(prop, info->cc);
case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
2001-05-06 06:27:28 +02:00
return !(matcherprop_string_match(prop, info->to)
|| matcherprop_string_match(prop, info->cc));
case MATCHCRITERIA_AGE_GREATER:
2001-05-06 06:27:28 +02:00
t = time(NULL);
2001-05-13 04:57:01 +02:00
return ((t - info->date_t) / (60 * 60 * 24)) >= prop->value;
case MATCHCRITERIA_AGE_LOWER:
2001-05-06 06:27:28 +02:00
t = time(NULL);
2001-05-13 04:57:01 +02:00
return ((t - info->date_t) / (60 * 60 * 24)) <= prop->value;
case MATCHCRITERIA_SCORE_GREATER:
2001-05-13 04:57:01 +02:00
return info->score >= prop->value;
case MATCHCRITERIA_SCORE_LOWER:
2001-05-13 04:57:01 +02:00
return info->score <= prop->value;
case MATCHCRITERIA_SCORE_EQUAL:
2001-09-18 13:02:34 +02:00
return info->score == prop->value;
case MATCHCRITERIA_SIZE_GREATER:
/* FIXME: info->size is an off_t */
return info->size > (off_t) prop->value;
case MATCHCRITERIA_SIZE_EQUAL:
/* FIXME: info->size is an off_t */
return info->size == (off_t) prop->value;
case MATCHCRITERIA_SIZE_SMALLER:
/* FIXME: info->size is an off_t */
return info->size < (off_t) prop->value;
case MATCHCRITERIA_PARTIAL:
/* FIXME: info->size is an off_t */
return (info->total_size != 0 && info->size != (off_t)info->total_size);
case MATCHCRITERIA_NOT_PARTIAL:
/* FIXME: info->size is an off_t */
return (info->total_size == 0 || info->size == (off_t)info->total_size);
case MATCHCRITERIA_NEWSGROUPS:
2001-05-06 06:27:28 +02:00
return matcherprop_string_match(prop, info->newsgroups);
case MATCHCRITERIA_NOT_NEWSGROUPS:
2001-05-06 06:27:28 +02:00
return !matcherprop_string_match(prop, info->newsgroups);
case MATCHCRITERIA_INREPLYTO:
return matcherprop_string_match(prop, info->inreplyto);
case MATCHCRITERIA_NOT_INREPLYTO:
return !matcherprop_string_match(prop, info->inreplyto);
/* FIXME: Using inreplyto, but matching the (newly implemented)
* list of references is better */
case MATCHCRITERIA_REFERENCES:
return matcherprop_string_match(prop, info->inreplyto);
case MATCHCRITERIA_NOT_REFERENCES:
return !matcherprop_string_match(prop, info->inreplyto);
2004-01-12 22:28:31 +01:00
case MATCHCRITERIA_TEST:
return matcherprop_match_test(prop, info);
case MATCHCRITERIA_NOT_TEST:
return !matcherprop_match_test(prop, info);
2001-05-06 06:27:28 +02:00
default:
return 0;
}
}
/* ********************* MatcherList *************************** */
/*!
*\brief Create a new list of matchers
*
*\param matchers List of matcher structures
*\param bool_and Operator
*
*\return MatcherList * New list
*/
MatcherList *matcherlist_new(GSList *matchers, gboolean bool_and)
2001-05-06 06:27:28 +02:00
{
MatcherList *cond;
2001-05-06 06:27:28 +02:00
cond = g_new0(MatcherList, 1);
cond->matchers = matchers;
cond->bool_and = bool_and;
return cond;
}
/*!
*\brief Frees a list of matchers
*
*\param cond List of matchers
*/
void matcherlist_free(MatcherList *cond)
2001-05-06 06:27:28 +02:00
{
GSList *l;
2001-05-11 01:00:27 +02:00
g_return_if_fail(cond);
for (l = cond->matchers ; l != NULL ; l = g_slist_next(l)) {
2001-05-06 06:27:28 +02:00
matcherprop_free((MatcherProp *) l->data);
}
g_free(cond);
}
/*!
*\brief Skip all headers in a message file
*
*\param fp Message file
2001-05-06 06:27:28 +02:00
*/
static void matcherlist_skip_headers(FILE *fp)
2001-05-06 06:27:28 +02:00
{
gchar buf[BUFFSIZE];
while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1)
;
2001-05-06 06:27:28 +02:00
}
/*!
*\brief Check if a header matches a matcher condition
*
*\param matcher Matcher structure to check header for
*\param buf Header name
*
*\return boolean TRUE if matching header
2001-05-06 06:27:28 +02:00
*/
static gboolean matcherprop_match_one_header(MatcherProp *matcher,
gchar *buf)
2001-05-06 06:27:28 +02:00
{
gboolean result;
Header *header;
switch (matcher->criteria) {
case MATCHCRITERIA_HEADER:
case MATCHCRITERIA_NOT_HEADER:
2001-05-06 06:27:28 +02:00
header = procheader_parse_header(buf);
2001-05-15 13:13:35 +02:00
if (!header)
return FALSE;
2001-05-06 06:27:28 +02:00
if (procheader_headername_equal(header->name,
matcher->header)) {
if (matcher->criteria == MATCHCRITERIA_HEADER)
2001-05-06 06:27:28 +02:00
result = matcherprop_string_match(matcher, header->body);
else
result = !matcherprop_string_match(matcher, header->body);
procheader_header_free(header);
return result;
}
2001-07-03 15:35:11 +02:00
else {
procheader_header_free(header);
}
2001-05-06 06:27:28 +02:00
break;
case MATCHCRITERIA_HEADERS_PART:
2001-05-06 06:27:28 +02:00
return matcherprop_string_match(matcher, buf);
case MATCHCRITERIA_MESSAGE:
return matcherprop_string_decode_match(matcher, buf);
case MATCHCRITERIA_NOT_MESSAGE:
return !matcherprop_string_decode_match(matcher, buf);
case MATCHCRITERIA_NOT_HEADERS_PART:
2001-05-06 06:27:28 +02:00
return !matcherprop_string_match(matcher, buf);
}
return FALSE;
}
/*!
*\brief Check if the matcher structure wants headers to
* be matched
*
*\param matcher Matcher structure
*
*\return gboolean TRUE if the matcher structure describes
* a header match condition
2001-05-06 06:27:28 +02:00
*/
static gboolean matcherprop_criteria_headers(const MatcherProp *matcher)
2001-05-06 06:27:28 +02:00
{
switch (matcher->criteria) {
case MATCHCRITERIA_HEADER:
case MATCHCRITERIA_NOT_HEADER:
case MATCHCRITERIA_HEADERS_PART:
case MATCHCRITERIA_NOT_HEADERS_PART:
return TRUE;
default:
return FALSE;
}
}
/*!
*\brief Check if the matcher structure wants the message
* to be matched (just perform an action on any
* message)
*
*\param matcher Matcher structure
*
*\return gboolean TRUE if matcher condition should match
* a message
*/
static gboolean matcherprop_criteria_message(MatcherProp *matcher)
{
switch (matcher->criteria) {
case MATCHCRITERIA_MESSAGE:
case MATCHCRITERIA_NOT_MESSAGE:
2001-05-06 06:27:28 +02:00
return TRUE;
default:
return FALSE;
}
}
/*!
*\brief Check if a list of conditions matches one header in
* a message file.
*
*\param matchers List of conditions
*\param fp Message file
*
*\return gboolean TRUE if one of the headers is matched by
* the list of conditions.
2001-05-06 06:27:28 +02:00
*/
static gboolean matcherlist_match_headers(MatcherList *matchers, FILE *fp)
2001-05-06 06:27:28 +02:00
{
GSList *l;
gchar buf[BUFFSIZE];
2001-05-15 06:50:27 +02:00
while (procheader_get_one_field(buf, sizeof(buf), fp, NULL) != -1) {
for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
MatcherProp *matcher = (MatcherProp *) l->data;
if (matcher->done)
continue;
/* if the criteria is ~headers_part or ~message, ZERO lines
* must NOT match for the rule to match. */
if (matcher->criteria == MATCHCRITERIA_NOT_HEADERS_PART ||
matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
if (matcherprop_match_one_header(matcher, buf)) {
matcher->result = TRUE;
} else {
matcher->result = FALSE;
matcher->done = TRUE;
}
/* else, just one line matching is enough for the rule to match
*/
} else if (matcherprop_criteria_headers(matcher) ||
matcherprop_criteria_message(matcher)){
if (matcherprop_match_one_header(matcher, buf)) {
matcher->result = TRUE;
matcher->done = TRUE;
}
}
/* if the rule matched and the matchers are OR, no need to
* check the others */
if (matcher->result && matcher->done) {
2001-05-06 06:27:28 +02:00
if (!matchers->bool_and)
return TRUE;
}
}
}
2001-05-15 06:50:27 +02:00
return FALSE;
2001-05-06 06:27:28 +02:00
}
/*!
*\brief Check if a matcher wants to check the message body
*
*\param matcher Matcher structure
*
*\return gboolean TRUE if body must be matched.
2001-05-06 06:27:28 +02:00
*/
static gboolean matcherprop_criteria_body(const MatcherProp *matcher)
2001-05-06 06:27:28 +02:00
{
switch (matcher->criteria) {
case MATCHCRITERIA_BODY_PART:
case MATCHCRITERIA_NOT_BODY_PART:
2001-05-06 06:27:28 +02:00
return TRUE;
default:
return FALSE;
}
}
/*!
*\brief Check if a (line) string matches the criteria
* described by a matcher structure
*
*\param matcher Matcher structure
*\param line String
*
*\return gboolean TRUE if string matches criteria
2001-05-06 06:27:28 +02:00
*/
static gboolean matcherprop_match_line(MatcherProp *matcher, const gchar *line)
2001-05-06 06:27:28 +02:00
{
switch (matcher->criteria) {
case MATCHCRITERIA_BODY_PART:
case MATCHCRITERIA_MESSAGE:
return matcherprop_string_decode_match(matcher, line);
case MATCHCRITERIA_NOT_BODY_PART:
case MATCHCRITERIA_NOT_MESSAGE:
return !matcherprop_string_decode_match(matcher, line);
2001-05-06 06:27:28 +02:00
}
return FALSE;
2001-05-06 06:27:28 +02:00
}
/*!
*\brief Check if a line in a message file's body matches
* the criteria
*
*\param matchers List of conditions
*\param fp Message file
*
*\return gboolean TRUE if succesful match
2001-05-06 06:27:28 +02:00
*/
static gboolean matcherlist_match_body(MatcherList *matchers, FILE *fp)
2001-05-06 06:27:28 +02:00
{
GSList *l;
gchar buf[BUFFSIZE];
while (fgets(buf, sizeof(buf), fp) != NULL) {
for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
MatcherProp *matcher = (MatcherProp *) l->data;
if (matcher->done)
continue;
/* if the criteria is ~body_part or ~message, ZERO lines
* must NOT match for the rule to match. */
if (matcher->criteria == MATCHCRITERIA_NOT_BODY_PART ||
matcher->criteria == MATCHCRITERIA_NOT_MESSAGE) {
if (matcherprop_match_line(matcher, buf)) {
matcher->result = TRUE;
} else {
matcher->result = FALSE;
matcher->done = TRUE;
}
/* else, just one line has to match */
} else if (matcherprop_criteria_body(matcher) ||
matcherprop_criteria_message(matcher)) {
if (matcherprop_match_line(matcher, buf)) {
matcher->result = TRUE;
matcher->done = TRUE;
}
2001-05-06 06:27:28 +02:00
}
/* if the matchers are OR'ed and the rule matched,
* no need to check the others. */
if (matcher->result && matcher->done) {
if (!matchers->bool_and)
return TRUE;
}
}
}
2001-05-15 06:50:27 +02:00
return FALSE;
2001-05-06 06:27:28 +02:00
}
/*!
*\brief Check if a message file matches criteria
*
*\param matchers Criteria
*\param info Message info
*\param result Default result
*
*\return gboolean TRUE if matched
*/
gboolean matcherlist_match_file(MatcherList *matchers, MsgInfo *info,
2001-05-06 06:27:28 +02:00
gboolean result)
{
gboolean read_headers;
gboolean read_body;
GSList *l;
FILE *fp;
gchar *file;
2001-11-25 14:47:45 +01:00
/* file need to be read ? */
2001-05-06 06:27:28 +02:00
read_headers = FALSE;
2001-11-25 14:47:45 +01:00
read_body = FALSE;
for (l = matchers->matchers ; l != NULL ; l = g_slist_next(l)) {
MatcherProp *matcher = (MatcherProp *) l->data;
2001-05-06 06:27:28 +02:00
if (matcherprop_criteria_headers(matcher))
read_headers = TRUE;
if (matcherprop_criteria_body(matcher))
read_body = TRUE;
if (matcherprop_criteria_message(matcher)) {
read_headers = TRUE;
read_body = TRUE;
}
matcher->result = FALSE;
matcher->done = FALSE;
2001-05-06 06:27:28 +02:00
}
if (!read_headers && !read_body)
return result;
file = procmsg_get_message_file_full(info, read_headers, read_body);
if (file == NULL)
return FALSE;
2001-05-06 06:27:28 +02:00
if ((fp = g_fopen(file, "rb")) == NULL) {
2001-05-06 06:27:28 +02:00
FILE_OP_ERROR(file, "fopen");
g_free(file);
return result;
}
2001-05-15 06:50:27 +02:00
2001-05-06 06:27:28 +02:00
/* read the headers */
if (read_headers) {
if (matcherlist_match_headers(matchers, fp))
read_body = FALSE;
} else {
matcherlist_skip_headers(fp);
2001-05-06 06:27:28 +02:00
}
2001-05-15 06:50:27 +02:00
2001-05-06 06:27:28 +02:00
/* read the body */
if (read_body) {
matcherlist_match_body(matchers, fp);
}
for (l = matchers->matchers; l != NULL; l = g_slist_next(l)) {
MatcherProp *matcher = (MatcherProp *) l->data;
if (matcherprop_criteria_headers(matcher) ||
2001-12-23 20:02:36 +01:00
matcherprop_criteria_body(matcher) ||
matcherprop_criteria_message(matcher)) {
if (matcher->result) {
if (!matchers->bool_and) {
result = TRUE;
break;
}
}
else {
if (matchers->bool_and) {
result = FALSE;
break;
}
2001-12-23 20:02:36 +01:00
}
}
2001-05-06 06:27:28 +02:00
}
g_free(file);
fclose(fp);
return result;
}
/*!
*\brief Test list of conditions on a message.
*
*\param matchers List of conditions
*\param info Message info
*
*\return gboolean TRUE if matched
*/
gboolean matcherlist_match(MatcherList *matchers, MsgInfo *info)
2001-05-06 06:27:28 +02:00
{
GSList *l;
2001-05-06 06:27:28 +02:00
gboolean result;
if (!matchers)
return FALSE;
2001-05-06 06:27:28 +02:00
if (matchers->bool_and)
result = TRUE;
else
result = FALSE;
/* test the cached elements */
for (l = matchers->matchers; l != NULL ;l = g_slist_next(l)) {
MatcherProp *matcher = (MatcherProp *) l->data;
2001-05-06 06:27:28 +02:00
2001-05-15 06:50:27 +02:00
switch(matcher->criteria) {
case MATCHCRITERIA_ALL:
case MATCHCRITERIA_UNREAD:
case MATCHCRITERIA_NOT_UNREAD:
case MATCHCRITERIA_NEW:
case MATCHCRITERIA_NOT_NEW:
case MATCHCRITERIA_MARKED:
case MATCHCRITERIA_NOT_MARKED:
case MATCHCRITERIA_DELETED:
case MATCHCRITERIA_NOT_DELETED:
case MATCHCRITERIA_REPLIED:
case MATCHCRITERIA_NOT_REPLIED:
case MATCHCRITERIA_FORWARDED:
case MATCHCRITERIA_NOT_FORWARDED:
case MATCHCRITERIA_LOCKED:
case MATCHCRITERIA_NOT_LOCKED:
case MATCHCRITERIA_COLORLABEL:
case MATCHCRITERIA_NOT_COLORLABEL:
case MATCHCRITERIA_IGNORE_THREAD:
case MATCHCRITERIA_NOT_IGNORE_THREAD:
case MATCHCRITERIA_SUBJECT:
case MATCHCRITERIA_NOT_SUBJECT:
case MATCHCRITERIA_FROM:
case MATCHCRITERIA_NOT_FROM:
case MATCHCRITERIA_TO:
case MATCHCRITERIA_NOT_TO:
case MATCHCRITERIA_CC:
case MATCHCRITERIA_NOT_CC:
case MATCHCRITERIA_TO_OR_CC:
case MATCHCRITERIA_NOT_TO_AND_NOT_CC:
case MATCHCRITERIA_AGE_GREATER:
case MATCHCRITERIA_AGE_LOWER:
case MATCHCRITERIA_NEWSGROUPS:
case MATCHCRITERIA_NOT_NEWSGROUPS:
case MATCHCRITERIA_INREPLYTO:
case MATCHCRITERIA_NOT_INREPLYTO:
case MATCHCRITERIA_REFERENCES:
case MATCHCRITERIA_NOT_REFERENCES:
case MATCHCRITERIA_SCORE_GREATER:
case MATCHCRITERIA_SCORE_LOWER:
case MATCHCRITERIA_SCORE_EQUAL:
case MATCHCRITERIA_SIZE_GREATER:
case MATCHCRITERIA_SIZE_SMALLER:
case MATCHCRITERIA_SIZE_EQUAL:
2004-01-12 22:28:31 +01:00
case MATCHCRITERIA_TEST:
case MATCHCRITERIA_NOT_TEST:
case MATCHCRITERIA_PARTIAL:
case MATCHCRITERIA_NOT_PARTIAL:
2001-05-15 06:50:27 +02:00
if (matcherprop_match(matcher, info)) {
if (!matchers->bool_and) {
return TRUE;
2001-05-15 06:50:27 +02:00
}
2001-05-06 06:27:28 +02:00
}
2001-05-15 06:50:27 +02:00
else {
if (matchers->bool_and) {
return FALSE;
2001-05-15 06:50:27 +02:00
}
2001-05-06 06:27:28 +02:00
}
}
}
/* test the condition on the file */
if (matcherlist_match_file(matchers, info, result)) {
if (!matchers->bool_and)
return TRUE;
}
else {
if (matchers->bool_and)
return FALSE;
}
2001-05-06 06:27:28 +02:00
return result;
}
2004-01-12 22:28:31 +01:00
static gint quote_filter_str(gchar * result, guint size,
const gchar * path)
{
const gchar * p;
gchar * result_p;
guint remaining;
result_p = result;
remaining = size;
for(p = path ; * p != '\0' ; p ++) {
if ((* p != '\"') && (* p != '\\')) {
if (remaining > 0) {
* result_p = * p;
result_p ++;
remaining --;
}
else {
result[size - 1] = '\0';
return -1;
}
}
else {
if (remaining >= 2) {
* result_p = '\\';
result_p ++;
* result_p = * p;
result_p ++;
remaining -= 2;
}
else {
result[size - 1] = '\0';
return -1;
}
}
}
if (remaining > 0) {
* result_p = '\0';
}
else {
result[size - 1] = '\0';
return -1;
}
return 0;
}
gchar * matcher_quote_str(const gchar * src)
{
gchar * res;
gint len;
len = strlen(src) * 2 + 1;
res = g_malloc(len);
quote_filter_str(res, len, src);
return res;
}
/*!
*\brief Convert a matcher structure to a string
*
*\param matcher Matcher structure
*
*\return gchar * Newly allocated string
*/
gchar *matcherprop_to_string(MatcherProp *matcher)
{
gchar *matcher_str = NULL;
const gchar *criteria_str;
const gchar *matchtype_str;
int i;
2004-01-12 22:28:31 +01:00
gchar * quoted_expr;
criteria_str = NULL;
for (i = 0; i < (int) (sizeof(matchparser_tab) / sizeof(MatchParser)); i++) {
2001-07-12 16:00:15 +02:00
if (matchparser_tab[i].id == matcher->criteria)
criteria_str = matchparser_tab[i].str;
}
if (criteria_str == NULL)
return NULL;
switch (matcher->criteria) {
case MATCHCRITERIA_AGE_GREATER:
case MATCHCRITERIA_AGE_LOWER:
case MATCHCRITERIA_SCORE_GREATER:
case MATCHCRITERIA_SCORE_LOWER:
case MATCHCRITERIA_SCORE_EQUAL:
case MATCHCRITERIA_SIZE_GREATER:
case MATCHCRITERIA_SIZE_SMALLER:
case MATCHCRITERIA_SIZE_EQUAL:
case MATCHCRITERIA_COLORLABEL:
case MATCHCRITERIA_NOT_COLORLABEL:
2001-05-13 04:57:01 +02:00
return g_strdup_printf("%s %i", criteria_str, matcher->value);
case MATCHCRITERIA_ALL:
case MATCHCRITERIA_UNREAD:
case MATCHCRITERIA_NOT_UNREAD:
case MATCHCRITERIA_NEW:
case MATCHCRITERIA_NOT_NEW:
case MATCHCRITERIA_MARKED:
case MATCHCRITERIA_NOT_MARKED:
case MATCHCRITERIA_DELETED:
case MATCHCRITERIA_NOT_DELETED:
case MATCHCRITERIA_REPLIED:
case MATCHCRITERIA_NOT_REPLIED:
case MATCHCRITERIA_FORWARDED:
case MATCHCRITERIA_NOT_FORWARDED:
case MATCHCRITERIA_LOCKED:
case MATCHCRITERIA_NOT_LOCKED:
case MATCHCRITERIA_PARTIAL:
case MATCHCRITERIA_NOT_PARTIAL:
case MATCHCRITERIA_IGNORE_THREAD:
case MATCHCRITERIA_NOT_IGNORE_THREAD:
return g_strdup(criteria_str);
2004-01-12 22:28:31 +01:00
case MATCHCRITERIA_TEST:
case MATCHCRITERIA_NOT_TEST:
quoted_expr = matcher_quote_str(matcher->expr);
matcher_str = g_strdup_printf("%s \"%s\"",
criteria_str, quoted_expr);
g_free(quoted_expr);
return matcher_str;
}
matchtype_str = NULL;
for (i = 0; i < sizeof matchparser_tab / sizeof matchparser_tab[0]; i++) {
2001-07-12 16:00:15 +02:00
if (matchparser_tab[i].id == matcher->matchtype)
matchtype_str = matchparser_tab[i].str;
}
if (matchtype_str == NULL)
return NULL;
2001-07-12 16:00:15 +02:00
switch (matcher->matchtype) {
case MATCHTYPE_MATCH:
case MATCHTYPE_MATCHCASE:
case MATCHTYPE_REGEXP:
case MATCHTYPE_REGEXPCASE:
2004-05-10 12:22:28 +02:00
case MATCHTYPE_ALL_IN_ADDRESSBOOK:
case MATCHTYPE_ANY_IN_ADDRESSBOOK:
2004-01-12 22:28:31 +01:00
quoted_expr = matcher_quote_str(matcher->expr);
if (matcher->header) {
gchar * quoted_header;
quoted_header = matcher_quote_str(matcher->header);
matcher_str = g_strdup_printf
("%s \"%s\" %s \"%s\"",
2004-01-12 22:28:31 +01:00
criteria_str, quoted_header,
matchtype_str, quoted_expr);
g_free(quoted_header);
}
2001-07-12 16:00:15 +02:00
else
matcher_str = g_strdup_printf
("%s %s \"%s\"", criteria_str,
2004-01-12 22:28:31 +01:00
matchtype_str, quoted_expr);
g_free(quoted_expr);
2003-01-06 10:16:45 +01:00
break;
2001-07-12 16:00:15 +02:00
}
return matcher_str;
}
/*!
*\brief Convert a list of conditions to a string
*
*\param matchers List of conditions
*
*\return gchar * Newly allocated string
*/
gchar *matcherlist_to_string(const MatcherList *matchers)
{
gint count;
gchar **vstr;
GSList *l;
gchar **cur_str;
gchar *result = NULL;
count = g_slist_length(matchers->matchers);
vstr = g_new(gchar *, count + 1);
for (l = matchers->matchers, cur_str = vstr; l != NULL;
l = g_slist_next(l), cur_str ++) {
*cur_str = matcherprop_to_string((MatcherProp *) l->data);
if (*cur_str == NULL)
break;
}
*cur_str = NULL;
if (matchers->bool_and)
result = g_strjoinv(" & ", vstr);
else
result = g_strjoinv(" | ", vstr);
for (cur_str = vstr ; *cur_str != NULL ; cur_str ++)
g_free(*cur_str);
g_free(vstr);
return result;
}
2004-01-12 22:28:31 +01:00
#define STRLEN_ZERO(s) ((s) ? strlen(s) : 0)
#define STRLEN_DEFAULT(s,d) ((s) ? strlen(s) : STRLEN_ZERO(d))
2004-01-12 22:28:31 +01:00
static void add_str_default(gchar ** dest,
const gchar * s, const gchar * d)
{
gchar quoted_str[4096];
const gchar * str;
if (s != NULL)
str = s;
else
str = d;
quote_cmd_argument(quoted_str, sizeof(quoted_str), str);
strcpy(* dest, quoted_str);
(* dest) += strlen(* dest);
}
/* matching_build_command() - preferably cmd should be unescaped */
/*!
*\brief Build the command line to execute
*
*\param cmd String with command line specifiers
*\param info Message info to use for command
*
*\return gchar * Newly allocated string
*/
gchar *matching_build_command(const gchar *cmd, MsgInfo *info)
{
const gchar *s = cmd;
gchar *filename = NULL;
gchar *processed_cmd;
gchar *p;
gint size;
const gchar *const no_subject = _("(none)") ;
const gchar *const no_from = _("(none)") ;
const gchar *const no_to = _("(none)") ;
const gchar *const no_cc = _("(none)") ;
const gchar *const no_date = _("(none)") ;
const gchar *const no_msgid = _("(none)") ;
const gchar *const no_newsgroups = _("(none)") ;
const gchar *const no_references = _("(none)") ;
size = STRLEN_ZERO(cmd) + 1;
while (*s != '\0') {
if (*s == '%') {
s++;
switch (*s) {
case '%':
size -= 1;
break;
case 's': /* subject */
size += STRLEN_DEFAULT(info->subject, no_subject) - 2;
break;
case 'f': /* from */
size += STRLEN_DEFAULT(info->from, no_from) - 2;
break;
case 't': /* to */
size += STRLEN_DEFAULT(info->to, no_to) - 2;
break;
case 'c': /* cc */
size += STRLEN_DEFAULT(info->cc, no_cc) - 2;
break;
case 'd': /* date */
size += STRLEN_DEFAULT(info->date, no_date) - 2;
break;
case 'i': /* message-id */
size += STRLEN_DEFAULT(info->msgid, no_msgid) - 2;
break;
case 'n': /* newsgroups */
size += STRLEN_DEFAULT(info->newsgroups, no_newsgroups) - 2;
break;
case 'r': /* references */
/* FIXME: using the inreplyto header for reference */
size += STRLEN_DEFAULT(info->inreplyto, no_references) - 2;
break;
case 'F': /* file */
2004-01-12 22:28:31 +01:00
if (filename == NULL)
filename = folder_item_fetch_msg(info->folder, info->msgnum);
if (filename == NULL) {
g_warning("filename is not set");
return NULL;
}
2004-01-12 22:28:31 +01:00
else {
size += strlen(filename) - 2;
2004-01-12 22:28:31 +01:00
}
break;
}
s++;
}
else s++;
}
2004-01-12 22:28:31 +01:00
/* as the string can be quoted, we double the result */
size *= 2;
processed_cmd = g_new0(gchar, size);
s = cmd;
p = processed_cmd;
while (*s != '\0') {
if (*s == '%') {
s++;
switch (*s) {
case '%':
*p = '%';
p++;
break;
case 's': /* subject */
2004-01-12 22:28:31 +01:00
add_str_default(&p, info->subject,
no_subject);
break;
case 'f': /* from */
2004-01-12 22:28:31 +01:00
add_str_default(&p, info->from,
no_from);
break;
case 't': /* to */
2004-01-12 22:28:31 +01:00
add_str_default(&p, info->to,
no_to);
break;
case 'c': /* cc */
2004-01-12 22:28:31 +01:00
add_str_default(&p, info->cc,
no_cc);
break;
case 'd': /* date */
2004-01-12 22:28:31 +01:00
add_str_default(&p, info->date,
no_date);
break;
case 'i': /* message-id */
2004-01-12 22:28:31 +01:00
add_str_default(&p, info->msgid,
no_msgid);
break;
case 'n': /* newsgroups */
2004-01-12 22:28:31 +01:00
add_str_default(&p, info->newsgroups,
no_newsgroups);
break;
case 'r': /* references */
/* FIXME: using the inreplyto header for references */
add_str_default(&p, info->inreplyto, no_references);
break;
case 'F': /* file */
2004-01-12 22:28:31 +01:00
if (filename != NULL)
add_str_default(&p, filename, NULL);
break;
default:
*p = '%';
p++;
*p = *s;
p++;
break;
}
s++;
}
else {
*p = *s;
p++;
s++;
}
}
2004-01-12 22:28:31 +01:00
g_free(filename);
return processed_cmd;
}
#undef STRLEN_DEFAULT
#undef STRLEN_ZERO
2001-07-12 16:00:15 +02:00
/* ************************************************************ */
/*!
*\brief Write filtering list to file
*
*\param fp File
*\param prefs_filtering List of filtering conditions
*/
static void prefs_filtering_write(FILE *fp, GSList *prefs_filtering)
{
GSList *cur;
for (cur = prefs_filtering; cur != NULL; cur = cur->next) {
gchar *filtering_str;
gchar *tmp_name = NULL;
FilteringProp *prop;
if (NULL == (prop = (FilteringProp *) cur->data))
continue;
if (NULL == (filtering_str = filteringprop_to_string(prop)))
continue;
if (prop->enabled) {
if (fputs("enabled ", fp) == EOF) {
FILE_OP_ERROR("filtering config", "fputs || fputc");
return;
}
} else {
if (fputs("disabled ", fp) == EOF) {
FILE_OP_ERROR("filtering config", "fputs || fputc");
return;
}
}
if (fputs("rulename \"", fp) == EOF) {
FILE_OP_ERROR("filtering config", "fputs || fputc");
g_free(filtering_str);
return;
}
tmp_name = prop->name;
while (tmp_name && *tmp_name != '\0') {
if (*tmp_name != '"') {
if (fputc(*tmp_name, fp) == EOF) {
FILE_OP_ERROR("filtering config", "fputc");
g_free(filtering_str);
return;
}
} else if (*tmp_name == '"') {
if (fputc('\\', fp) == EOF ||
fputc('"', fp) == EOF) {
FILE_OP_ERROR("filtering config", "fputc");
g_free(filtering_str);
return;
}
}
tmp_name ++;
}
if(fputs("\" ", fp) == EOF ||
fputs(filtering_str, fp) == EOF ||
fputc('\n', fp) == EOF) {
FILE_OP_ERROR("filtering config", "fputs || fputc");
g_free(filtering_str);
return;
}
g_free(filtering_str);
}
}
/*!
*\brief Write matchers from a folder item
*
*\param node Node with folder info
*\param data File pointer
*
*\return gboolean FALSE
*/
static gboolean prefs_matcher_write_func(GNode *node, gpointer data)
{
FolderItem *item;
FILE *fp = data;
gchar *id;
GSList *prefs_filtering;
2004-01-12 22:28:31 +01:00
item = node->data;
/* prevent warning */
if (item->path == NULL)
return FALSE;
id = folder_item_get_identifier(item);
if (id == NULL)
return FALSE;
prefs_filtering = item->prefs->processing;
2004-01-12 22:28:31 +01:00
if (prefs_filtering != NULL) {
fprintf(fp, "[%s]\n", id);
prefs_filtering_write(fp, prefs_filtering);
fputc('\n', fp);
}
g_free(id);
return FALSE;
}
/*!
*\brief Save matchers from folder items
*
*\param fp File
*/
static void prefs_matcher_save(FILE *fp)
{
GList *cur;
for (cur = folder_get_list() ; cur != NULL ; cur = g_list_next(cur)) {
Folder *folder;
folder = (Folder *) cur->data;
g_node_traverse(folder->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1,
prefs_matcher_write_func, fp);
}
2004-01-12 22:28:31 +01:00
/* pre global rules */
fprintf(fp, "[preglobal]\n");
prefs_filtering_write(fp, pre_global_processing);
fputc('\n', fp);
/* post global rules */
fprintf(fp, "[postglobal]\n");
prefs_filtering_write(fp, post_global_processing);
fputc('\n', fp);
/* filtering rules */
fprintf(fp, "[filtering]\n");
prefs_filtering_write(fp, filtering_rules);
fputc('\n', fp);
}
/*!
*\brief Write filtering / matcher configuration file
*/
void prefs_matcher_write_config(void)
{
gchar *rcpath;
PrefFile *pfile;
2002-08-15 09:38:17 +02:00
debug_print("Writing matcher configuration...\n");
rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
MATCHER_RC, NULL);
if ((pfile = prefs_write_open(rcpath)) == NULL) {
g_warning("failed to write configuration to file\n");
g_free(rcpath);
return;
}
prefs_matcher_save(pfile->fp);
g_free(rcpath);
if (prefs_file_close(pfile) < 0) {
g_warning("failed to write configuration to file\n");
return;
}
}
/* ******************************************************************* */
void matcher_add_rulenames(const gchar *rcpath)
{
gchar *newpath = g_strconcat(rcpath, ".new", NULL);
FILE *src = g_fopen(rcpath, "rb");
FILE *dst = g_fopen(newpath, "wb");
gchar buf[BUFFSIZE];
if (dst == NULL) {
perror("fopen");
g_free(newpath);
return;
}
while (fgets (buf, sizeof(buf), src) != NULL) {
if (strlen(buf) > 2 && buf[0] != '['
&& strncmp(buf, "rulename \"", 10)) {
fwrite("rulename \"\" ",
strlen("rulename \"\" "), 1, dst);
}
fwrite(buf, strlen(buf), 1, dst);
}
fclose(dst);
fclose(src);
move_file(newpath, rcpath, TRUE);
g_free(newpath);
}
/*!
*\brief Read matcher configuration
*/
void prefs_matcher_read_config(void)
{
gchar *rcpath;
gchar *rc_old_format;
FILE *f;
create_matchparser_hashtab();
prefs_filtering_clear();
rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC, NULL);
rc_old_format = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, MATCHER_RC,
".pre_names", NULL);
if (!is_file_exist(rc_old_format) && is_file_exist(rcpath)) {
/* backup file with no rules names, in case
* anything goes wrong */
copy_file(rcpath, rc_old_format, FALSE);
/* now hack the file in order to have it to the new format */
matcher_add_rulenames(rcpath);
}
g_free(rc_old_format);
f = g_fopen(rcpath, "rb");
g_free(rcpath);
2002-06-03 21:17:50 +02:00
if (f != NULL) {
matcher_parser_start_parsing(f);
2004-05-10 12:22:28 +02:00
fclose(matcher_parserin);
2002-06-03 20:57:50 +02:00
}
else {
/* previous version compatibily */
2002-03-25 23:41:55 +01:00
/* printf("reading filtering\n"); */
rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
FILTERING_RC, NULL);
f = g_fopen(rcpath, "rb");
g_free(rcpath);
if (f != NULL) {
matcher_parser_start_parsing(f);
fclose(matcher_parserin);
}
2002-03-25 23:41:55 +01:00
/* printf("reading scoring\n"); */
rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
SCORING_RC, NULL);
f = g_fopen(rcpath, "rb");
g_free(rcpath);
if (f != NULL) {
matcher_parser_start_parsing(f);
fclose(matcher_parserin);
}
}
2001-07-12 16:00:15 +02:00
}