GroupPrivateMessagePlugin.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. <?php
  2. /**
  3. * StatusNet - the distributed open-source microblogging tool
  4. * Copyright (C) 2011, StatusNet, Inc.
  5. *
  6. * Private groups for StatusNet 0.9.x
  7. *
  8. * PHP version 5
  9. *
  10. * This program is free software: you can redistribute it and/or modify
  11. * it under the terms of the GNU Affero General Public License as published by
  12. * the Free Software Foundation, either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU Affero General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU Affero General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. *
  23. * @category Privacy
  24. * @package StatusNet
  25. * @author Evan Prodromou <evan@status.net>
  26. * @copyright 2011 StatusNet, Inc.
  27. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  28. * @link http://status.net/
  29. */
  30. if (!defined('STATUSNET')) {
  31. // This check helps protect against security problems;
  32. // your code file can't be executed directly from the web.
  33. exit(1);
  34. }
  35. /**
  36. * Private groups
  37. *
  38. * This plugin allows users to send private messages to a group.
  39. *
  40. * @category Privacy
  41. * @package StatusNet
  42. * @author Evan Prodromou <evan@status.net>
  43. * @copyright 2011 StatusNet, Inc.
  44. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
  45. * @link http://status.net/
  46. */
  47. class GroupPrivateMessagePlugin extends Plugin
  48. {
  49. /**
  50. * Database schema setup
  51. *
  52. * @see Schema
  53. * @see ColumnDef
  54. *
  55. * @return boolean hook value
  56. */
  57. function onCheckSchema()
  58. {
  59. $schema = Schema::get();
  60. // For storing user-submitted flags on profiles
  61. $schema->ensureTable('group_privacy_settings', Group_privacy_settings::schemaDef());
  62. $schema->ensureTable('group_message', Group_message::schemaDef());
  63. $schema->ensureTable('group_message_profile', Group_message_profile::schemaDef());
  64. return true;
  65. }
  66. /**
  67. * Map URLs to actions
  68. *
  69. * @param URLMapper $m path-to-action mapper
  70. *
  71. * @return boolean hook value
  72. */
  73. public function onRouterInitialized(URLMapper $m)
  74. {
  75. $m->connect('group/:nickname/inbox',
  76. array('action' => 'groupinbox'),
  77. array('nickname' => Nickname::DISPLAY_FMT));
  78. $m->connect('group/message/:id',
  79. array('action' => 'showgroupmessage'),
  80. array('id' => '[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}'));
  81. $m->connect('group/:nickname/message/new',
  82. array('action' => 'newgroupmessage'),
  83. array('nickname' => Nickname::DISPLAY_FMT));
  84. return true;
  85. }
  86. /**
  87. * Add group inbox to the menu
  88. *
  89. * @param Action $action The current action handler. Use this to
  90. * do any output.
  91. *
  92. * @return boolean hook value; true means continue processing, false means stop.
  93. *
  94. * @see Action
  95. */
  96. function onEndGroupGroupNav(Menu $groupnav)
  97. {
  98. $action = $groupnav->action;
  99. $group = $groupnav->group;
  100. $action->menuItem(common_local_url('groupinbox',
  101. array('nickname' => $group->nickname)),
  102. // TRANS: Menu item in group page.
  103. _m('MENU','Inbox'),
  104. // TRANS: Menu title in group page.
  105. _m('Private messages for this group.'),
  106. $action->trimmed('action') == 'groupinbox',
  107. 'nav_group_inbox');
  108. return true;
  109. }
  110. /**
  111. * Create default group privacy settings at group create time
  112. *
  113. * @param User_group $group Group that was just created
  114. *
  115. * @result boolean hook value
  116. */
  117. function onEndGroupSave($group)
  118. {
  119. $gps = new Group_privacy_settings();
  120. $gps->group_id = $group->id;
  121. $gps->allow_privacy = Group_privacy_settings::SOMETIMES;
  122. $gps->allow_sender = Group_privacy_settings::MEMBER;
  123. $gps->created = common_sql_now();
  124. $gps->modified = $gps->created;
  125. // This will throw an exception on error
  126. $gps->insert();
  127. return true;
  128. }
  129. /**
  130. * Show group privacy controls on group edit form
  131. *
  132. * @param GroupEditForm $form form being shown
  133. */
  134. function onEndGroupEditFormData(GroupEditForm $form)
  135. {
  136. $gps = null;
  137. if (!empty($form->group)) {
  138. $gps = Group_privacy_settings::getKV('group_id', $form->group->id);
  139. }
  140. $form->out->elementStart('li');
  141. $form->out->dropdown('allow_privacy',
  142. // TRANS: Dropdown label in group settings page for if group allows private messages.
  143. _m('Private messages'),
  144. // TRANS: Dropdown option in group settings page for allowing private messages.
  145. array(Group_privacy_settings::SOMETIMES => _m('Sometimes'),
  146. // TRANS: Dropdown option in group settings page for allowing private messages.
  147. Group_privacy_settings::ALWAYS => _m('Always'),
  148. // TRANS: Dropdown option in group settings page for allowing private messages.
  149. Group_privacy_settings::NEVER => _m('Never')),
  150. // TRANS: Dropdown title in group settings page for if group allows private messages.
  151. _m('Whether to allow private messages to this group.'),
  152. false,
  153. (empty($gps)) ? Group_privacy_settings::SOMETIMES : $gps->allow_privacy);
  154. $form->out->elementEnd('li');
  155. $form->out->elementStart('li');
  156. $form->out->dropdown('allow_sender',
  157. // TRANS: Dropdown label in group settings page for who can send private messages to the group.
  158. _m('Private senders'),
  159. // TRANS: Dropdown option in group settings page for who can send private messages.
  160. array(Group_privacy_settings::EVERYONE => _m('Everyone'),
  161. // TRANS: Dropdown option in group settings page for who can send private messages.
  162. Group_privacy_settings::MEMBER => _m('Member'),
  163. // TRANS: Dropdown option in group settings page for who can send private messages.
  164. Group_privacy_settings::ADMIN => _m('Admin')),
  165. // TRANS: Dropdown title in group settings page for who can send private messages to the group.
  166. _m('Who can send private messages to the group.'),
  167. false,
  168. (empty($gps)) ? Group_privacy_settings::MEMBER : $gps->allow_sender);
  169. $form->out->elementEnd('li');
  170. return true;
  171. }
  172. function onEndGroupSaveForm(Action $action)
  173. {
  174. // The Action class must contain this method
  175. assert(is_callable(array($action, 'getGroup')));
  176. $gps = null;
  177. if ($action->getGroup() instanceof User_group) {
  178. $gps = Group_privacy_settings::getKV('group_id', $action->getGroup()->id);
  179. }
  180. $orig = null;
  181. if (empty($gps)) {
  182. $gps = new Group_privacy_settings();
  183. $gps->group_id = $action->getGroup()->id;
  184. } else {
  185. $orig = clone($gps);
  186. }
  187. $gps->allow_privacy = $action->trimmed('allow_privacy');
  188. $gps->allow_sender = $action->trimmed('allow_sender');
  189. if (empty($orig)) {
  190. $gps->created = common_sql_now();
  191. $gps->insert();
  192. } else {
  193. $gps->update($orig);
  194. }
  195. return true;
  196. }
  197. /**
  198. * Overload 'd' command to send private messages to groups.
  199. *
  200. * 'd !group word word word' will send the private message
  201. * 'word word word' to the group 'group'.
  202. *
  203. * @param string $cmd Command being run
  204. * @param string $arg Rest of the message (including address)
  205. * @param User $user User sending the message
  206. * @param Command &$result The resulting command object to be run.
  207. *
  208. * @return boolean hook value
  209. */
  210. function onStartInterpretCommand($cmd, $arg, User $user, &$result)
  211. {
  212. if ($cmd == 'd' || $cmd == 'dm') {
  213. $this->debug('Got a d command');
  214. // Break off the first word as the address
  215. $pieces = explode(' ', $arg, 2);
  216. if (count($pieces) == 1) {
  217. $pieces[] = null;
  218. }
  219. list($addr, $msg) = $pieces;
  220. if (!empty($addr) && $addr[0] == '!') {
  221. $result = new GroupMessageCommand($user, substr($addr, 1), $msg);
  222. Event::handle('EndInterpretCommand', array($cmd, $arg, $user, $result));
  223. return false;
  224. }
  225. }
  226. return true;
  227. }
  228. /**
  229. * To add a "Message" button to the group profile page
  230. *
  231. * @param Widget $widget The showgroup action being shown
  232. * @param User_group $group The current group
  233. *
  234. * @return boolean hook value
  235. */
  236. function onEndGroupActionsList(Widget $widget, User_group $group)
  237. {
  238. $cur = common_current_user();
  239. $action = $widget->out;
  240. if (empty($cur)) {
  241. return true;
  242. }
  243. try {
  244. Group_privacy_settings::ensurePost($cur, $group);
  245. } catch (Exception $e) {
  246. return true;
  247. }
  248. $action->elementStart('li', 'entity_send-a-message');
  249. $action->element('a', array('href' => common_local_url('newgroupmessage', array('nickname' => $group->nickname)),
  250. // TRANS: Title for action in group actions list.
  251. 'title' => _m('Send a direct message to this group.')),
  252. // TRANS: Link text for action in group actions list to send a private message to a group.
  253. _m('LINKTEXT','Message'));
  254. // $form = new GroupMessageForm($action, $group);
  255. // $form->hidden = true;
  256. // $form->show();
  257. $action->elementEnd('li');
  258. return true;
  259. }
  260. /**
  261. * When saving a notice, check its groups. If any of them has
  262. * privacy == always, force a group private message to all mentioned groups.
  263. * If any of the groups disallows private messages, skip it.
  264. *
  265. * @param
  266. */
  267. function onStartNoticeSave(Notice &$notice) {
  268. // Look for group tags
  269. // FIXME: won't work for remote groups
  270. // @fixme if Notice::saveNew is refactored so we can just pull its list
  271. // of groups between processing and saving, make use of it
  272. $count = preg_match_all('/(?:^|\s)!(' . Nickname::DISPLAY_FMT . ')/',
  273. strtolower($notice->content),
  274. $match);
  275. $groups = array();
  276. $ignored = array();
  277. $forcePrivate = false;
  278. $profile = $notice->getProfile();
  279. if ($count > 0) {
  280. /* Add them to the database */
  281. foreach (array_unique($match[1]) as $nickname) {
  282. $group = User_group::getForNickname($nickname, $profile);
  283. if (empty($group)) {
  284. continue;
  285. }
  286. $gps = Group_privacy_settings::forGroup($group);
  287. switch ($gps->allow_privacy) {
  288. case Group_privacy_settings::ALWAYS:
  289. $forcePrivate = true;
  290. // fall through
  291. case Group_privacy_settings::SOMETIMES:
  292. $groups[] = $group;
  293. break;
  294. case Group_privacy_settings::NEVER:
  295. $ignored[] = $group;
  296. break;
  297. }
  298. }
  299. if ($forcePrivate) {
  300. foreach ($ignored as $group) {
  301. common_log(LOG_NOTICE,
  302. "Notice forced to group direct message ".
  303. "but group ".$group->nickname." does not allow them.");
  304. }
  305. $user = User::getKV('id', $notice->profile_id);
  306. if (empty($user)) {
  307. common_log(LOG_WARNING,
  308. "Notice forced to group direct message ".
  309. "but profile ".$notice->profile_id." is not a local user.");
  310. } else {
  311. foreach ($groups as $group) {
  312. Group_message::send($user, $group, $notice->content);
  313. }
  314. }
  315. // Don't save the notice!
  316. // FIXME: this is probably cheating.
  317. // TRANS: Client exception thrown when a private group message has to be forced.
  318. throw new ClientException(sprintf(_m('Forced notice to private group message.')),
  319. 200);
  320. }
  321. }
  322. return true;
  323. }
  324. /**
  325. * Show an indicator that the group is (essentially) private on the group page
  326. *
  327. * @param Action $action The action being shown
  328. * @param User_group $group The group being shown
  329. *
  330. * @return boolean hook value
  331. */
  332. function onEndGroupProfileElements(Action $action, User_group $group)
  333. {
  334. $gps = Group_privacy_settings::forGroup($group);
  335. if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
  336. // TRANS: Indicator on the group page that the group is (essentially) private.
  337. $action->element('p', 'privategroupindicator', _m('Private'));
  338. }
  339. return true;
  340. }
  341. function onStartShowExportData(Action $action)
  342. {
  343. if ($action instanceof ShowgroupAction) {
  344. $gps = Group_privacy_settings::forGroup($action->getGroup());
  345. if ($gps->allow_privacy == Group_privacy_settings::ALWAYS) {
  346. return false;
  347. }
  348. }
  349. return true;
  350. }
  351. function onPluginVersion(array &$versions)
  352. {
  353. $versions[] = array('name' => 'GroupPrivateMessage',
  354. 'version' => GNUSOCIAL_VERSION,
  355. 'author' => 'Evan Prodromou',
  356. 'homepage' => 'https://git.gnu.io/gnu/gnu-social/tree/master/plugins/GroupPrivateMessage',
  357. 'rawdescription' =>
  358. // TRANS: Plugin description.
  359. _m('Allow posting private messages to groups.'));
  360. return true;
  361. }
  362. }