From a325bf8c54faf882fa86b57f2e546859e3b455a0 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Fri, 31 Mar 2006 07:18:15 +0000 Subject: [PATCH] changed voicemail's temporary file handling. It now uses AmAudio's tmpfile support. git-svn-id: http://svn.berlios.de/svnroot/repos/sems/trunk@13 8eb893ce-cfd4-0310-b710-fb5ebe64c474 --- apps/voicemail/AnswerMachine.cpp | 17 ++++++++++------- core/AmAudio.cpp | 7 +++++-- core/AmAudio.h | 8 +++++++- core/AmMail.cpp | 29 +++++++++++++++++++---------- core/AmMail.h | 12 +++++++++--- core/AmSmtpClient.cpp | 16 ++++++++-------- 6 files changed, 58 insertions(+), 31 deletions(-) diff --git a/apps/voicemail/AnswerMachine.cpp b/apps/voicemail/AnswerMachine.cpp index e77fc501..f499a915 100644 --- a/apps/voicemail/AnswerMachine.cpp +++ b/apps/voicemail/AnswerMachine.cpp @@ -276,7 +276,7 @@ void AnswerMachineDialog::onSessionStart(const AmSipRequest& req) msg_filename = "/tmp/" + getLocalTag() + "." + AnswerMachineFactory::RecFileExt; - if(a_msg.open(msg_filename,AmAudioFile::Write)) + if(a_msg.open(msg_filename,AmAudioFile::Write,true)) throw string("AnswerMachine: couldn't open ") + msg_filename + string(" for writing"); @@ -307,13 +307,16 @@ void AnswerMachineDialog::sendMailNotification() } else { try { - + // avoid tmp file to be closed + // ~AmMail will do that... + a_msg.setCloseOnDestroy(false); + AmMail* mail = new AmMail(tmpl->getEmail(email_dict)); - mail->attachements.push_back(Attachement(msg_filename, + mail->attachements.push_back(Attachement(a_msg.getfp(), "message." + AnswerMachineFactory::RecFileExt, a_msg.getMimeType())); - mail->clean_up = clean_up_mail; + //mail->clean_up = clean_up_mail; AmMailDeamon::instance()->sendQueued(mail); } catch(const string& err){ @@ -324,10 +327,10 @@ void AnswerMachineDialog::sendMailNotification() void AnswerMachineDialog::clean_up_mail(AmMail* mail) { - for( Attachements::const_iterator att_it = mail->attachements.begin(); - att_it != mail->attachements.end(); ++att_it ) +// for( Attachements::const_iterator att_it = mail->attachements.begin(); +// att_it != mail->attachements.end(); ++att_it ) - unlink(att_it->fullname.c_str()); +// unlink(att_it->fullname.c_str()); } void AnswerMachineDialog::request2dict(const AmSipRequest& req) diff --git a/core/AmAudio.cpp b/core/AmAudio.cpp index 0feed9e5..96c94004 100644 --- a/core/AmAudio.cpp +++ b/core/AmAudio.cpp @@ -392,6 +392,7 @@ int AmAudioFile::open(const string& filename, OpenMode mode, bool is_tmp) fmt.reset(f_fmt); open_mode = mode; + this->close_on_exit = close_on_exit; if(!is_tmp){ fp = fopen(filename.c_str(),mode == AmAudioFile::Read ? "r" : "w+"); @@ -529,7 +530,8 @@ int AmAudioFile::fpopen(const string& filename, OpenMode mode, FILE* n_fp) AmAudioFile::AmAudioFile() : AmAudio(), data_size(0), fp(0), begin(0), loop(false), - on_close_done(false) + on_close_done(false), + close_on_exit(true) { } @@ -583,7 +585,8 @@ void AmAudioFile::close() if(fp){ on_close(); - fclose(fp); + if(close_on_exit) + fclose(fp); fp = 0; } } diff --git a/core/AmAudio.h b/core/AmAudio.h index a701428a..d090ccc6 100644 --- a/core/AmAudio.h +++ b/core/AmAudio.h @@ -347,6 +347,7 @@ protected: int data_size; bool on_close_done; + bool close_on_exit; /** @see AmAudio::read */ int read(unsigned int user_ts, unsigned int size); @@ -378,7 +379,8 @@ public: * @return 0 if everything's OK * @see OpenMode */ - int open(const string& filename, OpenMode mode, bool is_tmp=false); + int open(const string& filename, OpenMode mode, + bool is_tmp=false); int fpopen(const string& filename, OpenMode mode, FILE* n_fp); @@ -405,6 +407,10 @@ public: * @return MIME type corresponding to the audio file. */ string getMimeType(); + + void setCloseOnDestroy(bool cod){ + close_on_exit = cod; + } }; #endif diff --git a/core/AmMail.cpp b/core/AmMail.cpp index 60348b90..c72e9136 100644 --- a/core/AmMail.cpp +++ b/core/AmMail.cpp @@ -44,6 +44,15 @@ AmMail::AmMail(const string& _from, const string& _subject, { } +AmMail::~AmMail() +{ + for(Attachements::iterator it = attachements.begin(); + it != attachements.end(); it++){ + + fclose(it->fp); + } +}; + AmMailDeamon* AmMailDeamon::_instance=0; AmMailDeamon* AmMailDeamon::instance() @@ -64,17 +73,17 @@ int AmMailDeamon::sendQueued(AmMail* mail) return -1; } - FILE* tst_fp; - for( Attachements::const_iterator att_it = mail->attachements.begin(); - att_it != mail->attachements.end(); ++att_it ){ +// FILE* tst_fp; +// for( Attachements::const_iterator att_it = mail->attachements.begin(); +// att_it != mail->attachements.end(); ++att_it ){ - if(!(tst_fp = fopen(att_it->fullname.c_str(),"r"))){ - ERROR("%s\n",strerror(errno)); - return -1; - } - else - fclose(tst_fp); - } +// if(!(tst_fp = fopen(att_it->fullname.c_str(),"r"))){ +// ERROR("%s\n",strerror(errno)); +// return -1; +// } +// else +// fclose(tst_fp); +// } event_fifo_mut.lock(); event_fifo.push(mail); diff --git a/core/AmMail.h b/core/AmMail.h index 422f2f89..e9acf384 100644 --- a/core/AmMail.h +++ b/core/AmMail.h @@ -43,15 +43,19 @@ using std::queue; struct Attachement { /** Local file name */ - string fullname; + //string fullname; + FILE* fp; /** Proposed remote file name */ string filename; /** Declared content type */ string content_type; - Attachement(const string& _full, const string& _file="", const string& _ct="") - : fullname(_full), filename(_file), content_type(_ct) {} +// Attachement(const string& _full, const string& _file="", const string& _ct="") +// : fullname(_full), filename(_file), content_type(_ct) {} + + Attachement(FILE* _fp, const string& _file="", const string& _ct="") + : fp(_fp), filename(_file), content_type(_ct) {} }; typedef vector Attachements; @@ -88,6 +92,8 @@ public: AmMail(const string& _from, const string& _subject, const string& _to, const string& _body = ""); + + ~AmMail(); }; /** diff --git a/core/AmSmtpClient.cpp b/core/AmSmtpClient.cpp index 914fdddf..16e801a5 100644 --- a/core/AmSmtpClient.cpp +++ b/core/AmSmtpClient.cpp @@ -223,7 +223,7 @@ bool AmSmtpClient::send_body(const vector& hdrs, const AmMail& mail) } static void base64_encode(unsigned char* in, unsigned char* out, unsigned int in_size); -static int base64_encode_file(const char* filename, int out); +static int base64_encode_file(FILE* in, int out); bool AmSmtpClient::send_data(const vector& hdrs, const AmMail& mail) { @@ -276,7 +276,7 @@ bool AmSmtpClient::send_data(const vector& hdrs, const AmMail& mail) } SEND_LINE(""); // EoH - base64_encode_file(att_it->fullname.c_str(),sd); + base64_encode_file(att_it->fp,sd); SEND_LINE(""); // base64_encode_file() doesn't generate any EoL } @@ -332,7 +332,7 @@ static void base64_encode(unsigned char* in, unsigned char* out, unsigned int in out[i] = '='; } -static int base64_encode_file(const char* filename, int out_fd) +static int base64_encode_file(FILE* in, int out_fd) { unsigned char ibuf[B64_INPUT_BUFFER_SIZE]; unsigned char obuf[B64_OUTPUT_BUFFER_SIZE]={' '}; @@ -345,11 +345,11 @@ static int base64_encode_file(const char* filename, int out_fd) return -1; } - FILE* in = fopen(filename,"rb"); - if(!in){ - ERROR("%s\n",strerror(errno)); - return -1; - } +// FILE* in = fopen(filename,"rb"); +// if(!in){ +// ERROR("%s\n",strerror(errno)); +// return -1; +// } int bytes_written=0; while((s = fread(ibuf,1,B64_INPUT_BUFFER_SIZE,in))){