forked from rahuldecoded/nutrient-bot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
111 lines (87 loc) · 3.22 KB
/
run.py
File metadata and controls
111 lines (87 loc) · 3.22 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
import os
import re
import socket
import traceback
from replies import reply, find_in_Replies
def sendmsg(chan, msg):
global irc
irc.send("PRIVMSG ".encode() + chan.encode() +" :".encode() + msg.encode() + "\n".encode())
# This is a subroutine which help to join a specified channel
def JoinChan(chan):
global irc
irc.send("JOIN ".encode() + chan.encode() + "\n".encode())
# This is a subroutine which responds to server pings
def ping():
global irc
irc.send("PONG :pingis\n".encode())
# This is a main routine
def main():
global irc, user_queue
botnick = os.environ.get("NB_USER", "nutrient-bot")
bufsize = 2048
admin = ["rahuldecoded"]
channel = os.environ.get("NB_CHANNEL", "#uit-foss")
port = int(os.environ.get("NB_PORT", 6667))
server = os.environ.get("NB_SERVER", "irc.freenode.net")
master = "rahuldecoded"
uname = "Nutrient Bot"
realname = "I'm a Test Bot!"
irc = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
irc.connect((server, port))
irc.send("USER ".encode() + botnick.encode() + " ".encode() + botnick.encode() + " ".encode()
+ botnick.encode() + " :Hello! I am a test bot!\r\n".encode())
irc.send("NICK ".encode() + botnick.encode() + "\n".encode())
JoinChan(channel)
pattern1 = '.*:(\w+)\W*%s\W*$' % botnick
pattern2 = '.*:%s\W*(\w+)\W*$' % botnick
while 1:
try:
msg = irc.recv(bufsize)
ircmsg = msg.decode()
ircmsg = ircmsg.strip('\n\r')
print(ircmsg)
m1 = re.match(pattern1, ircmsg, re.I)
m2 = re.match(pattern2, ircmsg, re.I)
if (m1 == None) and (m2 != None):
m1 = m2
if (m1 != None): # Yes
word = m1.group(1) # Word found
word = word.lower() # Make word lower case
# Print a reply
if find_in_Replies(word):
sendmsg(channel, reply(word))
except Exception:
pass
# Admin Commands
# For showing the length of the queue.
tokens = ircmsg.split(" ")
if tokens[0] == "PING":
ping()
continue
if tokens[1] != "PRIVMSG":
continue
try:
request = tokens[3]
tokenLength = len(tokens)
# For adding someone as a admin
if request == "::add":
if tokenLength > 4:
if ircmsg.strip(":").split("!")[0] in admin:
admin.append(tokens[1])
elif tokenLength == 4:
sendmsg(channel, "Usage: :add [nick]")
# For removing someone from admin privilege.
if request == "::remove" and tokenLength > 4:
if ircmsg.strip(":").split("!")[0] in admin:
try:
admin.remove(tokens[1])
except ValueError:
return tokens[1] + "is not in admin list."
elif tokenLength == 4:
sendmsg(channel, "Usage: :remove [nick]")
# User Commands
except Exception as e:
tb = traceback.format_exc()
print(tb)
main()
exit(0)