int BufWriteCmd::Init(char *inData, int inSize)
{
#ifdef NO_LIBSNDFILE
	SendFailure(&mReplyAddress, "/b_write", "scsynth compiled without libsndfile\n");
 	scprintf("scsynth compiled without libsndfile\n");
	return false;
#else
	sc_msg_iter msg(inSize, inData);
	mBufIndex = msg.geti();

	const char *filename = msg.gets();
	if (!filename) return kSCErr_WrongArgType;

	if(mWorld->mRestrictedPath){
		mFilename = allocAndRestrictPath(mWorld, filename, mWorld->mRestrictedPath);
	}else{
		mFilename = (char*)World_Alloc(mWorld, strlen(filename)+1);
		strcpy(mFilename, filename);
	}

	const char *headerFormatString = msg.gets("aiff");
	const char *sampleFormatString = msg.gets("int16");

	mNumFrames = msg.geti(-1);
	mBufOffset = msg.geti();
	mLeaveFileOpen = msg.geti();

	GET_COMPLETION_MSG(msg);

	memset(&mFileInfo, 0, sizeof(mFileInfo));
	return sndfileFormatInfoFromStrings(&mFileInfo, headerFormatString, sampleFormatString);
#endif
}
Esempio n. 2
0
void
SMTPMessage::Receive
	(
	JBroadcaster*					sender,
	const JBroadcaster::Message&	message
	)
{
	if (sender == itsLink && !itsIsFinished)
		{
		if (message.Is(JMessageProtocolT::kMessageReady))
			{
			itsSomethingRead = kJTrue;
			if (itsTimeoutTask != NULL)
				{
				delete itsTimeoutTask;
				itsTimeoutTask	= NULL;
				}
			ReadReturnValue();
			}
		else if (message.Is(JMessageProtocolT::kReceivedDisconnect))
			{
			JGetUserNotification()->ReportError("The connection to sendmail closed early.");
			Broadcast(SendFailure());
			itsDeleteTask = new JXTimerTask(1000,kJTrue);
			assert( itsDeleteTask != NULL );
			itsDeleteTask->Start();
			ListenTo(itsDeleteTask);
//			itsLink = NULL;
			}
		}
	else if (sender == itsTimeoutTask && message.Is(JXTimerTask::kTimerWentOff))
		{
		JGetUserNotification()->ReportError("The connection to the SMTP server timed out.");
		Broadcast(SendFailure());
		itsLink		= NULL;
		itsTimeoutTask	= NULL;
		delete this;
		}
	else if (sender == itsDeleteTask && message.Is(JXTimerTask::kTimerWentOff))
		{
		itsDeleteTask	= NULL;
		delete this;
		}
}
bool BufReadCmd::Stage2()
{
#ifdef NO_LIBSNDFILE
	SendFailure(&mReplyAddress, "/b_read", "scsynth compiled without libsndfile\n");
 	scprintf("scsynth compiled without libsndfile\n");
	return false;
#else
	SF_INFO fileinfo;

	SndBuf *buf = World_GetNRTBuf(mWorld, mBufIndex);
	int framesToEnd = buf->frames - mBufOffset;
	if (framesToEnd <= 0) return true;

	SNDFILE* sf = sf_open(mFilename, SFM_READ, &fileinfo);
	if (!sf) {
		char str[512];
		sprintf(str, "File '%s' could not be opened: %s\n", mFilename, sf_strerror(NULL));
		SendFailureWithIntValue(&mReplyAddress, "/b_read", str, mBufIndex); //SendFailure(&mReplyAddress, "/b_read", str);
		scprintf(str);
		return false;
	}
	if (fileinfo.channels != buf->channels) {
		char str[512];
		sf_close(sf);
		sprintf(str, "Channel mismatch. File '%s' has %d channels. Buffer has %d channels.\n", mFilename, fileinfo.channels, buf->channels);
		SendFailureWithIntValue(&mReplyAddress, "/b_read", str, mBufIndex); //SendFailure(&mReplyAddress, "/b_read", str);
		scprintf(str);
		return false;
	}

	if (mFileOffset < 0) mFileOffset = 0;
	else if (mFileOffset > fileinfo.frames) mFileOffset = fileinfo.frames;
	if (mNumFrames < 0 || mNumFrames + mFileOffset > fileinfo.frames) mNumFrames = fileinfo.frames - mFileOffset;

	if (mNumFrames > framesToEnd) mNumFrames = framesToEnd;

	sf_seek(sf, mFileOffset, SEEK_SET);
	if (mNumFrames > 0) {
		sf_readf_float(sf, buf->data + (mBufOffset * buf->channels), mNumFrames);
	}

	if(buf->sndfile)
		sf_close(buf->sndfile);

	if (mLeaveFileOpen) {
		buf->sndfile = sf;
	} else {
		sf_close(sf);
		buf->sndfile = 0;
	}

	mSampleRate = (double)fileinfo.samplerate;

	return true;
#endif
}
bool NotifyCmd::Stage2()
{
	HiddenWorld *hw = mWorld->hw;

	if (mOnOff) {
		for (uint32 i=0; i<hw->mNumUsers; ++i) {
			if (mReplyAddress == hw->mUsers[i]) {
				// already in table - don't fail though..
				SendFailureWithIntValue(&mReplyAddress, "/notify", "notify: already registered\n", hw->mClientIDdict->at(mReplyAddress));
				scprintf("/notify : already registered\n");
				return false;
			}
		}

		// add reply address to user table
		if (hw->mNumUsers >= hw->mMaxUsers) {
			SendFailure(&mReplyAddress, "/notify", "too many users\n");
			scprintf("too many users\n");
			return false;
		}

        uint32 clientID = hw->mClientIDs[hw->mClientIDTop++]; // pop an ID
        hw->mClientIDdict->insert(std::pair<ReplyAddress, uint32>(mReplyAddress,clientID));
		hw->mUsers[hw->mNumUsers++] = mReplyAddress;

		SendDoneWithIntValue("/notify", clientID);
	} else {
		for (uint32 i=0; i<hw->mNumUsers; ++i) {
			if (mReplyAddress == hw->mUsers[i]) {
				// remove from list
                hw->mClientIDs[--hw->mClientIDTop] = hw->mClientIDdict->at(mReplyAddress); // push the freed ID
                hw->mClientIDdict->erase(mReplyAddress);
				hw->mUsers[i] = hw->mUsers[--hw->mNumUsers];
				SendDone("/notify");
				return false;
			}
		}

		SendFailure(&mReplyAddress, "/notify", "not registered\n");
		scprintf("not registered\n");
	}
	return false;
}
bool AudioQuitCmd::Stage3()
{
#if SC_AUDIO_API == SC_AUDIO_API_AUDIOUNITS
	SendFailure(&mReplyAddress, "/quit", "not allowed in AU host\n");
	scprintf("/quit : quit not allowed in AU host\n");
	return false;
#else
	if (mWorld->hw->mShmem)
		mWorld->hw->mShmem->disconnect();
	return true;
#endif
}
bool BufCloseCmd::Stage2()
{
#ifdef NO_LIBSNDFILE
	SendFailure(&mReplyAddress, "/b_close", "scsynth compiled without libsndfile\n");
 	scprintf("scsynth compiled without libsndfile\n");
	return false;
#else
	SndBuf *buf = World_GetNRTBuf(mWorld, mBufIndex);
	if (buf->sndfile) {
		sf_close(buf->sndfile);
		buf->sndfile = 0;
	}
	return true;
#endif
}
Esempio n. 7
0
void
SMTPMessage::BlockUntilOkOrFail()
{
	if (itsIsFinished)
		{
		return;
		}

	if (itsTimeoutTask != NULL)
		{
		JGetUserNotification()->ReportError("Connection to SMTP server timed out.");
		Broadcast(SendFailure());
//		itsLink = NULL;
		delete this;
		return;
		}

	JBoolean success	= kJFalse;
	itsIsFinished		= kJFalse;
	itsIsTryingToQuit	= kJTrue;

	JSize i = 1;
	while((i <= 30) && !itsIsFinished)
		{
		do
			{
			itsSomethingRead = kJFalse;
			ACE_Time_Value timeout(0);
			(ACE_Reactor::instance())->handle_events(timeout);
			ACE_Reactor::check_reconfiguration(NULL);

//			itsLink->handle_input(0);
			}
		while (itsSomethingRead);// && !itsIsFinished);

		JWait(2);
		i++;
		}
	if (itsIsFinished)
		{
		delete this;
		}
}
bool BufAllocReadCmd::Stage2()
{
#ifdef NO_LIBSNDFILE
	SendFailure(&mReplyAddress, "/b_allocRead", "scsynth compiled without libsndfile\n");
 	scprintf("scsynth compiled without libsndfile\n");
	return false;
#else
	SndBuf *buf = World_GetNRTBuf(mWorld, mBufIndex);
	SF_INFO fileinfo;
	memset(&fileinfo, 0, sizeof(fileinfo));
	SNDFILE* sf = sf_open(mFilename, SFM_READ, &fileinfo);
	if (!sf) {
		char str[512];
		sprintf(str, "File '%s' could not be opened: %s\n", mFilename, sf_strerror(NULL));
		SendFailureWithIntValue(&mReplyAddress, "/b_allocRead", str, mBufIndex);	//SendFailure(&mReplyAddress, "/b_allocRead", str);
		scprintf(str);
		return false;
	}
	if (mFileOffset < 0) mFileOffset = 0;
	else if (mFileOffset > fileinfo.frames) mFileOffset = fileinfo.frames;
	if (mNumFrames <= 0 || mNumFrames + mFileOffset > fileinfo.frames) mNumFrames = fileinfo.frames - mFileOffset;

	// alloc data size
	mFreeData = buf->data;
	SCErr err = bufAlloc(buf, fileinfo.channels, mNumFrames, fileinfo.samplerate);
	if (err) goto leave;

	sf_seek(sf, mFileOffset, SEEK_SET);
	sf_readf_float(sf, buf->data, mNumFrames);

leave:
	mSndBuf = *buf;
	sf_close(sf);

	return true;
#endif
}
bool BufReadChannelCmd::Stage2()
{
#ifdef NO_LIBSNDFILE
	SendFailure(&mReplyAddress, "/b_readChannel", "scsynth compiled without libsndfile\n");
 	scprintf("scsynth compiled without libsndfile\n");
	return false;
#else
	SF_INFO fileinfo;

	SndBuf *buf = World_GetNRTBuf(mWorld, mBufIndex);
	int framesToEnd = buf->frames - mBufOffset;
	if (framesToEnd <= 0) return true;

	SNDFILE* sf = sf_open(mFilename, SFM_READ, &fileinfo);
	if (!sf) {
		char str[512];
		sprintf(str, "File '%s' could not be opened: %s\n", mFilename, sf_strerror(NULL));
		SendFailureWithIntValue(&mReplyAddress, "/b_readChannel", str, mBufIndex); //SendFailure(&mReplyAddress, "/b_read", str);
		scprintf(str);
		return false;
	}

	if (mNumChannels > 0) {
		// verify channel indexes
		if (!( CheckChannels(fileinfo.channels)) ) { // nescivi:  && CheckChannels(buf->channels) (should not check here for buf->channels)
            const char* str = "Channel index out of range.\n";
			SendFailureWithIntValue(&mReplyAddress, "/b_readChannel", str, mBufIndex); //SendFailure(&mReplyAddress, "/b_allocRead", str);
			scprintf(str);
			sf_close(sf);
			return false;
		}
// nescivi: this also seems out of place: we want to read from a file with more channels than are in the buffer
// 	} else if (fileinfo.channels != buf->channels) {
// 		char str[256];
// 		sf_close(sf);
// 		sprintf(str, "Channel mismatch. File '%s' has %d channels. Buffer has %d channels.\n",
// 				mFilename, fileinfo.channels, buf->channels);
// 		SendFailure(&mReplyAddress, "/b_read", str);
// 		scprintf(str);
// 		return false;
	}

	if (mFileOffset < 0) mFileOffset = 0;
	else if (mFileOffset > fileinfo.frames) mFileOffset = fileinfo.frames;
	if (mNumFrames < 0 || mNumFrames + mFileOffset > fileinfo.frames) mNumFrames = fileinfo.frames - mFileOffset;
	if (mNumFrames > framesToEnd) mNumFrames = framesToEnd;

	sf_seek(sf, mFileOffset, SEEK_SET);
	if (mNumFrames > 0) {
		if (mNumChannels == 0) {
			// read all channels
			sf_readf_float(sf, buf->data + (mBufOffset * buf->channels), mNumFrames);
		} else {
			// alloc temp buffer
			float* data = (float*)malloc(mNumFrames*fileinfo.channels*sizeof(float));
			if (data == 0) goto leave;
			// read some channels
			sf_seek(sf, mFileOffset, SEEK_SET);
			sf_readf_float(sf, data, mNumFrames);
			CopyChannels(buf->data + (mBufOffset * mNumChannels), data, fileinfo.channels, mNumFrames);
			// free temp buffer
			free(data);
		}
	}

leave:
	if(buf->sndfile)
		sf_close(buf->sndfile);

	if (mLeaveFileOpen) {
		buf->sndfile = sf;
	} else {
		sf_close(sf);
		buf->sndfile = 0;
	}

	mSampleRate = (double)fileinfo.samplerate;

	return true;
#endif
}
bool BufAllocReadChannelCmd::Stage2()
{
#ifdef NO_LIBSNDFILE
	SendFailure(&mReplyAddress, "/b_allocReadChannel", "scsynth compiled without libsndfile\n");
 	scprintf("scsynth compiled without libsndfile\n");
	return false;
#else
	SndBuf *buf = World_GetNRTBuf(mWorld, mBufIndex);

	SF_INFO fileinfo;
	memset(&fileinfo, 0, sizeof(fileinfo));
	SNDFILE* sf = sf_open(mFilename, SFM_READ, &fileinfo);
	if (!sf) {
		char str[512];
		sprintf(str, "File '%s' could not be opened: %s\n", mFilename, sf_strerror(NULL));
		SendFailureWithIntValue(&mReplyAddress, "/b_allocReadChannel", str, mBufIndex); //SendFailure(&mReplyAddress, "/b_allocRead", str);
		scprintf(str);
		return false;
	}
	if (mFileOffset < 0) mFileOffset = 0;
	else if (mFileOffset > fileinfo.frames) mFileOffset = fileinfo.frames;
	if (mNumFrames <= 0 || mNumFrames + mFileOffset > fileinfo.frames) mNumFrames = fileinfo.frames - mFileOffset;

	if (mNumChannels == 0) {
		// alloc data size
		mFreeData = buf->data;
		SCErr err = bufAlloc(buf, fileinfo.channels, mNumFrames, fileinfo.samplerate);
		if (err) goto leave;
		// read all channels
		sf_seek(sf, mFileOffset, SEEK_SET);
		sf_readf_float(sf, buf->data, mNumFrames);
	} else {
		// verify channel indexes
		if (!CheckChannels(fileinfo.channels)) {
            const char* str = "Channel index out of range.\n";
			SendFailureWithIntValue(&mReplyAddress, "/b_allocReadChannel", str, mBufIndex); //SendFailure(&mReplyAddress, "/b_allocRead", str);
			scprintf(str);
			sf_close(sf);
			return false;
		}
		// alloc data size
		mFreeData = buf->data;
		SCErr err = bufAlloc(buf, mNumChannels, mNumFrames, fileinfo.samplerate);
		if (err) goto leave;
		// alloc temp buffer
		float* data = (float*)malloc(mNumFrames*fileinfo.channels*sizeof(float));
		if (data == 0) goto leave;
		// read some channels
		sf_seek(sf, mFileOffset, SEEK_SET);
		sf_readf_float(sf, data, mNumFrames);
		CopyChannels(buf->data, data, fileinfo.channels, mNumFrames);
		// free temp buffer
		free(data);
	}

leave:
	mSndBuf = *buf;
	sf_close(sf);

	return true;
#endif
}
bool SendFailureCmd::Stage2()
{
	SendFailure(&mReplyAddress, mCmdName, mErrString);
	return false;
}
Esempio n. 12
0
void
SMTPMessage::ReadReturnValue()
{
	JString line;
	JBoolean ok = itsLink->GetNextMessage(&line);
	assert(ok);

	if (itsCurrentMode != kDataHeader)
		{
		GMGetSMTPDebugDir()->AddText(line);
		}

	if ((itsCurrentMode != kStartUp) && (itsCurrentMode != kDataHeader))
		{
		JInteger value;
		JIndex findex;
		ok = line.LocateSubstring(" ", &findex);
		if (ok && (findex > 1))
			{
			JString number = line.GetSubstring(1, findex - 1);
			if (number.IsInteger())
				{
				number.ConvertToInteger(&value);
				}
			else
				{
				ok	= kJFalse;
				}
			}
		else
			{
			ok	= kJFalse;
			}

		if (!ok)
			{
			const JIndex kDashIndex	= 4;
			if (line.GetCharacter(kDashIndex) == '-')
				{
				// this is a multiline response.
				return;
				}
			}

		if (!ok || (value != kOKValue))
			{
			if (!itsIsTryingToQuit)
				{
				const JCharacter* map[] =
					{
					"err", line
					};
				const JString msg = JGetString("SMTPERROR", map, sizeof(map));

				JGetUserNotification()->ReportError(msg);
				}
			itsIsFinished = kJTrue;
			Broadcast(SendFailure());
			if (!itsIsTryingToQuit)
				{
				itsDeleteTask = new JXTimerTask(1000,kJTrue);
				assert( itsDeleteTask != NULL );
				itsDeleteTask->Start();
				ListenTo(itsDeleteTask);
				}
			return;
			}
		}

	if (itsCurrentMode < kTo)
		{
		itsCurrentMode++;
		}
	else if (itsCurrentMode == kTo)
		{
		if (itsCurrentIndex < itsToNames->GetElementCount())
			{
			itsCurrentIndex++;
			}
		else if (itsCcNames->GetElementCount() != 0)
			{
			itsCurrentMode = kCc;
			itsCurrentIndex = 1;
			}
		else if (itsBccNames->GetElementCount() != 0)
			{
			itsCurrentMode = kBcc;
			itsCurrentIndex = 1;
			}
		else
			{
			itsCurrentMode = kDataHeader;
			}
		}
	else if (itsCurrentMode == kCc)
		{
		if (itsCurrentIndex < itsCcNames->GetElementCount())
			{
			itsCurrentIndex++;
			}
		else if (itsBccNames->GetElementCount() != 0)
			{
			itsCurrentMode = kBcc;
			itsCurrentIndex = 1;
			}
		else
			{
			itsCurrentMode = kDataHeader;
			}
		}
	else if (itsCurrentMode == kBcc)
		{
		if (itsCurrentIndex < itsBccNames->GetElementCount())
			{
			itsCurrentIndex++;
			}
		else
			{
			itsCurrentMode = kDataHeader;
			}
		}
	else if (itsCurrentMode == kData)
		{
		itsIsFinished = kJTrue;
		Broadcast(MessageSent());
		GMGetSMTPDebugDir()->AddText("\n-------------------------------------\n");
		if (!itsIsTryingToQuit)
			{
			itsDeleteTask = new JXTimerTask(1000,kJTrue);
			assert( itsDeleteTask != NULL );
			itsDeleteTask->Start();
			ListenTo(itsDeleteTask);
			}
		return;
		}
	else
		{
		itsCurrentMode++;
		}

	SendNextData();
}
Esempio n. 13
0
void
SMTPMessage::Send()
{
	HandleAddresses();

	AppendToOutbox("From ");
	AppendToOutbox(*itsFrom);
	AppendToOutbox(" ");

	struct timeb tp;
	ftime(&tp);
	JString date(ctime(&(tp.time)));
	AppendToOutbox(date);

	std::istrstream is(date.GetCString(), date.GetLength());
	JString dow;
	JString mon;
	JString day;
	JString rest;
	dow = JReadUntilws(is);
	mon = JReadUntilws(is);
	day = JReadUntilws(is);
	JReadAll(is, &rest);

	AppendToOutbox("Date: ");
	AppendToOutbox(dow);
	AppendToOutbox(", ");
	AppendToOutbox(day);
	AppendToOutbox(" ");
	AppendToOutbox(mon);
	AppendToOutbox(" ");
	AppendToOutbox(rest);

	JString addrStr = itsSMTPServer;
	if (!addrStr.Contains(":"))
		{
		addrStr += ":25";
		}
	ACE_INET_Addr addr(addrStr);

	itsLink = new InetLink;
	assert(itsLink != NULL);

	itsConnector = new INETConnector;
	assert( itsConnector != NULL );

	ACE_Synch_Options options(ACE_Synch_Options::USE_REACTOR, ACE_Time_Value(kMaxWaitSeconds));

	if (itsConnector->connect(itsLink, addr, options) == -1 &&
		jerrno() != EAGAIN)
		{
		JString notice	= "No response from sendmail : Error number ";
		int errNumber	= jerrno();
		notice += JString(errNumber);
		JGetUserNotification()->ReportError(notice);
		itsDeleteTask = new JXTimerTask(1000,kJTrue);
		assert( itsDeleteTask != NULL );
		itsDeleteTask->Start();
		ListenTo(itsDeleteTask);
		Broadcast(SendFailure());
//		itsLink = NULL;
		return;
		}
	itsTimeoutTask = new JXTimerTask(kMaxWaitSeconds * 1000,kJTrue);
	assert( itsTimeoutTask != NULL );
	itsTimeoutTask->Start();
	ListenTo(itsTimeoutTask);
	ClearWhenGoingAway(itsLink, &itsLink);
}