View source
<?php
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';
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;
}
}
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;
}
function slack_ctools_plugin_api($module, $api) {
if ($module == 'message_notify' && $api == 'notifier') {
return array(
'version' => 1,
);
}
}
function slack_ctools_plugin_directory($module, $plugin) {
if ($module == 'message_notify') {
return 'plugins/' . $plugin;
}
}
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);
}
}
}
}
function slack_cron_queue_info() {
$queues['slack_files_delete'] = array(
'worker callback' => 'slack_delete_file',
'time' => 30,
);
return $queues;
}
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']);
}
}
function slack_get_files_list(array $query) {
$files = slack_send_request('files.list', $query);
if ($files['ok'] == TRUE) {
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;
}
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);
}