forked from matrix-org/Matrix-NEB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathneb.py
executable file
·136 lines (110 loc) · 3.82 KB
/
neb.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
#!/usr/bin/env python
import argparse
from matrix_client.api import MatrixHttpApi
from neb.engine import Engine
from neb.matrix import MatrixConfig
from plugins.b64 import Base64Plugin
from plugins.guess_number import GuessNumberPlugin
#from plugins.jenkins import JenkinsPlugin
#from plugins.jira import JiraPlugin
from plugins.url import UrlPlugin
from plugins.time_utils import TimePlugin
#from plugins.github import GithubPlugin
from plugins.prometheus import PrometheusPlugin
import logging
import logging.handlers
import time
log = logging.getLogger(name=__name__)
# TODO:
# - Add utility plugins in neb package to do things like "invite x to room y"?
# - Add other plugins as tests of plugin architecture (e.g. anagrams, dictionary lookup, etc)
def generate_config(url, username, token, config_loc):
config = MatrixConfig(
hs_url=url,
user_id=username,
access_token=token,
admins=[]
)
save_config(config_loc, config)
return config
def save_config(loc, config):
with open(loc, 'w') as f:
MatrixConfig.to_file(config, f)
def load_config(loc):
try:
with open(loc, 'r') as f:
return MatrixConfig.from_file(f)
except:
pass
def configure_logging(logfile):
log_format = "%(asctime)s %(levelname)s: %(message)s"
logging.basicConfig(
level=logging.DEBUG,
format=log_format
)
if logfile:
formatter = logging.Formatter(log_format)
# rotate logs (20MB, max 6 = 120MB)
handler = logging.handlers.RotatingFileHandler(
logfile, maxBytes=(1000 * 1000 * 20), backupCount=5)
handler.setFormatter(formatter)
logging.getLogger('').addHandler(handler)
def main(config):
# setup api/endpoint
matrix = MatrixHttpApi(config.base_url, config.token)
log.debug("Setting up plugins...")
plugins = [
TimePlugin,
Base64Plugin,
GuessNumberPlugin,
UrlPlugin,
PrometheusPlugin,
]
# setup engine
engine = Engine(matrix, config)
for plugin in plugins:
engine.add_plugin(plugin)
engine.setup()
while True:
try:
log.info("Listening for incoming events.")
engine.event_loop()
except Exception as e:
log.error("Ruh roh: %s", e)
time.sleep(5)
log.info("Terminating.")
if __name__ == '__main__':
a = argparse.ArgumentParser("Runs NEB. See plugins for commands.")
a.add_argument(
"-c", "--config", dest="config",
help="The config to create or read from."
)
a.add_argument(
"-l", "--log-file", dest="log",
help="Log to this file."
)
args = a.parse_args()
configure_logging(args.log)
log.info(" ===== NEB initialising ===== ")
config = None
if args.config:
log.info("Loading config from %s", args.config)
config = load_config(args.config)
if not config:
log.info("Setting up for an existing account.")
print "Config file could not be loaded."
print ("NEB works with an existing Matrix account. "
"Please set up an account for NEB if you haven't already.'")
print "The config for this account will be saved to '%s'" % args.config
hsurl = raw_input("Home server URL (e.g. http://localhost:8008): ").strip()
if hsurl.endswith("/"):
hsurl = hsurl[:-1]
hsurl = hsurl + "/_matrix/client/api/v1" # v1 compatibility
username = raw_input("Full user ID (e.g. @user:domain): ").strip()
token = raw_input("Access token: ").strip()
config = generate_config(hsurl, username, token, args.config)
else:
a.print_help()
print "You probably want to run 'python neb.py -c neb.config'"
if config:
main(config)