bool LLViewerParcelMedia::allowedMedia(std::string media_url)
{
	LLStringUtil::trim(media_url);
	std::string domain = extractDomain(media_url);
	LLHost host;
	host.setHostByName(domain);
	std::string ip = host.getIPString();
	if (sAllowedMedia.count(domain) || sAllowedMedia.count(ip))
	{
		return true;
	}
	std::string server;
	for (S32 i = 0; i < (S32)sMediaFilterList.size(); i++)
	{
		server = sMediaFilterList[i]["domain"].asString();
		if (server == domain || server == ip)
		{
			if (sMediaFilterList[i]["action"].asString() == "allow")
			{
				return true;
			}
			else
			{
				return false;
			}
		}
	}
	return false;
}
void LLViewerParcelMedia::filterMedia(LLParcel* parcel, U32 type)
{
	std::string media_action;
	std::string media_url;
	std::string domain;
	std::string ip;

	if (parcel != LLViewerParcelMgr::getInstance()->getAgentParcel())
	{
		// The parcel just changed (may occur right out after a TP)
		sIsUserAction = false;
		return;
	}		

	if (type == 0)
	{
		media_url = parcel->getMediaURL();
	}
	else
	{
		media_url = parcel->getMusicURL();
	}
	LLStringUtil::trim(media_url);

	domain = extractDomain(media_url);

	if (sMediaQueries.count(domain) > 0)
	{
		sIsUserAction = false;
		return;
	}

	LLHost host;
	host.setHostByName(domain);
	ip = host.getIPString();

	if (sIsUserAction)
	{
		// This was a user manual request to play this media, so give
		// it another chance...
		sIsUserAction = false;
		bool dirty = false;
		if (sDeniedMedia.count(domain))
		{
			sDeniedMedia.erase(domain);
			dirty = true;
		}
		if (sDeniedMedia.count(ip))
		{
			sDeniedMedia.erase(ip);
			dirty = true;
		}
		if (dirty)
		{
			SLFloaterMediaFilter::setDirty();
		}
	}

	if (media_url.empty())
	{
		media_action = "allow";
	}
	else if (!sMediaFilterListLoaded || sDeniedMedia.count(domain) || sDeniedMedia.count(ip))
	{
		media_action = "ignore";
	}
	else if (sAllowedMedia.count(domain) || sAllowedMedia.count(ip))
	{
		media_action = "allow";
	}
	else
	{
		std::string server;
		for (S32 i = 0; i < (S32)sMediaFilterList.size(); i++)
		{
			server = sMediaFilterList[i]["domain"].asString();
			if (server == domain || server == ip)
			{
				media_action = sMediaFilterList[i]["action"].asString();
				break;
			}
		}
	}

	if (media_action == "allow")
	{
		if (type == 0)
		{
			play(parcel, false);
		}
		else
		{
			playStreamingMusic(parcel, false);
		}
		return;
	}
	if (media_action == "ignore")
	{
		if (type == 1)
		{
			LLViewerParcelMedia::stopStreamingMusic();
		}
		return;
	}

	LLSD args;
	if (ip != domain && domain.find('/') == std::string::npos)
	{
		args["DOMAIN"] = domain + " (" + ip + ")";
	}
	else
	{
		args["DOMAIN"] = domain;
	}

	if (media_action == "deny")
	{
		LLNotificationsUtil::add("MediaBlocked", args);
		if (type == 1)
		{
			LLViewerParcelMedia::stopStreamingMusic();
		}
		// So to avoid other "blocked" messages later in the session
		// for this url should it be requested again by a script.
		// We don't add the IP, on purpose (want to show different
		// blocks for different domains pointing to the same IP).
		sDeniedMedia.insert(domain);
	}
	else
	{
		sMediaQueries.insert(domain);
		args["URL"] = media_url;
		if (type == 0)
		{
			args["TYPE"] = "media";
		}
		else
		{
			args["TYPE"] = "audio";
		}
		LLNotificationsUtil::add("MediaAlert", args, LLSD(), boost::bind(callback_media_alert, _1, _2, parcel, type, domain));
	}
}
void LLViewerParcelMedia::filterAudioUrl(std::string media_url)
{
	// If there is no alert active, filter the media and flag the music
	//  queue empty.
	if (LLViewerParcelMedia::sMediaFilterAlertActive == false)
	{
		if (media_url == sCurrentMusic)
		{
			llinfos << "Audio URL filter: no active alert, same URL as previous: " + media_url << llendl;
			// The music hasn't changed, so keep playing.
			if (gAudiop != NULL)
			{
				gAudiop->startInternetStream(media_url);
				LLOverlayBar::audioFilterPlay();
			}
			sMusicQueueEmpty = true;
			return;
		}
		// New music, so flag the queue empty and filter it.
		llinfos << "Audio URL filter: no active alert, filtering new URL: " + media_url << llendl;
		sMusicQueueEmpty = true;
	}
	// If an alert is active, place the media url in the music queue
	//  if not the same as previous request.
	else
	{
		if (sMusicQueueEmpty == false)
		{
			if (media_url != sQueuedMusic)
			{
				llinfos << "Audio URL filter: active alert, replacing existing queue with: " + media_url << llendl;
				sQueuedMusic = media_url;
				sMusicQueueEmpty = false;
			}
			return;
		}
		else
		{
			if (media_url != sCurrentMusic)
			{
				llinfos << "Audio URL filter: active alert, nothing queued, adding queue with: " + media_url << llendl;
				sQueuedMusic = media_url;
				sMusicQueueEmpty = false;
				}
			return;
		}
	}	

	sCurrentMusic = media_url;

	// If the new URL is empty, just play it.
	if (media_url.empty())
	{
		llinfos << "Audio URL empty, allowing." << llendl;
		// Treat it as allowed; it'll get stopped elsewhere
		if (gAudiop != NULL)
		{
			gAudiop->startInternetStream(media_url);
			LLOverlayBar::audioFilterPlay();
		}
		return;
	}

	// If this is the same as the last one we asked about, don't bug the
	//  user with it again.
	if (media_url == sAudioLastURL)
	{
		llinfos << "Audio URL " << media_url << " unchanged, repeating action." << llendl;
		if (sAudioLastActionPlay)
		{
			llinfos << "Played last time, playing again." << llendl;
			if (gAudiop != NULL)
			{
				gAudiop->startInternetStream(media_url);
				LLOverlayBar::audioFilterPlay();
			}
		}
		return;
	}

	sAudioLastURL = media_url;

	std::string media_action;
	std::string domain = extractDomain(media_url);
    
	for(S32 i = 0;i<(S32)sMediaFilterList.size();i++)
	{
		std::string listed_domain = sMediaFilterList[i]["domain"].asString();
		if (domain.length() >= listed_domain.length())
		{
			size_t pos = domain.rfind(listed_domain);
			if ((pos != std::string::npos) && 
				(pos == domain.length()-listed_domain.length()))
			{
				media_action = sMediaFilterList[i]["action"].asString();
				break;
			}
		}
	}
	if (media_action=="allow")
	{
		if (gAudiop != NULL)
		{
			llinfos << "Audio filter: URL allowed by whitelist" << llendl;
			gAudiop->startInternetStream(media_url);
			LLOverlayBar::audioFilterPlay();
		}
		sAudioLastActionPlay = true;
	}
	else if (media_action=="deny")
	{
		LLChat chat;
		chat.mText = "Audio blocked - Blacklisted domain: "+domain;
		chat.mSourceType = CHAT_SOURCE_SYSTEM;
		LLFloaterChat::addChat(chat,FALSE,FALSE);
		LLOverlayBar::audioFilterStop();
		sAudioLastActionPlay = false;
	}
	else
	{
		llinfos << "Audio URL " << media_url << " neither on whitelist nor blacklist, prompting." << llendl;
		LLSD args;
		args["AUDIOURL"] = media_url;
		LLViewerParcelMedia::sMediaFilterAlertActive = true;
		LLNotifications::instance().add("AudioAlert", args,LLSD(),boost::bind(callback_audio_alert, _1, _2, media_url));
	}
}
void LLViewerParcelMedia::filterMediaUrl(LLParcel* parcel)
{
	LLParcel *currentparcel = LLViewerParcelMgr::getInstance()->getAgentParcel();

	llinfos << "Current media: "+sCurrentMedia.getMediaURL() << llendl;
	llinfos << "New media: "+parcel->getMediaURL() << llendl;
	// If there is no alert active, filter the media and flag media
	//  queue empty.
	if (LLViewerParcelMedia::sMediaFilterAlertActive == false)
	{
		if (parcel->getMediaURL() == sCurrentMedia.getMediaURL())
		{
			llinfos << "Media URL filter: no active alert, same URL as previous: " +parcel->getMediaURL() << llendl;
			sCurrentMedia = *parcel;
			if (parcel->getName() == currentparcel->getName())
			{
				// Only play if we're still there.
				llinfos << "Still on same parcel, playing." << llendl;
				LLViewerParcelMedia::play(parcel);
			}
			sMediaQueueEmpty = true;
			return;
		}
		llinfos << "Media URL filter: no active alert, filtering new URL: "+parcel->getMediaURL() << llendl;
		sMediaQueueEmpty = true;
	}
	// If an alert is active, place the media in the media queue if not the same as previous request
	else
	{
		if (sMediaQueueEmpty == false)
		{
			if (parcel->getMediaURL() != sQueuedMedia.getMediaURL())
			{	
				llinfos << "Media URL filter: active alert, replacing current queued media URL with: "+sQueuedMedia.getMediaURL() << llendl;
				sQueuedMedia = *parcel;
				sMediaQueueEmpty = false;
			}
			return;
		}
		else
		{
			if (parcel->getMediaURL() != sCurrentMedia.getMediaURL())
			{
				llinfos << "Media URL filter: active alert, nothing queued, adding new queued media URL: "+sQueuedMedia.getMediaURL() << llendl;
				sQueuedMedia = *parcel;
				sMediaQueueEmpty = false;
			}
			return;
		}
	}

	std::string media_url = parcel->getMediaURL();
	if (media_url.empty())
	{
		llinfos << "Media URL on parcel " << parcel->getName() << " empty, allowing." << llendl;
		// Treat it as allowed; it'll get stopped elsewhere
		sCurrentMedia = *parcel;
		if (parcel->getName() == currentparcel->getName())
		{
			// We haven't moved, so let it run.
			llinfos << "Still on same parcel, playing." << llendl;
			LLViewerParcelMedia::play(parcel);
		}
		return;
	}

	if (media_url == sMediaLastURL)
	{
		llinfos << "Media URL " << media_url << " hasn't changed, not filtering." << llendl;
		// Don't bother the user if all we're doing is repeating
		//  ourselves.
		if (sMediaLastActionPlay)
		{
			// We played it last time...so if we're still there...
			llinfos << "Played last time, approving this time." << llendl;
			sCurrentMedia = *parcel;
			if (parcel->getName() == currentparcel->getName())
			{
				// The parcel hasn't changed (we didn't
				//  teleport, or move), so play it again, Sam.
				llinfos << "Still on same parcel, playing." << llendl;
				LLViewerParcelMedia::play(parcel);
			}
		}
		return;
	}

	sMediaLastURL = media_url;

	std::string media_action;
	std::string domain = extractDomain(media_url);
    
	for(S32 i = 0;i<(S32)sMediaFilterList.size();i++)
	{
		std::string listed_domain = sMediaFilterList[i]["domain"].asString();
		if (domain.length() >= listed_domain.length())
		{
			size_t pos = domain.rfind(listed_domain);
			if ((pos != std::string::npos) && 
				(pos == domain.length()-listed_domain.length()))
			{
				media_action = sMediaFilterList[i]["action"].asString();
				break;
			}
		}
	}
	if (media_action=="allow")
	{
		llinfos << "Media filter: URL allowed by whitelist: "+parcel->getMediaURL() << llendl;
		sCurrentMedia = *parcel;
		if (parcel->getName() == currentparcel->getName())
		{
			LLViewerParcelMedia::play(parcel);
		}
		sMediaLastActionPlay = true;
	}
	else if (media_action=="deny")
	{
		LLChat chat;
		chat.mText = "Media blocked - Blacklisted domain: "+domain;
		chat.mSourceType = CHAT_SOURCE_SYSTEM;
		LLFloaterChat::addChat(chat,FALSE,FALSE);
		sMediaLastActionPlay = false;
	}
	else
	{
		llinfos << "Domain for URL " << media_url << " in neither blacklist nor whitelist, asking user." << llendl;
		LLSD args;
		args["MEDIAURL"] = media_url;
		LLViewerParcelMedia::sMediaFilterAlertActive = true;
		LLViewerParcelMedia::sCurrentAlertMedia = *parcel;
		LLParcel* pParcel = &LLViewerParcelMedia::sCurrentAlertMedia;
		LLNotifications::instance().add("MediaAlert", args,LLSD(),boost::bind(callback_media_alert, _1, _2, pParcel));
	}
}