-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.lua
190 lines (167 loc) · 4.46 KB
/
main.lua
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
-- Used for initializing packages without breaking circular dependencies.
-- Must be called before any requires, and the argument must match what will
-- be used for the require() calls. Passing ... works, because the first
-- argument to a chunk is the module name, and the other arguments are ignored.
-- See the other files for examples.
function safeinit(name)
package.loaded[name] = package.loaded[name] or {}
return package.loaded[name]
end
-- "capi" is provided by C
local Gs = require "state"
local argv = require "argv"
local buffers = require "buffers"
local commands = require "commands"
local i18n = require "i18n"
local irc = require "irc"
local ringbuf = require "ringbuf"
local ui = require "ui"
local util = require "util"
-- also see eof
-- for functions called by C
cback = {}
-- The whole external program piping shebang.
-- Basically: if an external program is launched, print() should
-- either buffer up the input to show later, or pipe it
-- to the program.
ext = {}
ext.running = false
ext.ringbuf = nil
ext._pipe = false
ext.reason = nil
ext.eof = capi.ext_eof
function print(...)
if ext.running then
if ext._pipe then
local args = {...}
for k,v in ipairs(args) do
if type(v) == "string" then
args[k] = ui.strip_ansi(v)
end
end
capi.print_internal(table.unpack(args))
else
ext.ringbuf:push({...})
end
else
capi.print_internal(...)
end
end
function printf(...)
print(string.format(...))
end
function ext.run(cmdline, reason, opts)
if ext.running then return end
opts = opts or {}
-- TODO move into a separate file and put most stuff into locals
ext.running = true
ext.ringbuf = ringbuf:new(1000)
ext._pipe = false
ext.reason = reason
ext.callback = opts.callback
capi.ext_run_internal(cmdline, opts.tty)
end
-- true: print()s should be passed to the external process
-- false: print()s should be cached until the ext process quits
function ext.setpipe(b)
ext._pipe = b
end
function cback.ext_quit()
ext.running = false
print("printing the messages you've missed...")
if ext.ringbuf:full() then
print("note: some older messages were dropped, so this isn't a complete list")
end
for v in ext.ringbuf:iter(ext.ringbuf) do
print(table.unpack(v))
end
if ext.callback then ext.callback() end
ext.callback = nil
ext.ringbuf = nil
ext.reason = nil
end
function cback.init(...)
util.config_load()
local opts = argv.parse{...}
local host = opts.host or config.host
local port = opts.port or config.port
Gs.user = opts.nick or config.nick or "nil"
if not capi.dial(host, port) then
printf("couldn't connect to %s:%s :(", host, port)
os.exit(1)
end
printf(i18n.connecting, ui.highlight(Gs.user))
irc.writecmd("USER", config.ident.username or Gs.user, "0", "*",
config.ident.realname or Gs.user)
irc.writecmd("NICK", Gs.user)
capi.history_resize(config.input_history)
Gs.chan = nil
buffers:make(":mentions")
end
function cback.disconnected()
-- TODO do something reasonable
Gs.active = false
print([[you got disconnected from the server :/]])
print([[restart hewwo with "/QUIT" to reconnect]])
end
function cback.in_net(line)
if config.debug then
print("<=", ui.escape(line))
end
irc.newcmd(line, true)
ui.updateprompt()
end
function cback.in_user(line)
if line == "" then return end
if line == nil then
ui.hint(i18n.hint.quit)
return
end
capi.history_add(line)
if string.sub(line, 1, 1) == "/" then
if string.sub(line, 2, 2) == "/" then
line = string.sub(line, 2)
irc.writecmd("PRIVMSG", Gs.chan, line)
else
commands.run(line)
end
elseif Gs.chan then
if string.sub(Gs.chan, 1, 1) == ":" then
printf(i18n.err_rochan, Gs.chan)
else
irc.writecmd("PRIVMSG", Gs.chan, line)
end
else
print(i18n.err_nochan)
end
ui.updateprompt()
end
function cback.completion(line)
local tbl = {}
local word = string.match(line, "[^ ]*$") or ""
if word == "" then return {} end
local wlen = string.len(word)
local rest = string.sub(line, 1, -string.len(word)-1)
local function addfrom(src, prefix, suffix)
if not src then return end
prefix = prefix or ""
suffix = suffix or " "
for k, v in pairs(src) do
k = prefix..k..suffix
if v and wlen < string.len(k) and word == string.sub(k, 1, wlen) then
table.insert(tbl, rest..k)
end
end
end
local buf = Gs.buffers[Gs.chan]
if buf then
if word == line then
addfrom(buf.users, "", ": ")
else
addfrom(buf.users)
end
end
addfrom(Gs.buffers)
addfrom(commands.tbl, "/")
return tbl
end