/
ircconnector.py
65 lines (52 loc) · 2.06 KB
/
ircconnector.py
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
# -*- coding: utf-8 -*-
import socket
import ssl
import threading
from setting import server, port, botname, botnick
from ircmessage import IRCMessage
from queue import Queue
class IRCConnector(threading.Thread):
ircsock = None
msgQueue = None
def __init__(self, msgQueue):
threading.Thread.__init__(self)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((server, port))
self.ircsock = ssl.wrap_socket(s)
self.ircsock.send(('USER ' + (botname + ' ') * 3 + ':' +
botnick + '\n').encode())
self.ircsock.send(('NICK ' + botnick + '\n').encode())
self.msgQueue = msgQueue
def ping(self):
self.ircsock.send(('PONG :pingis\n').encode())
def sendmsg(self, chan, msg):
self.ircsock.send(('PRIVMSG ' + chan + ' :' + msg + '\n').encode())
def joinchan(self, chan):
self.ircsock.send(('JOIN ' + chan + '\n').encode())
def partchan(self, chan):
self.ircsock.send(('PART ' + chan + '\n').encode())
def chanlist(self):
self.ircsock.send(('WHOIS ' + botnick + '\n').encode())
def settopic(self, chan, msg):
self.ircsock.send(('TOPIC ' + chan + ' :' + msg + '\n').encode())
def gettopic(self, chan):
self.ircsock.send(('LIST ' + chan + '\n').encode())
ircmsg = self.ircsock.recv(8192)
topic = (ircmsg.decode().split('\n')[1]).split(':')[2].strip('\n\r')
return topic
def run(self):
while True:
ircmsg = self.ircsock.recv(8192)
try:
ircmsg = ircmsg.decode().strip('\n\r')
except e:
print(e)
else:
print(ircmsg)
message = IRCMessage(ircmsg)
if message.isValid():
print(message)
if message.msgType == 'PING':
self.ping()
else:
self.msgQueue.put({'type': 'irc', 'content': message})