Skip to content

Commit d45af97

Browse files
committed
Merge branch 'webhook' of github.com:Shadow243/cypht into webhook
2 parents ca729ac + 5e0b7b9 commit d45af97

File tree

9 files changed

+158
-15
lines changed

9 files changed

+158
-15
lines changed

Makefile

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
1+
2+
DOCKERHUB_REPO=cypht/cypht
3+
14
.PHONY: docker-up
25
docker-up: ## start docker stack in foreground for development
36
docker compose -f docker-compose.dev.yaml up --build || true # --abort-on-container-exit
47

58
.PHONY: docker-push
69
.ONESHELL:
710
docker-push: ## build, tag, and push image to dockerhub. presumes you are logged in. run with a version like tag:1.2.3
8-
@username=$$(docker info | sed '/Username:/!d;s/.* //')
911
@[ "$(tag)" = "" ] && (echo "Tag required. Example tag=1.2.3" ; exit 1)
10-
@image=$${username}/cypht:$(tag)
12+
@image=$(DOCKERHUB_REPO):$(tag)
1113
@echo "Building image $${image}"
1214
@docker buildx build . --platform linux/amd64 \
1315
-t $${image} -f docker/Dockerfile --push
@@ -16,9 +18,7 @@ docker-push: ## build, tag, and push image to dockerhub. presumes you are logge
1618
.PHONY: dockerhub-push-readme
1719
.ONESHELL:
1820
dockerhub-push-readme: ## upload readme to dockerhub
19-
@username=$$(docker info | sed '/Username:/!d;s/.* //')
20-
@docker pushrm --file docker/DOCKERHUB-README.md $${username}/cypht
21-
@echo docker pushrm --file docker/DOCKERHUB-README.md $${username}/cypht
21+
docker pushrm --file docker/DOCKERHUB-README.md $(DOCKERHUB_REPO)
2222

2323
.PHONY: setup
2424
.ONESHELL:

docker/DOCKERHUB-README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ It recommended that you choose a specific version number tag instead of using 'l
1515
## Example docker-compose
1616

1717
See example file here:
18-
https://github.com/jonocodes/cypht/blob/docker-refresh/docker/docker-compose.yaml
18+
https://github.com/cypht-org/cypht/blob/master/docker/docker-compose.yaml
1919

2020
* Starts a database container to be for user authentication
2121
* Starts the Cypht container available on port 80 of the host with ...

docker/docker-compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ services:
1414
- MYSQL_USER=cypht
1515
- MYSQL_PASSWORD=cypht_password
1616
cypht:
17-
image: jonocodes/cypht:2.0.1-docker-wip
17+
image: cypht/cypht:2.0.1
1818
ports:
1919
- "80:80"
2020
# env_file:

lib/telegram_webhook.php

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
<?php
2+
3+
/**
4+
* Webhook
5+
* @package framework
6+
* @subpackage webhook
7+
*/
8+
9+
class Hm_Telegram_Webhook {
10+
11+
/* webhook_token value */
12+
private $webhook_token;
13+
14+
private $prefix_ui = 'https://api.telegram.org/';
15+
16+
/**
17+
* Load webhook token
18+
* @param string $webhook_token
19+
*/
20+
public function __construct($webhook_token) {
21+
$this->webhook_token = $webhook_token;
22+
}
23+
24+
// Function to send Telegram notification
25+
/**
26+
* send telegram notiofication using curl
27+
* @param array $extracted_msgs
28+
*/
29+
public function send(array $extracted_msgs) {
30+
// Delete the webhook
31+
if ($this->delete_webhook($this->webhook_token)) {
32+
// Get the chat ID
33+
$chatId = $this->get_chat_id();
34+
if ($chatId) {
35+
$text = "New Message\nFrom: {$extracted_msgs['from']}\nSubject: {$extracted_msgs['subject']}\nContent: {$extracted_msgs['body']}";
36+
37+
$curl_handle = curl_init();
38+
curl_setopt($curl_handle, CURLOPT_URL, "{$this->prefix_uri}bot{$this->webhook_token}/sendMessage");
39+
curl_setopt($curl_handle, CURLOPT_POST, true);
40+
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
41+
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, http_build_query(['chat_id' => $chatId, 'text' => $text]));
42+
curl_setopt($curl_handle, CURLOPT_HTTPHEADER, ["Content-Type: application/x-www-form-urlencoded"]);
43+
44+
$response = curl_exec($curl_handle);
45+
if (curl_errno($ch)) {
46+
Hm_Msgs::add('ERRError:' . curl_error($curl_handle));
47+
} else {
48+
$response_data = json_decode($response, true);
49+
if (!$response_data['ok']) {
50+
Hm_Msgs::add('ERRFailed to send message: ' . $response_data['description']);
51+
}
52+
}
53+
curl_close($curl_handle);
54+
unset($curl_handle);
55+
}
56+
}
57+
}
58+
59+
/**
60+
* get the chat ID using webhook_token
61+
*/
62+
private function get_chat_id() {
63+
$curl_handle = curl_init();
64+
curl_setopt($curl_handle, CURLOPT_URL, "{$this->prefix_ui}/bot{$this->webhook_token}/getUpdates");
65+
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
66+
$response = curl_exec($curl_handle);
67+
68+
if (curl_errno($curl_handle)) {
69+
Hm_Msgs::add('ERRError:' . curl_error($curl_handle));
70+
return false;
71+
} else {
72+
$response_data = json_decode($response, true);
73+
if ($response_data['ok']) {
74+
if (!empty($response_data['result'])) {
75+
$chatId = $response_data['result'][0]['message']['chat']['id'];
76+
return $chatId;
77+
} else {
78+
Hm_Msgs::add('ERRNo messages found. Please send a message to your bot first.<br>');
79+
return false;
80+
}
81+
} else {
82+
Hm_Msgs::add('ERRFailed to get chat ID: ' . $response_data['description'] . '<br>');
83+
return false;
84+
}
85+
}
86+
87+
curl_close($curl_handle);
88+
unset($curl_handle);
89+
}
90+
91+
/**
92+
* This function is usefull when trying to resend, we need to delete old webhook before we send a new one
93+
* delete the webhook using webhook_token
94+
*/
95+
private function delete_webhook() {
96+
$curl_handle = curl_init();
97+
curl_setopt($curl_handle, CURLOPT_URL, "{$this->prefix_ui}bot{$this->webhook_token}/delete_webhook");
98+
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, true);
99+
$response = curl_exec($curl_handle);
100+
101+
if (curl_errno($curl_handle)) {
102+
Hm_Msgs::add('ERRError:' . curl_error($curl_handle));
103+
return false;
104+
} else {
105+
$response_data = json_decode($response, true);
106+
if ($response_data['ok']) {
107+
Hm_Msgs::add('ERRWebhook was deleted successfully.<br>');
108+
return true;
109+
} else {
110+
Hm_Msgs::add('ERRFailed to delete webhook: ' . $response_data['description'] . '<br>');
111+
return false;
112+
}
113+
}
114+
115+
curl_close($curl_handle);
116+
unset($curl_handle);
117+
}
118+
}

modules/core/site.js

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -498,6 +498,7 @@ function Message_List() {
498498
'flagged': 'formatted_flagged_data',
499499
'junk': 'formatted_junk_data',
500500
'trash': 'formatted_trash_data',
501+
'sent': 'formatted_sent_data',
501502
'drafts': 'formatted_drafts_data'
502503
};
503504

@@ -566,7 +567,6 @@ function Message_List() {
566567
id = ids[i];
567568
if ((id+'').search('_') != -1) {
568569
parts = id.split('_', 2);
569-
parts[0] -= 0;
570570
re = new RegExp(parts[1]+'$');
571571
parts[1] = re;
572572
}
@@ -1799,6 +1799,24 @@ var reset_default_value_checkbox = function() {
17991799
}
18001800
};
18011801

1802+
var reset_default_value_input_webhook_token = function() {
1803+
let field = this.parentElement.parentElement.querySelector('input[type="text"]');
1804+
const defaultValue = field.getAttribute("data-default-value");
1805+
1806+
if (field.disabled == false) {
1807+
this.style.transform = "scaleX(1)";
1808+
this.parentElement.setAttribute("restore_aria_label", hm_trans("Restore current value"));
1809+
field.setAttribute("current_value", field.value);
1810+
field.value = defaultValue; // Use the default value (which is now an empty string)
1811+
field.disabled = true;
1812+
} else {
1813+
this.style.transform = "scaleX(-1)";
1814+
this.parentElement.setAttribute("restore_aria_label", hm_trans("Restore default value"));
1815+
field.value = field.getAttribute("current_value");
1816+
field.disabled = false;
1817+
}
1818+
};
1819+
18021820
var reset_default_value_select = function() {
18031821
let field = this.parentElement.parentElement.firstChild;
18041822
let tab_static_default_value = {"inline_message_style" : 0, "smtp_compose_type" : 0, "theme_setting" : 0,
@@ -1942,6 +1960,7 @@ $(function() {
19421960
$('.reset_default_value_checkbox').on("click", reset_default_value_checkbox);
19431961
$('.reset_default_value_select').on("click", reset_default_value_select);
19441962
$('.reset_default_value_input').on("click", reset_default_value_input);
1963+
$('.reset_default_value_input_webhook_token').on("click", reset_default_value_input_webhook_token);
19451964
}
19461965

19471966
if (hm_check_dirty_flag()) {
@@ -2602,7 +2621,7 @@ const observeMessageTextMutationAndHandleExternalResources = (inline) => {
26022621
if (mutation.addedNodes.length > 0) {
26032622
mutation.addedNodes.forEach(function (node) {
26042623
if (node.classList.contains('msg_text_inner')) {
2605-
handleExternalResources(inline);
2624+
handleExternalResources(inline);
26062625
}
26072626
});
26082627
}

modules/feeds/hm-feed.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ function get_feed_data($url) {
118118
}
119119
switch ($type) {
120120
case 'curl':
121-
$curl_handle=curl_init();
121+
$curl_handle = curl_init();
122122
curl_setopt($curl_handle, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36");
123123
curl_setopt($curl_handle, CURLOPT_URL, $url);
124124
curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,15);

modules/imap/handler_modules.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
<?php
2-
32
/**
43
* IMAP modules
54
* @package modules
@@ -8,7 +7,6 @@
87

98
if (!defined('DEBUG_MODE')) { die(); }
109

11-
1210
/**
1311
* Check for attachments when forwarding a message
1412
* @subpackage imap/handler
@@ -1316,6 +1314,12 @@ public function process() {
13161314
$this->out('imap_server_ids', $form['imap_server_ids']);
13171315
}
13181316
}
1317+
1318+
1319+
1320+
1321+
1322+
13191323
}
13201324

13211325
/**

modules/imap/hm-imap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,8 @@ class Hm_IMAP extends Hm_IMAP_Cache {
142142
public $con_error_msg = '';
143143
public $con_error_num = 0;
144144

145+
public $banner = '';
146+
145147
/* holds information about the currently selected mailbox */
146148
public $selected_mailbox = false;
147149

modules/imap/output_modules.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,11 +1133,11 @@ protected function output() {
11331133
$webhook_token = $settings['webhook_token'];
11341134
}
11351135
if (!empty($webhook_token)) {
1136-
$reset = '<span class="tooltip_restore" restore_aria_label="Restore default value"><i class="bi bi-arrow-repeat refresh_list reset_default_value_input" default-value=""></i></span>';
1136+
$reset = '<span class="tooltip_restore" restore_aria_label="Restore default value"><i class="bi bi-arrow-repeat refresh_list reset_default_value_input_webhook_token"></i></span>';
11371137
}
1138-
return '<tr class="general_setting"><td><label for="imap_per_page">'.
1138+
return '<tr class="general_setting"><td><label for="webhook_token">'.
11391139
$this->trans('Webhook telegram token').'</label></td><td><input class="form-control form-control-sm w-auto" type="text" id="webhook_token" '.
1140-
'name="webhook_token" value="'.$this->html_safe($webhook_token).'" />'.$reset.'</td></tr>';
1140+
'name="webhook_token" value="'.$this->html_safe($webhook_token).'" data-default-value="" />'.$reset.'</td></tr>';
11411141
}
11421142
}
11431143

0 commit comments

Comments
 (0)