-
Notifications
You must be signed in to change notification settings - Fork 1
/
toys.py
211 lines (168 loc) · 4.78 KB
/
toys.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
from module import XMPPModule
from subprocess import check_output as px
class Toys(XMPPModule):
cowsay = False
fortune = False
pom = False
morse = False
ppt = False
bcd = False
def init(self):
self.enable_features()
def help(self, feature):
d = " [Disabled]"
if feature in ['cowsay', '!cowsay']:
return '''
A cowfigurable cow to play with.
usage: !cowsay [arguments to cowsay]
usage: !cowsay <!fortune|!pom>{}
'''.format("\n\nNote: The binary for this could not be found, please install cowsay to use this feature" if not self.cowsay else "")
if feature in ['bcd', '!bcd']:
return '''
Displays input as a punch card.
usage: !bcd [arguments to bcd]
usage: !bcd <!fortune|!pom>{}
'''.format("\n\nNote: The binary for this could not be found, please install bcd to use this feature" if not self.bcd else "")
if feature in ['ppt', '!ppt']:
return '''
Displays input as a paper tape.
usage: !ppt [arguments to ppt]
usage: !ppt <!fortune|!pom>{}
'''.format("\n\nNote: The binary for this could not be found, please install ppt to use this feature" if not self.ppt else "")
if feature in ['morse', '!morse']:
return '''
Displays input as a morse code.
usage: !morse [arguments to morse]
usage: !morse <!fortune|!pom>{}
'''.format("\n\nNote: The binary for this could not be found, please install morse to use this feature" if not self.morse else "")
if feature in ['fortune', '!fortune']:
return '''
Forsees the future.
usage: !fortune{}
'''.format("\n\nNote: The binary for this could not be found, please install fortune to use this feature" if not self.fortune else "")
if feature in ['pom', '!pom']:
return '''
The current phase of the moon.
usage: !pom
'''.format("\n\nNote: The binary for this could not be found, please install pom to use this feature" if not self.pom else "")
return '''
A modules to provides some nice toys to play with.
Module features:
cowsay - A configurable cow to play with.abs{}
fortune - Forsees the future.{}
pom - The current phase of the moon.{}
bcd - Display input as a punch card.{}
ppt - Display input as paper tape.{}
morse - Display input as morse code.{}
'''.format(d if not self.cowsay else "",
d if not self.fortune else "",
d if not self.pom else "",
d if not self.bcd else "",
d if not self.ppt else "",
d if not self.morse else "")
def enable_features(self):
try:
px(["cowsay", "test"])
self.cowsay = True
except:
self.cowsay = False
try:
px(["bcd", "test"])
self.bcd = True
except:
self.bcd = False
try:
px(["ppt", "test"])
self.ppt = True
except:
self.ppt = False
try:
px(["morse", "test"])
self.morse = True
except:
self.morse = False
try:
px(["fortune"])
self.fortune = True
except:
self.fortune = False
try:
px(["pom"])
self.pom = True
except:
self.pom = False
def handleMessage(self, msg):
command, string = (msg['body'].split(" ")[0]," ".join(msg['body'].split(' ')[1:]))
if command in self.commands.keys():
reply = self.commands[command](self, string)
self.xmpp.reply(msg, reply) if reply else None
def get_fortune(self):
if not self.fortune:
return None
try:
out = px("fortune",universal_newlines=True)
except:
return None
return out.rstrip()
def get_pom(self):
if not self.pom:
return None
try:
out = px("pom",universal_newlines=True)
except:
return None
return out
def handle_fortune(self, string):
return self.get_fortune()
def handle_pom(self, string):
return self.get_pom()
def pipable_string(self, string):
if string == "!fortune":
string = self.get_fortune()
if string == "!pom":
string = self.get_pom()
return string
def handle_cowsay(self, string):
if not self.cowsay or not string:
return None
string = self.pipable_string(string)
try:
out = px(["cowsay"] + string.split(" "),universal_newlines=True)
except:
return None
return '\n' + out
def handle_bcd(self, string):
if not self.bcd or not string:
return None
string = self.pipable_string(string)
try:
out = px(["bcd"] + string.split(" "),universal_newlines=True)
except:
return None
return '\n' + out
def handle_ppt(self, string):
if not self.ppt or not string:
return None
string = self.pipable_string(string)
try:
out = px(["ppt"] + string.split(" "),universal_newlines=True)
except:
return None
return '\n' + out
def handle_morse(self, string):
if not self.morse or not string:
return None
string = self.pipable_string(string)
try:
out = px(["morse"] + string.split(" "),universal_newlines=True)
except:
return None
return '\n' + out
commands = {
"!cowsay":handle_cowsay,
"!fortune":handle_fortune,
"!pom":handle_pom,
"!bcd":handle_bcd,
"!ppt":handle_ppt,
"!morse":handle_morse
}