-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
120 lines (86 loc) · 2.63 KB
/
bot.py
File metadata and controls
120 lines (86 loc) · 2.63 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
112
113
114
115
116
117
118
119
120
import sys
import socket
import string
import os
import time
#HOST='irc.unixfreunde.de'
HOST='irc.freenode.net'
PORT=6667
NICK='system_pybot'
CHANNEL='#wemakeporn'
def parse(data):
print data
try:
data=data.rstrip()
dlist=data.split()
#server ping
if(dlist[0]=='PING'):
#pong back to server
sendcmd('PONG %s' % dlist[1].split(':')[1])
else:
user=dlist[0].lstrip(':').split('!')[0]
#private message
if(dlist[1]=='PRIVMSG'):
if((dlist[2]==NICK) & (checkuser(user))):
msg = data.split(':')[2]
cmd = msg.split()[0]
params = msg.lstrip('%s ' % cmd)
if(cmd == '.read'):
pastefile(params.split()[0])
elif(cmd == '.say'):
sendcmsg(params)
elif(cmd == '.do'):
sendcmd(params)
else:
errormsg(user, 404)
else:
errormsg(user, 403)
#events: join
elif(dlist[1]=='JOIN'):
if((dlist[2].lstrip(':')==CHANNEL) & (checkuser(user))):
chanmode('+o', user)
except Exception as e:
print e
if (user):
errormsg(user, 500)
def sendcmsg(msg):
sendmsg(CHANNEL, msg)
def sendmsg(target, msg):
sendcmd('PRIVMSG %s :%s' % (target, msg))
def errormsg(target, code):
msg = '#%s: ' % code
if(code == 403):
msg += 'permission denied'
elif(code == 404):
msg += 'not found'
elif(code == 500):
msg += 'internal bot error'
sendmsg(target, msg)
def pastefile(filename):
sendcmsg('- START %s -' % filename)
f=open(filename)
for l in f:
sendcmsg(l)
f.close()
sendcmsg('- END -')
def checkuser(user):
f=open('operators.conf')
for l in f:
if (user == l):
return True
f.close()
return False
def chanmode(mode, target):
sendcmd('MODE %s %s %s' % (CHANNEL, mode, target))
def sendcmd(cmd):
s.send('%s\r\n' % cmd)
time.sleep(1)
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
sendcmd('NICK %s' % NICK)
sendcmd('USER %s %s %s :%s %s' % (NICK, NICK, NICK, NICK, 'IRC'))
sendcmd('JOIN %s' % CHANNEL)
sendcmsg('> HELLO <')
while 1:
data=s.recv(4096)
parse(data)