| 1 |
<?php
|
| 2 |
// $Id: mailhandler.module,v 1.96.2.1 2008/05/15 10:43:55 weitzman Exp $
|
| 3 |
|
| 4 |
|
| 5 |
/**
|
| 6 |
* Implementation of hook_cron(). Process msgs from all enabled mailboxes.
|
| 7 |
*/
|
| 8 |
function mailhandler_cron() {
|
| 9 |
|
| 10 |
// Include mailhandler retrieval functions
|
| 11 |
module_load_include('inc', 'mailhandler', 'mailhandler.retrieve');
|
| 12 |
|
| 13 |
// Retrieve messages
|
| 14 |
$result = db_query('SELECT * FROM {mailhandler} WHERE enabled = 1 ORDER BY mail');
|
| 15 |
while ($mailbox = db_fetch_array($result)) {
|
| 16 |
mailhandler_cron_retrieve($mailbox);
|
| 17 |
}
|
| 18 |
|
| 19 |
}
|
| 20 |
|
| 21 |
|
| 22 |
/**
|
| 23 |
* Implementation of hook_perm().
|
| 24 |
*/
|
| 25 |
function mailhandler_perm() {
|
| 26 |
return array('administer mailhandler');
|
| 27 |
}
|
| 28 |
|
| 29 |
|
| 30 |
/**
|
| 31 |
* Implementation of hook_menu().
|
| 32 |
*/
|
| 33 |
function mailhandler_menu() {
|
| 34 |
|
| 35 |
$items = array();
|
| 36 |
|
| 37 |
$items['admin/content/mailhandler'] = array(
|
| 38 |
'title' => t('Mailhandler'),
|
| 39 |
'description' => t('Manage mailboxes and retrieve messages.'),
|
| 40 |
'page callback' => 'mailhandler_list_mailboxes',
|
| 41 |
'access arguments' => array('administer mailhandler'),
|
| 42 |
'file' => 'mailhandler.admin.inc',
|
| 43 |
);
|
| 44 |
|
| 45 |
$items['admin/content/mailhandler/list'] = array(
|
| 46 |
'title' => t('List'),
|
| 47 |
'description' => t('Manage mailboxes and retrieve messages.'),
|
| 48 |
'type' => MENU_DEFAULT_LOCAL_TASK,
|
| 49 |
'access arguments' => array('administer mailhandler'),
|
| 50 |
'weight' => -10,
|
| 51 |
);
|
| 52 |
|
| 53 |
$items['admin/content/mailhandler/add'] = array(
|
| 54 |
'title' => t('Add mailbox'),
|
| 55 |
'page callback' => 'drupal_get_form',
|
| 56 |
'page arguments' => array('mailhandler_add_edit_mailbox', NULL),
|
| 57 |
'access arguments' => array('administer mailhandler'),
|
| 58 |
'type' => MENU_LOCAL_TASK,
|
| 59 |
'file' => 'mailhandler.admin.inc',
|
| 60 |
);
|
| 61 |
|
| 62 |
$items['admin/content/mailhandler/retrieve/%'] = array(
|
| 63 |
'title' => t('Retrieve'),
|
| 64 |
'page callback' => 'mailhandler_admin_retrieve',
|
| 65 |
'page arguments' => array(4),
|
| 66 |
'access arguments' => array('administer mailhandler'),
|
| 67 |
'type' => MENU_CALLBACK,
|
| 68 |
'file' => 'mailhandler.retrieve.inc',
|
| 69 |
);
|
| 70 |
|
| 71 |
$items['admin/content/mailhandler/edit/%'] = array(
|
| 72 |
'title' => t('Edit mailbox'),
|
| 73 |
'page callback' => 'drupal_get_form',
|
| 74 |
'page arguments' => array('mailhandler_add_edit_mailbox', 4),
|
| 75 |
'access arguments' => array('administer mailhandler'),
|
| 76 |
'type' => MENU_CALLBACK,
|
| 77 |
'file' => 'mailhandler.admin.inc',
|
| 78 |
);
|
| 79 |
|
| 80 |
$items['admin/content/mailhandler/delete/%'] = array(
|
| 81 |
'title' => t('Delete mailbox'),
|
| 82 |
'page callback' => 'drupal_get_form',
|
| 83 |
'page arguments' => array('mailhandler_admin_delete_confirm', 4),
|
| 84 |
'access arguments' => array('administer mailhandler'),
|
| 85 |
'type' => MENU_CALLBACK,
|
| 86 |
'file' => 'mailhandler.admin.inc',
|
| 87 |
);
|
| 88 |
|
| 89 |
$items['admin/settings/mailhandler'] = array(
|
| 90 |
'title' => 'Mailhandler',
|
| 91 |
'description' => t('Set the default content type for incoming messages and set the cron limit.'),
|
| 92 |
'page callback' => 'drupal_get_form',
|
| 93 |
'page arguments' => array('mailhandler_admin_settings'),
|
| 94 |
'access arguments' => array('administer mailhandler'),
|
| 95 |
'file' => 'mailhandler.admin.inc',
|
| 96 |
);
|
| 97 |
|
| 98 |
return $items;
|
| 99 |
}
|
| 100 |
|
| 101 |
|
| 102 |
/**
|
| 103 |
* Implementation of hook_help().
|
| 104 |
*/
|
| 105 |
function mailhandler_help($path = 'admin/help#mailhandler', $arg) {
|
| 106 |
$output = '';
|
| 107 |
$link->add = l(t('Add mailbox'), 'admin/content/mailhandler/add');
|
| 108 |
|
| 109 |
// Gather examples of useful commands, and build a definition list with them:
|
| 110 |
$commands[] = array('command' => 'taxonomy: [term1, term2]',
|
| 111 |
'description' => t('Use this to add the terms <em>term1</em> and <em>term2</em> to the node.<br />
|
| 112 |
Both of the terms should already exist. In case they do not exist already, they will be quietly ommitted'));
|
| 113 |
$commands[] = array('command' => 'taxonomy[v]: [term1, term2]',
|
| 114 |
'description' => t('Similar to the above: adds the terms <em>term1</em> and <em>term2</em> to the node, but uses the vocabulary with the vocabulary id <em>v</em>. For example <em>taxonomy[3]</em> will chose only terms from the vocabulary which id is 3.<br />
|
| 115 |
In case some of the terms do not exist already, the behavior will depend on whether the vocabulary is a free tagging vocabulary or not. If it is a free tagging vocabulary, the term will be added, otherwise, it will be quietly ommitted'));
|
| 116 |
|
| 117 |
$commands_list = '<dl>';
|
| 118 |
foreach ($commands as $command) {
|
| 119 |
$commands_list .= '<dt>'. $command['command'] .'</dt>';
|
| 120 |
$commands_list .= '<dl>'. $command['description'] .'</dl>';
|
| 121 |
}
|
| 122 |
$commands_list .= '</dl>';
|
| 123 |
|
| 124 |
switch ($path) {
|
| 125 |
case 'admin/help#mailhandler':
|
| 126 |
$output = '<p>'. t('The mailhandler module allows registered users to create or edit nodes and comments via e-mail. Users may post taxonomy terms, teasers, and other post attributes using the mail commands capability. This module is useful because e-mail is the preferred method of communication by community members.') .'</p>';
|
| 127 |
$output .= '<p>'. t('The mailhandler module requires the use of a custom mailbox. Administrators can add mailboxes that should be customized to meet the needs of a mailing list. This mailbox will then be checked on every cron job. Administrators may also initiate a manual retrieval of messages.') .'</p>';
|
| 128 |
$output .= '<p>'. t('This is particularly useful when you want multiple sets of default commands. For example , if you want to authenticate based on a non-standard mail header like Sender: which is useful for accepting submissions from a listserv. Authentication is usually based on the From: e-mail address. Administrators can edit the individual mailboxes when they administer mailhandler.') .'</p>';
|
| 129 |
$output .= t('<p>You can</p>
|
| 130 |
<ul>
|
| 131 |
<li><a href="@run-cron">run cron</a> to retrieve messages from all cron enabled mailboxes.</li>
|
| 132 |
<li>list mailboxes at <a href="@admin-mailhandler">Administer >> Content management >> Mailhandler</a>.</li>
|
| 133 |
<li>add a mailbox at <a href="@admin-mailhandler-add">Administer >> Content management >> Mailhandler >> Add mailbox.</a></li>
|
| 134 |
<li>set default commands, (password, type, taxonomy, promote, status), for how to work with incoming mail at <a href="%admin-mailhandler">Administer >> Content management >> Mailhandler</a> and select <strong>edit</strong> for the email address being handled. Set commands in the default command field.</li>
|
| 135 |
<li>post email, such as from a mailing list, to a forum by adding the term id (number found in the URL) to the default commands using <strong>tid: #</strong>.</li>
|
| 136 |
<li>alter mailhandler settings (default content type and cron threshold) at <a href="@admin-mailhandler-settings">Administer >> Site configuration >> Mailhandler</a>.</li>
|
| 137 |
</ul>',
|
| 138 |
array(
|
| 139 |
'@run-cron' => url('admin/logs/status/run-cron'),
|
| 140 |
'@admin-mailhandler-add' => url('admin/content/mailhandler/add'),
|
| 141 |
'@admin-mailhandler' => url('admin/content/mailhandler'),
|
| 142 |
'@admin-mailhandler-settings' => url('admin/settings/mailhandler'),
|
| 143 |
));
|
| 144 |
$output .= '<h3 id="commands">'. t('Useful Commands') .'</h3>';
|
| 145 |
$output .= $commands_list;
|
| 146 |
$output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%mailhandler">Mailhandler page</a>.', array('%mailhandler' => 'http://www.drupal.org/handbook/modules/mailhandler/')) .'</p>';
|
| 147 |
return $output;
|
| 148 |
case 'admin/content/mailhandler':
|
| 149 |
return t('The mailhandler module allows registered users to create or edit nodes and comments via email. Authentication is usually based on the From: email address. There is also an email filter that can be used to prettify incoming email. Users may post taxonomy terms, teasers, and other node parameters using the Command capability.');
|
| 150 |
case 'admin/content/mailhandler/add':
|
| 151 |
return t('Add a mailbox whose mail you wish to import into Drupal. Can be IMAP, POP3, or local folder.');
|
| 152 |
case 'admin/content/mailhandler/edit/%':
|
| 153 |
return t('Edit the mailbox whose mail you wish to import into Drupal. Can be IMAP, POP3, or local folder.');
|
| 154 |
case 'admin/settings/mailhandler':
|
| 155 |
return t('The mailhandler module allows registered users to create or edit nodes and comments via e-mail.');
|
| 156 |
}
|
| 157 |
}
|
| 158 |
|
| 159 |
|
| 160 |
/**
|
| 161 |
* Implementation of hook_init to add mailhandler.css
|
| 162 |
*/
|
| 163 |
function mailhandler_init() {
|
| 164 |
drupal_add_css(drupal_get_path('module', 'mailhandler') .'/mailhandler.css');
|
| 165 |
return;
|
| 166 |
}
|
| 167 |
|
| 168 |
|
| 169 |
// The following functions are called by both mailhandler.admin.inc and mailhandler.retrieve.inc so they
|
| 170 |
// are defined here to make them available to both
|
| 171 |
|
| 172 |
/**
|
| 173 |
* Establish a connection to the mailbox specified by the array $mailbox
|
| 174 |
*/
|
| 175 |
function mailhandler_open_mailbox($mailbox) {
|
| 176 |
|
| 177 |
if ($mailbox['domain']) {
|
| 178 |
if ($mailbox['imap'] == 1) {
|
| 179 |
$box = '{'. $mailbox['domain'] .':'. $mailbox['port'] . $mailbox['extraimap'] .'}'. $mailbox['folder'];
|
| 180 |
}
|
| 181 |
else {
|
| 182 |
$box = '{'. $mailbox['domain'] .':'. $mailbox['port'] .'/pop3'. $mailbox['extraimap'] .'}'. $mailbox['folder'];
|
| 183 |
}
|
| 184 |
$result = imap_open($box, $mailbox['name'], $mailbox['pass']);
|
| 185 |
$err = 'domain';
|
| 186 |
}
|
| 187 |
else {
|
| 188 |
$box = $mailbox['folder'];
|
| 189 |
$result = imap_open($box, '', '');
|
| 190 |
}
|
| 191 |
|
| 192 |
return $result;
|
| 193 |
|
| 194 |
}
|
| 195 |
|
| 196 |
|
| 197 |
/**
|
| 198 |
* Fetch data for a specific mailbox from the database.
|
| 199 |
*/
|
| 200 |
function mailhandler_get_mailbox($mid) {
|
| 201 |
return db_fetch_array(db_query("SELECT * FROM {mailhandler} WHERE mid = %d", $mid));
|
| 202 |
}
|
| 203 |
|
| 204 |
|
| 205 |
/**
|
| 206 |
* Return a default content type if the user has not chosen a specific type on the settings page
|
| 207 |
* In order of priority, return blog, story, page
|
| 208 |
* This assumes that one of these basic types is in use on a site (page and story are active by default)
|
| 209 |
* A user can choose other content types via the settings page as this exposes all available types
|
| 210 |
*/
|
| 211 |
function mailhandler_default_type() {
|
| 212 |
|
| 213 |
// Get the current default setting, if defined
|
| 214 |
$default_type = variable_get('mailhandler_default_type', NULL);
|
| 215 |
|
| 216 |
// Find out what types are available
|
| 217 |
$available_types = node_get_types('names');
|
| 218 |
|
| 219 |
// Check the default type is still available (it could have been deleted)
|
| 220 |
if ($default_type && array_key_exists($default_type, $available_types)) {
|
| 221 |
return $default_type;
|
| 222 |
}
|
| 223 |
|
| 224 |
// If we get here then either no default is set, or the default type is no longer available
|
| 225 |
|
| 226 |
// Search for the array key (the machine readable name) for blog, story and page basic types
|
| 227 |
if (array_key_exists('blog', $available_types)) {
|
| 228 |
$default_type = 'blog';
|
| 229 |
}
|
| 230 |
else if (array_key_exists('story', $available_types)) {
|
| 231 |
$default_type = 'story';
|
| 232 |
}
|
| 233 |
else if (array_key_exists('page', $available_types)) {
|
| 234 |
$default_type = 'page';
|
| 235 |
}
|
| 236 |
else {
|
| 237 |
// If basic types not found then return the first item from the array as an alternative default
|
| 238 |
$default_type = key($available_types);
|
| 239 |
}
|
| 240 |
|
| 241 |
return $default_type;
|
| 242 |
|
| 243 |
}
|