-
Notifications
You must be signed in to change notification settings - Fork 3
/
fbot.py
executable file
·139 lines (116 loc) · 3.25 KB
/
fbot.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
#!/usr/bin/env python
import requests # http requests
import websocket # websocket connections
import sqlite3
# system libraries
import json # json
import argparse
import threading # lock
import time # sleep()
from varspace.settings import *
import botpackage
from botpackage.helper.mystrip import _space_chars, stripFromBegin
from botpackage.helper.split import split_with_quotation_marks
def on_close(ws):
print('ws closed')
def on_error(ws, error):
print('ws error: ' + error)
args = None
db_connection = sqlite3.connect('varspace/fbotdb.sqlite')
def on_message(ws, message):
messageDecoded = json.loads(message)
chatPost = messageDecoded['message']
print('received', repr(messageDecoded['message']))
if int(messageDecoded['bottag']) != 0:
return
args = split_with_quotation_marks(chatPost)
for bot in botpackage.__all__:
answer = bot.processMessage(args, messageDecoded, db_connection)
if answer is not None:
send(ws, answer['name'], answer['message'], messageDecoded['id']+1)
def send(ws, name, chatPost, position=0):
message = dict(
name = name,
message = chatPost,
delay = position,
publicid = '1',
bottag = 1
)
with sending_lock:
ws.send(json.dumps(message))
time.sleep(_time_between_botposts)
def getCookies():
while True:
try:
req = requests.post('https://chat.qed-verein.de/rubychat/account', data=credentials)
except ConnectionResetError as e:
print('Error while downloading cookies:', e)
else:
return req.cookies
time.sleep(1)
def create_ws(cookies, args):
try:
cookies['userid']
cookies['pwhash']
except AttributeError:
print('cookies not right')
return
ws = websocket.WebSocketApp('wss://chat.qed-verein.de/websocket?channel=' + args['channel'] + '&position=-0&version=2',
cookie = format_cookies(dict(
userid = cookies['userid'],
pwhash = cookies['pwhash'],
)),
on_message = on_message,
on_error = on_error,
on_close = on_close,
)
return ws
def format_cookies(obj):
retval = ''
for key in obj:
retval += key + '=' + obj[key] + ';'
return retval
def mainloop(args):
parser = argparse.ArgumentParser()
parser.add_argument('-i', '--interactive', action='store_true')
parser.add_argument('--channel', default='fbot')
parser.add_argument('--mainchannel-on-my-own-risk', action='store_true')
parsedArgs = vars(parser.parse_args())
if parsedArgs['interactive'] == True:
print('fbot interactive mode. first word will be used as nick')
eiDii = 0
while True:
eiDii += 1
try:
inp = input('> ')
except EOFError:
exit(0)
except:
raise
inpSplit = split_with_quotation_marks(inp)
message = {
'name': ''.join(inpSplit[:1]),
'username' : None,
'message': stripFromBegin(inp, inpSplit[:1]),
'id' : eiDii,
}
for bot in botpackage.__all__:
x = bot.processMessage(inpSplit[1:], message, db_connection)
if x is not None:
print(repr(x['name']), ':', repr(x['message']))
print()
if parsedArgs['mainchannel_on_my_own_risk'] == True:
parsedArgs['channel'] = ''
cookies = getCookies()
while True:
print('creating new websocket')
ws = create_ws(cookies, parsedArgs)
if ws:
ws.run_forever()
time.sleep(1)
if __name__ == '__main__':
sending_lock = threading.Lock()
try:
mainloop(args)
except KeyboardInterrupt:
pass