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

Contents of /contributions/modules/mm2/mm2.module

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


Revision 1.3 - (show annotations) (download) (as text)
Sat Apr 26 18:35:50 2008 UTC (19 months ago) by lordrich
Branch: MAIN
CVS Tags: HEAD
Changes since 1.2: +52 -26 lines
File MIME type: text/x-php
Added xml-rpc.patch for Mailman, tidied up the creation and editing of lists, enabled deletion of lists, internationalised mm2.module.
1 <?php
2 /*
3 * Author: lordrich@riseup.net
4 *
5 * Description: Drupal interface for Mailman XMLRPC server
6 *
7 * @todo: Actually create lists on the mailman server, add in correct fields for form creation, test
8 */
9
10 function mm2_node_info() {
11 return array(
12 'mm2'=> array(
13 'name' => t('Mailman List'),
14 'module' => 'mm2',
15 'description' => t('Create a new Mailman list'),
16 )
17 );
18 }
19
20 function mm2_menu() {
21 $items = array();
22
23 $items[] = array(
24 'path' => 'admin/settings/mm2',
25 'title' => t('Mailman2 module settings'),
26 'description' => t('Setup the Mailman 2 XMLRPC server'),
27 'callback' => 'drupal_get_form',
28 'callback arguments' => 'mm2_admin',
29 'access' => user_access('access administration pages'),
30 'type' => MENU_NORMAL_ITEM,
31 );
32
33 return $items;
34 }
35
36 function mm2_perm() {
37 return array('create mm2', 'edit own mm2');
38 }
39
40 function mm2_access($op, $node) {
41 global $user;
42
43 if ($op == 'create') {
44 // Only users with permission to do so may create this node type.
45 return user_access('create mm2');
46 }
47
48 if ($op == 'update' || $op =='delete') {
49 if (user_access('edit own mm2') && ($user->uid == $node->uid)) {
50 return TRUE;
51 }
52 }
53 }
54
55 function mm2_form(&$node) {
56 $type = node_get_types('type', $node);
57 $options = array('1' => t('Enabled'), '0' => t('Disabled'));
58
59 // We need to define form elements for the node's title and body.
60 if ($node->nid) {
61 $form['title'] = array(
62 '#disabled' => true,
63 '#type' => 'textfield',
64 '#title' => check_plain($type->title_label),
65 '#required' => TRUE,
66 '#default_value' => $node->title,
67 '#weight' => -5
68 );
69 $form['moderated'] = array(
70 '#disabled' => true,
71 '#type' => 'radios',
72 '#title' => t('Moderation'),
73 '#default_value' => $node->moderated,
74 '#options' => $options,
75 '#description' => t('Should new subscribers to the list be moderated by default?')
76 );
77 // disabled owner field
78 $form['owner'] = array(
79 '#disabled' => TRUE,
80 '#type' => 'textfield',
81 '#title' => t('Email address of list owner'),
82 '#required' => TRUE,
83 '#default_value' => $node->owner,
84 '#description' => t('The email address of the list owner, this must exist in order to send you the list password.')
85 );
86
87 } else {
88 $form['title'] = array(
89 '#type' => 'textfield',
90 '#title' => check_plain($type->title_label),
91 '#required' => TRUE,
92 '#default_value' => $node->title,
93 '#weight' => -5
94 );
95 $form['moderated'] = array(
96 '#type' => 'radios',
97 '#title' => t('Moderation'),
98 '#default_value' => isset($node->moderated) ? $node->moderated : 0,
99 '#options' => $options,
100 '#description' => t('Should new subscribers to the list be moderated by default?')
101 );
102 }
103 $form['owner'] = array( // old owner field - delete
104 '#type' => 'textfield',
105 '#title' => t('Email address of list owner'),
106 '#required' => TRUE,
107 '#default_value' => isset($node->owner) ? $node->owner : '',
108 '#description' => t('The email address of the list owner, this must exist in order to send you the list password.')
109 );
110
111 // We want the body and filter elements to be adjacent
112 $form['body_filter']['body'] = array(
113 '#type' => 'textarea',
114 '#title' => check_plain($type->body_label),
115 '#default_value' => $node->body,
116 '#required' => FALSE
117 );
118 $form['body_filter']['filter'] = filter_form($node->format);
119
120 return $form;
121 }
122
123 function mm2_admin() {
124 $form['mm2_url'] = array(
125 '#type' => 'textfield',
126 '#title' => t('URL of the Mailman XMLRPC server'),
127 '#default_value' => variable_get('mm2_url', ''),
128 '#description' => t('The url ending in RPC2.')
129 );
130
131 $form['mm2_password'] = array(
132 '#type' => 'password',
133 '#title' => t('Password for the user mailman'),
134 '#default_value' => variable_get('mm2_password', ''),
135 '#description' => t('The Mailman site administrator password.')
136 );
137
138 return system_settings_form($form);
139 }
140
141 function mm2_insert($node) {
142 db_query("INSERT INTO {node_mm2} (vid,nid,owner,moderated) VALUES (%d, %d, '%s', %d)", $node->vid, $node->nid, $node->owner, $node->moderated);
143 }
144
145 function mm2_validate(&$node) {
146 // check owner is a valid email address
147 if (!valid_email_address(trim($node->owner))) {
148 form_set_error('owner', 'Invalid owner email address');
149 }
150
151 // check title is a valid mailman listname
152 $user = '[a-zA-Z0-9_\-\.\+\^!#\$%&*+\/\=\?\`\|\{\}~\']+';
153 if (!preg_match("/^$user$/", $node->title)) {
154 form_set_error('title', 'Invalid Mailman listname');
155 }
156 if ($node->nid) {
157 // list has already been created, do nothing but let users change the body if they must
158 } else {
159 // list hasn't already been created
160 // generate a list password
161 $password = user_password(8);
162 $choppedhostname = parse_url(variable_get('mm2_url', ''));
163 $hostname = $choppedhostname[host];
164 $result = xmlrpc(variable_get('mm2_url', ''), 'Mailman.createList', variable_get('mm2_password', ''), $node->title, $hostname, $hostname, $node->moderated, $node->owner, $password, "en");
165 if ($result === FALSE) {
166 drupal_set_message(t('Error %code: %message', array('%code' => xmlrpc_errno(), '%message' => xmlrpc_error_msg())), 'error');
167 form_set_error('title', 'problem talking to the xmlrpc server');
168 }
169 $body = "Hi $node->owner,\n";
170 $body .= "Your new list $node->title has been created with the password $password.\n";
171 $body .= "You can log in to Mailman at http://$hostname/cgi-bin/mailman/admin/$node->title";
172 drupal_mail('new-mm2-list', "$node->owner, mailman@$hostname", 'New Mailman List', $body, "mailman@$hostname");
173 }
174 }
175
176 function mm2_delete($node) {
177 // @todo add in xmlrpc function to delete list
178 // Mailman's XMLRPC delete function is currently broken, so alert the user to this
179 $result = xmlrpc(variable_get('mm2_url', ''), 'Mailman.deleteList', $node->title, variable_get('mm2_password', ''), 'False');
180 if ($result === FALSE) {
181 drupal_set_message(t('Error %code: %message', array('%code' => xmlrpc_errno(), '%message' => xmlrpc_error_msg())), 'error');
182 } else {
183 db_query('DELETE FROM {node_mm2} WHERE nid = %d', $node->nid);
184 drupal_set_message('The list has been deleted, please contact the administrators if you want the archives removing as well', 'info');
185 }
186 }
187
188 function mm2_load($node) {
189 $additions = db_fetch_object(db_query('SELECT owner, moderated FROM {node_mm2} WHERE vid = %d', $node->vid));
190 return $additions;
191 }
192
193 function mm2_view($node, $teaser = FALSE, $page = FALSE) {
194 $node = node_prepare($node, $teaser);
195 return $node;
196 }
197 ?>

  ViewVC Help
Powered by ViewVC 1.1.2