No description
Find a file
2022-03-16 12:57:11 -05:00
LICENSES REUSE compliant licensing metadata 2019-12-17 05:44:12 +00:00
src Do not write to stdout 2022-03-16 12:57:11 -05:00
tests Manage object lifetime with destructors 2020-07-14 21:05:09 +05:30
.gitignore REUSE compliant licensing metadata 2019-12-17 05:44:12 +00:00
flake.lock Switch to flake development shell 2020-08-03 11:45:23 +02:00
flake.nix Use the upcoming toxav API 2020-08-03 13:55:22 +02:00
README.md Manage object lifetime with destructors 2020-07-14 21:05:09 +05:30
toxcore.nimble Export some max-length constants 2022-03-16 12:57:10 -05:00

Nim wrapper over the Toxcore library

import toxcore

from std/os import sleep

const
  bootstrapHost = "85.143.221.42"
  bootstrapKey = "DA4E4ED4B697F2E9B000EEFE3A34B554ACD3F45F5C96EAEA2516DD7FF9AF7B43".toPublicKey

type Bot = ref object
  tox: Tox

proc setupCallbacks(bot: Bot) =
  var echoCount = 0
    # bind a value for callback closure magic

  bot.tox.onFriendRequest do (pk: PublicKey; msg: string):
    discard bot.tox.addFriendNoRequest(pk)

  bot.tox.onFriendMessage do (f: Friend; msg: string; kind: MessageType):
    discard bot.tox.send(f, msg, kind)
    inc echoCount
    bot.tox.statusMessage = $echoCount & " echos served "

proc newEchoBot(name: string): Bot =
  result = Bot(tox: initTox())
  result.tox.name = name
  result.setupCallbacks()
  result.tox.bootstrap(bootstrapHost, bootstrapKey)
  echo result.tox.name, " echos messages to ", result.tox.address

let
  a = newEchoBot "alice"
  b = newEchoBot "bob"
while true:
  iterate a.tox
  iterate b.tox
  sleep min(a.tox.iterationInterval, b.tox.iterationInterval)