-
Notifications
You must be signed in to change notification settings - Fork 1
/
ircbot27.py
208 lines (164 loc) · 7.61 KB
/
ircbot27.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#############################################################
# #
# This program is relased in the GNU GPL v3.0 licence #
# you can modify/use this program as you wish. Please #
# link the original distribution of this software. If #
# you plan to redistribute your modified/copied copy #
# you need to relased the in GNU GPL v3.0 licence too #
# according to the overmentioned licence. #
# #
# "PROUDLY" MADE BY chkrr00k (i'm not THAT proud tbh) #
# #
#############################################################
# #
# I have tried to use a good mvc modelling while let- #
# letting the maximum usability, modularity and mode- #
# lling #
# #
#############################################################
import sys
import socket
import string
import ssl
#global variable with the bot's info
HOST = "ayy.lmao"
NICK = "nickname"
IDENT = "ident"
REALNAME = "realname"
##MODEL
class Server:
# private sock
# private stringBuffer
def __init__(self, server, port=6667, sslU=False):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
if sslU:
self.sock = ssl.wrap_socket(self.sock)
self.sock.connect((server, port))
self.stringBuffer = ""
def write(self, message):
self.sock.send(message)
def read(self):
#old string + string just read.
self.stringBuffer = self.stringBuffer + self.sock.recv(1024)
#split the string in corrispondence of "\n" to have a list of message ("\r" is still there)
tmp = self.stringBuffer.split("\n")
#
stringBuffer = tmp.pop()
return tmp
class Irc:
# private ServerObj
def __init__(self, server, port=6667, sslU=False):
self.ServerObj = Server(server, port,sllU);
self.lastString = ""
self.messages = []
def pingHandler(self, line):
self.ServerObj.write("PONG " + (line.split() [1]) + "\r\n")
def messageHandler(self, line):
tokLine = line.split()
nickName = ((tokLine[0]).split("!")[0]).strip()[1:]
channel = (tokLine[2]).strip()
message = line.split(" :")[1]
return nickName, channel, message
def modeHandler(self, line):
pass
def quitHandler(self, line):
tokLine = line.split()
nickName = ((tokLine[0]).split("!")[0]).strip()[1:]
return nickName
def partHandler(self, line):
tokLine = line.split()
nickName = ((tokLine[0]).split("!")[0]).strip()[1:]
channel = tokLine[2]
return nickName, channel
def joinHandler(self, line):
tokLine = line.split()
nickName = ((tokLine[0]).split("!")[0]).strip()[1:]
channel = line.split(" :")[1]
return nickName, channel
def inviteHandler(self, line):
return (line.split(" ")[3])[1:]
def sendMessage(self, channel, message):
self.ServerObj.write("PRIVMSG " + channel + " :" + message +"\r\n")
def sendNotice(self, channel, message):
self.ServerObj.write("NOTICE " + channel + " :" + message +"\r\n")
def modeHandler(self, line):
pass
def setMode(self, channel, moded, mode = ""):
self.ServerObj.write("MODE " + channel + " " + mode + " " + moded + "\r\n")
def readline(self):
return self.ServerObj.read()
def joinChannel(self, channel):
self.ServerObj.write("JOIN " + channel + "\r\n")
def connect(self):
self.ServerObj.write("NICK " + NICK + "\r\n")
self.ServerObj.write("USER " + IDENT + " " + HOST + " ayy :" + REALNAME + "\r\n")
def kick(self, channel, kicked, message=""):
self.ServerObj.write("KICK " + channel + " " + kicked + " :" + message + "\r\n")
def ban(self, channel, banned, message=""):
self.setMode(channel, "+b", banned)
def kickAndBan(self, channel, banned, message=""):
self.kick(channel, banned, message)
self.ban(channel, banned, message)
def quit(self, message=""):
self.ServerObj.write("QUIT" + " :" + message + "\r\n")
def nickChange(self, newNick):
self.ServerObj.write("NICK" + " " + newNick + "\r\n")
##CONTROLLER##
irc = Irc("server.name")
print("connecting")
irc.connect()
auth = 0
number = 0
channels = ["#chan", "#chan2"] #list of channels to join
instructionsOP = {".op" : "+o", ".deop" : "-o", ".protect" : "+a", ".deprotect" : "-a", ".voice" : "+v", ".devoice" : "-v", ".hop" : "+h", ".dehop" : "-h"} #possible commands form chat line (command : mode)
instructionsPR = {".k" : irc.kick, ".kb" : irc.kickAndBan, ".b" : irc.ban, ".quit" : irc.quit, ".nick" : irc.nickChange}
authorized = {"#chan" : {"authNickName" : [".op", ".deop", ".hop", ".dehop", ".voice",".devoice", ".k"]}} #authorized users + list of commands they can use
while 1:
msgList = irc.readline()
for msg in msgList:
if msg.startswith("PING"):
irc.pingHandler(msg.rstrip())
print("PONGED")
if msg.find("PRIVMSG") > -1:
nick, chan, messS = irc.messageHandler(msg)
print nick + " " + chan + " " + messS
mess = messS.split(" ")
# bot behaviour at messages promotion for user
if mess[0].strip() in instructionsOP and chan.strip().lower() in authorized:
if nick in authorized[chan.strip()]:
for mode in (authorized[chan.strip().lower()])[nick.strip()]:
if mess[0].strip() == mode:
if len(mess) > 1:
irc.setMode(chan, mess[1], instructionsOP[mess[0].strip()])
else:
irc.setMode(chan, nick, instructionsOP[mess[0].strip()])
if mess[0].strip() in instructionsPR and chan.strip().lower() in authorized:
if nick in authorized[chan.strip()]:
for command in (authorized[chan.strip().lower()])[nick.strip()]:
if mess[0].strip() == command:
if len(mess) == 1:
instructionsPR[mess[0]](chan, mess[1])
elif len(mess) > 1:
instructionsPR[mess[0]]("".join(str(x) for x in mess))
if msg.find("NOTICE") > -1:
nick, chan, mess = irc.messageHandler(msg)
print nick + " " + chan + " " + mess
if msg.find("INVITE") > -1:
irc.joinChannel(irc.inviteHandler(msg))
if msg.find("MODE") > -1:
irc.modeHandler(msg)
if msg.find("QUIT") > -1:
nick = irc.quitHandler(msg)
print nick + " has quit"
if msg.find("PART") > -1:
nick, chan = irc.partHandler(msg)
print nick + " has left " + chan
if msg.find("JOIN") > -1:
nick, chan = irc.joinHandler(msg)
print nick + " has join " + chan
if (number == 6) and not auth:
auth = 1
for chan in channels:
irc.joinChannel(chan)
print "joined to " + chan
number += 1