-
Notifications
You must be signed in to change notification settings - Fork 0
/
roundcube_structured_vacation_notice.php
187 lines (150 loc) · 6.24 KB
/
roundcube_structured_vacation_notice.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* TODO
*/
require __DIR__ . '/oof_util.php';
require __DIR__ . '/structured_data_util_oof.php';
require __DIR__ . '/message_send_util_oof.php';
require __DIR__ . '/handlers_for_managesieve_hooks.php';
class roundcube_structured_vacation_notice extends rcube_plugin
{
/**
* A regex-like variable which indicates that our plugin
* should be active in all tasks excluding "login" and "logout"
*
* @var string $task
*/
public $task = '?(?!login|logout).*';
/**
* Initialize the plugin
*
* @return void
*/
function init()
{
// Register the relevant hooks that we use in our plugin
$this->add_hook('message_load', [$this, 'on_message_load_hook']);
$this->add_hook('message_before_send', [$this, 'on_message_before_send_hook_oof']);
$this->add_hook('vacation_form_advanced', [$this, 'on_vacation_form_advanced_hook_oof']);
$this->add_hook('vacation_reason_modify_on_mangesieve_store', [$this, 'on_vacation_reason_modify_on_mangesieve_store_hook']);
$this->add_hook('message_objects', [$this, 'on_message_objects_hook']);
// Register an action handler for checking whether message recipients are OOF
$this->register_action(
'check_recipients_for_oof',
[$this, 'check_recipients_for_oof_action']
);
// Include our JS files
$this->include_script('roundcube_structured_vacation_notice.js');
$this->include_script('oof.js');
// Include Internationalization
$this->add_texts('localization/', true);
}
/**
* A handler function for the "message_load" hook
*
* @param array $args The arguments, passed to the "message_load" hook
*
* @return void
*/
function on_message_load_hook($args)
{
/**
* Call our util function to check for OOF-related structured data
* and save the relevant OOF data to the sender's contact
*/
oof_util::save_oof_data_from_message($args);
}
/**
* A handler function for our AJAX action for checking whether the recipients
* of a message are currently OOF
*
* @return void
*/
function check_recipients_for_oof_action()
{
oof_util::check_recipients_for_oof();
}
/**
* A handler for the "message_before_send" hook
* We use it to turn a hidden JSON-LD from a "div" into a "script" tag
* (used when composing messages with structured data via our plugin)
*
* @param $args array The hook arguments that we receive
*
* @return array The (potentially) modified hook arguments
*/
function on_message_before_send_hook_oof($args)
{
return message_send_util_oof::on_message_before_send_hook_oof($args);
}
// Handlers for the managesieve hooks
function on_vacation_form_advanced_hook_oof($args)
{
return handlers_for_managesieve_hooks::on_vacation_form_advanced_hook_oof($args);
}
function on_vacation_reason_modify_on_mangesieve_store_hook($args)
{
rcmail::console('triggers on save hook');
return handlers_for_managesieve_hooks::on_vacation_reason_modify_on_mangesieve_store_hook($args);
}
public function on_message_objects_hook($args)
{
$rcmail = rcmail::get_instance();
// A variable which holds an extracted JSON-LD with structured data
$jsonLd = '';
// A variable which holds the context type of a JSON-LD with structured data
$contextType = '';
// Obtain the arguments for the hook
$content = $args['content'];
$message = $args['message'];
// Get the message's sender
$messageSender = $message->sender['mailto'];
// Check if we have a "text/html" part in the message
foreach ($message->parts as $part) {
if ($part->ctype_primary === 'text'
&& $part->ctype_secondary === 'html'
) {
// Get the HTML part of the message's body
$htmlBody = $message->get_part_body($part->mime_id, true);
// Try to extract JSON-LD from the HTML part
$jsonLd = structured_data_util_oof::html2jsonld_oof($htmlBody);
// Get the message's "from" header
$from = $message->get_header('from');
// Set the context type appropriately
$contextType = structured_data_util_oof::get_context_type($jsonLd);
}
}
// The type of a trusted sender, we'll need it below
$trustedSenderType = rcube_addressbook::TYPE_TRUSTED_SENDER;
$showStructuredEmailForTrustedSenders = $rcmail->config->get(
'showStructuredEmailForTrustedSenders',
false
);
/**
* Only show structured email data if:
* * our config flag allows it
* * and the sender is a trusted one
*/
if ($showStructuredEmailForTrustedSenders
&& $contextType == "outOfOffice"
&& $rcmail->contact_exists($messageSender, $trustedSenderType)
)
{
// Decode jsonLd for our banner
$decoded_jsonLd = json_decode($jsonLd);
$replacement_name = $decoded_jsonLd->replacement[0]->name;
$replacement_topic = $decoded_jsonLd->replacement[0]->topic;
$start = $decoded_jsonLd->start->date;
$end = $decoded_jsonLd->end->date;
$replacement_email = $decoded_jsonLd->replacement[0]->email;
// OUTPUT Banner similar to the "remote-objects-message"
$html = '<div id="oof-message-box" style="background-color:rgba(255,212,82,.2);" class="ui alert alert-warning boxwarning">';
$html .= '<i class="icon"></i>';
$html .= '<span>The user is out-of-office from ' . $start . ' till ' . $end . ' mails will not be forwarded.';
$html .= ' During absence, contact ' . $replacement_name . ' regarding ' . $replacement_topic . ' under ' . $replacement_email . '.</span>';
$html .= '</div>';
array_push($content, $html);
}
return array('content' => $content);
}
}