]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/EmailReminder/scripts/sendemailreminder.php
EmailReminder plugin to send reminders about various things
[quix0rs-gnu-social.git] / plugins / EmailReminder / scripts / sendemailreminder.php
1 #!/usr/bin/env php
2 <?php
3 /*
4 * StatusNet - a distributed open-source microblogging tool
5 * Copyright (C) 2011, StatusNet, Inc.
6 *
7 * This program is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU Affero General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU Affero General Public License for more details.
16 *
17 * You should have received a copy of the GNU Affero General Public License
18 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 define('INSTALLDIR', realpath(dirname(__FILE__) . '/../../..'));
22
23 $shortoptions = 't:e:au';
24 $longoptions = array('type=', 'email=', 'all', 'universe');
25
26 $helptext = <<<END_OF_SENDEMAILREMINDER_HELP
27 sendemailreminder.php [options]
28 Send an email summary of the inbox to users
29
30  -t --type     type of reminder to send (register | invite | all)
31  -e --email    email address to send reminder to
32  -a --all      send reminder to all addresses
33  -u --universe send reminder to all addresses on all sites
34
35 END_OF_SENDEMAILREMINDER_HELP;
36
37 require_once INSTALLDIR . '/scripts/commandline.inc';
38
39 $quiet = have_option('q', 'quiet');
40
41 $types = array(
42     // registration confirmation reminder
43     'register' => array(
44         'type'       => 'register',
45         'className'  => 'Confirm_address',
46         'utransport' => 'uregrem'
47      ),
48     // invitation confirmation reminder
49     'invite'   => array(
50         'type'       => 'invite',
51         'className'  => 'Invitation',
52         'utransport' => 'uinvrem'
53     )
54     // ... add more here
55 );
56
57 $type = null;
58
59 if (have_option('t', 'type')) {
60     $type = trim(get_option_value('t', 'type'));
61     if (!in_array($type, array_keys($types)) && $type !== 'all') {
62        print _m("Unknown reminder type: {$type}.\n");
63        exit(1);
64     }
65 } else {
66    show_help();
67    exit(1);
68 }
69
70 $reminders = array();
71
72 switch($type) {
73 case 'register':
74     $reminders[] = $types['register'];
75     break;
76 case 'invite':
77     $reminders[] = $types['invite'];
78     break;
79 case 'all':
80     $reminders = $types;
81     break;
82 }
83
84 if (have_option('u', 'universe')) {
85     $sn = new Status_network();
86     if ($sn->find()) {
87         while ($sn->fetch()) {
88             $server = $sn->getServerName();
89             StatusNet::init($server);
90             // Different queue manager, maybe!
91             $qm = QueueManager::get();
92             foreach ($reminders as $reminder) {
93                 extract($reminder);
94                 $qm->enqueue($type, 'siterem');
95                 if (!$quiet) { print "Sent pending {$type} reminders to all unconfirmed addresses in the known universe.\n"; }
96             }
97         }
98     }
99 } else {
100     $qm = QueueManager::get();
101     try {
102         // enqueue reminder for specific email address or all unconfirmed addresses
103         if (have_option('e', 'email')) {
104             $address = trim(get_option_value('e', 'email'));
105             foreach ($reminders as $reminder) {
106                 // real bad voodoo here
107                 extract($reminder);
108                 $confirm = new $className;
109                 $confirm->address = $address;
110                 $result = $confirm->find(true);
111                 if (empty($result)) {
112                     throw new Exception("No confirmation code found for {$address}.");
113                 }
114                 $qm->enqueue($confirm, $utransport);
115                 if (!$quiet) { print "Sent all pending {$type} reminder to {$address}.\n"; }
116             }
117         } else if (have_option('a', 'all')) {
118             foreach ($reminders as $reminder) {
119                 extract($reminder);
120                 $qm->enqueue($type, 'siterem');
121                 if (!$quiet) { print "Sent pending {$type} reminders to all unconfirmed addresses on the site.\n"; }
122             }
123         } else {
124             show_help();
125             exit(1);
126         }
127     } catch (Exception $e) {
128         if (!$quiet) { print $e->getMessage() . "\n"; }
129         common_log(LOG_ERR, $e->getMessage(), __FILE__);
130         exit(1);
131     }
132 }