Allows connecting to an IRC server over the AX26 protocol.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.2 KiB

  1. import kiss2
  2. import ax26
  3. import time
  4. import _thread
  5. from twisted.words.protocols import irc
  6. from twisted.internet import protocol, reactor
  7. CALLSIGN = "CALLSIGNHERE-IRC"
  8. NICKNAME = "ax26-irc-gateway"
  9. SERVER = "irc.geekshed.net"
  10. CHANNEL = "#redditnet"
  11. k = kiss2.TCPKISS(host='localhost', port=8001)
  12. k.start()
  13. ax = ax26.Server(k, CALLSIGN)
  14. bot = None
  15. def blockingMethod(botfunc): #Not really
  16. global bot
  17. bot = botfunc
  18. backlog = ""
  19. class IRCClient(irc.IRCClient):
  20. nickname = NICKNAME
  21. def signedOn(self):
  22. self.join(self.factory.channel)
  23. print ("Signed on as %s." % (self.nickname,))
  24. def joined(self, channel):
  25. print ("Joined %s." % (channel,))
  26. global bot
  27. bot = self
  28. def privmsg(self, user, channel, msg):
  29. global backlog
  30. backlog += "<" + user + "> " + msg + "\r\n"
  31. def threadSafeMsg(self, channel, message):
  32. reactor.callFromThread(self.msg, channel, message)
  33. class IRCClientFactory(protocol.ClientFactory):
  34. protocol = IRCClient
  35. def __init__(self, channel):
  36. self.channel = channel
  37. def clientConnectionLost(self, connector, reason):
  38. print ("Lost connection (%s), reconnecting." % (reason,))
  39. connector.connect()
  40. def clientConnectionFailed(self, connector, reason):
  41. print ("Could not connect: %s" % (reason,))
  42. #ax26
  43. client = ""
  44. def send_msg(msg):
  45. global client
  46. if client != "":
  47. ax.send_data(client, msg.encode())
  48. class handler:
  49. def __init__(self, client_callsign):
  50. global client
  51. client = client_callsign
  52. def on_recv(self, data):
  53. text = data.decode().strip()
  54. bot.threadSafeMsg(channel, text)
  55. def on_disconnect(self):
  56. client = ""
  57. def ax26():
  58. global backlog
  59. while (True):
  60. if backlog != "":
  61. backlog_temp = backlog
  62. backlog = ""
  63. send_msg(backlog_temp[:-2]) #Remove last \r\n
  64. time.sleep(5);
  65. _thread.start_new_thread(ax26, ())
  66. def ax26_server():
  67. ax.serve_forever(handler)
  68. _thread.start_new_thread(ax26_server, ())
  69. #Setup IRC
  70. reactor.connectTCP(SERVER, 6667, IRCClientFactory(channel))
  71. reactor.run()