commit b862e5f79401db65ed81796de979a705679a2e99 Author: Emery Hemingway Date: Tue Dec 17 03:53:51 2019 +0000 Initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ded282e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +hqtoxbot +result diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..93f455d --- /dev/null +++ b/flake.lock @@ -0,0 +1,18 @@ +{ + "inputs": { + "nimble": { + "inputs": { + "nixpkgs": { + "inputs": {}, + "narHash": "sha256-aLJ6PHTU1VbWBdyZbI/lLoj2JelUCGgovUsHlnAFIOE=", + "originalUrl": "nixpkgs", + "url": "git+file:///home/repo/nixpkgs?ref=genode-19.09&rev=cc2b10a7ed78f62dc2b3afa50d34d613c36e619e" + } + }, + "narHash": "sha256-ZzL+Gdl1R7UJK1I8s/ff+aQVaMCB9pu0gFmTZJeb8mA=", + "originalUrl": "git+https://git.sr.ht/~ehmry/nimble_flake", + "url": "git+https://git.sr.ht/~ehmry/nimble_flake?ref=master&rev=11ee779063eb4c78f7bfa281e36e1cc3ae161635" + } + }, + "version": 3 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..0f7a622 --- /dev/null +++ b/flake.nix @@ -0,0 +1,19 @@ +{ + description = "C3D2 Tox bot"; + + edition = 201909; + + inputs.nimble.uri = "git+https://git.sr.ht/~ehmry/nimble_flake"; + + outputs = { self, nimble }: { + + packages.x86_64-linux.hqtoxbot = + nimble.defaultPackage.x86_64-linux.buildNimble { + name = "hqtoxbot"; + src = self; + homepage = "https://gitea.c3d2.de/ehmry/hqtoxbot"; + }; + + defaultPackage.x86_64-linux = self.packages.x86_64-linux.hqtoxbot; + }; +} diff --git a/hqtoxbot.nimble b/hqtoxbot.nimble new file mode 100644 index 0000000..4c84c94 --- /dev/null +++ b/hqtoxbot.nimble @@ -0,0 +1,12 @@ +# Package + +version = "0.1.0" +author = "Emery Hemingway" +description = "A Tox bot for C3D2 HQ" +license = "GPL-3.0-or-later" +srcDir = "src" +bin = @["hqtoxbot"] + +# Dependencies + +requires "nim >= 1.0.0", "toxcore" diff --git a/src/hqtoxbot.nim b/src/hqtoxbot.nim new file mode 100644 index 0000000..c9678cb --- /dev/null +++ b/src/hqtoxbot.nim @@ -0,0 +1,65 @@ +import toxcore + +import std/asyncdispatch, std/json, std/httpclient + +const spaceApiUrl = "http://spaceapi.hq.c3d2.de:3000/spaceapi.json" + +const adminIds = [ + toAddress "DF0AC9107E0A30E7201C6832B017AC836FBD1EDAC390EE99B68625D73C3FD929FB47F1872CA4" + # Emery + ] + +proc bootstrap(bot: Tox) = + const servers = [ ("tox.neuland.technology", "15E9C309CFCB79FDDF0EBA057DABB49FE15F3803B1BFF06536AE2E5BA5E4690E".toPublicKey ) ] + for host, key in servers.items: + bot.bootstrap(host, key) + +proc addAdmin(bot: Tox; id: Address) = + discard bot.addFriend( + id, "You have been granted administrative rights to " & bot.name) + +proc setupCallbacks(bot: Tox) = + + #[ + bot.onConferenceInvite do (friend: Friend; kind: TOX_CONFERENCE_TYPE; + cookie: string): + discard bot.join(friend, cookie) + ]# + + bot.onFriendMessage do (f: Friend; msg: string; kind: TOX_MESSAGE_TYPE): + echo msg + + for id in adminIds: + bot.addAdmin(id) + +proc newBot(name: string): Tox = + result = newTox do (opts: Options): + opts.localDiscoveryEnabled = true + opts.ipv6Enabled = true + result.name = name + result.setupCallbacks() + result.bootstrap() + echo result.name, " is at ", result.address + +proc updateStatus(bot: Tox; http: AsyncHttpClient) {.async.} = + let + rsp = await http.get(spaceApiUrl) + body = await rsp.body + space = parseJson body + status = $(space["status"]) + if bot.statusMessage != status: + bot.statusMessage = $(space["status"]) + +proc main() = + let + bot = newBot("HQ Bot") + http = newAsyncHttpClient() + + addTimer(20*1000, oneshot=false) do (fd: AsyncFD) -> bool: + asyncCheck updateStatus(bot, http) + + while true: + iterate bot + poll bot.iterationInterval + +main()