from PyQt5.QtCore import QUrl, QThread, pyqtSignal, QObject, pyqtSlot, Qt
from PyQt5.QtQuick import QQuickView, QQuickItem, QQuickWindow
import IrcConnection
import Channel
import User
class IrcHandler(QObject):
sig = pyqtSignal(str,str)
join_sig = pyqtSignal(str)
def __init__(self, window):
super(self.__class__, self).__init__()
self.channels = {}
self.nick = "wtest"
self.server = "irc.blatech.net"
self.irc = IrcConnection.ircConnectThread(self)
self.window = window
def get_channels():
return self.channels
def set_channels(channels):
self.channels = channels
def add_channel(self, channel):
self.channels[channel.get_name()] = channel
def get_server():
return self.server
def set_server(server):
self.server = server
def get_nick(self):
return self.nick
def set_nick(self, nick):
self.nick = nick
@pyqtSlot(str)
def join_channel(self, channelStr):
res = self.irc.join_channel(channelStr)
if (res):
channel = Channel.Channel(channelStr)
self.add_channel(channel)
self.window.channel_tabs.add_channel(channelStr)
channel_view = self.window.view.findChild(QQuickItem, "channel-" + channelStr)
send_button = channel_view.findChild(QQuickItem, "send_button")
input_field = channel_view.findChild(QQuickItem, "input")
send_button.clicked.connect(self.handle_input)
input_field.send_to_irc.connect(self.handle_input)
channel.set_view(channel_view)
def handle_input(self):
current_channel = self.window.channel_tabs.property('currentItem').property('text')
text = self.window.get_input(current_channel)
if (text.startswith("/")):
command = text.split(" ")[0][1:]
if (command == "join"):
channel = text.split(" ")[1]
self.join_channel(channel)
else:
self.sig.emit(current_channel, text)
self.window.update_chat(current_channel, "<" + self.nick + "> " + text)
self.window.reset_input(current_channel)
@pyqtSlot(str, str, str)
def handle_msg(self, channelStr, nick, msg):
if channelStr in self.channels:
channel = self.channels[channelStr]
self.window.update_chat(channelStr, "<" + nick + "> " + msg)
@pyqtSlot(str, str)
def handle_nicks(self, channelStr, nicks):
nick_listStr = nicks.split(" ")[0:-1]
if (channelStr in self.channels):
channel = self.channels.get(channelStr)
user_list = channel.get_view().findChild(QQuickItem, "nick_list")
for nickStr in nick_listStr:
if (nickStr[0] in ["@", "+", "~", "%"]):
mode = nickStr[0]
nickStr = nickStr[1:]
else:
mode = ""
user = User.User(nickStr, mode)
channel.add_user(user)
user_list.add_nick(nickStr, mode)
def handle_join(self, channelStr, nickStr):
if (channelStr in self.channels):
channel = self.channels.get(channelStr)
user_list = channel.get_view().findChild(QQuickItem, "nick_list")
chat_area = channel.get_view().findChild(QQuickItem, "chat_area")
if (nickStr != self.get_nick()):
if (nickStr[0] in ["@", "+", "~", "%"]):
mode = nickStr[0]
nickStr = nickStr[1:]
else:
mode = ""
user = User.User(nickStr, mode)
channel.add_user(user)
user_list.add_nick(nickStr, mode)
chat_area.append(nickStr + " joined " + channelStr)
def handle_part(self, channelStr, user):
if (channelStr in self.channels):
channel = self.channels.get(channelStr)
print(channel.get_users())
user_list = channel.get_view().findChild(QQuickItem, "nick_list")
chat_area = channel.get_view().findChild(QQuickItem, "chat_area")
if (user != self.get_nick()):
user_list.remove_nick(user)
channel.remove_user(user)
chat_area.append(user + " left " + channelStr)
def handle_topic(self, channelStr, topic):
if (channelStr in self.channels):
channel = self.channels.get(channelStr)
channel.set_topic(topic)
self.window.update_topic(channel, topic)
def connect_to_irc(self, view):
self.irc.sig.connect(self.handle_msg)
self.irc.join_sig.connect(self.join_channel)
self.irc.namreply_sig.connect(self.handle_nicks)
self.irc.nick_sig.connect(self.handle_join)
self.irc.part_sig.connect(self.handle_part)
self.irc.topic_sig.connect(self.handle_topic)
# self.irc.join_sig.connect(self.join_channel)
self.irc.start()
self.sig.connect(self.irc.send_msg)