-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexample.py
More file actions
191 lines (141 loc) · 5.03 KB
/
example.py
File metadata and controls
191 lines (141 loc) · 5.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import sys
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from qirc import QIRC
NICKNAME = "qirc_client"
CHANNEL = "#qirc"
def writeChat(nickname,message):
INTERFACE.channelChatDisplay.append("<b>"+nickname+"</b>: "+message)
INTERFACE.channelChatDisplay.moveCursor(QTextCursor.End)
class Interface(QMainWindow):
def writeUserlist(self,userlist):
self.channelUserDisplay.clear()
for user in userlist:
ui = QListWidgetItem()
ui.setText(user.nickname)
self.channelUserDisplay.addItem(ui)
def writeText(self,data):
self.channelChatDisplay.append("<i>"+data+"</i>")
self.channelChatDisplay.moveCursor(QTextCursor.End)
def writeChat(self,nickname,text):
self.channelChatDisplay.append("<b>"+nickname+"</b>: "+text)
self.channelChatDisplay.moveCursor(QTextCursor.End)
def handleUserInput(self):
user_input = self.userTextInput.text()
self.userTextInput.setText('')
tokens = user_input.split(' ')
if len(tokens)>=2:
if tokens[0].lower()=="/join":
if len(tokens)==2:
channel = tokens[1]
self.ircClient.part(CHANNEL)
self.ircClient.join(channel)
return
elif len(tokens)>2:
tokens.pop(0)
channel = tokens.pop(0)
key = ' '.join(tokens)
self.ircClient.part(CHANNEL)
self.ircClient.join(channel,key)
return
self.ircClient.privmsg(CHANNEL,user_input)
self.writeChat(NICKNAME,user_input)
def closeEvent(self, event):
self.close()
sys.exit()
def __init__(self,parent=None):
super(Interface, self).__init__(parent)
self.channel = ""
self.setWindowTitle("QIRC Example")
self.channelChatDisplay = QTextBrowser(self)
self.channelChatDisplay.setObjectName("channelChatDisplay")
self.channelChatDisplay.setFocusPolicy(Qt.NoFocus)
self.channelUserDisplay = QListWidget(self)
self.channelUserDisplay.setObjectName("channelUserDisplay")
self.channelUserDisplay.installEventFilter(self)
self.channelUserDisplay.setFocusPolicy(Qt.NoFocus)
self.userTextInput = QLineEdit(self)
self.userTextInput.setObjectName("userTextInput")
self.userTextInput.returnPressed.connect(self.handleUserInput)
self.horizontalSplitter = QSplitter(Qt.Horizontal)
self.horizontalSplitter.addWidget(self.channelChatDisplay)
self.horizontalSplitter.addWidget(self.channelUserDisplay)
self.horizontalSplitter.setSizes([475,125])
self.verticalSplitter = QSplitter(Qt.Vertical)
self.verticalSplitter.addWidget(self.horizontalSplitter)
self.verticalSplitter.addWidget(self.userTextInput)
self.verticalSplitter.setSizes([575,25])
finalLayout = QVBoxLayout()
finalLayout.addWidget(self.verticalSplitter)
x = QWidget()
x.setLayout(finalLayout)
self.setCentralWidget(x)
self.userTextInput.setFocus()
self.ircClient = QIRC(server="localhost",port=6667,nickname=NICKNAME)
self.ircClient.server_connect.connect(self.gotConnected)
self.ircClient.server_register.connect(self.gotRegistered)
self.ircClient.nick_collision.connect(self.gotCollision)
self.ircClient.message_public.connect(self.gotPublic)
self.ircClient.message_private.connect(self.gotPrivate)
self.ircClient.message_action.connect(self.gotAction)
self.ircClient.user_list.connect(self.gotUserlist)
self.ircClient.server_error.connect(self.gotError)
self.ircClient.tick.connect(self.tick)
self.ircClient.user_join.connect(self.gotJoin)
self.ircClient.start()
def gotJoin(self,joindata):
if joindata["nickname"].lower()==NICKNAME.lower():
global CHANNEL
CHANNEL = joindata["channel"]
self.writeText("Joined "+CHANNEL)
def tick(self,uptime):
self.channelChatDisplay.update()
self.channelUserDisplay.update()
def gotError(self,errordata):
print(errordata)
def gotUserlist(self,userdata):
self.channelUserDisplay.clear()
ops = []
voiced = []
normal = []
for user in userdata["users"]:
p = user.split('!')
if len(p)==2:
nick = p[0]
else:
nick = user
if '@' in nick:
ops.append(nick)
elif '+' in nick:
voiced.append(nick)
else:
normal.append(nick)
sortedusers = ops + voiced + normal
for user in sortedusers:
ui = QListWidgetItem()
ui.setText(user)
self.channelUserDisplay.addItem(ui)
self.channelUserDisplay.repaint()
def gotAction(self,msgdata):
self.writeText("<b>"+msgdata["nickname"]+"</b> "+msgdata["message"])
self.ircClient.join("#flarp")
def gotPublic(self,msgdata):
self.writeChat(msgdata["nickname"],msgdata["message"])
def gotPrivate(self,msgdata):
self.writeChat("PRIVATE "+msgdata["nickname"],msgdata["message"])
def gotCollision(self,msgdata):
self.writeText("Your nickname is now "+msgdata["new"])
def gotRegistered(self,msgdata):
self.writeText("Registered with "+msgdata["server"]+":"+str(msgdata["port"])+"!")
self.writeText("Joining "+CHANNEL)
self.ircClient.join(CHANNEL)
def gotConnected(self,msgdata):
self.writeText("Connected to "+msgdata["server"]+":"+str(msgdata["port"])+"!")
# app = QApplication(sys.argv)
app = QApplication([])
app.setFont(QFont("Consolas",10))
INTERFACE = Interface()
if __name__ == '__main__':
INTERFACE.show()
sys.exit(app.exec())