Exemple #1
0
        public ActionResult RequestSong(string song)
        {
            if (song.Contains(Path.DirectorySeparatorChar))
            {
                return(Json(new { success = false }));
            }
            song = song.Replace('+', ' ');
            song = Path.Combine(Program.Configuration.MusicPath, song + ".mp3");
            if (!File.Exists(song))
            {
                return(Json(new { success = false }));
            }
            Song requestedSong;
            bool success = MusicRunner.QueueUserSong(song, Request.RemoteEndPoint.Address.ToString(), out requestedSong);

            if (Program.Configuration.Irc.Enabled && success)
            {
                Program.IrcBot.AnnounceRequest(requestedSong);
            }
            return(Json(new
            {
                success,
                canRequest = MusicRunner.CanUserRequest(Request.RemoteEndPoint.Address.ToString()),
                queue = MusicRunner.MasterQueue.Take(8)
            }));
        }
Exemple #2
0
        public ActionResult CanUpload(string filename)
        {
            filename = filename.Replace('+', ' ');
            if (Uploads.ContainsKey(Request.RemoteEndPoint.Address.ToString()))
            {
                return(Json(new { success = false, reason = "One upload at a time, please." }));
            }
            if (filename.Contains(Path.DirectorySeparatorChar))
            {
                return(Json(new { success = false, reason = "Invalid filename." }));
            }
            if (!SongNameRegex.IsMatch(filename))
            {
                return(Json(new { success = false, reason = "File name is not in \"Artist Name - Song Title.mp3\" format." }));
            }
            if (File.Exists(Path.Combine(Program.Configuration.MusicPath, filename)))
            {
                return(Json(new { success = false, reason = "This song is already in our library." }));
            }
            var minutes = MusicRunner.MinutesUntilNextUpload(Request.RemoteEndPoint.Address.ToString());

            if (minutes > 0)
            {
                return(Json(new { success = false, reason = string.Format("You need to wait another {0} minutes before you can upload again.", minutes) }));
            }

            return(Json(new { success = true }));
        }
Exemple #3
0
        public static void Main(string[] args)
        {
            Configuration = new Configuration();
            if (!File.Exists("config.json"))
            {
                File.WriteAllText("config.json", JsonConvert.SerializeObject(Configuration, Formatting.Indented));
                Console.WriteLine("Empty config.json file created. Populate it and restart.");
                return;
            }
            JsonConvert.PopulateObject(File.ReadAllText("config.json"), Configuration);
            File.WriteAllText("config.json", JsonConvert.SerializeObject(Configuration, Formatting.Indented));

            if (Configuration.Irc.Enabled)
            {
                IrcBot = new IrcBot();
            }
            var httpd  = new HttpServer();
            var router = new HttpRouter();

            httpd.LogRequests = Configuration.LogRequests;
            httpd.Request     = router.Route;

            var staticContent = new StaticContentHandler(Configuration.MusicPath);

            router.AddRoute(new StaticContentRoute(staticContent));
            var staticResources = new StaticContentHandler(Path.Combine(".", "Static"));

            router.AddRoute(new StaticContentRoute(staticResources));

            var mvc = new MvcRouter();

            mvc.RegisterController(new IndexController());
            mvc.AddRoute("Default", "{action}", new { controller = "Index", action = "Index" });
            router.AddRoute(mvc);

            router.AddRoute(new RegexRoute("/download/(?<path>.*)", (context, request, response) =>
            {
                response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", Path.GetFileName(context["path"])));
                staticContent.Serve(context["path"], request, response);
            }));

            MusicRunner.Start();
            httpd.Start(new IPEndPoint(IPAddress.Parse(Configuration.EndPoint), Configuration.Port));
            if (Configuration.Irc.Enabled)
            {
                IrcBot.Start();
            }

            Console.WriteLine("Type 'quit' to exit, or 'help' for help.");
            string command = null;

            while (command != "quit")
            {
                command = Console.ReadLine();
                HandleCommand(command);
            }
        }
Exemple #4
0
 public ActionResult RequestSkip()
 {
     return(Json(new
     {
         success = MusicRunner.RequestSkip(Request.RemoteEndPoint.Address.ToString()),
         song = MusicRunner.NowPlaying,
         queue = MusicRunner.MasterQueue.Take(8),
         seek = (DateTime.Now - MusicRunner.StartTime).TotalSeconds,
         skipsRequested = MusicRunner.SkipRequestsIssued,
         skipsRequired = MusicRunner.SkipRequestsRequired
     }));
 }
Exemple #5
0
 public ActionResult NowPlaying()
 {
     MusicRunner.UpdateListener(Request.RemoteEndPoint.Address.ToString());
     return(Json(new
     {
         song = MusicRunner.NowPlaying,
         seek = (DateTime.Now - MusicRunner.StartTime).TotalSeconds,
         queue = MusicRunner.MasterQueue.Take(8),
         skipsRequested = MusicRunner.SkipRequestsIssued,
         skipsRequired = MusicRunner.SkipRequestsRequired,
         listeners = MusicRunner.Listeners,
         announcement = MusicRunner.Announcement
     }));
 }
Exemple #6
0
        public ActionResult Search(string query, int page)
        {
            query = query.Replace('+', ' ').ToUpper(); // TODO: WebSharp should probably handle this
            var files = Directory.GetFiles(Program.Configuration.MusicPath, "*.mp3")
                        .OrderBy(Path.GetFileNameWithoutExtension)
                        .Where(f => Path.GetFileNameWithoutExtension(f).ToUpper().Contains(query)).ToArray();
            bool canRequest = MusicRunner.CanUserRequest(Request.RemoteEndPoint.Address.ToString());

            return(Json(new {
                results = files
                          .Skip(10 * (page - 1)).Take(10).Select(f =>
                                                                 new
                {
                    Name = Path.GetFileNameWithoutExtension(f),
                    Download = "/download/" + Path.GetFileName(f),
                    Stream = Path.GetFileName(f),
                    CanRequest = canRequest && !MusicRunner.MasterQueue.Any(s => s.Name.Equals(Path.GetFileNameWithoutExtension(f)))
                }).ToArray(),
                totalPages = files.Count() / 10
            }));
        }
Exemple #7
0
        public ActionResult StartUpload(string filename, long length64)
        {
            filename = filename.Replace('+', ' ');
            if (Uploads.ContainsKey(Request.RemoteEndPoint.Address.ToString()))
            {
                return(Json(new { success = false, reason = "One upload at a time, please." }));
            }
            if (length64 > 104857600) // 100 MB
            {
                return(Json(new { success = false, reason = "File too large." }));
            }
            if (filename.Contains(Path.DirectorySeparatorChar))
            {
                return(Json(new { success = false, error = "Invalid filename." }));
            }
            if (!SongNameRegex.IsMatch(filename))
            {
                return(Json(new { success = false, error = "File name is not in \"Artist Name - Song Title.mp3\" format." }));
            }
            if (File.Exists(Path.Combine(Program.Configuration.MusicPath, filename)))
            {
                return(Json(new { success = false, error = "This song is already in our library." }));
            }
            var minutes = MusicRunner.MinutesUntilNextUpload(Request.RemoteEndPoint.Address.ToString());

            if (minutes < 0)
            {
                return(Json(new { success = false, error = string.Format("You need to wait another {0} minutes before you can upload again.", minutes) }));
            }
            Uploads.Add(Request.RemoteEndPoint.Address.ToString(), new UploadInProgress
            {
                PartialData = string.Empty,
                Filename    = filename,
                FinalLength = length64
            });
            return(Json(new { success = true, partLength = UploadedPartLength }));
        }
Exemple #8
0
        public void HandleChannelMessageRecieved(object sender, PrivateMessageEventArgs e)
        {
            ShowNowPlaying = true; // We only show now playing if we weren't the last one to speak. This reduces clutter in the channel.
            try
            {
                if (e.PrivateMessage.Message.StartsWith("~"))
                {
                    // Handle command
                    var command    = e.PrivateMessage.Message.Substring(1);
                    var parameters = string.Empty;
                    if (command.Contains(" "))
                    {
                        parameters = command.Substring(command.IndexOf(' ') + 1);
                        command    = command.Remove(command.IndexOf(' '));
                    }
                    switch (command)
                    {
                    case "help":
                        if (!string.IsNullOrEmpty(parameters))
                        {
                            break;
                        }
                        Client.SendMessage("Help: " + GetSiteUrl() + "/ircHelp", e.PrivateMessage.Source);
                        break;

                    case "np":
                        if (!string.IsNullOrEmpty(parameters))
                        {
                            break;
                        }
                        Client.SendMessage(string.Format("Now Playing: \"{0} ({1}:{2:00})\" for {3} listener{4} - {5}",
                                                         MusicRunner.NowPlaying.Name,
                                                         MusicRunner.NowPlaying.Duration.Minutes,
                                                         MusicRunner.NowPlaying.Duration.Seconds,
                                                         MusicRunner.Listeners,
                                                         MusicRunner.Listeners != 1 ? "s" : "",
                                                         GetSiteUrl()),
                                           e.PrivateMessage.Source);
                        break;

                    case "q":
                        if (!string.IsNullOrEmpty(parameters))
                        {
                            break;
                        }
                        Client.SendMessage("Queue: " + string.Join("; ", MusicRunner.MasterQueue.Take(3).Select(s => s.Name)), e.PrivateMessage.Source);
                        break;

                    case "r":
                    case "sr":
                        if (string.IsNullOrEmpty(parameters))
                        {
                            break;
                        }
                        int index;
                        if (int.TryParse(parameters, out index))
                        {
                            index--;
                            if (index >= PreviousSearchResults.Length)
                            {
                                Client.SendMessage("Song not found.", e.PrivateMessage.Source);
                                break;
                            }
                            if (MusicRunner.MasterQueue.Any(s => s.Name.Equals(Path.GetFileNameWithoutExtension(PreviousSearchResults[index]))))
                            {
                                Client.SendMessage("That song is already in the queue.", e.PrivateMessage.Source);
                            }
                            else if (!MusicRunner.CanUserRequest(e.PrivateMessage.User.Hostname))
                            {
                                Client.SendMessage("You need to wait a while before you can request again.", e.PrivateMessage.Source);
                            }
                            else
                            {
                                Song song;
                                MusicRunner.QueueUserSong(Path.Combine(Program.Configuration.MusicPath, PreviousSearchResults[index]),
                                                          e.PrivateMessage.User.Hostname, out song);
                                Client.SendMessage(Path.GetFileNameWithoutExtension(PreviousSearchResults[index]) +
                                                   " has been added to the queue.", e.PrivateMessage.Source);
                            }
                        }
                        else
                        {
                            var file = Directory.GetFiles(Program.Configuration.MusicPath, "*.mp3")
                                       .FirstOrDefault(f => Path.GetFileNameWithoutExtension(f).ToUpper().Contains(parameters.ToUpper()));
                            if (file == null)
                            {
                                Client.SendMessage("Song not found.", e.PrivateMessage.Source);
                            }
                            else
                            {
                                if (MusicRunner.MasterQueue.Any(s => s.Name.Equals(Path.GetFileNameWithoutExtension(file))))
                                {
                                    Client.SendMessage("That song is already in the queue.", e.PrivateMessage.Source);
                                }
                                else if (!MusicRunner.CanUserRequest(e.PrivateMessage.User.Hostname))
                                {
                                    Client.SendMessage("You need to wait a while before you can request again.", e.PrivateMessage.Source);
                                }
                                else
                                {
                                    Song song;
                                    MusicRunner.QueueUserSong(Path.Combine(Program.Configuration.MusicPath, file),
                                                              e.PrivateMessage.User.Hostname, out song);
                                    Client.SendMessage(Path.GetFileNameWithoutExtension(file) +
                                                       " has been added to the queue.", e.PrivateMessage.Source);
                                }
                            }
                        }
                        break;

                    case "s":
                        if (string.IsNullOrEmpty(parameters))
                        {
                            break;
                        }
                        var files = Directory.GetFiles(Program.Configuration.MusicPath, "*.mp3")
                                    .OrderBy(Path.GetFileNameWithoutExtension)
                                    .Where(f => Path.GetFileNameWithoutExtension(f).ToUpper().Contains(parameters.ToUpper())).ToArray();
                        string result;
                        if (!files.Any())
                        {
                            result = "No results.";
                        }
                        else
                        {
                            result = string.Format("{0} result{1}: ", files.Length, files.Length != 1 ? "s" : "");
                            for (int i = 0; i < files.Length && i < 3; i++)
                            {
                                result += string.Format("[{0}] {1}; ", i + 1, Path.GetFileNameWithoutExtension(files[i]));
                            }
                            result = result.Remove(result.Length - 2);
                        }
                        PreviousSearchResults = files.Take(3).ToArray();
                        Client.SendMessage(result, e.PrivateMessage.Source);
                        break;

                    case "skip":
                        if (MusicRunner.SkipRequests.Contains(e.PrivateMessage.User.Hostname))
                        {
                            Client.SendMessage(string.Format("You've already voted to skip this song. You need {0} more vote{1} to skip the song.",
                                                             MusicRunner.SkipRequestsRequired - MusicRunner.SkipRequestsIssued,
                                                             MusicRunner.SkipRequestsRequired - MusicRunner.SkipRequestsIssued != 1 ? "s" : ""), e.PrivateMessage.Source);
                            break;
                        }
                        if (((MusicRunner.StartTime + MusicRunner.NowPlaying.Duration) - DateTime.Now).TotalSeconds < 10)
                        {
                            Client.SendMessage("The song is nearly over. Hang tight.", e.PrivateMessage.Source);
                            break;
                        }
                        if (!MusicRunner.RequestSkip(e.PrivateMessage.User.Hostname))
                        {
                            Client.SendMessage(string.Format("Your vote has been noted. You need {0} more vote{1} to skip the song.",
                                                             MusicRunner.SkipRequestsRequired - MusicRunner.SkipRequestsIssued,
                                                             MusicRunner.SkipRequestsRequired - MusicRunner.SkipRequestsIssued != 1 ? "s" : ""), e.PrivateMessage.Source);
                        }
                        break;
                    }
                }
            }
            catch
            {
                // This is just here in case something goes wrong
                // We're dealing with user input here, after all
            }
        }
Exemple #9
0
        public const int UploadedPartLength = 16384; // 16K per chunk

        public ActionResult Index()
        {
            MusicRunner.UpdateListener(Request.RemoteEndPoint.Address.ToString());
            return(View());
        }