-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmessages.py
256 lines (172 loc) · 6.8 KB
/
messages.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# -*- coding: utf-8 -*-
import config
import os
import inspect
import sys
from datetime import datetime as dt
from models import *
class MessageParser(object):
def __init__(self):
self.types = self.get_message_types()
def parse(self, user, message_string):
for message_type in self.types:
if message_type.handles(message_string):
return message_type(user, message_string).execute()
return Message(message_string, user)
def get_message_types(self):
types = inspect.getmembers(sys.modules[__name__], inspect.isclass)
return (t[1] for t in types if issubclass(t[1], BaseMessage))
class BaseMessage(object):
def __init__(self):
pass
@staticmethod
def handles(message):
return False
class PrivateMessage(BaseMessage):
"""@<user> - Private Nachricht an @<user> senden"""
def __init__(self, user, message_string):
self.message_string = message_string
self.user = user
self.recipient = Users().find_by_name(
self.message_string.split()[0][1:])
def execute(self):
return Message(self.message_string, self.user, visible_to=[
self.user, self.recipient])
@staticmethod
def handles(message):
return Message.is_private(message)
class SmsMessage(BaseMessage):
"""/sms <user> <text>
- SMS mit <text> an <user> versenden"""
def __init__(self, user, message_string):
self.user = user
self.recipient = Users().find_by_name(message_string.split()[1])
self.text = " ".join(message_string.split()[2:])
def execute(self):
SMS(self.user, self.recipient, self.text).send()
message = "SMS gesendet an {0}".format(self.recipient.username)
return Message(message, Users().alfabot(), visible_to=[self.user])
@staticmethod
def handles(message):
return message.startswith("/sms")
class GuestMessage(BaseMessage):
"""/guest - Letzten Tweet des Gastes anzeigen"""
def __init__(self, user, message_string):
self.user = user
def execute(self):
tweet = Tweet()
if tweet.is_new():
return Message(tweet.text, Users().guest())
else:
return Message("Tweet wurde schon gelesen.",
Users().alfabot(), visible_to=[self.user])
@staticmethod
def handles(message):
return message.startswith("/guest")
class AnnouncementMessage(BaseMessage):
"""/announce <text> - Öffentliche Kundmachung versenden"""
def __init__(self, user, message_string):
self.user = user
self.message_string = message_string
self.text = "<b>++++Öffentliche Kundmachung++++</b> <br/><br/>{0}"
def execute(self):
announcement_text = " ".join(self.message_string.split()[1:])
message = self.text.format(announcement_text)
return Message(message, Users().alfabot())
@staticmethod
def handles(message):
return message.startswith("/announce")
class AppointmentMessage(BaseMessage):
"""/termine - Thekentermine anzeigen"""
def __init__(self, user, message_string):
self.message_string = message_string
self.user = user
def execute(self):
return Message(self.get_appointments(),
Users().alfabot(),
visible_to=[self.user])
def get_appointments(self):
if os.path.isfile(config.appointments_path):
with open(config.appointments_path, 'r') as f:
return f.read().replace("\n", " ")
else:
return "Keine Termine."
@staticmethod
def handles(message):
return message.startswith("/termine")
class ShowsMessage(BaseMessage):
"""/shows - Liste aller anstehenden Shows"""
def __init__(self, user, message_string):
self.user = user
def execute(self):
if not os.path.isfile("shows.log"):
return Message("Keine Shows", Users().alfabot(),
visible_to=[self.user])
all_shows = []
with open("shows.log", 'r') as shows:
all_shows = [Show(s) for s in shows]
all_shows = filter(lambda show: show.is_upcoming(), all_shows)
all_shows = sorted(all_shows, key=lambda show: show.date)
all_shows = [str(show) for show in all_shows]
all_shows = "<b>Shows</b><br/><br/>" + "<br/>".join(all_shows)
return Message(all_shows, Users().alfabot(), visible_to=[self.user])
@staticmethod
def handles(message):
return message.startswith("/shows")
class AddShowMessage(BaseMessage):
"""/addshow - Neue Show hinzufügen (dd.mm.yyyy bands location)"""
def __init__(self, user, message_string):
self.show = " ".join(message_string.split()[1:])
self.message = "Show added: {0}".format(self.show)
self.user = user
def execute(self):
if not self.parse_showdate():
error_msg = "Unzulässiges Datumsformat (erwartet: dd.mm.yyyy)"
alfabot = Users().alfabot()
return Message(error_msg, alfabot, visible_to=[self.user])
with open("shows.log", 'a+') as shows:
shows.write(self.show + "\n")
return Message(self.message, Users().alfabot(), visible_to=[self.user])
def parse_showdate(self):
try:
return dt.strptime(self.show.split()[0], '%d.%m.%Y')
except ValueError:
return None
@staticmethod
def handles(message):
return message.startswith("/addshow")
class HelpMessage(BaseMessage):
"""/help - Diese Hilfe anzeigen"""
def __init__(self, user, message_string):
self.user = user
def execute(self):
help_text = "<b>Hilfe</b><br/><br/>"
for message_type in MessageParser().types:
doc_str = message_type.__doc__
help_text += "{0}<br/>".format(doc_str) if doc_str else ""
return Message(help_text, Users().alfabot(), visible_to=[self.user])
@staticmethod
def handles(message):
return message.startswith("/help")
class DeleteMessage(BaseMessage):
"""/delete - Die letzte Nachricht entfernen"""
def __init__(self, user, message_string):
self.user = user
def execute(self):
with Chat() as chat:
chat.delete_latest_message_of(self.user)
return None
@staticmethod
def handles(message):
return message.startswith("/delete")
class ClearMessage(BaseMessage):
"""/clear - Entferne alle Messages vom Alfabot"""
def __init__(self, user, message_string):
self.user = user
def execute(self):
with Chat() as chat:
chat.remove_bot_messages_for(self.user)
return None
@staticmethod
def handles(message):
return message.startswith("/clear")