main.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. # srIRCeBot, a Simple IRC RElay Bot
  2. #
  3. # Copyright (C) 2022 Ferass EL HAFIDI <vitali64pmemail@protonmail.com>
  4. # Copyright (C) 2022 Andrew Yu <andrew@andrewyu.org>
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. import miniirc
  19. import miniirc_idc
  20. from miniirc_extras import *
  21. from time import sleep
  22. # Configuration
  23. server = [ ['irc.andrewyu.org', 'IRC'], ['andrewyu.org', 'IDC'] ]
  24. relayedChannels = [ '#IDC' , '#hackers@andrewyu.org' ]
  25. nick = 'idcbot'
  26. debug = True
  27. # IRC
  28. irc = miniirc.IRC(server[0][0], 6697, nick, relayedChannels[0], ns_identity=None, debug=False)
  29. # IDC
  30. idc = miniirc_idc.IDC(server[1][0], 6835, nick, relayedChannels[1], ns_identity=["idcbot", ""], debug=debug, ssl=True)
  31. # Relay functions
  32. def relay_msgs(chat, hostmask, args, which):
  33. #sleep(0.5) # Sleep for 0.5 seconds
  34. whichchan = 0 if which == 1 else 1
  35. # Send in the irc side what the user sent in the idc side and vice-versa
  36. print("NEW MESSAGE")
  37. chat.send('PRIVMSG', relayedChannels[whichchan],
  38. "<\x03%s%s\x0300> %s"
  39. % (len(hostmask[0]), hostmask[0], args[-1]))
  40. def relay_nick(chat, hostmask, args, which):
  41. #sleep(0.5)
  42. whichchan = 0 if which == 1 else 1
  43. # Send in the idc side the nick change in the irc side
  44. chat.send('PRIVMSG', relayedChannels[whichchan],
  45. "\x03%s%s\x0300 is now known as \x0302%s"
  46. % (len(hostmask[0]), hostmask[0], args[-1]))
  47. def relay_joins(chat, hostmask, args, which):
  48. #sleep(0.5)
  49. whichchan = 0 if which == 0 else 1
  50. # Send in the idc side joins in the irc side and vice-versa
  51. chat.send('PRIVMSG', relayedChannels[whichchan],
  52. "\x03%s%s\x03 has joined \x02%s"
  53. % (len(hostmask[0]), hostmask[0], args[-1]))
  54. def relay_mode(chat, hostmask, args, which):
  55. #sleep(0.5)
  56. whichchan = 0 if which == 1 else 1
  57. chat.send('PRIVMSG', relayedChannels[whichchan],
  58. "mode/%s: [%s %s] by %s"
  59. % (server[which][1], args[0], args[1], args[2], hostmask[0]))
  60. def relay_quits(chat, hostmask, args, which):
  61. #sleep(0.5)
  62. whichchan = 0 if which == 1 else 1
  63. # Send in the idc side quits in the irc side and vice-versa
  64. chat.send('PRIVMSG', relayedChannels[whichchan],
  65. "\x03%s%s\x03 has quit %s"
  66. % (len(hostmask[0]), hostmask[0], args[0]))
  67. def relay_kicks(chat, hostmask, args, which):
  68. #sleep(0.5)
  69. whichchan = 0 if which == 1 else 1
  70. # Send in the idc side kicks in the irc side and vice-versa
  71. chat.send('PRIVMSG', relayedChannels[whichchan],
  72. "%s was kicked from %s by %s"
  73. % (server[which][1], args[1], args[0], hostmask[0]))
  74. # Functions that run the relay functions, depending on the server where the message has been sent
  75. @irc.Handler('NICK', colon=False)
  76. def handle_nicks(chat, hostmask, args):
  77. if chat == idc:
  78. relay_nick(irc, hostmask, args, 0)
  79. elif chat == irc:
  80. relay_nick(idc, hostmask, args, 1)
  81. @irc.Handler('KICK', colon=False)
  82. def handle_kicks(chat, hostmask, args):
  83. if chat == irc:
  84. relay_kicks(idc, hostmask, args, 0)
  85. elif chat == idc:
  86. relay_kicks(irc, hostmask, args, 1)
  87. @miniirc.Handler('JOIN', colon=False)
  88. def handle_joins(chat, hostmask, args):
  89. if chat == irc:
  90. relay_joins(idc, hostmask, args, 0)
  91. elif chat == idc:
  92. relay_joins(irc, hostmask, args, 1)
  93. @miniirc.Handler('PART', colon=False)
  94. def handle_quits(chat, hostmask, args):
  95. if chat == irc:
  96. relay_quits(irc, hostmask, args, 0)
  97. elif chat == idc:
  98. relay_quits(idc, hostmask, args, 1)
  99. @miniirc.Handler('PRIVMSG', colon=False)
  100. def handle_privmsgs(chat, hostmask, args):
  101. words = args[-1].split(" ")
  102. w = [x.lower() for x in words]
  103. channel = args[0]
  104. command = words[0].lower()
  105. if chat == irc:
  106. print("IRC->IDC")
  107. relay_msgs(idc, hostmask, args, 0)
  108. else:
  109. relay_msgs(irc, hostmask, args, 1)
  110. print("IDC->IRC")
  111. @miniirc.Handler('CHANMSG', colon=False)
  112. def handle_privmsgs(chat, hostmask, args):
  113. words = args[-1].split(" ")
  114. w = [x.lower() for x in words]
  115. channel = args[0]
  116. command = words[0].lower()
  117. if chat == irc:
  118. print("IRC->IDC")
  119. relay_msgs(idc, hostmask, args, 0)
  120. else:
  121. relay_msgs(irc, hostmask, args, 1)
  122. print("IDC->IRC")
  123. @miniirc.Handler('MODE', colon=False)
  124. def handle_mode(chat, hostmask, args):
  125. if chat == irc:
  126. relay_mode(irc, hostmask, args, 0)
  127. elif chat == idc:
  128. relay_mode(idc, hostmask, args, 1)