This repository has been archived by the owner on Feb 25, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
slowircbot.py
executable file
·145 lines (130 loc) · 3.65 KB
/
slowircbot.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
#!/bin/python3
# https://www.tutorialspoint.com/python/python_command_line_arguments.htm
# Inspired by Aswin Ganesh
# https://agzuniverse.blogspot.com/2016/05/irc-bot-in-python-tutorial.html
import sys
import getopt
import math
import time
import socket
import re
# PURPLE = '\033[95m'
# CYAN = '\033[96m'
# DARKCYAN = '\033[36m'
# BLUE = '\033[94m'
# GREEN = '\033[92m'
# YELLOW = '\033[93m'
# RED = '\033[91m'
# BOLD = '\033[1m'
# UNDERLINE = '\033[4m'
# END = '\033[0m'
iskick=0
isban=0
waitingtime=30
server="irc.freenode.net"
port=6667
botnick="SlowDownBot"
channel="#testslowbot"
# CLI Arguments
try:
opts, args = getopt.getopt(
sys.argv[1:],
"hkbs:p:n:c:t:",
["help", "kick", "ban", "server=", "port=", "nick=", "channel=", "time="]
)
except getopt.GetoptError:
print('slowircbot.py [options]')
sys.exit(2)
for opt, arg in opts:
if opt in ('-h','--help'):
print('''\
slowircbot.py [options]
\033[1mOPTIONS:\033[0m
-h, --help Show this help message
-k, --kick Kick people who do not respect the curfew (default off)
-b, --ban Ban people who do not respect the curfew (default off)
-s, --server IRC Server (default irc.freenode.net)
-p, --port IRC Server Port (default 6667)
-n, --nick Nick of bot (default SlowDownBot)
-c, --channel Channel to enforce curfew on (with #)
-t, --time Waiting time of curfew in seconds (default 30)
''')
sys.exit()
elif opt in ('-k', '--kick'):
iskick=1
elif opt in ('-b', '--ban'):
isban=1
elif opt in ('-s', '--server'):
server=str(arg)
elif opt in ('-p', '--port'):
port=int(arg)
elif opt in ('-n', '--nick'):
botnick=str(arg)
elif opt in ('-c', '--channel'):
channel=str(arg)
elif opt in ('-t', '--time'):
waitingtime=int(arg)
#Establish connection
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
irc.connect((server,port))
irc.setblocking(False)
time.sleep(1)
irc.send(("USER "+botnick+" "+botnick+" "+botnick+" :Hello! I am a slowdown bot!\r\n").encode())
time.sleep(1)
irc.send(("NICK "+botnick+"\n").encode())
time.sleep(1)
irc.send(("JOIN "+channel+"\n").encode())
print("starting...")
time.sleep(1)
timer=0
writingmode=1
waitingtime=waitingtime*2
hasbeenactive=0
while 1:
time.sleep(0.5)
try:
text=irc.recv(2040)
except Exception:
pass
if str(text).find("PING") != -1:
irc.send(("PONG "+str(text).split()[1]+"\r\n").encode())
# testing
# if str(text).lower().find(":@hi")!=-1:
# irc.send(("PRIVMSG "+channel+" :Hello!\r\n").encode())
# end testing
# police
if str(text).find("PRIVMSG "+channel) != -1:
print(str(text))
hasbeenactive=1
if writingmode != 1 and timer > 2:
# get nick of curfew breaker
try:
evilnick = re.search(':.{2,16}(?=!)', str(text)).group(0)[1:]
except Exception:
print('No nick found')
else:
# policing evilnick
if iskick == 1:
irc.send(('KICK '+channel+' '+evilnick+' :Posting during curfew.\r\n').encode())
print('Kicking '+evilnick)
elif isban == 1:
irc.send(('BAN '+channel+' '+evilnick+' :Posting during curfew.\r\n').encode())
print('Banning '+evilnick)
else:
irc.send(("PRIVMSG "+channel+" :"+evilnick+" Curfew is in effect for "+str( math.floor((waitingtime - timer)/2) )+" seconds.\r\n").encode())
# street lights
if timer == waitingtime:
timer=0
if writingmode == 1 and hasbeenactive == 1:
irc.send(("PRIVMSG "+channel+" :Please stop posting for "+str( math.floor(waitingtime/2) )+" seconds.\r\n").encode())
writingmode = 0
elif writingmode !=1:
if hasbeenactive == 1:
irc.send(("PRIVMSG "+channel+" :"+str( math.floor(waitingtime/2) )+" seconds are up.\r\n").encode())
writingmode = 1
hasbeenactive = 0
else:
timer+=1
# testing
# print(str(text))
text=""