1f7215ac8SNathan Whitehorn /* BMailProtocol - the base class for protocol filters 2f7215ac8SNathan Whitehorn ** 3f7215ac8SNathan Whitehorn ** Copyright 2001-2003 Dr. Zoidberg Enterprises. All rights reserved. 4f7215ac8SNathan Whitehorn */ 5f7215ac8SNathan Whitehorn 6f7215ac8SNathan Whitehorn 7f7215ac8SNathan Whitehorn #include <stdio.h> 8f7215ac8SNathan Whitehorn #include <fs_attr.h> 96e927a5fSIngo Weinhold #include <stdlib.h> 10f7215ac8SNathan Whitehorn #include <assert.h> 11f7215ac8SNathan Whitehorn 12dfc6cf01SPhilippe Houdoin #include <Alert.h> 13dfc6cf01SPhilippe Houdoin #include <Directory.h> 14dfc6cf01SPhilippe Houdoin #include <FindDirectory.h> 15dfc6cf01SPhilippe Houdoin #include <Query.h> 16dfc6cf01SPhilippe Houdoin #include <E-mail.h> 17dfc6cf01SPhilippe Houdoin #include <Node.h> 18dfc6cf01SPhilippe Houdoin #include <NodeInfo.h> 19dfc6cf01SPhilippe Houdoin #include <NodeMonitor.h> 20dfc6cf01SPhilippe Houdoin #include <Path.h> 21*1af4fa4bSClemens Zeidler #include <Roster.h> 22dfc6cf01SPhilippe Houdoin #include <String.h> 23dfc6cf01SPhilippe Houdoin #include <StringList.h> 24dfc6cf01SPhilippe Houdoin #include <VolumeRoster.h> 25dfc6cf01SPhilippe Houdoin 26f7215ac8SNathan Whitehorn #include <MDRLanguage.h> 27f7215ac8SNathan Whitehorn 28*1af4fa4bSClemens Zeidler #include <MailAddon.h> 29f7215ac8SNathan Whitehorn #include <MailProtocol.h> 30*1af4fa4bSClemens Zeidler #include <MailSettings.h> 31f7215ac8SNathan Whitehorn 32*1af4fa4bSClemens Zeidler #include "HaikuMailFormatFilter.h" 33f7215ac8SNathan Whitehorn 34f7215ac8SNathan Whitehorn 35*1af4fa4bSClemens Zeidler using std::map; 36f7215ac8SNathan Whitehorn 37f7215ac8SNathan Whitehorn 38*1af4fa4bSClemens Zeidler MailFilter::MailFilter(MailProtocol& protocol, AddonSettings* settings) 39f7215ac8SNathan Whitehorn : 40*1af4fa4bSClemens Zeidler fMailProtocol(protocol), 41*1af4fa4bSClemens Zeidler fAddonSettings(settings) 42f7215ac8SNathan Whitehorn { 43*1af4fa4bSClemens Zeidler 44*1af4fa4bSClemens Zeidler } 45*1af4fa4bSClemens Zeidler 46*1af4fa4bSClemens Zeidler 47*1af4fa4bSClemens Zeidler MailFilter::~MailFilter() 48*1af4fa4bSClemens Zeidler { 49*1af4fa4bSClemens Zeidler 50f7215ac8SNathan Whitehorn } 51f7215ac8SNathan Whitehorn 52f7215ac8SNathan Whitehorn 53f7215ac8SNathan Whitehorn void 54*1af4fa4bSClemens Zeidler MailFilter::HeaderFetched(const entry_ref& ref, BFile* file) 55f7215ac8SNathan Whitehorn { 56*1af4fa4bSClemens Zeidler 57*1af4fa4bSClemens Zeidler } 58*1af4fa4bSClemens Zeidler 59*1af4fa4bSClemens Zeidler 60*1af4fa4bSClemens Zeidler void 61*1af4fa4bSClemens Zeidler MailFilter::BodyFetched(const entry_ref& ref, BFile* file) 62*1af4fa4bSClemens Zeidler { 63*1af4fa4bSClemens Zeidler 64*1af4fa4bSClemens Zeidler } 65*1af4fa4bSClemens Zeidler 66*1af4fa4bSClemens Zeidler 67*1af4fa4bSClemens Zeidler void 68*1af4fa4bSClemens Zeidler MailFilter::MailboxSynced(status_t status) 69*1af4fa4bSClemens Zeidler { 70*1af4fa4bSClemens Zeidler 71*1af4fa4bSClemens Zeidler } 72*1af4fa4bSClemens Zeidler 73*1af4fa4bSClemens Zeidler 74*1af4fa4bSClemens Zeidler void 75*1af4fa4bSClemens Zeidler MailFilter::MessageReadyToSend(const entry_ref& ref, BFile* file) 76*1af4fa4bSClemens Zeidler { 77*1af4fa4bSClemens Zeidler 78*1af4fa4bSClemens Zeidler } 79*1af4fa4bSClemens Zeidler 80*1af4fa4bSClemens Zeidler 81*1af4fa4bSClemens Zeidler void 82*1af4fa4bSClemens Zeidler MailFilter::MessageSent(const entry_ref& ref, BFile* file) 83*1af4fa4bSClemens Zeidler { 84*1af4fa4bSClemens Zeidler 85*1af4fa4bSClemens Zeidler } 86*1af4fa4bSClemens Zeidler 87*1af4fa4bSClemens Zeidler 88*1af4fa4bSClemens Zeidler MailProtocol::MailProtocol(BMailAccountSettings* settings) 89*1af4fa4bSClemens Zeidler : 90*1af4fa4bSClemens Zeidler fMailNotifier(NULL), 91*1af4fa4bSClemens Zeidler fProtocolThread(NULL) 92*1af4fa4bSClemens Zeidler { 93*1af4fa4bSClemens Zeidler fAccountSettings = *settings; 94*1af4fa4bSClemens Zeidler 95*1af4fa4bSClemens Zeidler AddFilter(new HaikuMailFormatFilter(*this, settings)); 96*1af4fa4bSClemens Zeidler } 97*1af4fa4bSClemens Zeidler 98*1af4fa4bSClemens Zeidler 99*1af4fa4bSClemens Zeidler MailProtocol::~MailProtocol() 100*1af4fa4bSClemens Zeidler { 101*1af4fa4bSClemens Zeidler delete fMailNotifier; 102*1af4fa4bSClemens Zeidler 103*1af4fa4bSClemens Zeidler for (int i = 0; i < fFilterList.CountItems(); i++) 104*1af4fa4bSClemens Zeidler delete fFilterList.ItemAt(i); 105*1af4fa4bSClemens Zeidler 106*1af4fa4bSClemens Zeidler map<entry_ref, image_id>::iterator it = fFilterImages.begin(); 107*1af4fa4bSClemens Zeidler for (; it != fFilterImages.end(); it++) 108*1af4fa4bSClemens Zeidler unload_add_on(it->second); 109*1af4fa4bSClemens Zeidler } 110*1af4fa4bSClemens Zeidler 111*1af4fa4bSClemens Zeidler 112*1af4fa4bSClemens Zeidler BMailAccountSettings& 113*1af4fa4bSClemens Zeidler MailProtocol::AccountSettings() 114*1af4fa4bSClemens Zeidler { 115*1af4fa4bSClemens Zeidler return fAccountSettings; 116*1af4fa4bSClemens Zeidler } 117*1af4fa4bSClemens Zeidler 118*1af4fa4bSClemens Zeidler 119*1af4fa4bSClemens Zeidler void 120*1af4fa4bSClemens Zeidler MailProtocol::SetProtocolThread(MailProtocolThread* protocolThread) 121*1af4fa4bSClemens Zeidler { 122*1af4fa4bSClemens Zeidler if (fProtocolThread) { 123*1af4fa4bSClemens Zeidler fProtocolThread->Lock(); 124*1af4fa4bSClemens Zeidler for (int i = 0; i < fHandlerList.CountItems(); i++) 125*1af4fa4bSClemens Zeidler fProtocolThread->RemoveHandler(fHandlerList.ItemAt(i)); 126*1af4fa4bSClemens Zeidler fProtocolThread->Unlock(); 127*1af4fa4bSClemens Zeidler } 128*1af4fa4bSClemens Zeidler 129*1af4fa4bSClemens Zeidler fProtocolThread = protocolThread; 130*1af4fa4bSClemens Zeidler 131*1af4fa4bSClemens Zeidler if (!fProtocolThread) 132*1af4fa4bSClemens Zeidler return; 133*1af4fa4bSClemens Zeidler 134*1af4fa4bSClemens Zeidler fProtocolThread->Lock(); 135*1af4fa4bSClemens Zeidler for (int i = 0; i < fHandlerList.CountItems(); i++) 136*1af4fa4bSClemens Zeidler fProtocolThread->AddHandler(fHandlerList.ItemAt(i)); 137*1af4fa4bSClemens Zeidler fProtocolThread->Unlock(); 138*1af4fa4bSClemens Zeidler 139*1af4fa4bSClemens Zeidler AddedToLooper(); 140*1af4fa4bSClemens Zeidler } 141*1af4fa4bSClemens Zeidler 142*1af4fa4bSClemens Zeidler 143*1af4fa4bSClemens Zeidler MailProtocolThread* 144*1af4fa4bSClemens Zeidler MailProtocol::Looper() 145*1af4fa4bSClemens Zeidler { 146*1af4fa4bSClemens Zeidler return fProtocolThread; 147*1af4fa4bSClemens Zeidler } 148*1af4fa4bSClemens Zeidler 149*1af4fa4bSClemens Zeidler 150*1af4fa4bSClemens Zeidler bool 151*1af4fa4bSClemens Zeidler MailProtocol::AddHandler(BHandler* handler) 152*1af4fa4bSClemens Zeidler { 153*1af4fa4bSClemens Zeidler if (!fHandlerList.AddItem(handler)) 154*1af4fa4bSClemens Zeidler return false; 155*1af4fa4bSClemens Zeidler if (fProtocolThread) { 156*1af4fa4bSClemens Zeidler fProtocolThread->Lock(); 157*1af4fa4bSClemens Zeidler fProtocolThread->AddHandler(handler); 158*1af4fa4bSClemens Zeidler fProtocolThread->Unlock(); 159*1af4fa4bSClemens Zeidler } 160*1af4fa4bSClemens Zeidler return true; 161*1af4fa4bSClemens Zeidler } 162*1af4fa4bSClemens Zeidler 163*1af4fa4bSClemens Zeidler 164*1af4fa4bSClemens Zeidler bool 165*1af4fa4bSClemens Zeidler MailProtocol::RemoveHandler(BHandler* handler) 166*1af4fa4bSClemens Zeidler { 167*1af4fa4bSClemens Zeidler if (!fHandlerList.RemoveItem(handler)) 168*1af4fa4bSClemens Zeidler return false; 169*1af4fa4bSClemens Zeidler if (fProtocolThread) { 170*1af4fa4bSClemens Zeidler fProtocolThread->Lock(); 171*1af4fa4bSClemens Zeidler fProtocolThread->RemoveHandler(handler); 172*1af4fa4bSClemens Zeidler fProtocolThread->Unlock(); 173*1af4fa4bSClemens Zeidler } 174*1af4fa4bSClemens Zeidler return true; 175*1af4fa4bSClemens Zeidler } 176*1af4fa4bSClemens Zeidler 177*1af4fa4bSClemens Zeidler 178*1af4fa4bSClemens Zeidler void 179*1af4fa4bSClemens Zeidler MailProtocol::SetMailNotifier(MailNotifier* mailNotifier) 180*1af4fa4bSClemens Zeidler { 181*1af4fa4bSClemens Zeidler delete fMailNotifier; 182*1af4fa4bSClemens Zeidler fMailNotifier = mailNotifier; 183*1af4fa4bSClemens Zeidler } 184*1af4fa4bSClemens Zeidler 185*1af4fa4bSClemens Zeidler 186*1af4fa4bSClemens Zeidler void 187*1af4fa4bSClemens Zeidler MailProtocol::ShowError(const char* error) 188*1af4fa4bSClemens Zeidler { 189*1af4fa4bSClemens Zeidler if (fMailNotifier) 190*1af4fa4bSClemens Zeidler fMailNotifier->ShowError(error); 191*1af4fa4bSClemens Zeidler } 192*1af4fa4bSClemens Zeidler 193*1af4fa4bSClemens Zeidler 194*1af4fa4bSClemens Zeidler void 195*1af4fa4bSClemens Zeidler MailProtocol::ShowMessage(const char* message) 196*1af4fa4bSClemens Zeidler { 197*1af4fa4bSClemens Zeidler if (fMailNotifier) 198*1af4fa4bSClemens Zeidler fMailNotifier->ShowMessage(message); 199*1af4fa4bSClemens Zeidler } 200*1af4fa4bSClemens Zeidler 201*1af4fa4bSClemens Zeidler 202*1af4fa4bSClemens Zeidler void 203*1af4fa4bSClemens Zeidler MailProtocol::SetTotalItems(int32 items) 204*1af4fa4bSClemens Zeidler { 205*1af4fa4bSClemens Zeidler if (fMailNotifier) 206*1af4fa4bSClemens Zeidler fMailNotifier->SetTotalItems(items); 207*1af4fa4bSClemens Zeidler } 208*1af4fa4bSClemens Zeidler 209*1af4fa4bSClemens Zeidler 210*1af4fa4bSClemens Zeidler void 211*1af4fa4bSClemens Zeidler MailProtocol::SetTotalItemsSize(int32 size) 212*1af4fa4bSClemens Zeidler { 213*1af4fa4bSClemens Zeidler if (fMailNotifier) 214*1af4fa4bSClemens Zeidler fMailNotifier->SetTotalItemsSize(size); 215*1af4fa4bSClemens Zeidler } 216*1af4fa4bSClemens Zeidler 217*1af4fa4bSClemens Zeidler 218*1af4fa4bSClemens Zeidler void 219*1af4fa4bSClemens Zeidler MailProtocol::ReportProgress(int bytes, int messages, const char* message) 220*1af4fa4bSClemens Zeidler { 221*1af4fa4bSClemens Zeidler if (fMailNotifier) 222*1af4fa4bSClemens Zeidler fMailNotifier->ReportProgress(bytes, messages, message); 223*1af4fa4bSClemens Zeidler } 224*1af4fa4bSClemens Zeidler 225*1af4fa4bSClemens Zeidler 226*1af4fa4bSClemens Zeidler void 227*1af4fa4bSClemens Zeidler MailProtocol::ResetProgress(const char* message) 228*1af4fa4bSClemens Zeidler { 229*1af4fa4bSClemens Zeidler if (fMailNotifier) 230*1af4fa4bSClemens Zeidler fMailNotifier->ResetProgress(message); 231*1af4fa4bSClemens Zeidler } 232*1af4fa4bSClemens Zeidler 233*1af4fa4bSClemens Zeidler 234*1af4fa4bSClemens Zeidler bool 235*1af4fa4bSClemens Zeidler MailProtocol::AddFilter(MailFilter* filter) 236*1af4fa4bSClemens Zeidler { 237*1af4fa4bSClemens Zeidler return fFilterList.AddItem(filter); 238*1af4fa4bSClemens Zeidler } 239*1af4fa4bSClemens Zeidler 240*1af4fa4bSClemens Zeidler 241*1af4fa4bSClemens Zeidler int32 242*1af4fa4bSClemens Zeidler MailProtocol::CountFilter() 243*1af4fa4bSClemens Zeidler { 244*1af4fa4bSClemens Zeidler return fFilterList.CountItems(); 245*1af4fa4bSClemens Zeidler } 246*1af4fa4bSClemens Zeidler 247*1af4fa4bSClemens Zeidler 248*1af4fa4bSClemens Zeidler MailFilter* 249*1af4fa4bSClemens Zeidler MailProtocol::FilterAt(int32 index) 250*1af4fa4bSClemens Zeidler { 251*1af4fa4bSClemens Zeidler return fFilterList.ItemAt(index); 252*1af4fa4bSClemens Zeidler } 253*1af4fa4bSClemens Zeidler 254*1af4fa4bSClemens Zeidler 255*1af4fa4bSClemens Zeidler MailFilter* 256*1af4fa4bSClemens Zeidler MailProtocol::RemoveFilter(int32 index) 257*1af4fa4bSClemens Zeidler { 258*1af4fa4bSClemens Zeidler return fFilterList.RemoveItemAt(index); 259*1af4fa4bSClemens Zeidler } 260*1af4fa4bSClemens Zeidler 261*1af4fa4bSClemens Zeidler 262*1af4fa4bSClemens Zeidler bool 263*1af4fa4bSClemens Zeidler MailProtocol::RemoveFilter(MailFilter* filter) 264*1af4fa4bSClemens Zeidler { 265*1af4fa4bSClemens Zeidler return fFilterList.RemoveItem(filter); 266*1af4fa4bSClemens Zeidler } 267*1af4fa4bSClemens Zeidler 268*1af4fa4bSClemens Zeidler 269*1af4fa4bSClemens Zeidler void 270*1af4fa4bSClemens Zeidler MailProtocol::NotifyNewMessagesToFetch(int32 nMessages) 271*1af4fa4bSClemens Zeidler { 272*1af4fa4bSClemens Zeidler ResetProgress(); 273*1af4fa4bSClemens Zeidler SetTotalItems(nMessages); 274*1af4fa4bSClemens Zeidler } 275*1af4fa4bSClemens Zeidler 276*1af4fa4bSClemens Zeidler 277*1af4fa4bSClemens Zeidler void 278*1af4fa4bSClemens Zeidler MailProtocol::NotifyHeaderFetched(const entry_ref& ref, BFile* data) 279*1af4fa4bSClemens Zeidler { 280*1af4fa4bSClemens Zeidler for (int i = 0; i < fFilterList.CountItems(); i++) 281*1af4fa4bSClemens Zeidler fFilterList.ItemAt(i)->HeaderFetched(ref, data); 282*1af4fa4bSClemens Zeidler 283*1af4fa4bSClemens Zeidler ReportProgress(0, 1); 284*1af4fa4bSClemens Zeidler } 285*1af4fa4bSClemens Zeidler 286*1af4fa4bSClemens Zeidler 287*1af4fa4bSClemens Zeidler void 288*1af4fa4bSClemens Zeidler MailProtocol::NotifyBodyFetched(const entry_ref& ref, BFile* data) 289*1af4fa4bSClemens Zeidler { 290*1af4fa4bSClemens Zeidler for (int i = 0; i < fFilterList.CountItems(); i++) 291*1af4fa4bSClemens Zeidler fFilterList.ItemAt(i)->BodyFetched(ref, data); 292*1af4fa4bSClemens Zeidler } 293*1af4fa4bSClemens Zeidler 294*1af4fa4bSClemens Zeidler 295*1af4fa4bSClemens Zeidler void 296*1af4fa4bSClemens Zeidler MailProtocol::NotifyMessageReadyToSend(const entry_ref& ref, BFile* data) 297*1af4fa4bSClemens Zeidler { 298*1af4fa4bSClemens Zeidler for (int i = 0; i < fFilterList.CountItems(); i++) 299*1af4fa4bSClemens Zeidler fFilterList.ItemAt(i)->MessageReadyToSend(ref, data); 300*1af4fa4bSClemens Zeidler } 301*1af4fa4bSClemens Zeidler 302*1af4fa4bSClemens Zeidler 303*1af4fa4bSClemens Zeidler void 304*1af4fa4bSClemens Zeidler MailProtocol::NotifyMessageSent(const entry_ref& ref, BFile* data) 305*1af4fa4bSClemens Zeidler { 306*1af4fa4bSClemens Zeidler for (int i = 0; i < fFilterList.CountItems(); i++) 307*1af4fa4bSClemens Zeidler fFilterList.ItemAt(i)->MessageSent(ref, data); 308*1af4fa4bSClemens Zeidler } 309*1af4fa4bSClemens Zeidler 310*1af4fa4bSClemens Zeidler 311*1af4fa4bSClemens Zeidler status_t 312*1af4fa4bSClemens Zeidler MailProtocol::MoveMessage(const entry_ref& ref, BDirectory& dir) 313*1af4fa4bSClemens Zeidler { 314*1af4fa4bSClemens Zeidler BEntry entry(&ref); 315*1af4fa4bSClemens Zeidler return entry.MoveTo(&dir); 316*1af4fa4bSClemens Zeidler } 317*1af4fa4bSClemens Zeidler 318*1af4fa4bSClemens Zeidler 319*1af4fa4bSClemens Zeidler status_t 320*1af4fa4bSClemens Zeidler MailProtocol::DeleteMessage(const entry_ref& ref) 321*1af4fa4bSClemens Zeidler { 322*1af4fa4bSClemens Zeidler BEntry entry(&ref); 323*1af4fa4bSClemens Zeidler return entry.Remove(); 324*1af4fa4bSClemens Zeidler } 325*1af4fa4bSClemens Zeidler 326*1af4fa4bSClemens Zeidler 327*1af4fa4bSClemens Zeidler void 328*1af4fa4bSClemens Zeidler MailProtocol::FileRenamed(const entry_ref& from, const entry_ref& to) 329*1af4fa4bSClemens Zeidler { 330*1af4fa4bSClemens Zeidler 331*1af4fa4bSClemens Zeidler } 332*1af4fa4bSClemens Zeidler 333*1af4fa4bSClemens Zeidler 334*1af4fa4bSClemens Zeidler void 335*1af4fa4bSClemens Zeidler MailProtocol::FileDeleted(const node_ref& node) 336*1af4fa4bSClemens Zeidler { 337*1af4fa4bSClemens Zeidler 338*1af4fa4bSClemens Zeidler } 339*1af4fa4bSClemens Zeidler 340*1af4fa4bSClemens Zeidler 341*1af4fa4bSClemens Zeidler void 342*1af4fa4bSClemens Zeidler MailProtocol::LoadFilters(MailAddonSettings& settings) 343*1af4fa4bSClemens Zeidler { 344*1af4fa4bSClemens Zeidler for (int i = 0; i < settings.CountFilterSettings(); i++) { 345*1af4fa4bSClemens Zeidler AddonSettings* filterSettings = settings.FilterSettingsAt(i); 346*1af4fa4bSClemens Zeidler MailFilter* filter = _LoadFilter(filterSettings); 347*1af4fa4bSClemens Zeidler if (!filter) 348*1af4fa4bSClemens Zeidler continue; 349*1af4fa4bSClemens Zeidler AddFilter(filter); 350*1af4fa4bSClemens Zeidler } 351*1af4fa4bSClemens Zeidler } 352*1af4fa4bSClemens Zeidler 353*1af4fa4bSClemens Zeidler 354*1af4fa4bSClemens Zeidler MailFilter* 355*1af4fa4bSClemens Zeidler MailProtocol::_LoadFilter(AddonSettings* filterSettings) 356*1af4fa4bSClemens Zeidler { 357*1af4fa4bSClemens Zeidler const entry_ref& ref = filterSettings->AddonRef(); 358*1af4fa4bSClemens Zeidler map<entry_ref, image_id>::iterator it = fFilterImages.find(ref); 359*1af4fa4bSClemens Zeidler image_id image; 360*1af4fa4bSClemens Zeidler if (it != fFilterImages.end()) 361*1af4fa4bSClemens Zeidler image = it->second; 362*1af4fa4bSClemens Zeidler else { 363*1af4fa4bSClemens Zeidler BEntry entry(&ref); 364*1af4fa4bSClemens Zeidler BPath path(&entry); 365*1af4fa4bSClemens Zeidler image = load_add_on(path.Path()); 366*1af4fa4bSClemens Zeidler } 367*1af4fa4bSClemens Zeidler if (image < 0) 368*1af4fa4bSClemens Zeidler return NULL; 369*1af4fa4bSClemens Zeidler 370*1af4fa4bSClemens Zeidler MailFilter* (*instantiate_mailfilter)(MailProtocol& protocol, 371*1af4fa4bSClemens Zeidler AddonSettings* settings); 372*1af4fa4bSClemens Zeidler if (get_image_symbol(image, "instantiate_mailfilter", 373*1af4fa4bSClemens Zeidler B_SYMBOL_TYPE_TEXT, (void **)&instantiate_mailfilter) 374*1af4fa4bSClemens Zeidler != B_OK) { 375*1af4fa4bSClemens Zeidler unload_add_on(image); 376*1af4fa4bSClemens Zeidler return NULL; 377*1af4fa4bSClemens Zeidler } 378*1af4fa4bSClemens Zeidler 379*1af4fa4bSClemens Zeidler fFilterImages[ref] = image; 380*1af4fa4bSClemens Zeidler return (*instantiate_mailfilter)(*this, filterSettings); 381*1af4fa4bSClemens Zeidler } 382*1af4fa4bSClemens Zeidler 383*1af4fa4bSClemens Zeidler 384*1af4fa4bSClemens Zeidler InboundProtocol::InboundProtocol(BMailAccountSettings* settings) 385*1af4fa4bSClemens Zeidler : 386*1af4fa4bSClemens Zeidler MailProtocol(settings) 387*1af4fa4bSClemens Zeidler { 388*1af4fa4bSClemens Zeidler LoadFilters(fAccountSettings.InboundSettings()); 389*1af4fa4bSClemens Zeidler } 390*1af4fa4bSClemens Zeidler 391*1af4fa4bSClemens Zeidler 392*1af4fa4bSClemens Zeidler InboundProtocol::~InboundProtocol() 393*1af4fa4bSClemens Zeidler { 394*1af4fa4bSClemens Zeidler 395*1af4fa4bSClemens Zeidler } 396*1af4fa4bSClemens Zeidler 397*1af4fa4bSClemens Zeidler 398*1af4fa4bSClemens Zeidler status_t 399*1af4fa4bSClemens Zeidler InboundProtocol::AppendMessage(const entry_ref& ref) 400*1af4fa4bSClemens Zeidler { 401*1af4fa4bSClemens Zeidler return false; 402*1af4fa4bSClemens Zeidler } 403*1af4fa4bSClemens Zeidler 404*1af4fa4bSClemens Zeidler 405*1af4fa4bSClemens Zeidler status_t 406*1af4fa4bSClemens Zeidler InboundProtocol::MarkMessageAsRead(const entry_ref& ref, bool read) 407*1af4fa4bSClemens Zeidler { 408*1af4fa4bSClemens Zeidler BNode node(&ref); 409*1af4fa4bSClemens Zeidler BString statusString = (read == true) ? "Read" : "New"; 410*1af4fa4bSClemens Zeidler if (node.WriteAttr("MAIL:status", B_STRING_TYPE, 0, statusString.String(), 411*1af4fa4bSClemens Zeidler statusString.Length()) < 0) 412*1af4fa4bSClemens Zeidler return B_ERROR; 413*1af4fa4bSClemens Zeidler return B_OK; 414*1af4fa4bSClemens Zeidler } 415*1af4fa4bSClemens Zeidler 416*1af4fa4bSClemens Zeidler 417*1af4fa4bSClemens Zeidler OutboundProtocol::OutboundProtocol(BMailAccountSettings* settings) 418*1af4fa4bSClemens Zeidler : 419*1af4fa4bSClemens Zeidler MailProtocol(settings) 420*1af4fa4bSClemens Zeidler { 421*1af4fa4bSClemens Zeidler LoadFilters(fAccountSettings.OutboundSettings()); 422*1af4fa4bSClemens Zeidler } 423*1af4fa4bSClemens Zeidler 424*1af4fa4bSClemens Zeidler 425*1af4fa4bSClemens Zeidler OutboundProtocol::~OutboundProtocol() 426*1af4fa4bSClemens Zeidler { 427*1af4fa4bSClemens Zeidler 428*1af4fa4bSClemens Zeidler } 429*1af4fa4bSClemens Zeidler 430*1af4fa4bSClemens Zeidler 431*1af4fa4bSClemens Zeidler const uint32 kMsgMoveFile = '&MoF'; 432*1af4fa4bSClemens Zeidler const uint32 kMsgDeleteFile = '&DeF'; 433*1af4fa4bSClemens Zeidler const uint32 kMsgFileRenamed = '&FiR'; 434*1af4fa4bSClemens Zeidler const uint32 kMsgFileDeleted = '&FDe'; 435*1af4fa4bSClemens Zeidler const uint32 kMsgInit = '&Ini'; 436*1af4fa4bSClemens Zeidler 437*1af4fa4bSClemens Zeidler 438*1af4fa4bSClemens Zeidler MailProtocolThread::MailProtocolThread(MailProtocol* protocol) 439*1af4fa4bSClemens Zeidler : 440*1af4fa4bSClemens Zeidler fMailProtocol(protocol) 441*1af4fa4bSClemens Zeidler { 442*1af4fa4bSClemens Zeidler PostMessage(kMsgInit); 443*1af4fa4bSClemens Zeidler } 444*1af4fa4bSClemens Zeidler 445*1af4fa4bSClemens Zeidler 446*1af4fa4bSClemens Zeidler void 447*1af4fa4bSClemens Zeidler MailProtocolThread::SetStopNow() 448*1af4fa4bSClemens Zeidler { 449*1af4fa4bSClemens Zeidler fMailProtocol->SetStopNow(); 450*1af4fa4bSClemens Zeidler } 451*1af4fa4bSClemens Zeidler 452*1af4fa4bSClemens Zeidler 453*1af4fa4bSClemens Zeidler void 454*1af4fa4bSClemens Zeidler MailProtocolThread::MessageReceived(BMessage* message) 455*1af4fa4bSClemens Zeidler { 456*1af4fa4bSClemens Zeidler switch (message->what) { 457*1af4fa4bSClemens Zeidler case kMsgInit: 458*1af4fa4bSClemens Zeidler fMailProtocol->SetProtocolThread(this); 459*1af4fa4bSClemens Zeidler break; 460*1af4fa4bSClemens Zeidler 461*1af4fa4bSClemens Zeidler case kMsgMoveFile: 462*1af4fa4bSClemens Zeidler { 463*1af4fa4bSClemens Zeidler entry_ref file; 464*1af4fa4bSClemens Zeidler message->FindRef("file", &file); 465*1af4fa4bSClemens Zeidler entry_ref dir; 466*1af4fa4bSClemens Zeidler message->FindRef("directory", &dir); 467*1af4fa4bSClemens Zeidler BDirectory directory(&dir); 468*1af4fa4bSClemens Zeidler fMailProtocol->MoveMessage(file, directory); 469*1af4fa4bSClemens Zeidler break; 470*1af4fa4bSClemens Zeidler } 471*1af4fa4bSClemens Zeidler 472*1af4fa4bSClemens Zeidler case kMsgDeleteFile: 473*1af4fa4bSClemens Zeidler { 474*1af4fa4bSClemens Zeidler entry_ref file; 475*1af4fa4bSClemens Zeidler message->FindRef("file", &file); 476*1af4fa4bSClemens Zeidler fMailProtocol->DeleteMessage(file); 477*1af4fa4bSClemens Zeidler break; 478*1af4fa4bSClemens Zeidler } 479*1af4fa4bSClemens Zeidler 480*1af4fa4bSClemens Zeidler case kMsgFileRenamed: 481*1af4fa4bSClemens Zeidler { 482*1af4fa4bSClemens Zeidler entry_ref from; 483*1af4fa4bSClemens Zeidler message->FindRef("from", &from); 484*1af4fa4bSClemens Zeidler entry_ref to; 485*1af4fa4bSClemens Zeidler message->FindRef("to", &to); 486*1af4fa4bSClemens Zeidler fMailProtocol->FileRenamed(from, to); 487*1af4fa4bSClemens Zeidler } 488*1af4fa4bSClemens Zeidler 489*1af4fa4bSClemens Zeidler case kMsgFileDeleted: 490*1af4fa4bSClemens Zeidler { 491*1af4fa4bSClemens Zeidler node_ref node; 492*1af4fa4bSClemens Zeidler message->FindInt32("device",&node.device); 493*1af4fa4bSClemens Zeidler message->FindInt64("node", &node.node); 494*1af4fa4bSClemens Zeidler fMailProtocol->FileDeleted(node); 495*1af4fa4bSClemens Zeidler } 496*1af4fa4bSClemens Zeidler 497*1af4fa4bSClemens Zeidler default: 498*1af4fa4bSClemens Zeidler BLooper::MessageReceived(message); 499*1af4fa4bSClemens Zeidler } 500*1af4fa4bSClemens Zeidler } 501*1af4fa4bSClemens Zeidler 502*1af4fa4bSClemens Zeidler 503*1af4fa4bSClemens Zeidler void 504*1af4fa4bSClemens Zeidler MailProtocolThread::TriggerFileMove(const entry_ref& ref, BDirectory& dir) 505*1af4fa4bSClemens Zeidler { 506*1af4fa4bSClemens Zeidler BMessage message(kMsgMoveFile); 507*1af4fa4bSClemens Zeidler message.AddRef("file", &ref); 508*1af4fa4bSClemens Zeidler BEntry entry; 509*1af4fa4bSClemens Zeidler dir.GetEntry(&entry); 510*1af4fa4bSClemens Zeidler entry_ref dirRef; 511*1af4fa4bSClemens Zeidler entry.GetRef(&dirRef); 512*1af4fa4bSClemens Zeidler message.AddRef("directory", &dirRef); 513*1af4fa4bSClemens Zeidler PostMessage(&message); 514*1af4fa4bSClemens Zeidler } 515*1af4fa4bSClemens Zeidler 516*1af4fa4bSClemens Zeidler 517*1af4fa4bSClemens Zeidler void 518*1af4fa4bSClemens Zeidler MailProtocolThread::TriggerFileDeletion(const entry_ref& ref) 519*1af4fa4bSClemens Zeidler { 520*1af4fa4bSClemens Zeidler BMessage message(kMsgDeleteFile); 521*1af4fa4bSClemens Zeidler message.AddRef("file", &ref); 522*1af4fa4bSClemens Zeidler PostMessage(&message); 523*1af4fa4bSClemens Zeidler } 524*1af4fa4bSClemens Zeidler 525*1af4fa4bSClemens Zeidler 526*1af4fa4bSClemens Zeidler void 527*1af4fa4bSClemens Zeidler MailProtocolThread::TriggerFileRenamed(const entry_ref& from, 528*1af4fa4bSClemens Zeidler const entry_ref& to) 529*1af4fa4bSClemens Zeidler { 530*1af4fa4bSClemens Zeidler BMessage message(kMsgFileRenamed); 531*1af4fa4bSClemens Zeidler message.AddRef("from", &from); 532*1af4fa4bSClemens Zeidler message.AddRef("to", &to); 533*1af4fa4bSClemens Zeidler PostMessage(&message); 534*1af4fa4bSClemens Zeidler } 535*1af4fa4bSClemens Zeidler 536*1af4fa4bSClemens Zeidler 537*1af4fa4bSClemens Zeidler void 538*1af4fa4bSClemens Zeidler MailProtocolThread::TriggerFileDeleted(const node_ref& node) 539*1af4fa4bSClemens Zeidler { 540*1af4fa4bSClemens Zeidler BMessage message(kMsgFileDeleted); 541*1af4fa4bSClemens Zeidler message.AddInt32("device", node.device); 542*1af4fa4bSClemens Zeidler message.AddInt64("node", node.node); 543*1af4fa4bSClemens Zeidler PostMessage(&message); 544*1af4fa4bSClemens Zeidler } 545*1af4fa4bSClemens Zeidler 546*1af4fa4bSClemens Zeidler 547*1af4fa4bSClemens Zeidler const uint32 kMsgSyncMessages = '&SyM'; 548*1af4fa4bSClemens Zeidler const uint32 kMsgFetchBody = '&FeB'; 549*1af4fa4bSClemens Zeidler const uint32 kMsgMarkMessageAsRead = '&MaR'; 550*1af4fa4bSClemens Zeidler const uint32 kMsgDeleteMessage = '&DeM'; 551*1af4fa4bSClemens Zeidler const uint32 kMsgAppendMessage = '&ApM'; 552*1af4fa4bSClemens Zeidler 553*1af4fa4bSClemens Zeidler 554*1af4fa4bSClemens Zeidler InboundProtocolThread::InboundProtocolThread(InboundProtocol* protocol) 555*1af4fa4bSClemens Zeidler : 556*1af4fa4bSClemens Zeidler MailProtocolThread(protocol), 557*1af4fa4bSClemens Zeidler fProtocol(protocol) 558*1af4fa4bSClemens Zeidler { 559*1af4fa4bSClemens Zeidler 560*1af4fa4bSClemens Zeidler } 561*1af4fa4bSClemens Zeidler 562*1af4fa4bSClemens Zeidler 563*1af4fa4bSClemens Zeidler InboundProtocolThread::~InboundProtocolThread() 564*1af4fa4bSClemens Zeidler { 565*1af4fa4bSClemens Zeidler fProtocol->SetProtocolThread(NULL); 566*1af4fa4bSClemens Zeidler } 567*1af4fa4bSClemens Zeidler 568*1af4fa4bSClemens Zeidler 569*1af4fa4bSClemens Zeidler void 570*1af4fa4bSClemens Zeidler InboundProtocolThread::MessageReceived(BMessage* message) 571*1af4fa4bSClemens Zeidler { 572*1af4fa4bSClemens Zeidler switch (message->what) { 573*1af4fa4bSClemens Zeidler case kMsgSyncMessages: 574*1af4fa4bSClemens Zeidler { 575*1af4fa4bSClemens Zeidler status_t status = fProtocol->SyncMessages(); 576*1af4fa4bSClemens Zeidler _NotiyMailboxSynced(status); 577*1af4fa4bSClemens Zeidler break; 578*1af4fa4bSClemens Zeidler } 579*1af4fa4bSClemens Zeidler 580*1af4fa4bSClemens Zeidler case kMsgFetchBody: 581*1af4fa4bSClemens Zeidler { 582*1af4fa4bSClemens Zeidler entry_ref ref; 583*1af4fa4bSClemens Zeidler message->FindRef("ref", &ref); 584*1af4fa4bSClemens Zeidler status_t status = fProtocol->FetchBody(ref); 585*1af4fa4bSClemens Zeidler if (status != B_OK || !message->FindBool("launch")) 586*1af4fa4bSClemens Zeidler break; 587*1af4fa4bSClemens Zeidler BMessage argv(B_ARGV_RECEIVED); 588*1af4fa4bSClemens Zeidler BPath path(&ref); 589*1af4fa4bSClemens Zeidler argv.AddString("argv", "E-mail"); 590*1af4fa4bSClemens Zeidler argv.AddString("argv", path.Path()); 591*1af4fa4bSClemens Zeidler argv.AddInt32("argc", 2); 592*1af4fa4bSClemens Zeidler be_roster->Launch("text/x-email", &argv); 593*1af4fa4bSClemens Zeidler break; 594*1af4fa4bSClemens Zeidler } 595*1af4fa4bSClemens Zeidler 596*1af4fa4bSClemens Zeidler case kMsgMarkMessageAsRead: 597*1af4fa4bSClemens Zeidler { 598*1af4fa4bSClemens Zeidler entry_ref ref; 599*1af4fa4bSClemens Zeidler message->FindRef("ref", &ref); 600*1af4fa4bSClemens Zeidler bool read = message->FindBool("read"); 601*1af4fa4bSClemens Zeidler fProtocol->MarkMessageAsRead(ref, read); 602*1af4fa4bSClemens Zeidler break; 603*1af4fa4bSClemens Zeidler } 604*1af4fa4bSClemens Zeidler 605*1af4fa4bSClemens Zeidler case kMsgDeleteMessage: 606*1af4fa4bSClemens Zeidler { 607*1af4fa4bSClemens Zeidler entry_ref ref; 608*1af4fa4bSClemens Zeidler message->FindRef("ref", &ref); 609*1af4fa4bSClemens Zeidler fProtocol->DeleteMessage(ref); 610*1af4fa4bSClemens Zeidler break; 611*1af4fa4bSClemens Zeidler } 612*1af4fa4bSClemens Zeidler 613*1af4fa4bSClemens Zeidler case kMsgAppendMessage: 614*1af4fa4bSClemens Zeidler { 615*1af4fa4bSClemens Zeidler entry_ref ref; 616*1af4fa4bSClemens Zeidler message->FindRef("ref", &ref); 617*1af4fa4bSClemens Zeidler fProtocol->AppendMessage(ref); 618*1af4fa4bSClemens Zeidler break; 619*1af4fa4bSClemens Zeidler } 620*1af4fa4bSClemens Zeidler 621*1af4fa4bSClemens Zeidler default: 622*1af4fa4bSClemens Zeidler MailProtocolThread::MessageReceived(message); 623*1af4fa4bSClemens Zeidler } 624*1af4fa4bSClemens Zeidler } 625*1af4fa4bSClemens Zeidler 626*1af4fa4bSClemens Zeidler 627*1af4fa4bSClemens Zeidler void 628*1af4fa4bSClemens Zeidler InboundProtocolThread::SyncMessages() 629*1af4fa4bSClemens Zeidler { 630*1af4fa4bSClemens Zeidler PostMessage(kMsgSyncMessages); 631*1af4fa4bSClemens Zeidler } 632*1af4fa4bSClemens Zeidler 633*1af4fa4bSClemens Zeidler 634*1af4fa4bSClemens Zeidler void 635*1af4fa4bSClemens Zeidler InboundProtocolThread::FetchBody(const entry_ref& ref, bool launch) 636*1af4fa4bSClemens Zeidler { 637*1af4fa4bSClemens Zeidler BMessage message(kMsgFetchBody); 638*1af4fa4bSClemens Zeidler message.AddRef("ref", &ref); 639*1af4fa4bSClemens Zeidler message.AddBool("launch", launch); 640*1af4fa4bSClemens Zeidler PostMessage(&message); 641*1af4fa4bSClemens Zeidler } 642*1af4fa4bSClemens Zeidler 643*1af4fa4bSClemens Zeidler 644*1af4fa4bSClemens Zeidler void 645*1af4fa4bSClemens Zeidler InboundProtocolThread::MarkMessageAsRead(const entry_ref& ref, bool read) 646*1af4fa4bSClemens Zeidler { 647*1af4fa4bSClemens Zeidler BMessage message(kMsgMarkMessageAsRead); 648*1af4fa4bSClemens Zeidler message.AddRef("ref", &ref); 649*1af4fa4bSClemens Zeidler message.AddBool("read", read); 650*1af4fa4bSClemens Zeidler PostMessage(&message); 651*1af4fa4bSClemens Zeidler } 652*1af4fa4bSClemens Zeidler 653*1af4fa4bSClemens Zeidler 654*1af4fa4bSClemens Zeidler void 655*1af4fa4bSClemens Zeidler InboundProtocolThread::DeleteMessage(const entry_ref& ref) 656*1af4fa4bSClemens Zeidler { 657*1af4fa4bSClemens Zeidler BMessage message(kMsgDeleteMessage); 658*1af4fa4bSClemens Zeidler message.AddRef("ref", &ref); 659*1af4fa4bSClemens Zeidler PostMessage(&message); 660*1af4fa4bSClemens Zeidler } 661*1af4fa4bSClemens Zeidler 662*1af4fa4bSClemens Zeidler 663*1af4fa4bSClemens Zeidler void 664*1af4fa4bSClemens Zeidler InboundProtocolThread::AppendMessage(const entry_ref& ref) 665*1af4fa4bSClemens Zeidler { 666*1af4fa4bSClemens Zeidler BMessage message(kMsgAppendMessage); 667*1af4fa4bSClemens Zeidler message.AddRef("ref", &ref); 668*1af4fa4bSClemens Zeidler PostMessage(&message); 669*1af4fa4bSClemens Zeidler } 670*1af4fa4bSClemens Zeidler 671*1af4fa4bSClemens Zeidler 672*1af4fa4bSClemens Zeidler void 673*1af4fa4bSClemens Zeidler InboundProtocolThread::_NotiyMailboxSynced(status_t status) 674*1af4fa4bSClemens Zeidler { 675*1af4fa4bSClemens Zeidler for (int i = 0; i < fProtocol->CountFilter(); i++) 676*1af4fa4bSClemens Zeidler fProtocol->FilterAt(i)->MailboxSynced(status); 677*1af4fa4bSClemens Zeidler } 678*1af4fa4bSClemens Zeidler 679*1af4fa4bSClemens Zeidler 680*1af4fa4bSClemens Zeidler const uint32 kMsgSendMessage = '&SeM'; 681*1af4fa4bSClemens Zeidler 682*1af4fa4bSClemens Zeidler 683*1af4fa4bSClemens Zeidler OutboundProtocolThread::OutboundProtocolThread(OutboundProtocol* protocol) 684*1af4fa4bSClemens Zeidler : 685*1af4fa4bSClemens Zeidler MailProtocolThread(protocol), 686*1af4fa4bSClemens Zeidler fProtocol(protocol) 687*1af4fa4bSClemens Zeidler { 688*1af4fa4bSClemens Zeidler 689*1af4fa4bSClemens Zeidler } 690*1af4fa4bSClemens Zeidler 691*1af4fa4bSClemens Zeidler 692*1af4fa4bSClemens Zeidler OutboundProtocolThread::~OutboundProtocolThread() 693*1af4fa4bSClemens Zeidler { 694*1af4fa4bSClemens Zeidler fProtocol->SetProtocolThread(NULL); 695*1af4fa4bSClemens Zeidler } 696*1af4fa4bSClemens Zeidler 697*1af4fa4bSClemens Zeidler 698*1af4fa4bSClemens Zeidler void 699*1af4fa4bSClemens Zeidler OutboundProtocolThread::MessageReceived(BMessage* message) 700*1af4fa4bSClemens Zeidler { 701*1af4fa4bSClemens Zeidler switch (message->what) { 702*1af4fa4bSClemens Zeidler case kMsgSendMessage: 703*1af4fa4bSClemens Zeidler { 704*1af4fa4bSClemens Zeidler std::vector<entry_ref> mails; 705*1af4fa4bSClemens Zeidler for (int32 i = 0; ;i++) { 706*1af4fa4bSClemens Zeidler entry_ref ref; 707*1af4fa4bSClemens Zeidler if (message->FindRef("ref", i, &ref) != B_OK) 708*1af4fa4bSClemens Zeidler break; 709*1af4fa4bSClemens Zeidler mails.push_back(ref); 710*1af4fa4bSClemens Zeidler } 711*1af4fa4bSClemens Zeidler size_t size = message->FindInt32("size"); 712*1af4fa4bSClemens Zeidler fProtocol->SendMessages(mails, size); 713*1af4fa4bSClemens Zeidler break; 714*1af4fa4bSClemens Zeidler } 715*1af4fa4bSClemens Zeidler 716*1af4fa4bSClemens Zeidler default: 717*1af4fa4bSClemens Zeidler MailProtocolThread::MessageReceived(message); 718*1af4fa4bSClemens Zeidler } 719*1af4fa4bSClemens Zeidler } 720*1af4fa4bSClemens Zeidler 721*1af4fa4bSClemens Zeidler 722*1af4fa4bSClemens Zeidler void 723*1af4fa4bSClemens Zeidler OutboundProtocolThread::SendMessages(const std::vector<entry_ref>& mails, 724*1af4fa4bSClemens Zeidler size_t totalBytes) 725*1af4fa4bSClemens Zeidler { 726*1af4fa4bSClemens Zeidler BMessage message(kMsgSendMessage); 727*1af4fa4bSClemens Zeidler for (unsigned int i = 0; i < mails.size(); i++) 728*1af4fa4bSClemens Zeidler message.AddRef("ref", &mails[i]); 729*1af4fa4bSClemens Zeidler message.AddInt32("size", totalBytes); 730*1af4fa4bSClemens Zeidler PostMessage(&message); 731f7215ac8SNathan Whitehorn } 732