imsettings.php 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. <?php
  2. /**
  3. * StatusNet, the distributed open-source microblogging tool
  4. *
  5. * Settings for Jabber/XMPP integration
  6. *
  7. * PHP version 5
  8. *
  9. * LICENCE: This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as published by
  11. * the Free Software Foundation, either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. * @category Settings
  23. * @package StatusNet
  24. * @author Evan Prodromou <evan@status.net>
  25. * @copyright 2008-2009 StatusNet, Inc.
  26. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  27. * @link http://status.net/
  28. */
  29. if (!defined('GNUSOCIAL')) { exit(1); }
  30. /**
  31. * Settings for Jabber/XMPP integration
  32. *
  33. * @category Settings
  34. * @package StatusNet
  35. * @author Evan Prodromou <evan@status.net>
  36. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
  37. * @link http://status.net/
  38. *
  39. * @see SettingsAction
  40. */
  41. class ImsettingsAction extends SettingsAction
  42. {
  43. /**
  44. * Title of the page
  45. *
  46. * @return string Title of the page
  47. */
  48. function title()
  49. {
  50. // TRANS: Title for Instant Messaging settings.
  51. return _('IM settings');
  52. }
  53. /**
  54. * Instructions for use
  55. *
  56. * @return instructions for use
  57. */
  58. function getInstructions()
  59. {
  60. // TRANS: Instant messaging settings page instructions.
  61. // TRANS: [instant messages] is link text, "(%%doc.im%%)" is the link.
  62. // TRANS: the order and formatting of link text and link should remain unchanged.
  63. return _('You can send and receive notices through '.
  64. '[instant messaging](%%doc.im%%). '.
  65. 'Configure your addresses and settings below.');
  66. }
  67. /**
  68. * Content area of the page
  69. *
  70. * We make different sections of the form for the different kinds of
  71. * functions, and have submit buttons with different names. These
  72. * are muxed by handlePost() to see what the user really wants to do.
  73. *
  74. * @return void
  75. */
  76. function showContent()
  77. {
  78. $transports = array();
  79. Event::handle('GetImTransports', array(&$transports));
  80. if (! $transports) {
  81. $this->element('div', array('class' => 'error'),
  82. // TRANS: Message given in the IM settings if IM is not enabled on the site.
  83. _('IM is not available.'));
  84. return;
  85. }
  86. $user = common_current_user();
  87. $user_im_prefs_by_transport = array();
  88. foreach($transports as $transport=>$transport_info)
  89. {
  90. $this->elementStart('form', array('method' => 'post',
  91. 'id' => 'form_settings_im',
  92. 'class' => 'form_settings',
  93. 'action' =>
  94. common_local_url('imsettings')));
  95. $this->elementStart('fieldset', array('id' => 'settings_im_address'));
  96. // TRANS: Form legend for IM settings form.
  97. $this->element('legend', null, $transport_info['display']);
  98. $this->hidden('token', common_session_token());
  99. $this->hidden('transport', $transport);
  100. if ($user_im_prefs = User_im_prefs::pkeyGet( array('transport' => $transport, 'user_id' => $user->id) )) {
  101. $user_im_prefs_by_transport[$transport] = $user_im_prefs;
  102. $this->element('p', 'form_confirmed', $user_im_prefs->screenname);
  103. $this->element('p', 'form_note',
  104. // TRANS: Form note in IM settings form. %s is the type of IM address that was confirmed.
  105. sprintf(_('Current confirmed %s address.'),$transport_info['display']));
  106. $this->hidden('screenname', $user_im_prefs->screenname);
  107. // TRANS: Button label to remove a confirmed IM address.
  108. $this->submit('remove', _m('BUTTON','Remove'));
  109. } else {
  110. try {
  111. $confirm = $this->getConfirmation($transport);
  112. $this->element('p', 'form_unconfirmed', $confirm->address);
  113. // TRANS: Form note in IM settings form.
  114. $this->element('p', 'form_note',
  115. // TRANS: Form note in IM settings form.
  116. // TRANS: %s is the IM service name, %2$s is the IM address set.
  117. sprintf(_('Awaiting confirmation on this address. '.
  118. 'Check your %1$s account for a '.
  119. 'message with further instructions. '.
  120. '(Did you add %2$s to your buddy list?)'),
  121. $transport_info['display'],
  122. $transport_info['daemonScreenname']));
  123. $this->hidden('screenname', $confirm->address);
  124. // TRANS: Button label to cancel an IM address confirmation procedure.
  125. $this->submit('cancel', _m('BUTTON','Cancel'));
  126. } catch (NoResultException $e) {
  127. $this->elementStart('ul', 'form_data');
  128. $this->elementStart('li');
  129. // TRANS: Field label for IM address.
  130. $this->input('screenname', _('IM address'),
  131. ($this->arg('screenname')) ? $this->arg('screenname') : null,
  132. // TRANS: Field title for IM address. %s is the IM service name.
  133. sprintf(_('%s screenname.'),
  134. $transport_info['display']));
  135. $this->elementEnd('li');
  136. $this->elementEnd('ul');
  137. // TRANS: Button label for adding an IM address in IM settings form.
  138. $this->submit('add', _m('BUTTON','Add'));
  139. }
  140. }
  141. $this->elementEnd('fieldset');
  142. $this->elementEnd('form');
  143. }
  144. if($user_im_prefs_by_transport)
  145. {
  146. $this->elementStart('form', array('method' => 'post',
  147. 'id' => 'form_settings_im',
  148. 'class' => 'form_settings',
  149. 'action' =>
  150. common_local_url('imsettings')));
  151. $this->elementStart('fieldset', array('id' => 'settings_im_preferences'));
  152. // TRANS: Header for IM preferences form.
  153. $this->element('legend', null, _('IM Preferences'));
  154. $this->hidden('token', common_session_token());
  155. $this->elementStart('table');
  156. $this->elementStart('tr');
  157. foreach($user_im_prefs_by_transport as $transport=>$user_im_prefs)
  158. {
  159. $this->element('th', null, $transports[$transport]['display']);
  160. }
  161. $this->elementEnd('tr');
  162. $preferences = array(
  163. // TRANS: Checkbox label in IM preferences form.
  164. array('name'=>'notify', 'description'=>_('Send me notices')),
  165. // TRANS: Checkbox label in IM preferences form.
  166. array('name'=>'updatefrompresence', 'description'=>_('Post a notice when my status changes.')),
  167. // TRANS: Checkbox label in IM preferences form.
  168. array('name'=>'replies', 'description'=>_('Send me replies '.
  169. 'from people I\'m not subscribed to.')),
  170. );
  171. foreach($preferences as $preference)
  172. {
  173. $this->elementStart('tr');
  174. foreach($user_im_prefs_by_transport as $transport=>$user_im_prefs)
  175. {
  176. $preference_name = $preference['name'];
  177. $this->elementStart('td');
  178. $this->checkbox($transport . '_' . $preference['name'],
  179. $preference['description'],
  180. $user_im_prefs->$preference_name);
  181. $this->elementEnd('td');
  182. }
  183. $this->elementEnd('tr');
  184. }
  185. $this->elementEnd('table');
  186. // TRANS: Button label to save IM preferences.
  187. $this->submit('save', _m('BUTTON','Save'));
  188. $this->elementEnd('fieldset');
  189. $this->elementEnd('form');
  190. }
  191. }
  192. /**
  193. * Get a confirmation code for this user
  194. *
  195. * @return Confirm_address address object for this user
  196. */
  197. function getConfirmation($transport)
  198. {
  199. $confirm = new Confirm_address();
  200. $confirm->user_id = $this->scoped->getID();
  201. $confirm->address_type = $transport;
  202. if ($confirm->find(true)) {
  203. return $confirm;
  204. }
  205. throw new NoResultException($confirm);
  206. }
  207. protected function doPost()
  208. {
  209. if ($this->arg('save')) {
  210. return $this->savePreferences();
  211. } else if ($this->arg('add')) {
  212. return $this->addAddress();
  213. } else if ($this->arg('cancel')) {
  214. return $this->cancelConfirmation();
  215. } else if ($this->arg('remove')) {
  216. return $this->removeAddress();
  217. }
  218. // TRANS: Message given submitting a form with an unknown action in Instant Messaging settings.
  219. throw new ClientException(_('Unexpected form submission.'));
  220. }
  221. /**
  222. * Save user's XMPP preferences
  223. *
  224. * These are the checkboxes at the bottom of the page. They're used to
  225. * set different settings
  226. *
  227. * @return void
  228. */
  229. function savePreferences()
  230. {
  231. $user_im_prefs = new User_im_prefs();
  232. $user_im_prefs->query('BEGIN');
  233. $user_im_prefs->user_id = $this->scoped->getID();
  234. if($user_im_prefs->find() && $user_im_prefs->fetch())
  235. {
  236. $preferences = array('notify', 'updatefrompresence', 'replies');
  237. do
  238. {
  239. $original = clone($user_im_prefs);
  240. $new = clone($user_im_prefs);
  241. foreach($preferences as $preference)
  242. {
  243. $new->$preference = $this->boolean($new->transport . '_' . $preference);
  244. }
  245. $result = $new->update($original);
  246. if ($result === false) {
  247. common_log_db_error($user_im_prefs, 'UPDATE', __FILE__);
  248. // TRANS: Server error thrown on database error updating IM preferences.
  249. throw new ServerException(_('Could not update IM preferences.'));
  250. }
  251. }while($user_im_prefs->fetch());
  252. }
  253. $user_im_prefs->query('COMMIT');
  254. // TRANS: Confirmation message for successful IM preferences save.
  255. return _('Preferences saved.');
  256. }
  257. /**
  258. * Sends a confirmation to the address given
  259. *
  260. * Stores a confirmation record and sends out a
  261. * message with the confirmation info.
  262. *
  263. * @return void
  264. */
  265. function addAddress()
  266. {
  267. $screenname = $this->trimmed('screenname');
  268. $transport = $this->trimmed('transport');
  269. // Some validation
  270. if (empty($screenname)) {
  271. // TRANS: Message given saving IM address without having provided one.
  272. throw new ClientException(_('No screenname.'));
  273. }
  274. if (empty($transport)) {
  275. // TRANS: Form validation error when no transport is available setting an IM address.
  276. throw new ClientException(_('No transport.'));
  277. }
  278. Event::handle('NormalizeImScreenname', array($transport, &$screenname));
  279. if (empty($screenname)) {
  280. // TRANS: Message given saving IM address that cannot be normalised.
  281. throw new ClientException(_('Cannot normalize that screenname.'));
  282. }
  283. $valid = false;
  284. Event::handle('ValidateImScreenname', array($transport, $screenname, &$valid));
  285. if (!$valid) {
  286. // TRANS: Message given saving IM address that not valid.
  287. throw new ClientException(_('Not a valid screenname.'));
  288. } else if ($this->screennameExists($transport, $screenname)) {
  289. // TRANS: Message given saving IM address that is already set for another user.
  290. throw new ClientException(_('Screenname already belongs to another user.'));
  291. }
  292. $confirm = new Confirm_address();
  293. $confirm->address = $screenname;
  294. $confirm->address_type = $transport;
  295. $confirm->user_id = $this->scoped->getID();
  296. $confirm->code = common_confirmation_code(64);
  297. $confirm->sent = common_sql_now();
  298. $confirm->claimed = common_sql_now();
  299. $result = $confirm->insert();
  300. if ($result === false) {
  301. common_log_db_error($confirm, 'INSERT', __FILE__);
  302. // TRANS: Server error thrown on database error adding Instant Messaging confirmation code.
  303. $this->serverError(_('Could not insert confirmation code.'));
  304. }
  305. Event::handle('SendImConfirmationCode', array($transport, $screenname, $confirm->code, $this->scoped));
  306. // TRANS: Message given saving valid IM address that is to be confirmed.
  307. return _('A confirmation code was sent to the IM address you added.');
  308. }
  309. /**
  310. * Cancel a confirmation
  311. *
  312. * If a confirmation exists, cancel it.
  313. *
  314. * @return void
  315. */
  316. function cancelConfirmation()
  317. {
  318. $screenname = $this->trimmed('screenname');
  319. $transport = $this->trimmed('transport');
  320. try {
  321. $confirm = $this->getConfirmation($transport);
  322. if ($confirm->address != $screenname) {
  323. // TRANS: Message given canceling IM address confirmation for the wrong IM address.
  324. throw new ClientException(_('That is the wrong IM address.'));
  325. }
  326. } catch (NoResultException $e) {
  327. // TRANS: Message given canceling Instant Messaging address confirmation that is not pending.
  328. throw new AlreadyFulfilledException(_('No pending confirmation to cancel.'));
  329. }
  330. $confirm->delete();
  331. // TRANS: Message given after successfully canceling IM address confirmation.
  332. return _('IM confirmation cancelled.');
  333. }
  334. /**
  335. * Remove an address
  336. *
  337. * If the user has a confirmed address, remove it.
  338. *
  339. * @return void
  340. */
  341. function removeAddress()
  342. {
  343. $screenname = $this->trimmed('screenname');
  344. $transport = $this->trimmed('transport');
  345. // Maybe an old tab open...?
  346. $user_im_prefs = new User_im_prefs();
  347. $user_im_prefs->user_id = $this->scoped->getID();
  348. $user_im_prefs->transport = $transport;
  349. if (!$user_im_prefs->find(true)) {
  350. // TRANS: Message given trying to remove an IM address that is not
  351. // TRANS: registered for the active user.
  352. throw new AlreadyFulfilledException(_('There were no preferences stored for this transport.'));
  353. }
  354. $result = $user_im_prefs->delete();
  355. if ($result === false) {
  356. common_log_db_error($user_im_prefs, 'UPDATE', __FILE__);
  357. // TRANS: Server error thrown on database error removing a registered IM address.
  358. throw new ServerException(_('Could not update user IM preferences.'));
  359. }
  360. // XXX: unsubscribe to the old address
  361. // TRANS: Message given after successfully removing a registered Instant Messaging address.
  362. return _('The IM address was removed.');
  363. }
  364. /**
  365. * Does this screenname exist?
  366. *
  367. * Checks if we already have another user with this address.
  368. *
  369. * @param string $transport Transport to check
  370. * @param string $screenname Screenname to check
  371. *
  372. * @return boolean whether the screenname exists
  373. */
  374. function screennameExists($transport, $screenname)
  375. {
  376. $user_im_prefs = new User_im_prefs();
  377. $user_im_prefs->transport = $transport;
  378. $user_im_prefs->screenname = $screenname;
  379. return $user_im_prefs->find(true) ? true : false;
  380. }
  381. }