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