diff --git a/server/core/src/main/java/cc/blynk/server/core/processors/BaseProcessorHandler.java b/server/core/src/main/java/cc/blynk/server/core/processors/BaseProcessorHandler.java index fca084ff93..c2bbd0a628 100644 --- a/server/core/src/main/java/cc/blynk/server/core/processors/BaseProcessorHandler.java +++ b/server/core/src/main/java/cc/blynk/server/core/processors/BaseProcessorHandler.java @@ -31,7 +31,7 @@ protected void processEventorAndWebhook(User user, DashBoard dash, int deviceId, PinType pinType, String value, long now) { try { eventorProcessor.process(user, session, dash, deviceId, pin, pinType, value, now); - webhookProcessor.process(session, dash, deviceId, pin, pinType, value, now); + webhookProcessor.process(user, session, dash, deviceId, pin, pinType, value, now); } catch (QuotaLimitException qle) { log.debug("User {} reached notification limit for eventor/webhook.", user.name); } catch (IllegalArgumentException iae) { diff --git a/server/core/src/main/java/cc/blynk/server/core/processors/WebhookProcessor.java b/server/core/src/main/java/cc/blynk/server/core/processors/WebhookProcessor.java index 0bc33b6ac6..ab058329e1 100644 --- a/server/core/src/main/java/cc/blynk/server/core/processors/WebhookProcessor.java +++ b/server/core/src/main/java/cc/blynk/server/core/processors/WebhookProcessor.java @@ -3,6 +3,7 @@ import cc.blynk.server.core.model.DashBoard; import cc.blynk.server.core.model.DataStream; import cc.blynk.server.core.model.auth.Session; +import cc.blynk.server.core.model.auth.User; import cc.blynk.server.core.model.enums.PinType; import cc.blynk.server.core.model.widgets.others.webhook.Header; import cc.blynk.server.core.model.widgets.others.webhook.WebHook; @@ -24,6 +25,7 @@ import static cc.blynk.server.core.protocol.enums.Command.WEB_HOOKS; import static cc.blynk.utils.StringUtils.DATETIME_PATTERN; +import static cc.blynk.utils.StringUtils.DEVICE_OWNER_EMAIL; import static cc.blynk.utils.StringUtils.GENERIC_PLACEHOLDER; import static cc.blynk.utils.StringUtils.PIN_PATTERN; import static cc.blynk.utils.StringUtils.PIN_PATTERN_0; @@ -68,7 +70,7 @@ public WebhookProcessor(DefaultAsyncHttpClient httpclient, this.webhookFailureLimit = failureLimit; } - public void process(Session session, DashBoard dash, int deviceId, short pin, + public void process(User user, Session session, DashBoard dash, int deviceId, short pin, PinType pinType, String triggerValue, long now) { WebHook webhook = dash.findWebhookByPin(deviceId, pin, pinType); if (webhook == null) { @@ -78,12 +80,12 @@ public void process(Session session, DashBoard dash, int deviceId, short pin, checkIfNotificationQuotaLimitIsNotReached(now); if (webhook.isNotFailed(webhookFailureLimit) && webhook.url != null) { - process(session, dash.id, deviceId, webhook, triggerValue); + process(user, session, dash.id, deviceId, webhook, triggerValue); } } - private void process(Session session, int dashId, int deviceId, WebHook webHook, String triggerValue) { - String newUrl = format(webHook.url, triggerValue); + private void process(User user, Session session, int dashId, int deviceId, WebHook webHook, String triggerValue) { + String newUrl = format(webHook.url, triggerValue, user.email); if (!WebHook.isValidUrl(newUrl)) { return; @@ -114,7 +116,7 @@ private void process(Session session, int dashId, int deviceId, WebHook webHook builder.setHeader(header.name, header.value); if (webHook.body != null && !webHook.body.isEmpty()) { if (CONTENT_TYPE.equals(header.name)) { - String newBody = format(webHook.body, triggerValue); + String newBody = format(webHook.body, triggerValue, user.email); log.trace("Webhook formatted body : {}", newBody); builder.setBody(newBody); } @@ -123,7 +125,7 @@ private void process(Session session, int dashId, int deviceId, WebHook webHook } } - log.trace("Sending webhook. ", webHook); + log.trace("Sending webhook. {}", webHook); builder.execute(new AsyncCompletionHandler() { private int length = 0; @@ -184,7 +186,7 @@ private static boolean isValidResponseCode(int responseCode) { } } - private static String format(String data, String triggerValue) { + private static String format(String data, String triggerValue, String ownerEmail) { //this is an ugly hack to make it work with Blynk HTTP API. String quotedValue = Matcher.quoteReplacement(triggerValue); data = PIN_PATTERN.matcher(data).replaceFirst(quotedValue); @@ -214,6 +216,7 @@ private static String format(String data, String triggerValue) { default : data = GENERIC_PLACEHOLDER.matcher(data).replaceFirst(quotedValue); data = DATETIME_PATTERN.matcher(data).replaceFirst(Instant.now().toString()); + data = DEVICE_OWNER_EMAIL.matcher(data).replaceFirst(ownerEmail); } return data; } diff --git a/server/utils/src/main/java/cc/blynk/utils/StringUtils.java b/server/utils/src/main/java/cc/blynk/utils/StringUtils.java index 956d767fb5..3481acd27d 100644 --- a/server/utils/src/main/java/cc/blynk/utils/StringUtils.java +++ b/server/utils/src/main/java/cc/blynk/utils/StringUtils.java @@ -34,6 +34,7 @@ public final class StringUtils { private static final Pattern NOT_SUPPORTED_CHARS = Pattern.compile("[\\\\/:*?\"<>| ]"); public static final Pattern DATETIME_PATTERN = Pattern.compile("/datetime_iso/", Pattern.LITERAL); + public static final Pattern DEVICE_OWNER_EMAIL = Pattern.compile("device_owner_email", Pattern.LITERAL); public static final String WEBSOCKET_PATH = "/websocket"; public static final String WEBSOCKET_WEB_PATH = "/dashws";