forked from mosbth/irc2phpbb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·205 lines (153 loc) · 5.42 KB
/
main.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
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
"""
An IRC bot that answers random questions, keeps a log from the IRC-chat,
easy to integrate in a webpage and montores a phpBB forum for latest topics
by loggin in to the forum and checking the RSS-feed.
You need to install additional modules.
# Install needed modules in local directory
pip3 install --target modules/ feedparser beautifulsoup4 chardet
Modules in modules/ will be loaded automatically. If you want to use a
different directory you can start the program like this instead:
PYTHONPATH=modules python3 main.py
# To get help
PYTHONPATH=modules python3 main.py --help
# Example of using options
--server=irc.bsnet.se --channel=#db-o-webb
--server=irc.bsnet.se --port=6667 --channel=#db-o-webb
--nick=marvin --ident=secret
# Configuration
Check out the file 'marvin_config_default.json' on how to configure, instead
of using cli-options. The default configfile is 'marvin_config.json' but you
can change that using cli-options.
# Make own actions
Check the file 'marvin_strings.json' for the file where most of the strings
are defined and check out 'marvin_actions.py' to see how to write your own
actions. Its just a small function.
# Read from incoming
Marvin reads messages from the incoming/ directory, if it exists, and writes
it out the the irc channel.
"""
import getopt
import json
import os
import sys
import marvin
import marvin_actions
#
# General stuff about this program
#
PROGRAM = "marvin"
AUTHOR = "Mikael Roos"
EMAIL = "[email protected]"
VERSION = "0.3.0"
MSG_USAGE = """{program} - Act as an IRC bot and do useful things.
By {author} ({email}), version {version}.
Usage:
{program} [options]
Options:
-h --help Display this help message.
-v --version Print version and exit.
--config= Use this file as configfile.
--server= Set the IRC server to connect to.
--port= Set the port to use, default is 6667.
--channel= Set what channel to join.
--nick= Set nick to identify by.
--realname= Set realname for verbose presentation.
--ident= Set password for IDENTIFY for nick.
GitHub: https://github.com/mosbth/irc2phpbb
Issues: https://github.com/mosbth/irc2phpbb/issues
""".format(program=PROGRAM, author=AUTHOR, email=EMAIL, version=VERSION)
MSG_VERSION = "{program} version {version}.".format(program=PROGRAM, version=VERSION)
MSG_USAGE_SHORT = "Use {program} --help to get usage.\n".format(program=PROGRAM)
def printUsage(exitStatus):
"""
Print usage information about the script and exit.
"""
print(MSG_USAGE)
sys.exit(exitStatus)
def printVersion():
"""
Print version information and exit.
"""
print(MSG_VERSION)
sys.exit(0)
def mergeOptionsWithConfigFile(options, configFile):
"""
Read information from config file.
"""
if os.path.isfile(configFile):
with open(configFile) as f:
data = json.load(f)
options.update(data)
res = json.dumps(options, sort_keys=True, indent=4, separators=(',', ': '))
msg = "Read configuration from config file '{file}'. Current configuration is:\n{config}"
print(msg.format(config=res, file=configFile))
else:
print("Config file '{file}' is not readable, skipping.".format(file=configFile))
return options
def parseOptions():
"""
Merge default options with incoming options and arguments and return them as a dictionary.
"""
# Default options to start with
options = marvin.getConfig()
# Read from config file if available
options.update(mergeOptionsWithConfigFile(options, "marvin_config.json"))
# Switch through all options, commandline options overwrites.
try:
opts, args = getopt.getopt(sys.argv[1:], "hv", [
"help",
"version",
"config=",
"server=",
"port=",
"channel=",
"nick=",
"realname=",
"ident="
])
for opt, arg in opts:
if opt in ("-h", "--help"):
printUsage(0)
elif opt in ("-v", "--version"):
printVersion()
elif opt in "--config":
options = mergeOptionsWithConfigFile(options, arg)
elif opt in "--server":
options["server"] = arg
elif opt in "--port":
options["port"] = arg
elif opt in "--channel":
options["channel"] = arg
elif opt in "--nick":
options["nick"] = arg
elif opt in "--realname":
options["realname"] = arg
elif opt in "--ident":
options["ident"] = arg
else:
raise Exception("Unhandled option")
if args:
raise Exception("Too many arguments, unknown argument.")
except Exception as err:
print(err)
print(MSG_USAGE_SHORT)
sys.exit(1)
res = json.dumps(options, sort_keys=True, indent=4, separators=(',', ': '))
print("Configuration updated after cli options:\n{config}".format(config=res))
return options
def main():
"""
Main function to carry out the work.
"""
options = parseOptions()
marvin.setConfig(options)
marvin_actions.setConfig(options)
actions = marvin_actions.getAllActions()
marvin.registerActions(actions)
marvin.connectToServer()
marvin.mainLoop()
sys.exit(0)
if __name__ == "__main__":
main()