-
Notifications
You must be signed in to change notification settings - Fork 6
/
matterfeed.py
executable file
·264 lines (238 loc) · 10.9 KB
/
matterfeed.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
257
258
259
260
261
262
263
264
#!/usr/bin/env python3
import ast
import configargparse
import fnmatch
import importlib.util
import logging
import requests
import os
import queue
import shelve
import sys
import threading
import time
import traceback
from mattermostdriver import Driver
class TokenAuth():
def __call__(self, r):
r.headers['Authorization'] = "Bearer %s" % options.Matterbot['password']
r.headers['X-Requested-With'] = 'XMLHttpRequest'
return r
class MattermostManager(object):
def __init__(self):
logQueue.put(('INFO', "Going to set up driver for connection to %s " % (options.Matterbot['host'],) ))
self.mmDriver = Driver(options={
'url' : options.Matterbot['host'],
'port' : options.Matterbot['port'],
'login_id' : options.Matterbot['username'],
'token' : options.Matterbot['password'],
'basepath' : options.Matterbot['basepath'],
'scheme' : options.Matterbot['scheme'],
'auth' : TokenAuth,
#'debug' : options.debug,
'keepalive' : True,
'keepalive_delay': 30,
'websocket_kw_args': {'ping_interval': 5},
})
self.me = self.mmDriver.users.get_user( user_id='me' )
self.my_team_id = self.mmDriver.teams.get_team_by_name(options.Matterbot['teamname'])['id']
self.channels = {}
self.test = {}
userchannels = self.mmDriver.channels.get_channels_for_user(self.me['id'],self.my_team_id)
for userchannel in userchannels:
channel_info = self.mmDriver.channels.get_channel(userchannel['id'])
self.channels[channel_info['name']] = channel_info['id']
def createPost(self, channel, text):
try:
if len(text) > options.Matterbot['msglength']: # Mattermost message limit
blocks = []
lines = text.split('\n')
blocksize = 0
block = ''
for line in lines:
lensize = len(line)
if (blocksize + lensize) < options.Matterbot['msglength']:
blocksize += lensize
block += line + '\n'
else:
blocks.append(block.strip())
blocksize = 0
block = ''
blocks.append(block.strip())
else:
blocks = [text]
for block in blocks:
self.mmDriver.posts.create_post(options={'channel_id': channel,
'message': block,
})
except:
raise
class MsgWorker(threading.Thread):
def __init__(self, mm, logQueue, msgQueue):
threading.Thread.__init__(self)
self.terminate = False
self.mm = mm
self.logQueue = logQueue
self.msgQueue = msgQueue
def HandleMsg(self):
self.logQueue.put(('INFO', 'Starting PostMsg Worker ...'))
while True:
newsItem = self.msgQueue.get()
channel, module, content = newsItem
self.logQueue.put(('INFO', 'Message : ' + module.lower() + ' => ' + channel + ' => ' + content[:20] + '...'))
self.mm.createPost(self.mm.channels[channel], content)
self.msgQueue.task_done()
def run(self):
while not self.terminate:
self.HandleMsg()
class LogWorker(threading.Thread):
def __init__(self, logQueue):
threading.Thread.__init__(self)
self.terminate = False
self.logQueue = logQueue
def HandleLog(self):
if not options.debug:
logging.basicConfig(filename=options.Matterbot['logfile'], format='%(levelname)s - %(name)s - %(asctime)s - %(message)s')
else:
logging.basicConfig(format='%(levelname)s - %(name)s - %(asctime)s - %(message)s')
log = logging.getLogger( 'MatterBot' )
if options.debug:
log.setLevel(logging.DEBUG)
else:
log.setLevel(logging.INFO)
log.info('Starting up log thread ...')
while True:
logItem = self.logQueue.get()
logLevel, logText = logItem
if logLevel.upper() == 'INFO':
log.info(logText)
elif logLevel.upper() == 'ERROR':
log.error(logText)
elif logLevel.upper() == 'DEBUG':
log.debug(logText)
else:
log.info('Unhandled log entry: ' + logText)
self.logQueue.task_done()
def run(self):
while not self.terminate:
self.HandleLog()
def loadModules():
modules = {}
modulepath = options.Modules['moduledir'].strip('/')
sys.path.append(modulepath)
for root, dirs, files in os.walk(modulepath):
for module in fnmatch.filter(files, "feed.py"):
module_name = root.split('/')[-1]
logQueue.put(('INFO', 'Attempting to load the ' + module_name + ' module...'))
module = importlib.import_module(module_name + '.' + 'feed')
modules[module_name] = getattr(module, 'query')
return modules
class ModuleWorker(threading.Thread):
def __init__(self, mm, module, logQueue, msgQueue):
threading.Thread.__init__(self)
self.terminate = False
self.mm = mm
self.module = module
self.logQueue = logQueue
self.msgQueue = msgQueue
def runModule(self):
logQueue.put(('INFO', 'Starting : ' + self.module))
try:
items = modules[self.module]()
modulepath = options.Modules['moduledir']+'/'+self.module+'/'+self.module+'.cache'
if os.path.isfile(modulepath):
if options.debug:
logQueue.put(('DEBUG', 'Found : ' + self.module + ' database at ' + modulepath + ' location...'))
with shelve.open(modulepath,writeback=True) as history:
if not self.module in history:
history[self.module] = []
first_run = True
else:
first_run = False
if len(items):
for item in items:
channel, content = item
# Deal with self-referential calls. They should always trigger and not once.
if content.startswith('@') or content.startswith('!'):
if not first_run:
if options.debug:
self.logQueue.put(('DEBUG', 'Posting : ' + self.module + ' => ' + channel + ' => ' + content + '...'))
else:
self.logQueue.put(('INFO', 'Posting : ' + self.module + ' => ' + channel + ' => ' + content[:80] + '...'))
self.msgQueue.put((channel, self.module, content))
elif not item in history[self.module]:
history[self.module].append(item)
if not first_run:
if options.debug:
self.logQueue.put(('DEBUG', 'Posting : ' + self.module + ' => ' + channel + ' => ' + content + '...'))
else:
self.logQueue.put(('INFO', 'Posting : ' + self.module + ' => ' + channel + ' => ' + content[:80] + '...'))
self.msgQueue.put((channel, self.module, content))
if options.debug:
logQueue.put(('DEBUG', 'Summary : ' + self.module + ' => '+ str(len(items)) + ' messages ...'))
history.sync()
history.close()
logQueue.put(('INFO', 'Completed: ' + self.module + ' => sleeping for ' + str(options.Modules['timer']) + ' seconds ...'))
except:
logQueue.put(('ERROR', 'Error : ' + self.module + ' => sleeping for ' + str(options.Modules['timer']) + ' seconds ...'))
time.sleep(options.Modules['timer'])
def run(self):
while not self.terminate:
self.runModule()
if __name__ == '__main__' :
'''
Interactive run from the command-line
'''
parser = configargparse.ArgParser(config_file_parser_class=configargparse.YAMLConfigFileParser,
description='Matterbot loads modules '
'and sends their output '
'to Mattermost.',
default_config_files=['config.yaml'])
parser.add('--Matterbot', type=str, help='MatterBot configuration, as a dictionary (see YAML config)')
parser.add('--Modules', type=str, help='Modules configuration, as a dictionary (see YAML config)')
parser.add('--debug', default=False, action='store_true', help='Enable debug mode and log to foreground')
options, unknown = parser.parse_known_args()
options.Matterbot = ast.literal_eval(options.Matterbot)
options.Modules = ast.literal_eval(options.Modules)
threads = []
# Start the Mattermost connection
try:
# Start the logging thread
logQueue = queue.Queue()
thread = LogWorker(logQueue)
threads.append(thread)
thread.start()
logQueue.put(('INFO', 'Logging thread started ...'))
# Fire up the Mattermost connection
mm = MattermostManager()
if mm:
# Start the message handler
msgQueue = queue.Queue()
thread = MsgWorker(mm, logQueue, msgQueue)
threads.append(thread)
thread.start()
logQueue.put(('INFO', 'Message thread started ...'))
# Find and load all modules concurrently, and build a lookback history
modules = loadModules()
if len(modules):
# Schedule the modules and run forever
for module in modules:
thread = ModuleWorker(mm, module, logQueue, msgQueue)
threads.append(thread)
thread.start()
while len(threads)>0:
try:
threads = [t.join(1) for t in threads if t is not None and t.is_alive()]
except KeyboardInterrupt:
logQueue.put(('INFO', 'Shutting down all workers and exiting ...'))
for thread in threads:
thread.terminate = True
else:
logQueue.put(('ERROR', 'No modules found - exiting!'))
else:
logQueue.put(('ERROR', 'Could not connect to Mattermost - exiting!'))
except Exception as e:
logQueue.put(('ERROR', 'Error occurred:\n%s' % (traceback.format_exc(),)))
finally:
logQueue.put(('INFO', 'Matterfeed main loop running ...'))
sys.exit(0)