private void ChatMessageHook(object sender, ChatMessageArgs e) { String[] words = e.Message.Split(' ', '.', ',', '?', '!', ';', ':', '\'', '"'); int argPos = 0; bool marvinFound = e.Data.HasMentionPrefix(_bot.Client.CurrentUser, ref argPos); // Find Marv or Marvin in message if (!marvinFound) { foreach (String word in words) { if (word.Length == 4 && (word[0] == 'M' || word[0] == 'm') && (word[1] == 'A' || word[1] == 'a') && (word[2] == 'R' || word[2] == 'r') && (word[3] == 'V' || word[3] == 'v') || word.Length == 6 && (word[0] == 'M' || word[0] == 'm') && (word[1] == 'A' || word[1] == 'a') && (word[2] == 'R' || word[2] == 'r') && (word[3] == 'V' || word[3] == 'v') && (word[4] == 'I' || word[4] == 'i') && (word[5] == 'N' || word[5] == 'n')) { marvinFound = true; break; } } } if (marvinFound) { int num = 0; bool numFound = false; // Find a number in message foreach (String word in words) { int length = word.Length; if (word.IndexOf("SCP", StringComparison.OrdinalIgnoreCase) == 0) { if (length > 3 && word[3] == '-' && Int32.TryParse(word.Substring(4), out num)) { numFound = true; break; } if (Int32.TryParse(word.Substring(3), out num)) { numFound = true; break; } } else if(Int32.TryParse(word, out num)) { numFound = true; break; } } if (numFound) { ScpObject scp = null; if (Scps.ContainsKey(num)) { scp = Scps[num]; SendMessage(e, scp); } else { try { scp = GrabOnlineVersion(num); } catch (Exception exception) { _bot.WriteConsole("[SCP] Attempt to pull SCP-" + num + " from website has failed!\nDisplaying error info:\n" + exception); } if (scp != null) { Scps.Add(num, scp); SaveChanges(); SendMessage(e, scp); } else { Random rand = new Random(); String response = UnknownResponses[rand.Next(UnknownResponses.Count)].Replace("%s", ScpObject.GetViewId(num)).Replace("%u", e.User.Mention); _bot.Chat.SendMessage(e.Channel, response); } } } } return; }
private ScpObject GrabOnlineVersion(int id) { ScpObject scp = null; String page = ""; WebClient client = new WebClient(); _bot.WriteConsole("[SCP] Attempting to pull SCP-" + id + " from website."); page = client.DownloadString("http://www.scp-wiki.net/scp-" + ScpObject.GetViewId(id)); var match = Regex.Match(page, "<strong>Object Class:*</strong>"); int classStart = match.Index; if (classStart != -1) { scp = new ScpObject { Id = id }; classStart += 30; int classEnd = page.IndexOf("</p>", classStart, StringComparison.OrdinalIgnoreCase); if (classEnd != -1) { String objectClass = page.Substring(classStart, classEnd - classStart).Trim(); int anomaly = objectClass.LastIndexOf('<'); if (anomaly != -1) { int anomalyStart = 0; for (int x = anomaly; x >= 0; x--) { if (objectClass[x] == '>') { anomalyStart = x; break; } } objectClass = objectClass.Substring(anomalyStart + 1, anomaly - anomalyStart - 1).Trim(); } scp.SetObjectClass(objectClass); } match = Regex.Match(page, "<strong>Description:*</strong>"); int descStart = match.Index; if (descStart != -1) { descStart += 29; int descEnd = page.IndexOf("</p>", descStart, StringComparison.OrdinalIgnoreCase); var desc = descEnd != 1 ? page.Substring(descStart, descEnd - descStart).Trim().Replace(""", "\"") .Replace(" ", " ") : "[REDACTED]"; scp.Description = Regex.Replace(desc, "<[^>]*>", ""); } else { scp.Description = "[REDACTED]"; } scp.Generated = true; String series = ""; if (id > 999) { series = "-" + (id / 1000 + 1); } page = client.DownloadString("http://www.scp-wiki.net/scp-series" + series); int nameStart = page.IndexOf("SCP-" + scp.ViewId + "</a> -", StringComparison.OrdinalIgnoreCase); if (nameStart != -1) { nameStart += 14; int nameEnd = page.IndexOf("</li>", nameStart, StringComparison.OrdinalIgnoreCase); if (nameEnd != -1) { String name = page.Substring(nameStart, nameEnd - nameStart).Trim(); int anomaly = name.LastIndexOf('<'); if (anomaly != -1) { int anomalyStart = 0; for (int x = anomaly; x >= 0; x--) { if (name[x] == '>') { anomalyStart = x; break; } } name = name.Substring(anomalyStart + 1, anomaly - anomalyStart - 1).Trim(); } scp.Name = name; } else { scp.Name = "[REDACTED]"; } } else { scp.Name = "[REDACTED]"; } } else { _bot.WriteConsole("[SCP] Couldn't detect object class for SCP-" + id + "!"); } client.Dispose(); return scp; }