1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace JarvisPHP\Plugins\CheckMail_plugin; |
4
|
|
|
|
5
|
|
|
use JarvisPHP\Core\JarvisSession; |
6
|
|
|
use JarvisPHP\Core\JarvisPHP; |
7
|
|
|
use JarvisPHP\Core\JarvisLanguage; |
8
|
|
|
use JarvisPHP\Core\JarvisTTS; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* CheckMail plugin - check an imap mailbox for unread emails |
12
|
|
|
* @author Stefano Bianchini |
13
|
|
|
* @website http://www.stefanobianchini.net |
14
|
|
|
*/ |
15
|
|
|
class CheckMail_plugin implements \JarvisPHP\Core\JarvisPluginInterface{ |
16
|
|
|
/** |
17
|
|
|
* Priority of plugin |
18
|
|
|
* @var int |
19
|
|
|
*/ |
20
|
|
|
var $priority = 5; |
|
|
|
|
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* the behaviour of plugin |
24
|
|
|
* @param string $command |
25
|
|
|
*/ |
26
|
|
|
function answer($command) { |
|
|
|
|
27
|
|
|
$answer = ''; |
28
|
|
|
|
29
|
|
|
//Load credentials from json config |
30
|
|
|
if(file_exists('Plugins/CheckMail_plugin/credentials.json')) { |
31
|
|
|
$json_config = json_decode(file_get_contents('Plugins/CheckMail_plugin/credentials.json')); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
if($json_config) { |
35
|
|
|
|
36
|
|
|
$mbox = imap_open($json_config->connection_string, $json_config->user, $json_config->password, OP_READONLY); |
|
|
|
|
37
|
|
|
|
38
|
|
|
$uids = imap_search($mbox, 'UNSEEN', SE_UID); |
39
|
|
|
|
40
|
|
|
if(is_array($uids)) { |
41
|
|
|
$answer = sprintf(JarvisLanguage::translate('you_have_new_messages',get_called_class()), count($uids)); |
42
|
|
|
} else { |
43
|
|
|
$answer = JarvisLanguage::translate('no_messages',get_called_class()); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
if(preg_match(JarvisLanguage::translate('preg_match_read',get_called_class()), $command) && is_array($uids)) { |
47
|
|
|
//Read last message |
48
|
|
|
$result = imap_fetch_overview($mbox, end($uids), FT_UID); |
49
|
|
|
$mail = end($result); |
50
|
|
|
$answer = JarvisLanguage::translate('from',get_called_class()).': '.$mail->from.'. '.JarvisLanguage::translate('subject',get_called_class()).': '.imap_utf8($mail->subject); |
51
|
|
|
JarvisSession::terminate(); |
52
|
|
|
} |
53
|
|
|
imap_close($mbox); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
JarvisTTS::speak($answer); |
57
|
|
|
$response = new \JarvisPHP\Core\JarvisResponse($answer, JarvisPHP::getRealClassName(get_called_class()), true); |
58
|
|
|
$response->send(); |
59
|
|
|
} |
60
|
|
|
/** |
61
|
|
|
* Get plugin's priority |
62
|
|
|
* @return int |
63
|
|
|
*/ |
64
|
|
|
function getPriority() { |
|
|
|
|
65
|
|
|
return $this->priority; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Is it the right plugin for the command? |
70
|
|
|
* @param string $command |
71
|
|
|
* @return boolean |
72
|
|
|
*/ |
73
|
|
|
function isLikely($command) { |
|
|
|
|
74
|
|
|
return preg_match(JarvisLanguage::translate('preg_match_activate_plugin',get_called_class()), $command); |
|
|
|
|
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Does the plugin need a session? |
79
|
|
|
* @return boolean |
80
|
|
|
*/ |
81
|
|
|
function hasSession() { |
|
|
|
|
82
|
|
|
return true; |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
The PSR-2 coding standard requires that all properties in a class have their visibility explicitly declared. If you declare a property using
the property is implicitly global.
To learn more about the PSR-2, please see the PHP-FIG site on the PSR-2.