1/* Add Fortune - adds fortunes to your mail
2**
3** Copyright 2001 Dr. Zoidberg Enterprises. All rights reserved.
4*/
5
6
7#include "ConfigView.h"
8
9#include <Message.h>
10#include <Entry.h>
11#include <String.h>
12#include <MailAddon.h>
13#include <MailMessage.h>
14
15#include <stdio.h>
16
17#include "NodeMessage.h"
18
19
20class FortuneFilter : public MailFilter
21{
22public:
23								FortuneFilter(MailProtocol& protocol,
24									AddonSettings* settings);
25			void				MessageReadyToSend(const entry_ref& ref,
26									BFile* file);
27};
28
29
30FortuneFilter::FortuneFilter(MailProtocol& protocol, AddonSettings* settings)
31	:
32	MailFilter(protocol, settings)
33{
34
35}
36
37
38void
39FortuneFilter::MessageReadyToSend(const entry_ref& ref, BFile* file)
40{
41	// What we want to do here is to change the message body. To do that we use the
42	// framework we already have by creating a new BEmailMessage based on the
43	// BPositionIO, changing the message body and rendering it back to disk. Of course
44	// this method ends up not being super-efficient, but it works. Ideas on how to
45	// improve this are welcome.
46	BString	fortuneFile;
47	BString	tagLine;
48
49	const BMessage* settings = &fAddonSettings->Settings();
50	// Obtain relevant settings
51	settings->FindString("fortune_file", &fortuneFile);
52	settings->FindString("tag_line", &tagLine);
53
54	// Add command to be executed
55	fortuneFile.Prepend("/bin/fortune ");
56
57	char buffer[768];
58	FILE *fd;
59
60	fd = popen(fortuneFile.String(), "r");
61	if (!fd) {
62		printf("Could not open pipe to fortune!\n");
63		return;
64	}
65
66	BEmailMessage mailMessage(&ref);
67	BString fortuneText;
68	fortuneText += "\n--\n";
69	fortuneText += tagLine;
70
71	while (fgets(buffer, 768, fd)) {
72		fortuneText += buffer;
73		fortuneText += "\n";
74	}
75	pclose(fd);
76
77	// Update the message body
78	BTextMailComponent* body = mailMessage.Body();
79	body->AppendText(fortuneText);
80
81	file->SetSize(0);
82	mailMessage.RenderToRFC822(file);
83}
84
85
86MailFilter*
87instantiate_mailfilter(MailProtocol& protocol, AddonSettings* settings)
88{
89	return new FortuneFilter(protocol, settings);
90}
91
92
93BView*
94instantiate_filter_config_panel(AddonSettings& settings)
95{
96	ConfigView *view = new ConfigView();
97	view->SetTo(&settings.Settings());
98
99	return view;
100}
101