Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
304 lines (288 sloc) 9.96 KB
<?php
require('mailgroup.features.inc');
/**
* Implementation of hook_mailhandler()
*
* Assigns OG id and type to incoming message based on group feature's settings
*/
function mailgroup_mailhandler($node, $result, $i, $header, $mailbox) {
$groupname = mailgroup_parse_groupname($header);
$groupinfo = mailgroup_get_settings($groupname, 'prefix');
$space = spaces_load('og', $groupinfo['id']);
$features = $space->controllers->variable->get('spaces_features');
if ($features['mailgroup']) {
if ($groupinfo['id'] && $groupinfo['nodetype']) {
// The $node->uid should be set by now using mailhandler authentication API
if (og_is_group_member($groupinfo['id'], TRUE, $node->uid)) {
$node->og_groups[$groupinfo['id']] = $groupinfo['id'];
$node->type = $groupinfo['nodetype'];
}
}
}
return $node;
}
/**
* Parses the group name from the "to" address and returns it.
*
* @param $header
* Object - email header object
*
* @return
* String - the name of the group
*/
function mailgroup_parse_groupname($header) {
$mailbox = $header->to[0]->mailbox;
return substr($mailbox, strpos($mailbox, "-") +1);
}
/**
* Implementation of hook_block()
*/
function mailgroup_block($op = 'list', $delta = 0) {
switch ($op) {
case 'list':
$blocks['info'] = array('info' => t('MailGroup Information'));
return $blocks;
case 'view':
switch ($delta) {
case 'info':
$blocks['subject'] = t('Post via email');
$blocks['content'] = mailgroup_block_information();
}
return $blocks;
}
}
/**
* Implementation of hook_menu()
*/
function mailgroup_menu() {
$items = array();
$items['features/mailgroup'] = array(
'title' => 'Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('mailgroup_features_settings_form'),
'access callback' => 'spaces_access_admin',
'access arguments' => array(),
'type' => MENU_CALLBACK,
);
$items['admin/settings/mailgroup'] = array(
'title' => 'MailGroup Settings',
'page callback' => 'drupal_get_form',
'page arguments' => array('mailgroup_global_settings'),
'access arguments' => array('administer site configuration'),
'type' => MENU_CALLBACK,
);
$items['mailgroup'] = array(
'title' => 'Mail Group',
'page callback' => 'mailgroup_page',
'page arguments' => array(),
'access callback' => 'spaces_access_feature',
'access arguments' => array('view', 'mailgroup'),
'type' => MENU_NORMAL_ITEM,
'menu_name' => 'features',
);
$items['mailgroup/tokenauth_reset'] = array(
'title' => 'Reset tokenauth token',
'page callback' => 'mailgroup_tokenauth_reset',
'access arguments' => array('access content'),
'type' => MENU_CALLBACK,
);
return $items;
}
/**
* Provides settings form for mailgroup feature within each group.
*/
function mailgroup_features_settings_form() {
$id = spaces_get_space()->id;
$settings = mailgroup_get_settings($id);
$form = array();
$form['mailgroup'] = array(
'#type' => 'fieldset',
'#title' => t('MailGroup Settings'),
);
$form['mailgroup']['prefix'] = array(
'#type' => 'textfield',
'#title' => t('Address prefix'),
'#description' => 'Define an address prefix. The final "to" address for the group will be an address like
"sd89dk22-foo@example.com" where "sd89dk22" is an automatically created token and "foo" is the address prefix.
',
'#default_value' => $settings['prefix'],
);
$form['mailgroup']['subject'] = array(
'#type' => 'textfield',
'#title' => t('Subject name'),
'#description' => t('If you chose to "Use mailing list-style subjects" in mailcomment settings, this string will be used in message
subjects. For example, if you enter "Foo" here, message subjects will appear as "[Site Name] [Foo] Title of post".'),
'#default_value' => $settings['subject'],
);
$feature_map = spaces_features_map('node');
$item = menu_get_item('node/add');
$types = array();
foreach (system_admin_menu_block($item) as $menu_item) {
$args = explode('/', $menu_item['href']);
$type = str_replace('-', '_', array_pop($args));
if (isset($feature_map[$type])) {
$types[substr($menu_item['link_path'], 9)] = node_get_types('name', str_replace('-', '_', substr($menu_item['link_path'], 9)));
}
}
$form['mailgroup']['nodetype'] = array(
'#type' => 'select',
'#title' => t('Content type'),
'#description' => t('Choose which content type incoming messages should become.'),
'#default_value' => $settings['nodetype'],
'#options' => $types,
);
$form['mailgroup']['id'] = array(
'#type' => 'hidden',
'#value' => $id,
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
/**
* Validate handler for mailgroup_features_settings_form().
*/
function mailgroup_features_settings_form_validate($form, &$form_state) {
$values = $form_state['values'];
if (($id = db_result(db_query("SELECT id FROM {mailgroup} WHERE prefix = '%s'", $values['prefix']))) && $id != $values['id']) {
form_set_error('prefix', t('Prefix %prefix already in use by another group. Please enter a different prefix.', array('%prefix' => $values['prefix'])));
}
}
/**
* Submit handler for mailgroup_features_settings_form().
*/
function mailgroup_features_settings_form_submit($form, &$form_state) {
if (db_result(db_query("SELECT id FROM {mailgroup} WHERE id = %d", $form_state['values']['id']))) {
drupal_write_record('mailgroup', $form_state['values'], 'id');
drupal_set_message(t('Mailgroup settings updated.'));
}
else {
drupal_write_record('mailgroup', $form_state['values']);
drupal_set_message(t('Mailgroup settings saved'));
}
}
/**
* Provides sitewide mailgroup settings form.
*/
function mailgroup_global_settings() {
$form = array();
$form['mailgroup'] = array(
'#title' => t('MailGroup Global Settings'),
'#type' => 'fieldset',
);
$hosts = _mailcomment_catchall_hosts();
$form['mailgroup']['mailgroup_mailbox'] = array(
'#title' => t('MailGroup Master Address'),
'#type' => 'select',
'#options' => $hosts,
'#required' => TRUE,
'#default_value' => variable_get('mailgroup_mailbox', ''),
'#description' => t('The mailbox to base all MailGroup mailboxes off of.'),
);
return system_settings_form($form);
}
/**
* Implementation of hook_message_alter().
*/
function mailgroup_message_alter(&$message, $info) {
// TODO: Use static cache of spaces object if id is same for following message
$event = current($message->notifications['events']);
if (variable_get('mailcomment_alter_subjects', 1) && in_array($event->objects['node']->type, variable_get('mailcomment_nodetypes', array()))) {
if (($id = array_pop($event->objects['node']->og_groups)) && $space = spaces_load('og', $id)) {
if ($subject = $space->controllers->variable->get('mailgroup_subject')) {
$old = '[' . variable_get('site_name', '') . ']';
$new = $old . ' ' . "[$subject]";
$message->subject = str_replace($old, $new, $message->subject);
$message->params['mail']['headers']['Reply-To'] = variable_get('mailgroup_mailbox', '');
}
}
}
}
/**
* Provides block content that displays user's tokenauth token address and AJAX reset button.
*
* @TODO: degrade when no JS is available
*
* @return themed block content.
*/
function mailgroup_block_information() {
if (($space = spaces_get_space()) && $space->type == 'og') {
global $user;
$token = tokenauth_get_token($user->uid);
$type = $space->controllers->variable->get('mailgroup_type');
$prefix = $space->controllers->variable->get('mailgroup_address');
$groupaddress = variable_get('mailgroup_mailbox', '');
if ($token && $type && $prefix && $groupaddress) {
$address = "$token-$prefix@" . substr($groupaddress, strpos($groupaddress, '@') +1);
return theme('mailgroup_block_information', $type, $address, $token, $prefix);
}
}
return FALSE;
}
/**
* Main page callback for Mailgroup feature.
*/
function mailgroup_page() {
$output = '';
// TODO: Causes js settings to get added twice.
//$output .= mailgroup_block_information();
$output .= t('Do not share this email address with anyone; it is for your own personal use.');
return $output;
}
/**
* Returns array of mailgroup settings for specified group
*
* @param $id
* Id of spaces space.
* @param $type
* Specify type to select record based on id or prefix.
*
* @return array of mailgroup settings
*/
function mailgroup_get_settings($id, $type = 'id') {
if ($type == 'id') {
return db_fetch_array(db_query("SELECT * FROM {mailgroup} WHERE id = %d", $id));
}
else {
return db_fetch_array(db_query("SELECT * FROM {mailgroup} WHERE prefix = '%s'", $id));
}
}
/**
* Resets user's tokenauth token, AJAX callback
*/
function mailgroup_tokenauth_reset() {
tokenauth_user_reset();
print tokenauth_get_token();
exit;
}
/**
* Implementation of hook_theme()
*/
function mailgroup_theme() {
return array(
'mailgroup_block_information' => array(
'type' => '',
'address' => '',
'token' => '',
'prefix' => '',
),
);
}
/**
* Theme function for mailgroup_block_information
*/
function theme_mailgroup_block_information($type, $address, $token, $prefix) {
// Strip down to just the base domain to make AJAX replacement easier.
$domain = substr($address, strpos($address, '@') +1);
drupal_add_js(array('mailgroup' => array(
'prefix' => $prefix,
'domain' => $domain,
)), 'setting');
drupal_add_js(drupal_get_path('module', 'mailgroup') .'/mailgroup.js', 'module');
drupal_add_css(drupal_get_path('module', 'mailgroup') .'/mailgroup.css');
$content = t('You may create new @type posts by sending an email to the following address:', array('@type' => $type)) . " <div class='mailgroup-address'>$address</div>";
$content .= l(t('Generate new address'), 'mailgroup/tokenauth_reset', array('attributes' => array('class' => 'mailgroup-address-generate')));
return $content;
}
Jump to Line
Something went wrong with that request. Please try again.