<commit_before><?php

namespace CarlBennett\API\Libraries;

use \CarlBennett\API\Libraries\Common;
use \CarlBennett\API\Libraries\Logger;
use \CarlBennett\API\Libraries\Magic8Ball;
use \CarlBennett\API\Libraries\WeatherReport;
use \CarlBennett\MVC\Libraries\GeoIP;

class Slack {

  public function handleWebhook(&$data) {
    foreach ($data as $k => $v) {
      Logger::logMetric($k, $v);
    }

    $token        = (isset($data["token"])
                  ? $data["token"] : null);
    $team_id      = (isset($data["team_id"])
                  ? $data["team_id"] : null);
    $team_domain  = (isset($data["team_domain"])
                  ? $data["team_domain"] : null);
    $channel_id   = (isset($data["channel_id"])
                  ? $data["channel_id"] : null);
    $channel_name = (isset($data["channel_name"])
                  ? $data["channel_name"] : null);
    $timestamp    = (isset($data["timestamp"])
                  ? $data["timestamp"] : null);
    $user_id      = (isset($data["user_id"])
                  ? $data["user_id"] : null);
    $user_name    = (isset($data["user_name"])
                  ? $data["user_name"] : null);
    $command      = (isset($data["command"])
                  ? $data["command"] : null);
    $text         = (isset($data["text"])
                  ? $data["text"] : null);
    $trigger_word = (isset($data["trigger_word"])
                  ? $data["trigger_word"] : null);

    if (empty($command) && !empty($trigger_word)) {
      $command = $trigger_word;
      $text    = preg_replace("#<https?://.*\|(.*)>#i", "$1", $text);
    }

    if (substr($text, 0, strlen($command)) === $command) {
      $text = substr($text, strlen($command) + 1);
    }

    $command = ltrim($command, "./");

    $response = null;
    switch ($command) {
      case "8ball":
      case "magic8ball": {
        $question = trim($text);
        if (strpos($question, "\n") !== false) {
          $response = "Only one-line sentences please!";
        } else if (substr(trim($question), -1) != "?") {
          $response = "Please ask me a question.";
        } else {
          $response = "> " . $question . "\n"
            . (new Magic8Ball())->getPrediction($question);
        }
        break;
      }
      case "dig":
      case "host":
      case "nslookup":
      case "ping":
      case "whois": {
        $output = Common::shellSafeExecute($command, $text);
        if (empty($output)) {
          $response = "No output from the command-line program.";
        } else {
          $response = "```" . Common::stripExcessLines($output) . "```";
        }
        break;
      }
      case "geoip": {
        $ip = trim($text);
        if (empty($ip)) {
          $response = "Error: Please provide an IP address or hostname.";
        } else {
          $geoinfo = GeoIP::get($ip);
          $response = "query_address " . $ip . "\n";
          if ($geoinfo) {<commit_msg>Revert "Add ping command to Slack webhook"<commit_after><?php

namespace CarlBennett\API\Libraries;

use \CarlBennett\API\Libraries\Common;
use \CarlBennett\API\Libraries\Logger;
use \CarlBennett\API\Libraries\Magic8Ball;
use \CarlBennett\API\Libraries\WeatherReport;
use \CarlBennett\MVC\Libraries\GeoIP;

class Slack {

  public function handleWebhook(&$data) {
    foreach ($data as $k => $v) {
      Logger::logMetric($k, $v);
    }

    $token        = (isset($data["token"])
                  ? $data["token"] : null);
    $team_id      = (isset($data["team_id"])
                  ? $data["team_id"] : null);
    $team_domain  = (isset($data["team_domain"])
                  ? $data["team_domain"] : null);
    $channel_id   = (isset($data["channel_id"])
                  ? $data["channel_id"] : null);
    $channel_name = (isset($data["channel_name"])
                  ? $data["channel_name"] : null);
    $timestamp    = (isset($data["timestamp"])
                  ? $data["timestamp"] : null);
    $user_id      = (isset($data["user_id"])
                  ? $data["user_id"] : null);
    $user_name    = (isset($data["user_name"])
                  ? $data["user_name"] : null);
    $command      = (isset($data["command"])
                  ? $data["command"] : null);
    $text         = (isset($data["text"])
                  ? $data["text"] : null);
    $trigger_word = (isset($data["trigger_word"])
                  ? $data["trigger_word"] : null);

    if (empty($command) && !empty($trigger_word)) {
      $command = $trigger_word;
      $text    = preg_replace("#<https?://.*\|(.*)>#i", "$1", $text);
    }

    if (substr($text, 0, strlen($command)) === $command) {
      $text = substr($text, strlen($command) + 1);
    }

    $command = ltrim($command, "./");

    $response = null;
    switch ($command) {
      case "8ball":
      case "magic8ball": {
        $question = trim($text);
        if (strpos($question, "\n") !== false) {
          $response = "Only one-line sentences please!";
        } else if (substr(trim($question), -1) != "?") {
          $response = "Please ask me a question.";
        } else {
          $response = "> " . $question . "\n"
            . (new Magic8Ball())->getPrediction($question);
        }
        break;
      }
      case "dig":
      case "host":
      case "nslookup":
      case "whois": {
        $output = Common::shellSafeExecute($command, $text);
        if (empty($output)) {
          $response = "No output from the command-line program.";
        } else {
          $response = "```" . Common::stripExcessLines($output) . "```";
        }
        break;
      }
      case "geoip": {
        $ip = trim($text);
        if (empty($ip)) {
          $response = "Error: Please provide an IP address or hostname.";
        } else {
          $geoinfo = GeoIP::get($ip);
          $response = "query_address " . $ip . "\n";
          if ($geoinfo) {
{'fraction_non_alphanumeric': 0.18792749658002736, 'fraction_numerical': 0.0027359781121751026, 'mean_word_length': 2.0930724484399787, 'pattern_counts': {'":': 15, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3272486', 'n_tokens_mistral': 1876, 'n_tokens_neox': 1818, 'n_words': 354}