/[drupal]/contributions/modules/mailhandler/mailhandler.module
ViewVC logotype

Contents of /contributions/modules/mailhandler/mailhandler.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.96.2.3 - (show annotations) (download) (as text)
Sun Oct 26 21:43:24 2008 UTC (13 months ago) by zstolar
Branch: DRUPAL-6--1
CVS Tags: DRUPAL-6--1-8, DRUPAL-6--1-5, DRUPAL-6--1-6, DRUPAL-6--1-7
Branch point for: DRUPAL-6--2
Changes since 1.96.2.2: +10 -1 lines
File MIME type: text/x-php
Add an option to clone mailbox configuration
1 <?php
2 // $Id: mailhandler.module,v 1.96.2.2 2008/08/09 11:54:43 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/clone/%'] = array(
63 'title' => t('Add mailbox'),
64 'page callback' => 'drupal_get_form',
65 'page arguments' => array('mailhandler_add_edit_mailbox', 4, TRUE),
66 'access arguments' => array('administer mailhandler'),
67 'type' => MENU_CALLBACK,
68 'file' => 'mailhandler.admin.inc',
69 );
70
71 $items['admin/content/mailhandler/retrieve/%'] = array(
72 'title' => t('Retrieve'),
73 'page callback' => 'mailhandler_admin_retrieve',
74 'page arguments' => array(4),
75 'access arguments' => array('administer mailhandler'),
76 'type' => MENU_CALLBACK,
77 'file' => 'mailhandler.retrieve.inc',
78 );
79
80 $items['admin/content/mailhandler/edit/%'] = array(
81 'title' => t('Edit mailbox'),
82 'page callback' => 'drupal_get_form',
83 'page arguments' => array('mailhandler_add_edit_mailbox', 4),
84 'access arguments' => array('administer mailhandler'),
85 'type' => MENU_CALLBACK,
86 'file' => 'mailhandler.admin.inc',
87 );
88
89 $items['admin/content/mailhandler/delete/%'] = array(
90 'title' => t('Delete mailbox'),
91 'page callback' => 'drupal_get_form',
92 'page arguments' => array('mailhandler_admin_delete_confirm', 4),
93 'access arguments' => array('administer mailhandler'),
94 'type' => MENU_CALLBACK,
95 'file' => 'mailhandler.admin.inc',
96 );
97
98 $items['admin/settings/mailhandler'] = array(
99 'title' => 'Mailhandler',
100 'description' => t('Set the default content type for incoming messages and set the cron limit.'),
101 'page callback' => 'drupal_get_form',
102 'page arguments' => array('mailhandler_admin_settings'),
103 'access arguments' => array('administer mailhandler'),
104 'file' => 'mailhandler.admin.inc',
105 );
106
107 return $items;
108 }
109
110
111 /**
112 * Implementation of hook_help().
113 */
114 function mailhandler_help($path = 'admin/help#mailhandler', $arg) {
115 $output = '';
116 $link->add = l(t('Add mailbox'), 'admin/content/mailhandler/add');
117
118 // Gather examples of useful commands, and build a definition list with them:
119 $commands[] = array('command' => 'taxonomy: [term1, term2]',
120 'description' => t('Use this to add the terms <em>term1</em> and <em>term2</em> to the node.<br />
121 Both of the terms should already exist. In case they do not exist already, they will be quietly ommitted'));
122 $commands[] = array('command' => 'taxonomy[v]: [term1, term2]',
123 '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 />
124 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'));
125
126 $commands_list = '<dl>';
127 foreach ($commands as $command) {
128 $commands_list .= '<dt>'. $command['command'] .'</dt>';
129 $commands_list .= '<dl>'. $command['description'] .'</dl>';
130 }
131 $commands_list .= '</dl>';
132
133 switch ($path) {
134 case 'admin/help#mailhandler':
135 $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>';
136 $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>';
137 $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>';
138 $output .= t('<p>You can</p>
139 <ul>
140 <li><a href="@run-cron">run cron</a> to retrieve messages from all cron enabled mailboxes.</li>
141 <li>list mailboxes at <a href="@admin-mailhandler">Administer &gt;&gt; Content management &gt;&gt; Mailhandler</a>.</li>
142 <li>add a mailbox at <a href="@admin-mailhandler-add">Administer &gt;&gt; Content management &gt;&gt; Mailhandler &gt;&gt; Add mailbox.</a></li>
143 <li>set default commands, (password, type, taxonomy, promote, status), for how to work with incoming mail at <a href="%admin-mailhandler">Administer &gt;&gt; Content management &gt;&gt; Mailhandler</a> and select <strong>edit</strong> for the email address being handled. Set commands in the default command field.</li>
144 <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>
145 <li>alter mailhandler settings (default content type and cron threshold) at <a href="@admin-mailhandler-settings">Administer &gt;&gt; Site configuration &gt;&gt; Mailhandler</a>.</li>
146 </ul>',
147 array(
148 '@run-cron' => url('admin/logs/status/run-cron'),
149 '@admin-mailhandler-add' => url('admin/content/mailhandler/add'),
150 '@admin-mailhandler' => url('admin/content/mailhandler'),
151 '@admin-mailhandler-settings' => url('admin/settings/mailhandler'),
152 ));
153 $output .= '<h3 id="commands">'. t('Useful Commands') .'</h3>';
154 $output .= $commands_list;
155 $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>';
156 return $output;
157 case 'admin/content/mailhandler':
158 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.');
159 case 'admin/content/mailhandler/add':
160 return t('Add a mailbox whose mail you wish to import into Drupal. Can be IMAP, POP3, or local folder.');
161 case 'admin/content/mailhandler/edit/%':
162 return t('Edit the mailbox whose mail you wish to import into Drupal. Can be IMAP, POP3, or local folder.');
163 case 'admin/settings/mailhandler':
164 return t('The mailhandler module allows registered users to create or edit nodes and comments via e-mail.');
165 }
166 }
167
168
169 /**
170 * Implementation of hook_init to add mailhandler.css
171 */
172 function mailhandler_init() {
173 drupal_add_css(drupal_get_path('module', 'mailhandler') .'/mailhandler.css');
174 return;
175 }
176
177
178 // The following functions are called by both mailhandler.admin.inc and mailhandler.retrieve.inc so they
179 // are defined here to make them available to both
180
181 /**
182 * Establish a connection to the mailbox specified by the array $mailbox
183 */
184 function mailhandler_open_mailbox($mailbox) {
185
186 if ($mailbox['domain']) {
187 if ($mailbox['imap'] == 1) {
188 $box = '{'. $mailbox['domain'] .':'. $mailbox['port'] . $mailbox['extraimap'] .'}'. $mailbox['folder'];
189 }
190 else {
191 $box = '{'. $mailbox['domain'] .':'. $mailbox['port'] .'/pop3'. $mailbox['extraimap'] .'}'. $mailbox['folder'];
192 }
193 $result = imap_open($box, $mailbox['name'], $mailbox['pass']);
194 $err = 'domain';
195 }
196 else {
197 $box = $mailbox['folder'];
198 $result = imap_open($box, '', '');
199 }
200
201 return $result;
202
203 }
204
205
206 /**
207 * Fetch data for a specific mailbox from the database.
208 */
209 function mailhandler_get_mailbox($mid) {
210 return db_fetch_array(db_query("SELECT * FROM {mailhandler} WHERE mid = %d", $mid));
211 }
212
213
214 /**
215 * Return a default content type if the user has not chosen a specific type on the settings page
216 * In order of priority, return blog, story, page
217 * This assumes that one of these basic types is in use on a site (page and story are active by default)
218 * A user can choose other content types via the settings page as this exposes all available types
219 */
220 function mailhandler_default_type() {
221
222 // Get the current default setting, if defined
223 $default_type = variable_get('mailhandler_default_type', NULL);
224
225 // Find out what types are available
226 $available_types = node_get_types('names');
227
228 // Check the default type is still available (it could have been deleted)
229 if ($default_type && array_key_exists($default_type, $available_types)) {
230 return $default_type;
231 }
232
233 // If we get here then either no default is set, or the default type is no longer available
234
235 // Search for the array key (the machine readable name) for blog, story and page basic types
236 if (array_key_exists('blog', $available_types)) {
237 $default_type = 'blog';
238 }
239 else if (array_key_exists('story', $available_types)) {
240 $default_type = 'story';
241 }
242 else if (array_key_exists('page', $available_types)) {
243 $default_type = 'page';
244 }
245 else {
246 // If basic types not found then return the first item from the array as an alternative default
247 $default_type = key($available_types);
248 }
249
250 return $default_type;
251
252 }

  ViewVC Help
Powered by ViewVC 1.1.2