1715bf3d1SAxel Dörfler/*
2549949b2SAxel Dörfler * Copyright 2004-2013, Haiku, Inc. All rights reserved.
3715bf3d1SAxel Dörfler * Copyright 2001 Dr. Zoidberg Enterprises. All rights reserved.
4715bf3d1SAxel Dörfler *
5715bf3d1SAxel Dörfler * Distributed under the terms of the MIT License.
6715bf3d1SAxel Dörfler */
7715bf3d1SAxel Dörfler
8715bf3d1SAxel Dörfler
9715bf3d1SAxel Dörfler//!	Performs action depending on matching a header value.
10f7215ac8SNathan Whitehorn
11f7215ac8SNathan Whitehorn
12d26fee1aSClemens Zeidler#include "RuleFilter.h"
13f7215ac8SNathan Whitehorn
14f7215ac8SNathan Whitehorn#include <stdlib.h>
15f7215ac8SNathan Whitehorn#include <stdio.h>
16f7215ac8SNathan Whitehorn
1783c3aba2SClemens Zeidler#include <Catalog.h>
18d26fee1aSClemens Zeidler#include <Directory.h>
19d26fee1aSClemens Zeidler#include <fs_attr.h>
20d26fee1aSClemens Zeidler#include <Node.h>
21d26fee1aSClemens Zeidler#include <String.h>
22d26fee1aSClemens Zeidler
23d26fee1aSClemens Zeidler#include <MailProtocol.h>
24f7215ac8SNathan Whitehorn
25ca3341c7SAxel Dörfler#include "MatchHeaderSettings.h"
26ca3341c7SAxel Dörfler
27f7215ac8SNathan Whitehorn
28546208a5SOliver Tappe#undef B_TRANSLATION_CONTEXT
29546208a5SOliver Tappe#define B_TRANSLATION_CONTEXT "RuleFilter"
3083c3aba2SClemens Zeidler
3183c3aba2SClemens Zeidler
32ca3341c7SAxel DörflerRuleFilter::RuleFilter(BMailProtocol& protocol,
33ca3341c7SAxel Dörfler	const BMailAddOnSettings& addOnSettings)
34d26fee1aSClemens Zeidler	:
35ca3341c7SAxel Dörfler	BMailFilter(protocol, &addOnSettings)
36d26fee1aSClemens Zeidler{
37ca3341c7SAxel Dörfler	MatchHeaderSettings settings(addOnSettings);
38ca3341c7SAxel Dörfler
39f7215ac8SNathan Whitehorn	// attribute is adapted to our "capitalize-each-word-in-the-header" policy
40ca3341c7SAxel Dörfler	fAttribute = settings.Attribute();
41d26fee1aSClemens Zeidler	fAttribute.CapitalizeEachWord();
42f7215ac8SNathan Whitehorn
43ca3341c7SAxel Dörfler	fExpression = settings.Expression();
44715bf3d1SAxel Dörfler	int32 index = fExpression.FindFirst("REGEX:");
45870cb85fSDarkWyrm	if (index == B_ERROR || index > 0)
46715bf3d1SAxel Dörfler		EscapeRegexTokens(fExpression);
47870cb85fSDarkWyrm	else
48715bf3d1SAxel Dörfler		fExpression.RemoveFirst("REGEX:");
49715bf3d1SAxel Dörfler
50715bf3d1SAxel Dörfler	fMatcher.SetPattern(fExpression, false);
51715bf3d1SAxel Dörfler
52ca3341c7SAxel Dörfler	fAction = settings.Action();
53ca3341c7SAxel Dörfler	fMoveTarget = settings.MoveTarget();
54ca3341c7SAxel Dörfler	fSetFlags = settings.SetFlagsTo();
55ca3341c7SAxel Dörfler	fReplyAccount = settings.ReplyAccount();
56715bf3d1SAxel Dörfler}
57715bf3d1SAxel Dörfler
58715bf3d1SAxel Dörfler
59549949b2SAxel DörflerBMailFilterAction
60d33e4744SAxel DörflerRuleFilter::HeaderFetched(entry_ref& ref, BFile& file, BMessage& attributes)
61d26fee1aSClemens Zeidler{
62715bf3d1SAxel Dörfler	// That field doesn't exist? NO match
63d26fee1aSClemens Zeidler	if (fAttribute == "")
64549949b2SAxel Dörfler		return B_NO_MAIL_ACTION;
65d26fee1aSClemens Zeidler
66d26fee1aSClemens Zeidler	attr_info info;
67549949b2SAxel Dörfler	if (file.GetAttrInfo("Subject", &info) != B_OK
68d26fee1aSClemens Zeidler		|| info.type != B_STRING_TYPE)
69549949b2SAxel Dörfler		return B_NO_MAIL_ACTION;
70d26fee1aSClemens Zeidler
71d33e4744SAxel Dörfler	BString data = attributes.GetString(fAttribute.String(), NULL);
72d33e4744SAxel Dörfler	if (data.IsEmpty() || !fMatcher.Match(data)) {
73715bf3d1SAxel Dörfler		// We're not supposed to do anything
74549949b2SAxel Dörfler		return B_NO_MAIL_ACTION;
75715bf3d1SAxel Dörfler	}
76715bf3d1SAxel Dörfler
77715bf3d1SAxel Dörfler	switch (fAction) {
78715bf3d1SAxel Dörfler		case ACTION_MOVE_TO:
79d26fee1aSClemens Zeidler		{
80ca3341c7SAxel Dörfler			BDirectory dir(fMoveTarget);
81549949b2SAxel Dörfler			node_ref nodeRef;
82549949b2SAxel Dörfler			status_t status = dir.GetNodeRef(&nodeRef);
83549949b2SAxel Dörfler			if (status != B_OK)
84549949b2SAxel Dörfler				return status;
85549949b2SAxel Dörfler
86549949b2SAxel Dörfler			ref.device = nodeRef.device;
87549949b2SAxel Dörfler			ref.directory = nodeRef.node;
88549949b2SAxel Dörfler			return B_MOVE_MAIL_ACTION;
89d26fee1aSClemens Zeidler		}
90549949b2SAxel Dörfler
91715bf3d1SAxel Dörfler		case ACTION_DELETE_MESSAGE:
92549949b2SAxel Dörfler			return B_DELETE_MAIL_ACTION;
93d26fee1aSClemens Zeidler
94715bf3d1SAxel Dörfler		case ACTION_SET_FLAGS_TO:
95549949b2SAxel Dörfler			file.WriteAttrString("MAIL:filter_flags", &fSetFlags);
96f7215ac8SNathan Whitehorn			break;
97d26fee1aSClemens Zeidler
98715bf3d1SAxel Dörfler		case ACTION_REPLY_WITH:
99549949b2SAxel Dörfler			file.WriteAttr("MAIL:reply_with", B_INT32_TYPE, 0, &fReplyAccount,
100715bf3d1SAxel Dörfler				sizeof(int32));
101f7215ac8SNathan Whitehorn			break;
102715bf3d1SAxel Dörfler		case ACTION_SET_AS_READ:
103d26fee1aSClemens Zeidler		{
104715bf3d1SAxel Dörfler			BInboundMailProtocol& protocol
105715bf3d1SAxel Dörfler				= (BInboundMailProtocol&)fMailProtocol;
1069967dfd9SClemens Zeidler			protocol.MarkMessageAsRead(ref, B_READ);
107d26fee1aSClemens Zeidler			break;
108d26fee1aSClemens Zeidler		}
109f7215ac8SNathan Whitehorn		default:
110715bf3d1SAxel Dörfler			fprintf(stderr,"Unknown do_what: 0x%04x!\n", fAction);
111f7215ac8SNathan Whitehorn	}
112715bf3d1SAxel Dörfler
113549949b2SAxel Dörfler	return B_NO_MAIL_ACTION;
114f7215ac8SNathan Whitehorn}
115f7215ac8SNathan Whitehorn
116d26fee1aSClemens Zeidler
117715bf3d1SAxel Dörfler// #pragma mark -
118f7215ac8SNathan Whitehorn
119f7215ac8SNathan Whitehorn
120715bf3d1SAxel DörflerBString
121ca3341c7SAxel Dörflerfilter_name(const BMailAccountSettings& accountSettings,
122ca3341c7SAxel Dörfler	const BMailAddOnSettings* addOnSettings)
123715bf3d1SAxel Dörfler{
124ca3341c7SAxel Dörfler	if (addOnSettings != NULL) {
125ca3341c7SAxel Dörfler		MatchHeaderSettings settings(*addOnSettings);
126ca3341c7SAxel Dörfler		if (settings.Attribute() != NULL && settings.Expression() != NULL) {
127ca3341c7SAxel Dörfler			BString name(
128ca3341c7SAxel Dörfler				B_TRANSLATE("Match \"%attribute\" against \"%regex\""));
129ca3341c7SAxel Dörfler			name.ReplaceAll("%attribute", settings.Attribute());
130ca3341c7SAxel Dörfler			name.ReplaceAll("%regex", settings.Expression());
131ca3341c7SAxel Dörfler			return name;
132ca3341c7SAxel Dörfler		}
133ca3341c7SAxel Dörfler	}
134ca3341c7SAxel Dörfler	return B_TRANSLATE("Match header");
135f7215ac8SNathan Whitehorn}
136f7215ac8SNathan Whitehorn
137d26fee1aSClemens Zeidler
138715bf3d1SAxel DörflerBMailFilter*
139ca3341c7SAxel Dörflerinstantiate_filter(BMailProtocol& protocol, const BMailAddOnSettings& settings)
140f7215ac8SNathan Whitehorn{
141d26fee1aSClemens Zeidler	return new RuleFilter(protocol, settings);
142f7215ac8SNathan Whitehorn}
143