/[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.97 - (show annotations) (download) (as text)
Fri May 2 14:54:58 2008 UTC (18 months, 3 weeks ago) by weitzman
Branch: MAIN
CVS Tags: HEAD
Changes since 1.96: +1 -1 lines
File MIME type: text/x-php
FILE REMOVED
Please use the relevant branches. HEAD is not currently in use.
1 <?php
2 // $Id: mailhandler.module,v 1.96 2008/05/02 12:58:16 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', '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 switch ($path) {
110 case 'admin/help#mailhandler':
111 $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>';
112 $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>';
113 $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>';
114 $output .= t('<p>You can</p>
115 <ul>
116 <li><a href="@run-cron">run cron</a> to retrieve messages from all cron enabled mailboxes.</li>
117 <li>list mailboxes at <a href="@admin-mailhandler">Administer &gt;&gt; Content management &gt;&gt; Mailhandler</a>.</li>
118 <li>add a mailbox at <a href="@admin-mailhandler-add">Administer &gt;&gt; Content management &gt;&gt; Mailhandler &gt;&gt; Add mailbox.</a></li>
119 <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>
120 <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>
121 <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>
122 </ul>',
123 array(
124 '@run-cron' => url('admin/logs/status/run-cron'),
125 '@admin-mailhandler-add' => url('admin/content/mailhandler/add'),
126 '@admin-mailhandler' => url('admin/content/mailhandler'),
127 '@admin-mailhandler-settings' => url('admin/settings/mailhandler'),
128 ));
129 $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>';
130 return $output;
131 case 'admin/content/mailhandler':
132 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.');
133 case 'admin/content/mailhandler/add':
134 return t('Add a mailbox whose mail you wish to import into Drupal. Can be IMAP, POP3, or local folder.');
135 case 'admin/content/mailhandler/edit/%':
136 return t('Edit the mailbox whose mail you wish to import into Drupal. Can be IMAP, POP3, or local folder.');
137 case 'admin/settings/mailhandler':
138 return t('The mailhandler module allows registered users to create or edit nodes and comments via e-mail.');
139 }
140 }
141
142
143 /**
144 * Implementation of hook_init to add mailhandler.css
145 */
146 function mailhandler_init() {
147 drupal_add_css(drupal_get_path('module', 'mailhandler') .'/mailhandler.css');
148 return;
149 }
150
151
152 // The following functions are called by both mailhandler.admin.inc and mailhandler.retrieve.inc so they
153 // are defined here to make them available to both
154
155 /**
156 * Establish a connection to the mailbox specified by the array $mailbox
157 */
158 function mailhandler_open_mailbox($mailbox) {
159
160 if ($mailbox['domain']) {
161 if ($mailbox['imap'] == 1) {
162 $box = '{'. $mailbox['domain'] .':'. $mailbox['port'] . $mailbox['extraimap'] .'}'. $mailbox['folder'];
163 }
164 else {
165 $box = '{'. $mailbox['domain'] .':'. $mailbox['port'] .'/pop3'. $mailbox['extraimap'] .'}'. $mailbox['folder'];
166 }
167 $result = imap_open($box, $mailbox['name'], $mailbox['pass']);
168 $err = 'domain';
169 }
170 else {
171 $box = $mailbox['folder'];
172 $result = imap_open($box, '', '');
173 }
174
175 return $result;
176
177 }
178
179
180 /**
181 * Fetch data for a specific mailbox from the database.
182 */
183 function mailhandler_get_mailbox($mid) {
184 return db_fetch_array(db_query("SELECT * FROM {mailhandler} WHERE mid = %d", $mid));
185 }
186
187
188 /**
189 * Return a default content type if the user has not chosen a specific type on the settings page
190 * In order of priority, return blog, story, page
191 * This assumes that one of these basic types is in use on a site (page and story are active by default)
192 * A user can choose other content types via the settings page as this exposes all available types
193 */
194 function mailhandler_default_type() {
195
196 // Get the current default setting, if defined
197 $default_type = variable_get('mailhandler_default_type', NULL);
198
199 // Find out what types are available
200 $available_types = node_get_types('names');
201
202 // Check the default type is still available (it could have been deleted)
203 if ($default_type && array_key_exists($default_type, $available_types)) {
204 return $default_type;
205 }
206
207 // If we get here then either no default is set, or the default type is no longer available
208
209 // Search for the array key (the machine readable name) for blog, story and page basic types
210 if (array_key_exists('blog', $available_types)) {
211 $default_type = 'blog';
212 }
213 else if (array_key_exists('story', $available_types)) {
214 $default_type = 'story';
215 }
216 else if (array_key_exists('page', $available_types)) {
217 $default_type = 'page';
218 }
219 else {
220 // If basic types not found then return the first item from the array as an alternative default
221 $default_type = key($available_types);
222 }
223
224 return $default_type;
225
226 }

  ViewVC Help
Powered by ViewVC 1.1.2