]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/EmailReminder/scripts/sendemailreminder.php
Add ability to send special one-time reminders
[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:auo';
24 $longoptions = array('type=', 'email=', 'all', 'universe', 'onetime');
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  -o --onetime  send one-time reminder to older addresses
35
36 END_OF_SENDEMAILREMINDER_HELP;
37
38 require_once INSTALLDIR . '/scripts/commandline.inc';
39
40 $quiet = have_option('q', 'quiet');
41
42 $types = array(
43     // registration confirmation reminder
44     'register' => array(
45         'type'       => 'register',
46         'className'  => 'Confirm_address',
47         'utransport' => 'uregrem'
48      ),
49     // invitation confirmation reminder
50     'invite'   => array(
51         'type'       => 'invite',
52         'className'  => 'Invitation',
53         'utransport' => 'uinvrem'
54     )
55     // ... add more here
56 );
57
58 $type = null;
59 $opts = array(); // special options like "onetime"
60
61 if (have_option('t', 'type')) {
62     $type = trim(get_option_value('t', 'type'));
63     if (!in_array($type, array_keys($types)) && $type !== 'all') {
64        print _m("Unknown reminder type: {$type}.\n");
65        exit(1);
66     }
67 } else {
68    show_help();
69    exit(1);
70 }
71
72 if (have_option('o', 'onetime')) {
73     $opts['onetime'] = true;
74     if (!$quiet) { print "Special one-time reminder mode.\n"; }
75 }
76
77 $reminders = array();
78
79 switch($type) {
80 case 'register':
81     $reminders[] = $types['register'];
82     break;
83 case 'invite':
84     $reminders[] = $types['invite'];
85     break;
86 case 'all':
87     $reminders = $types;
88     break;
89 }
90
91 if (have_option('u', 'universe')) {
92     $sn = new Status_network();
93     if ($sn->find()) {
94         while ($sn->fetch()) {
95             $server = $sn->getServerName();
96             StatusNet::init($server);
97             // Different queue manager, maybe!
98             $qm = QueueManager::get();
99             foreach ($reminders as $reminder) {
100                 extract($reminder);
101                 $qm->enqueue(array($type, $opts), 'siterem');
102                 if (!$quiet) { print "Sent pending {$type} reminders to all unconfirmed addresses in the known universe.\n"; }
103             }
104         }
105     }
106 } else {
107     $qm = QueueManager::get();
108     try {
109         // enqueue reminder for specific email address or all unconfirmed addresses
110         if (have_option('e', 'email')) {
111             $address = trim(get_option_value('e', 'email'));
112             foreach ($reminders as $reminder) {
113                 // real bad voodoo here
114                 extract($reminder);
115                 $confirm = new $className;
116                 $confirm->address = $address;
117                 $result = $confirm->find(true);
118                 if (empty($result)) {
119                     throw new Exception("No confirmation code found for {$address}.");
120                 }
121                 $qm->enqueue(array($confirm, $opts), $utransport);
122                 if (!$quiet) { print "Sent all pending {$type} reminder to {$address}.\n"; }
123             }
124         } else if (have_option('a', 'all')) {
125             foreach ($reminders as $reminder) {
126                 extract($reminder);
127                 $qm->enqueue(array($type, $opts), 'siterem');
128                 if (!$quiet) { print "Sent pending {$type} reminders to all unconfirmed addresses on the site.\n"; }
129             }
130         } else {
131             show_help();
132             exit(1);
133         }
134     } catch (Exception $e) {
135         if (!$quiet) { print $e->getMessage() . "\n"; }
136         common_log(LOG_ERR, $e->getMessage(), __FILE__);
137         exit(1);
138     }
139 }