]> git.mxchange.org Git - quix0rs-gnu-social.git/blob - plugins/EmailReminder/scripts/sendemailreminder.php
4bfa811e1014faafc948253919b75e59a70d7877
[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 for {$server}.\n"; }
103             }
104         }
105        if (!$quiet) { print "Done! Reminders sent to all unconfirmed addresses in the known universe.\n"; }
106     }
107 } else {
108     $qm = QueueManager::get();
109     try {
110         // enqueue reminder for specific email address or all unconfirmed addresses
111         if (have_option('e', 'email')) {
112             $address = trim(get_option_value('e', 'email'));
113             foreach ($reminders as $reminder) {
114                 // real bad voodoo here
115                 extract($reminder);
116                 $confirm = new $className;
117                 $confirm->address = $address;
118                 $result = $confirm->find(true);
119                 if (empty($result)) {
120                     throw new Exception("No confirmation code found for {$address}.");
121                 }
122                 $qm->enqueue(array($confirm, $opts), $utransport);
123                 if (!$quiet) { print "Sent all pending {$type} reminder to {$address}.\n"; }
124             }
125         } else if (have_option('a', 'all')) {
126             foreach ($reminders as $reminder) {
127                 extract($reminder);
128                 $qm->enqueue(array($type, $opts), 'siterem');
129                 if (!$quiet) { print "Sent pending {$type} reminders to all unconfirmed addresses on the site.\n"; }
130             }
131         } else {
132             show_help();
133             exit(1);
134         }
135     } catch (Exception $e) {
136         if (!$quiet) { print $e->getMessage() . "\n"; }
137         common_log(LOG_ERR, $e->getMessage(), __FILE__);
138         exit(1);
139     }
140 }