-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchatdj.py
144 lines (114 loc) · 4.03 KB
/
chatdj.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
#!/usr/bin/env python3
# simpleircbot.py - A simple IRC-bot written in python
#
# Copyright (C) 2015 : Niklas Hempel - http://liq-urt.de
# Modified by mcen1 on github 2019
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
import re
import socket
import cgi
from playsound import playsound
with open('config.txt','r') as inf:
mydict = eval(inf.read())
HOST = str(mydict["host"])
PORT = int(mydict["port"])
CHAN = str(mydict["channel"])
NICK = str(mydict["username"])
PASS = str(mydict["oauth"])
def send_pong(msg):
con.send(bytes('PONG %s\r\n' % msg, 'UTF-8'))
def send_message(chan, msg):
con.send(bytes('PRIVMSG %s :%s\r\n' % (chan, msg), 'UTF-8'))
def send_nick(nick):
con.send(bytes('NICK %s\r\n' % nick, 'UTF-8'))
def send_pass(password):
con.send(bytes('PASS %s\r\n' % password, 'UTF-8'))
def join_channel(chan):
con.send(bytes('JOIN %s\r\n' % chan, 'UTF-8'))
def part_channel(chan):
con.send(bytes('PART %s\r\n' % chan, 'UTF-8'))
# --------------------------------------------- End Functions ------------------------------------------------------
# --------------------------------------------- Start Helper Functions ---------------------------------------------
def get_sender(msg):
result = ""
for char in msg:
if char == "!":
break
if char != ":":
result += char
return result
def get_message(msg):
result = ""
i = 3
length = len(msg)
while i < length:
result += msg[i] + " "
i += 1
result = result.lstrip(':')
return result
def parse_message(msg):
if len(msg) >= 1:
msg = msg.split(' ')
options = {'!test': command_test,
'!asdf': command_asdf}
if msg[0] in options:
options[msg[0]]()
# --------------------------------------------- End Helper Functions -----------------------------------------------
# --------------------------------------------- Start Command Functions --------------------------------------------
def command_test():
send_message(CHAN, 'testing some stuff')
def command_asdf():
send_message(CHAN, 'asdfster')
# --------------------------------------------- End Command Functions ----------------------------------------------
print("Starting up...")
con = socket.socket()
print("Connecting to "+HOST+" on port "+str(PORT))
con.connect((HOST, PORT))
print("Sending password")
send_pass(PASS)
print("Sending username")
send_nick(NICK)
print("Joining channel...")
join_channel(CHAN)
print("Maybe joined?")
data = ""
while True:
try:
data = data+con.recv(1024).decode('UTF-8')
data_split = re.split(r"[~\r\n]+", data)
data = data_split.pop()
for line in data_split:
line = str.rstrip(line)
line = str.split(line)
if len(line) >= 1:
if line[1] == 'PRIVMSG':
sender = get_sender(line[0])
message = get_message(line)
parse_message(message)
print(sender + ": " + message)
if str(message).startswith("!sound"):
mysong=message.split(" ")[1]
try:
playsound('./'+cgi.escape(str(mysong).replace('.','').replace('/','').replace("\\",'').replace('"','').replace("'","").replace('?','').replace('!','').replace('*','').replace('$',''))+'.mp3')
except:
pass
if line[0] == 'PING':
send_pong(line[1])
except socket.error:
print("Socket died")
except socket.timeout:
print("Socket timeout")
except:
pass