Advertisement
nonogamer9

CC-IRC IRC To ComputerCraft Gateway

Mar 11th, 2025
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.79 KB | Software | 0 0
  1. import socket
  2. import threading
  3. from flask import Flask, request, jsonify
  4. from flask_socketio import SocketIO, emit
  5.  
  6. app = Flask(__name__)
  7. socketio = SocketIO(app, cors_allowed_origins="*")
  8.  
  9. irc_socket = None
  10. irc_server = ""
  11. irc_port = 6667
  12. irc_nick = ""
  13. irc_channel = ""
  14. irc_messages = []
  15.  
  16. def connect_to_irc(server, port, nick, channel):
  17.     global irc_socket, irc_server, irc_port, irc_nick, irc_channel
  18.     irc_server = server
  19.     irc_port = port
  20.     irc_nick = nick
  21.     irc_channel = channel
  22.     try:
  23.         irc_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  24.         irc_socket.connect((irc_server, int(irc_port)))
  25.         irc_socket.sendall(f"NICK {irc_nick}\r\n".encode("utf-8"))
  26.         irc_socket.sendall(f"USER {irc_nick} 0 * :CC-IRC Bot\r\n".encode("utf-8"))
  27.         irc_socket.sendall(f"JOIN {irc_channel}\r\n".encode("utf-8"))
  28.         threading.Thread(target=listen_to_irc).start()
  29.         return {"status": "Connected to IRC"}
  30.     except Exception as e:
  31.         return {"error": str(e)}
  32.  
  33. def listen_to_irc():
  34.     global irc_socket, irc_messages
  35.     while True:
  36.         try:
  37.             response = irc_socket.recv(2048).decode("utf-8")
  38.             if response.startswith("PING"):
  39.                 irc_socket.sendall(f"PONG {response.split()[1]}\r\n".encode("utf-8"))
  40.             else:
  41.                 print(response.strip())
  42.                 irc_messages.append(response.strip())
  43.                 socketio.emit('new_message', {'message': response.strip()})
  44.         except Exception as e:
  45.             print(f"Error: {e}")
  46.             break
  47.  
  48. @app.route("/config", methods=["POST"])
  49. def configure_irc():
  50.     data = request.json
  51.     server = data.get("server", "")
  52.     port = data.get("port", 6667)
  53.     nick = data.get("nick", "")
  54.     channel = data.get("channel", "")
  55.     if not all([server, port, nick, channel]):
  56.         return jsonify({"error": "Invalid configuration"}), 400
  57.     result = connect_to_irc(server, port, nick, channel)
  58.     return jsonify(result)
  59.  
  60. @app.route("/send", methods=["POST"])
  61. def send_message():
  62.     global irc_socket, irc_channel
  63.     data = request.json
  64.     message = data.get("message", "")
  65.     if message and irc_socket:
  66.         try:
  67.             irc_socket.sendall(f"PRIVMSG {irc_channel} :{message}\r\n".encode("utf-8"))
  68.             return jsonify({"status": "Message sent!"}), 200
  69.         except Exception as e:
  70.             return jsonify({"error": str(e)}), 500
  71.     return jsonify({"error": "No message provided or not connected"}), 400
  72.  
  73. @app.route("/messages", methods=["GET"])
  74. def get_messages():
  75.     global irc_messages
  76.     messages_to_return = list(irc_messages)
  77.     irc_messages.clear()
  78.     return jsonify({"messages": messages_to_return})
  79.  
  80. if __name__ == "__main__":
  81.     socketio.run(app, host="0.0.0.0", port=8000)
  82.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement