Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,21 @@
gnomeDir
];
};
nativeBuildInputs = [ pkgs.gnome-shell ];
nativeBuildInputs = [
pkgs.gnome-shell
pkgs.jq
];
# extensions.gnome.org shows an extension's version-name on its
# listing, and reads it only from the zip.
buildPhase = ''
pack() {
tar --sort=name --mtime=@1 --owner=0 --group=0 --numeric-owner -I 'gzip -n' "$@"
}
pack -C plugin -cf cvesmic-quickshell.tar.gz cvesmic
pack -C plugin/cvesmic/kwin -cf cvesmic-kwin.tar.gz metadata.json contents
jq --arg version "$version" '. + {"version-name": $version}' \
gnome/cvesmic@codeforbreakfast.co/metadata.json > metadata.json
mv metadata.json gnome/cvesmic@codeforbreakfast.co/metadata.json
gnome-extensions pack gnome/cvesmic@codeforbreakfast.co \
--extra-source=../../plugin/cvesmic/Feed.mjs \
--extra-source=../../plugin/cvesmic/Shake.mjs
Expand Down
8 changes: 4 additions & 4 deletions gnome/cvesmic@codeforbreakfast.co/extension.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class CVEsmicExtension extends Extension {

// Mutter announces each screen cast and remote desktop session that
// starts from now on, so one already running stays unknown.
this._shares = new Set();
this._shares = new Map();
this._shareController = global.backend.get_remote_access_controller();
this._newShareId = this._shareController?.connect('new-handle', (_controller, share) => this._onNewShare(share));

Expand All @@ -65,6 +65,8 @@ export default class CVEsmicExtension extends Extension {
if (this._newShareId)
this._shareController.disconnect(this._newShareId);
this._shareController = null;
for (const [share, stoppedId] of this._shares)
share.disconnect(stoppedId);
this._shares = null;
this._stopPollTimer();
this._request?.cancel();
Expand All @@ -88,7 +90,6 @@ export default class CVEsmicExtension extends Extension {

_report(result) {
this._lastResult = result;
console.info('cvesmic:', result);
}

_feedUrl() {
Expand Down Expand Up @@ -200,8 +201,7 @@ export default class CVEsmicExtension extends Extension {
}

_onNewShare(share) {
this._shares.add(share);
share.connect('stopped', () => this._shares?.delete(share));
this._shares.set(share, share.connect('stopped', () => this._shares.delete(share)));
if (this._strength) {
this._endShake();
this._announce();
Expand Down
2 changes: 1 addition & 1 deletion gnome/cvesmic@codeforbreakfast.co/metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"uuid": "cvesmic@codeforbreakfast.co",
"name": "CVEsmic",
"description": "Shakes the screen when a new item appears in the pwn.watch feed, harder for a more severe one",
"shell-version": ["48", "49", "50"],
"shell-version": ["48", "49", "50", "51"],
"url": "https://github.com/CodeForBreakfast/cvesmic",
"settings-schema": "org.gnome.shell.extensions.cvesmic"
}
110 changes: 55 additions & 55 deletions plugin/cvesmic/Feed.mjs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Reads the feed and decides when to shake. Everything the feed sends is
// untrusted, so each value is checked before it is used.

export const defaultFeedUrl = "https://pwn.watch/feed.json";
export const defaultFeedUrl = 'https://pwn.watch/feed.json';
export const pollIntervalMs = 5 * 60 * 1000;
export const maxBackoffMs = 60 * 60 * 1000;
export const requestTimeoutMs = 30 * 1000;
Expand All @@ -13,13 +13,13 @@ export const maxEtagChars = 256;
export const maxTitleChars = 200;
export const maxNotices = 5;

export const severities = ["low", "medium", "high", "critical"];
export const severities = ['low', 'medium', 'high', 'critical'];

export const strengths = {
low: { offsetPx: 6, rotationDeg: 0, durationMs: 1000 },
medium: { offsetPx: 15, rotationDeg: 0.15, durationMs: 1500 },
high: { offsetPx: 30, rotationDeg: 0.35, durationMs: 2200 },
critical: { offsetPx: 50, rotationDeg: 0.6, durationMs: 3000 }
low: {offsetPx: 6, rotationDeg: 0, durationMs: 1000},
medium: {offsetPx: 15, rotationDeg: 0.15, durationMs: 1500},
high: {offsetPx: 30, rotationDeg: 0.35, durationMs: 2200},
critical: {offsetPx: 50, rotationDeg: 0.6, durationMs: 3000},
};

export function rank(severity) {
Expand All @@ -28,11 +28,11 @@ export function rank(severity) {

export function severityOf(tags) {
if (!Array.isArray(tags))
return "low";
var best = "low";
for (var i = 0; i < tags.length; i++) {
if (severities.indexOf(tags[i]) > rank(best))
best = tags[i];
return 'low';
let best = 'low';
for (const tag of tags) {
if (severities.indexOf(tag) > rank(best))
best = tag;
}
return best;
}
Expand All @@ -41,28 +41,29 @@ export function severityOf(tags) {
// tag, and rich text can load remote images, so a title loses its angle
// brackets.
export function plainTitle(value) {
if (typeof value !== "string")
return "";
return value.replace(/[<>]/g, "").replace(/\s+/g, " ").trim().slice(0, maxTitleChars);
if (typeof value !== 'string')
return '';
return value.replace(/[<>]/g, '').replace(/\s+/g, ' ').trim().slice(0, maxTitleChars);
}

// Returns [{ id, title, url, severity }] for each usable item, and throws on
// text that is not a JSON Feed. url is the item's web page, or "" if it has
// Returns [{id, title, url, severity}] for each usable item, and throws on
// text that is not a JSON Feed. url is the item's web page, or '' if it has
// none.
export function parseFeed(text) {
var feed = JSON.parse(text);
if (!feed || typeof feed !== "object" || !Array.isArray(feed.items))
throw new Error("not a JSON Feed: no items array");
var items = [];
for (var i = 0; i < feed.items.length && items.length < maxItems; i++) {
var entry = feed.items[i];
if (!entry || typeof entry.id !== "string" || entry.id.length === 0 || entry.id.length > maxIdChars)
const feed = JSON.parse(text);
if (!feed || typeof feed !== 'object' || !Array.isArray(feed.items))
throw new Error('not a JSON Feed: no items array');
const items = [];
for (const entry of feed.items) {
if (items.length >= maxItems)
break;
if (!entry || typeof entry.id !== 'string' || entry.id.length === 0 || entry.id.length > maxIdChars)
continue;
items.push({
id: entry.id,
title: plainTitle(entry.title) || plainTitle(entry.id),
url: usableWebUrl(entry.url) || usableWebUrl(entry.id),
severity: severityOf(entry.tags)
severity: severityOf(entry.tags),
});
}
return items;
Expand All @@ -72,31 +73,29 @@ export function parseFeed(text) {
// first poll of a feed address records its items and shakes nothing. notices
// holds the unseen items, in feed order, for one notification each.
export function assess(memory, feedUrl, items) {
var known = memory && memory.feedUrl === feedUrl && Array.isArray(memory.seenIds);
var seenIds = known ? memory.seenIds.slice() : [];
var seen = {};
for (var i = 0; i < seenIds.length; i++)
seen[seenIds[i]] = true;
const known = memory && memory.feedUrl === feedUrl && Array.isArray(memory.seenIds);
const seenIds = known ? memory.seenIds.slice() : [];
const seen = new Set(seenIds);

var shake = null;
var notices = [];
for (var j = 0; j < items.length; j++) {
if (seen[items[j].id])
let shake = null;
const notices = [];
for (const item of items) {
if (seen.has(item.id))
continue;
seen[items[j].id] = true;
seenIds.push(items[j].id);
seen.add(item.id);
seenIds.push(item.id);
if (!known)
continue;
if (shake === null || rank(items[j].severity) > rank(shake))
shake = items[j].severity;
if (shake === null || rank(item.severity) > rank(shake))
shake = item.severity;
if (notices.length < maxNotices)
notices.push(items[j]);
notices.push(item);
}

return {
shake: shake,
notices: notices,
memory: { feedUrl: feedUrl, seenIds: seenIds.slice(-maxSeenIds) }
shake,
notices,
memory: {feedUrl, seenIds: seenIds.slice(-maxSeenIds)},
};
}

Expand All @@ -105,23 +104,24 @@ export function assess(memory, feedUrl, items) {
// severity to shake at and the notices to give once the shake ends.
export function readAnswer(memory, feedUrl, status, text, etagHeader) {
if (status === 304)
return { succeeded: true, result: "the feed is unchanged" };
return {succeeded: true, result: 'the feed is unchanged'};
if (status !== 200)
return { succeeded: false, result: "the feed answered HTTP " + status };
var items;
return {succeeded: false, result: `the feed answered HTTP ${status}`};
let items;
try {
items = parseFeed(text);
} catch (e) {
return { succeeded: false, result: "the feed is unreadable: " + e.message };
return {succeeded: false, result: `the feed is unreadable: ${e.message}`};
}
var outcome = assess(memory, feedUrl, items);
const outcome = assess(memory, feedUrl, items);
const news = outcome.shake ? `a new ${outcome.shake} item` : 'none new';
return {
succeeded: true,
result: "read " + items.length + " items, " + (outcome.shake ? "a new " + outcome.shake + " item" : "none new"),
result: `read ${items.length} items, ${news}`,
memory: outcome.memory,
etag: usableEtag(etagHeader),
shake: outcome.shake,
notices: outcome.notices
notices: outcome.notices,
};
}

Expand All @@ -132,16 +132,16 @@ export function strengthFor(severity) {
// An entity tag goes back to the server in a request header, so anything that
// could break the header is refused.
export function usableEtag(value) {
if (typeof value !== "string" || value.length > maxEtagChars)
return "";
return /^(W\/)?"[\x21\x23-\x7e]*"$/.test(value) ? value : "";
if (typeof value !== 'string' || value.length > maxEtagChars)
return '';
return /^(W\/)?"[\x21\x23-\x7e]*"$/.test(value) ? value : '';
}

export function usableWebUrl(value) {
if (typeof value !== "string")
return "";
var url = value.trim();
return url.length <= maxIdChars && /^https?:\/\/[^\s]+$/i.test(url) ? url : "";
if (typeof value !== 'string')
return '';
const url = value.trim();
return url.length <= maxIdChars && /^https?:\/\/[^\s]+$/i.test(url) ? url : '';
}

export function nextDelayMs(currentMs, succeeded) {
Expand Down
35 changes: 15 additions & 20 deletions plugin/cvesmic/Shake.mjs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Where the frozen frame sits at each moment of a shake.

export const rest = { x: 0, y: 0, rotation: 0, scale: 1 };
export const rest = {x: 0, y: 0, rotation: 0, scale: 1};

// t runs from 0 to 1 over the shake, and seconds is the time since it began.
// The frame is scaled just enough to cover the screen's edges, so it starts
Expand All @@ -9,30 +9,25 @@ export function poseAt(strength, t, seconds, size) {
if (!(t > 0 && t < 1))
return rest;
// Sharp onset, then a long decay, like a quake's main shock.
var env = Math.min(t / 0.1, 1) * Math.pow(1 - t, 2);
var sway = function (f, p) {
return Math.sin(2 * Math.PI * f * seconds + p);
};
var jolt = function () {
return Math.random() * 2 - 1;
};
var x = strength.offsetPx * env * (0.6 * sway(2.3, 0) + 0.25 * sway(7.1, 1.3) + 0.15 * jolt());
var y = strength.offsetPx * env * (0.6 * sway(1.7, 2.1) + 0.25 * sway(8.3, 0.4) + 0.15 * jolt());
var rotation = strength.rotationDeg * env * sway(1.1, 0.7);
return { x: x, y: y, rotation: rotation, scale: coveringScale(x, y, rotation, size) };
const env = Math.min(t / 0.1, 1) * Math.pow(1 - t, 2);
const sway = (f, p) => Math.sin(2 * Math.PI * f * seconds + p);
const jolt = () => Math.random() * 2 - 1;
const x = strength.offsetPx * env * (0.6 * sway(2.3, 0) + 0.25 * sway(7.1, 1.3) + 0.15 * jolt());
const y = strength.offsetPx * env * (0.6 * sway(1.7, 2.1) + 0.25 * sway(8.3, 0.4) + 0.15 * jolt());
const rotation = strength.rotationDeg * env * sway(1.1, 0.7);
return {x, y, rotation, scale: coveringScale(x, y, rotation, size)};
}

// The smallest scale at which the moved and turned frame still covers every
// corner of the screen.
export function coveringScale(x, y, rotation, size) {
var theta = rotation * Math.PI / 180;
var cos = Math.cos(theta);
var sin = Math.sin(theta);
var scale = 1;
var corners = [[1, 1], [1, -1], [-1, 1], [-1, -1]];
for (var i = 0; i < corners.length; i++) {
var dx = corners[i][0] * size.width / 2 - x;
var dy = corners[i][1] * size.height / 2 - y;
const theta = rotation * Math.PI / 180;
const cos = Math.cos(theta);
const sin = Math.sin(theta);
let scale = 1;
for (const [cx, cy] of [[1, 1], [1, -1], [-1, 1], [-1, -1]]) {
const dx = cx * size.width / 2 - x;
const dy = cy * size.height / 2 - y;
scale = Math.max(scale, Math.abs(dx * cos + dy * sin) / (size.width / 2), Math.abs(-dx * sin + dy * cos) / (size.height / 2));
}
return scale;
Expand Down
Loading