· 10 years ago · Sep 27, 2016, 09:50 PM
1package.path = package.path .. ';.luarocks/share/lua/5.2/?.lua'
2 ..';.luarocks/share/lua/5.2/?/init.lua'
3package.cpath = package.cpath .. ';.luarocks/lib/lua/5.2/?.so'
4
5require("./bot/utils")
6
7local f = assert(io.popen('/usr/bin/git describe --tags', 'r'))
8VERSION = assert(f:read('*a'))
9f:close()
10
11-- This function is called when tg receive a msg
12function on_msg_receive (msg)
13 if not started then
14 return
15 end
16
17 local receiver = get_receiver(msg)
18
19 -- vardump(msg)
20 msg = pre_process_service_msg(msg)
21 if msg_valid(msg) then
22 msg = pre_process_msg(msg)
23 if msg then
24 match_plugins(msg)
25 mark_read(receiver, ok_cb, false)
26 end
27 end
28end
29
30function ok_cb(extra, success, result)
31end
32
33function on_binlog_replay_end()
34 started = true
35 postpone (cron_plugins, false, 60*5.0)
36 -- See plugins/isup.lua as an example for cron
37
38 _config = load_config()
39
40 -- load plugins
41 plugins = {}
42 load_plugins()
43end
44
45function msg_valid(msg)
46 -- Don't process outgoing messages
47 if msg.out then
48 print('\27[36mNot valid: msg from us\27[39m')
49 return false
50 end
51
52 -- Before bot was started
53 if msg.date < now then
54 print('\27[36mNot valid: old msg\27[39m')
55 return false
56 end
57
58 if msg.unread == 0 then
59 print('\27[36mNot valid: readed\27[39m')
60 return false
61 end
62
63 if not msg.to.id then
64 print('\27[36mNot valid: To id not provided\27[39m')
65 return false
66 end
67
68 if not msg.from.id then
69 print('\27[36mNot valid: From id not provided\27[39m')
70 return false
71 end
72
73 if msg.from.id == our_id then
74 print('\27[36mNot valid: Msg from our id\27[39m')
75 return false
76 end
77
78 if msg.to.type == 'encr_chat' then
79 print('\27[36mNot valid: Encrypted chat\27[39m')
80 return false
81 end
82
83 if msg.from.id == 777000 then
84 print('\27[36mNot valid: Telegram message\27[39m')
85 return false
86 end
87
88 return true
89end
90
91--
92function pre_process_service_msg(msg)
93 if msg.service then
94 local action = msg.action or {type=""}
95 -- Double ! to discriminate of normal actions
96 msg.text = "!!tgservice " .. action.type
97
98 -- wipe the data to allow the bot to read service messages
99 if msg.out then
100 msg.out = false
101 end
102 if msg.from.id == our_id then
103 msg.from.id = 0
104 end
105 end
106 return msg
107end
108
109-- Apply plugin.pre_process function
110function pre_process_msg(msg)
111 for name,plugin in pairs(plugins) do
112 if plugin.pre_process and msg then
113 print('Preprocess', name)
114 msg = plugin.pre_process(msg)
115 end
116 end
117
118 return msg
119end
120
121-- Go over enabled plugins patterns.
122function match_plugins(msg)
123 for name, plugin in pairs(plugins) do
124 match_plugin(plugin, name, msg)
125 end
126end
127
128-- Check if plugin is on _config.disabled_plugin_on_chat table
129local function is_plugin_disabled_on_chat(plugin_name, receiver)
130 local disabled_chats = _config.disabled_plugin_on_chat
131 -- Table exists and chat has disabled plugins
132 if disabled_chats and disabled_chats[receiver] then
133 -- Checks if plugin is disabled on this chat
134 for disabled_plugin,disabled in pairs(disabled_chats[receiver]) do
135 if disabled_plugin == plugin_name and disabled then
136 local warning = 'Plugin '..disabled_plugin..' is disabled on this chat'
137 print(warning)
138 send_msg(receiver, warning, ok_cb, false)
139 return true
140 end
141 end
142 end
143 return false
144end
145
146function match_plugin(plugin, plugin_name, msg)
147 local receiver = get_receiver(msg)
148
149 -- Go over patterns. If one matches it's enough.
150 for k, pattern in pairs(plugin.patterns) do
151 local matches = match_pattern(pattern, msg.text)
152 if matches then
153 print("msg matches: ", pattern)
154
155 if is_plugin_disabled_on_chat(plugin_name, receiver) then
156 return nil
157 end
158 -- Function exists
159 if plugin.run then
160 -- If plugin is for privileged users only
161 if not warns_user_not_allowed(plugin, msg) then
162 local result = plugin.run(msg, matches)
163 if result then
164 send_large_msg(receiver, result)
165 end
166 end
167 end
168 -- One patterns matches
169 return
170 end
171 end
172end
173
174-- DEPRECATED, use send_large_msg(destination, text)
175function _send_msg(destination, text)
176 send_large_msg(destination, text)
177end
178
179-- Save the content of _config to config.lua
180function save_config( )
181 serialize_to_file(_config, './data/config.lua')
182 print ('saved config into ./data/config.lua')
183end
184
185-- Returns the config from config.lua file.
186-- If file doesn't exist, create it.
187function load_config( )
188 local f = io.open('./data/config.lua', "r")
189 -- If config.lua doesn't exist
190 if not f then
191 print ("Created new config file: data/config.lua")
192 create_config()
193 else
194 f:close()
195 end
196 local config = loadfile ("./data/config.lua")()
197 for v,user in pairs(config.sudo_users) do
198 print("Allowed user: " .. user)
199 end
200 return config
201end
202
203-- Create a basic config.json file and saves it.
204function create_config( )
205 -- A simple config with basic plugins and ourselves as privileged user
206 config = {
207 enabled_plugins = {
208 "9gag",
209 "eur",
210 "echo",
211 "btc",
212 "get",
213 "giphy",
214 "google",
215 "gps",
216 "help",
217 "id",
218 "images",
219 "img_google",
220 "location",
221 "media",
222 "plugins",
223 "channels",
224 "set",
225 "stats",
226 "time",
227 "version",
228 "weather",
229 "xkcd",
230 "youtube" },
231 sudo_users = {259813050},
232 disabled_channels = {}
233 }
234 serialize_to_file(config, './data/config.lua')
235 print ('saved config into ./data/config.lua')
236end
237
238function on_our_id (id)
239 our_id = id
240end
241
242function on_user_update (user, what)
243 --vardump (user)
244end
245
246function on_chat_update (chat, what)
247 --vardump (chat)
248end
249
250function on_secret_chat_update (schat, what)
251 --vardump (schat)
252end
253
254function on_get_difference_end ()
255end
256
257-- Enable plugins in config.json
258function load_plugins()
259 for k, v in pairs(_config.enabled_plugins) do
260 print("Loading plugin", v)
261
262 local ok, err = pcall(function()
263 local t = loadfile("plugins/"..v..'.lua')()
264 plugins[v] = t
265 end)
266
267 if not ok then
268 print('\27[31mError loading plugin '..v..'\27[39m')
269 print('\27[31m'..err..'\27[39m')
270 end
271
272 end
273end
274
275-- Call and postpone execution for cron plugins
276function cron_plugins()
277
278 for name, plugin in pairs(plugins) do
279 -- Only plugins with cron function
280 if plugin.cron ~= nil then
281 plugin.cron()
282 end
283 end
284
285 -- Called again in 5 mins
286 postpone (cron_plugins, false, 5*60.0)
287end
288
289-- Start and load values
290our_id = 0
291now = os.time()
292math.randomseed(now)
293started = false