/[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.2 - (hide annotations) (download) (as text)
Sat Aug 9 11:54:43 2008 UTC (15 months, 2 weeks ago) by weitzman
Branch: DRUPAL-6--1
CVS Tags: DRUPAL-6--1-4
Changes since 1.96.2.1: +18 -1 lines
File MIME type: text/x-php
#159853 by z.stolar. free tagging support.
1 killes 1.76 <?php
2 weitzman 1.96.2.2 // $Id: mailhandler.module,v 1.96.2.1 2008/05/15 10:43:55 weitzman Exp $
3 weitzman 1.93
4 weitzman 1.96
5 killes 1.76 /**
6 weitzman 1.96 * Implementation of hook_cron(). Process msgs from all enabled mailboxes.
7 killes 1.76 */
8 weitzman 1.96 function mailhandler_cron() {
9    
10     // Include mailhandler retrieval functions
11 weitzman 1.96.2.1 module_load_include('inc', 'mailhandler', 'mailhandler.retrieve');
12 killes 1.76
13 weitzman 1.96 // 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 killes 1.76 }
18    
19 weitzman 1.96 }
20 killes 1.76
21    
22     /**
23 weitzman 1.96 * Implementation of hook_perm().
24 killes 1.76 */
25 weitzman 1.96 function mailhandler_perm() {
26     return array('administer mailhandler');
27 killes 1.76 }
28    
29    
30     /**
31 weitzman 1.96 * Implementation of hook_menu().
32 killes 1.76 */
33 weitzman 1.96 function mailhandler_menu() {
34 killes 1.76
35 weitzman 1.96 $items = array();
36 killes 1.76
37 weitzman 1.96 $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 killes 1.76
98 weitzman 1.96 return $items;
99 killes 1.76 }
100    
101    
102     /**
103 weitzman 1.96 * Implementation of hook_help().
104 killes 1.76 */
105 weitzman 1.96 function mailhandler_help($path = 'admin/help#mailhandler', $arg) {
106     $output = '';
107     $link->add = l(t('Add mailbox'), 'admin/content/mailhandler/add');
108 killes 1.76
109 weitzman 1.96.2.2 // 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 weitzman 1.96 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 &gt;&gt; Content management &gt;&gt; Mailhandler</a>.</li>
133     <li>add a mailbox at <a href="@admin-mailhandler-add">Administer &gt;&gt; Content management &gt;&gt; Mailhandler &gt;&gt; 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 &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>
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 &gt;&gt; Site configuration &gt;&gt; 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 weitzman 1.96.2.2 $output .= '<h3 id="commands">'. t('Useful Commands') .'</h3>';
145     $output .= $commands_list;
146 weitzman 1.96 $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 killes 1.76 }
157     }
158    
159    
160     /**
161 weitzman 1.96 * Implementation of hook_init to add mailhandler.css
162 killes 1.76 */
163 weitzman 1.96 function mailhandler_init() {
164     drupal_add_css(drupal_get_path('module', 'mailhandler') .'/mailhandler.css');
165     return;
166 killes 1.76 }
167    
168    
169 weitzman 1.96 // 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 weitzman 1.92
172     /**
173 weitzman 1.96 * Establish a connection to the mailbox specified by the array $mailbox
174 weitzman 1.92 */
175 weitzman 1.96 function mailhandler_open_mailbox($mailbox) {
176 weitzman 1.92
177 weitzman 1.96 if ($mailbox['domain']) {
178     if ($mailbox['imap'] == 1) {
179     $box = '{'. $mailbox['domain'] .':'. $mailbox['port'] . $mailbox['extraimap'] .'}'. $mailbox['folder'];
180 weitzman 1.92 }
181 weitzman 1.96 else {
182     $box = '{'. $mailbox['domain'] .':'. $mailbox['port'] .'/pop3'. $mailbox['extraimap'] .'}'. $mailbox['folder'];
183 weitzman 1.92 }
184 weitzman 1.96 $result = imap_open($box, $mailbox['name'], $mailbox['pass']);
185     $err = 'domain';
186 weitzman 1.92 }
187 weitzman 1.94 else {
188 weitzman 1.96 $box = $mailbox['folder'];
189     $result = imap_open($box, '', '');
190 weitzman 1.94 }
191 weitzman 1.92
192 weitzman 1.96 return $result;
193 weitzman 1.92
194 weitzman 1.80 }
195    
196 killes 1.76
197     /**
198 weitzman 1.96 * Fetch data for a specific mailbox from the database.
199 killes 1.76 */
200 weitzman 1.96 function mailhandler_get_mailbox($mid) {
201     return db_fetch_array(db_query("SELECT * FROM {mailhandler} WHERE mid = %d", $mid));
202 weitzman 1.80 }
203 killes 1.76
204    
205 weitzman 1.80 /**
206 weitzman 1.96 * 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 weitzman 1.80 */
211 weitzman 1.96 function mailhandler_default_type() {
212 killes 1.76
213 weitzman 1.96 // Get the current default setting, if defined
214     $default_type = variable_get('mailhandler_default_type', NULL);
215 killes 1.76
216 weitzman 1.96 // Find out what types are available
217     $available_types = node_get_types('names');
218 weitzman 1.80
219 weitzman 1.96 // 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 killes 1.76 }
223 weitzman 1.80
224 weitzman 1.96 // If we get here then either no default is set, or the default type is no longer available
225 killes 1.76
226 weitzman 1.96 // 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 killes 1.76 }
230 weitzman 1.96 else if (array_key_exists('story', $available_types)) {
231     $default_type = 'story';
232 killes 1.76 }
233 weitzman 1.96 else if (array_key_exists('page', $available_types)) {
234     $default_type = 'page';
235 killes 1.76 }
236     else {
237 weitzman 1.96 // If basic types not found then return the first item from the array as an alternative default
238     $default_type = key($available_types);
239 weitzman 1.80 }
240 chriskennedy 1.87
241 weitzman 1.96 return $default_type;
242 killes 1.76
243 chriskennedy 1.87 }

  ViewVC Help
Powered by ViewVC 1.1.2