Advertisement
Guest User

bot

a guest
Dec 3rd, 2024
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. ###changelog
  2. ###removed server name from relayed msg
  3. ###added authentication
  4. import socket
  5. import threading
  6. import time
  7. import time
  8. from threading import Lock
  9.  
  10. # Configuration for rate limiting (minimum delay in seconds)
  11. RATE_LIMIT = .5 # Minimum 1.5 seconds between messages per channel/server
  12.  
  13. # Dictionary to track last sent times: {server_host: {channel: timestamp}}
  14. last_sent_times = {}
  15. rate_limit_lock = Lock() # Ensure thread safety for the shared dictionary
  16.  
  17. # Configuration for multiple servers. VERY CASE SENSITIVE FYI
  18. servers = [
  19. {
  20. "host": "irc.rizon.net",
  21. "port": 6667,
  22. "nickname": "BridgeBT",
  23. "password": "",
  24. "channels": ["#shitposters","#/g/autism","#pleb", "#/g/tv", "#perwl", "#playchan", "#steam"]
  25. },
  26. {
  27. "host": "irc.inet.tele.dk",
  28. "port": 6667,
  29. "nickname": "BridgeBT",
  30. "password": "",
  31. "channels": ["#2600","#IRC30","#asians", "#hax", "#headshop"]
  32. }
  33. ]
  34.  
  35. # Shared dictionary to track active connections
  36. connections = {}
  37.  
  38. def parse_message(data, host, channels):
  39. """
  40. Parse the raw IRC message to extract user, channel, and message.
  41. Returns (user, channel, message) or (None, None, None) if the message isn't relevant.
  42. """
  43.  
  44. if "PRIVMSG" in data:
  45. parts = data.split(":", 2)
  46. if len(parts) < 3:
  47. return None, None, None
  48.  
  49. prefix = parts[1].split(" ")
  50.  
  51. if len(prefix) < 3:
  52. #logging.warning(f"Received malformed message: {data.strip()}")
  53. return None, None, None
  54.  
  55. user = prefix[0].split("!")[0]
  56. channel = prefix[2]
  57. message = parts[2]
  58. #logging.debug(f"Parsed message from {user} in channel {channel}: {message}")
  59.  
  60. # Only process the message if it is from a valid channel in the server
  61. if channel in channels:
  62. return user, channel, message
  63. return None, None, None
  64.  
  65.  
  66.  
  67.  
  68.  
  69. def relay_message(message, source_server, source_channel=None):
  70. """
  71. Relay the message to all other channels on all other servers.
  72. Avoid relaying back to the source server and its channels.
  73. If `source_channel` is provided, it will relay the message within the same server to other channels.
  74. """
  75. print(f"DEBUG: Relaying message '{message}' from {source_channel} on server {source_server['host']}")
  76.  
  77. current_time = time.time()
  78.  
  79. for server_config in servers:
  80. host = server_config["host"]
  81. if host not in last_sent_times:
  82. last_sent_times[host] = {}
  83.  
  84. irc = connections.get(host)
  85. if not irc:
  86. continue # Skip if no active connection
  87.  
  88. # Cross-server relay
  89. if server_config["host"] != source_server["host"]:
  90. for target_channel in server_config["channels"]:
  91. with rate_limit_lock:
  92. last_sent = last_sent_times[host].get(target_channel, 0)
  93. if current_time - last_sent >= RATE_LIMIT:
  94. irc.send(f"PRIVMSG {target_channel} :{message}\r\n".encode("utf-8"))
  95. print(f"DEBUG: Sent message to {target_channel} on {host}")
  96. with rate_limit_lock:
  97. last_sent_times[host][target_channel] = current_time
  98.  
  99. # Intra-server relay
  100. elif source_channel: # Relay within the same server if source_channel is provided
  101. for target_channel in server_config["channels"]:
  102. if target_channel != source_channel: # Avoid sending back to source channel
  103. with rate_limit_lock:
  104. last_sent = last_sent_times[host].get(target_channel, 0)
  105. if current_time - last_sent >= RATE_LIMIT:
  106. irc.send(f"PRIVMSG {target_channel} :{message}\r\n".encode("utf-8"))
  107. print(f"DEBUG: Relayed within {host} to {target_channel}")
  108. with rate_limit_lock:
  109. last_sent_times[host][target_channel] = current_time
  110.  
  111.  
  112. def handle_server(server_config):
  113. """
  114. Connect to a single IRC server and handle messages for relaying.
  115. """
  116. host = server_config["host"]
  117. port = server_config["port"]
  118. nickname = server_config["nickname"]
  119. password = server_config["password"]
  120. channels = server_config["channels"]
  121.  
  122. irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  123. irc.connect((host, port))
  124. irc.send(f"NICK {nickname}\r\n".encode("utf-8"))
  125. irc.send(f"USER {nickname} 0 * :Bridge Bot\r\n".encode("utf-8"))
  126.  
  127. # Identify with NickServ
  128. irc.send(f"PRIVMSG NickServ IDENTIFY {password}\r\n".encode("utf-8"))
  129.  
  130. # Save the connection object in the global dict for later use
  131. connections[host] = irc
  132.  
  133. while True:
  134. data = irc.recv(2048).decode("utf-8", errors="ignore")
  135. print(f"[{host}] Received: {data.strip()}")
  136.  
  137. # Respond to PING
  138. if data.startswith("PING"):
  139. irc.send(f"PONG {data.split()[1]}\r\n".encode("utf-8"))
  140. #ident
  141. if "This nickname is registered" in data or "please choose a different nick" in data:
  142. irc.send(f"PRIVMSG NickServ :IDENTIFY {password}\r\n".encode("utf-8"))
  143. identified = True
  144. continue
  145. # Join channels upon successful connection
  146. if "001" in data: # 001 = Successful connection
  147. for channel in channels:
  148. irc.send(f"JOIN {channel}\r\n".encode("utf-8"))
  149.  
  150. # Relay messages across servers and within the same server's channels
  151. user, channel, message = parse_message(data, host, channels)
  152. if user and message:
  153. relay_message(f"<{user}> {message}", server_config, channel)
  154.  
  155.  
  156. # Start a thread for each server
  157. for server_config in servers:
  158. threading.Thread(target=handle_server, args=(server_config,), daemon=True).start()
  159.  
  160. # Keep the main thread alive
  161. while True:
  162. time.sleep(1) # Sleep to prevent the main thread from exiting
  163.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement