You are here

slack.module in Slack 7

Same filename and directory in other branches
  1. 6 slack.module

Main module file, only hooks are allowed here.

File

slack.module
View source
<?php

/**
 * @file
 * Main module file, only hooks are allowed here.
 */
define('SLACK_CODE_OK', 200);
define('SLACK_CODE_NOT_FOUND', 404);
define('SLACK_CODE_SERVER_ERROR', 500);
require_once 'includes/slack.api.inc';
require_once 'slack.tokens.inc';

/**
 * Implements hook_help().
 */
function slack_help($path, $arg) {
  switch ($path) {
    case 'admin/help#slack':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The <a href="@slack">Slack</a> module brings all your communication together in one place. It has real-time messaging, archiving and search for modern teams, includes has cool system integrations features.', array(
        '@slack' => 'https://www.drupal.org/project/slack',
      )) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('General') . '</dt>';
      $output .= '<dd>' . t('The Slack module allows you to send messages from your website to Slack.') . '</dd>';
      $output .= '<dt>' . t('Rules Integration') . '</dt>';
      $output .= '<dd>' . t('The Slack module includes Rules module integration. Also, you can use the module API in your modules.') . '</dd>';
      $output .= '<dt>' . t('Icon Support') . '</dt>';
      $output .= '<dd>' . t('Icons support for your slackbot (icons and emoji) is included along with Slack friendly tags. If "http://www."-part is not added in href, slack will represent the link as relative path.') . '</dd>';
      $output .= '</dl>';
      return $output;
  }
}

/**
 * Implements hook_menu().
 */
function slack_menu() {
  $items = array();
  $slack_module_url = 'admin/config/services/slack';
  $items[$slack_module_url] = array(
    'title' => 'Slack',
    'description' => 'Configure slack module.',
    'page callback' => 'system_admin_menu_block_page',
    'access arguments' => array(
      'administer site configuration',
    ),
    'file' => 'system.admin.inc',
    'file path' => drupal_get_path('module', 'system'),
  );
  $items[$slack_module_url . '/config'] = array(
    'title' => 'Configuration',
    'description' => 'Adjust slack settings.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'slack_configure_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'includes/pages/slack.admin.inc',
  );
  $items[$slack_module_url . '/test'] = array(
    'title' => 'Send a message',
    'description' => 'Allows to send a test message to the Slack.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'slack_send_test_message_form',
    ),
    'access arguments' => array(
      'administer site configuration',
    ),
    'type' => MENU_NORMAL_ITEM,
    'file' => 'includes/pages/slack.pages.inc',
  );
  return $items;
}

/**
 * Implements hook_ctools_plugin_api().
 */
function slack_ctools_plugin_api($module, $api) {
  if ($module == 'message_notify' && $api == 'notifier') {
    return array(
      'version' => 1,
    );
  }
}

/**
 * Implements hook_ctools_plugin_directory().
 */
function slack_ctools_plugin_directory($module, $plugin) {
  if ($module == 'message_notify') {
    return 'plugins/' . $plugin;
  }
}

/**
 * Implements hook_cron().
 */
function slack_cron() {
  if (variable_get('slack_token')) {
    $slack_files_delete = DrupalQueue::get('slack_files_delete');
    $types = '';
    if ($filtered = array_filter(variable_get('slack_file_types'))) {
      $types = implode(',', $filtered);
    }
    $query = array(
      'token' => variable_get('slack_token'),
      'ts_to' => strtotime('-' . slack_get_files_age_number() . ' ' . slack_get_files_age_unit()),
      'types' => $types,
    );
    $files = slack_get_files_list($query);
    if (!empty($files['files'])) {
      foreach ($files['files'] as $file) {
        $slack_files_delete
          ->createItem($file);
      }
    }
  }
}

/**
 * Implements hook_cron_queue_info().
 */
function slack_cron_queue_info() {
  $queues['slack_files_delete'] = array(
    'worker callback' => 'slack_delete_file',
    'time' => 30,
  );
  return $queues;
}

/**
 * Deletes file from slack.
 *
 * @param array $file
 *   Array with file info, that was get from slack_get_files_list().
 */
function slack_delete_file(array $file) {
  $delete = slack_send_request('files.delete', array(
    'file' => $file['id'],
  ));
  if ($delete['ok'] == TRUE) {
    watchdog('Slack', '@fid (@filename) was deleted via cron.', array(
      '@fid' => $file['id'],
      '@filename' => $file['name'],
    ));
  }
  else {
    watchdog('Slack', '@fid (@filename) not deleted', array(
      '@fid' => $file['id'],
      '@filename' => $file['name'],
    ), WATCHDOG_NOTICE, $file['permalink']);
  }
}

/**
 * Deletes file from slack.
 *
 * @param array $query
 *   Array with query params.
 */
function slack_get_files_list(array $query) {
  $files = slack_send_request('files.list', $query);
  if ($files['ok'] == TRUE) {

    // If more than 1 page, append to original call.
    if ($files['paging']['pages'] > 1) {
      for ($i = 2; $i <= $files['paging']['pages']; $i++) {
        $query['page'] = $i;
        $files_next_page = slack_send_request('files.list', $query);
        if ($files_next_page['ok'] == TRUE) {
          $files['paging']['count'] = $files['paging']['count'] + $files_next_page['paging']['count'];
          $files['files'] = array_merge($files['files'], $files_next_page['files']);
        }
        else {
          watchdog('Slack', '@page page error', array(
            '@page' => $query['page'],
          ));
        }
      }
      $files['paging']['pages'] = 1;
    }
    return $files;
  }
  else {
    watchdog('Slack', '@page page error', array(
      '@page' => $query['page'],
    ));
  }
  return FALSE;
}

/**
 * Deletes file from slack.
 *
 * @param string $method
 *   String containing slack api method.
 * @param array $query
 *   Array with query params.
 */
function slack_send_request($method, array $query) {
  $query['token'] = variable_get('slack_token');
  $url = 'https://slack.com/api/' . $method . '?' . drupal_http_build_query($query);
  $response = drupal_http_request($url);
  return json_decode($response->data, TRUE);
}

Functions

Namesort descending Description
slack_cron Implements hook_cron().
slack_cron_queue_info Implements hook_cron_queue_info().
slack_ctools_plugin_api Implements hook_ctools_plugin_api().
slack_ctools_plugin_directory Implements hook_ctools_plugin_directory().
slack_delete_file Deletes file from slack.
slack_get_files_list Deletes file from slack.
slack_help Implements hook_help().
slack_menu Implements hook_menu().
slack_send_request Deletes file from slack.

Constants

Namesort descending Description
SLACK_CODE_NOT_FOUND
SLACK_CODE_OK @file Main module file, only hooks are allowed here.
SLACK_CODE_SERVER_ERROR