-
Notifications
You must be signed in to change notification settings - Fork 85
/
Copy pathConvos.pm
372 lines (263 loc) · 9.76 KB
/
Convos.pm
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package Convos;
use Mojo::Base 'Mojolicious';
use Carp qw(croak);
use Cwd ();
use Convos::Core;
use Convos::Util qw(is_true require_module);
use File::HomeDir ();
use Mojo::File qw(path);
use Mojo::JSON qw(false true);
use Mojo::Util;
use Scalar::Util qw(blessed);
use constant CONVOS_GET => +($ENV{CONVOS_COMMAND} || '') eq 'get';
our $VERSION = '8.07';
$ENV{CONVOS_REVERSE_PROXY} //= is_true 'ENV:MOJO_REVERSE_PROXY';
$ENV{MOJO_REVERSE_PROXY} //= is_true 'ENV:CONVOS_REVERSE_PROXY';
has core => sub {
my $self = shift;
my $home = Cwd::abs_path($self->config('home')) || $self->config('home');
return Convos::Core->new(
backend => $self->config('backend'),
home => path(split '/', $home),
log => $self->log,
);
};
sub startup {
my $self = shift;
my $config = $self->_config;
$self->_home_in_share unless -d $self->home->rel_file('public');
$self->_patch_mojo_promise if $ENV{MOJO_PROMISE_CROAK};
$self->defaults(existing_user => 0, lang => 'en', start_app => '');
$self->routes->namespaces(['Convos::Controller']);
$self->sessions->cookie_name('convos');
$self->sessions->default_expiration(86400 * 7);
$self->types->type(yaml => $self->types->type('txt'));
push @{$self->renderer->classes}, __PACKAGE__;
# Autogenerate routes from the OpenAPI specification
my $api_route = $self->routes->under('/')->to('user#check_if_ready', openapi => 1);
$self->plugin(
OpenAPI => {
format => ['json'],
route => $api_route,
url => $self->static->file('convos-api.yaml')->path
}
);
# Add basic routes
my $r = $self->routes;
my @format_constraints = (format => [qw(html json txt yaml)]);
$r->get('/', [@format_constraints])->to('cms#index', format => undef)->name('index');
$r->get('/blog', [@format_constraints])->to('cms#blog_list', format => undef);
$r->get('/blog/:year/:mon/:mday/:name', [@format_constraints])
->to('cms#blog_entry', format => undef, page => 1)->name('blog_entry');
$r->get('/doc/*file', [@format_constraints])->to('cms#doc', file => 'index', format => undef)
->name('doc');
$r->get('/logout', [format => [qw(html json)]])->to('user#logout', format => undef);
$r->get('/assets/browserconfig.<:hash>', [format => ['xml']])
->to(template => 'assets/browserconfig');
$r->get('/assets/site.<:hash>', [format => ['webmanifest']])->to(template => 'assets/site');
$r->get('/err/500')->to(cb => sub { die 'Test 500 page' });
$r->get('/err/:code')->to('url#err');
$r->get('/sw' => [format => 'js']);
$r->get('/sw/info' => {json => {mode => $self->mode, version => $self->VERSION}});
$r->get('/video/#domain/:name')->to(cb => sub { shift->render('video') });
# Friendly alias for /api/file/:uid/:fid
$r->get('/file/:uid/:fid', [format => 1])->to('files#get');
# Event channel
$r->websocket('/events')->to('events#start')->name('events');
# Chat resources
my $user_r = $r->under('/')->to('user#check_if_ready');
$user_r->get('/help');
$user_r->get('/chat')->name('chat');
$user_r->get('/chat/:connection_id')->name('chat.connection_id');
$user_r->get('/chat/:connection_id/#conversation_id')->name('chat.connection_id.conversation_id');
$user_r->get('/login');
$user_r->get('/register')->to('user#register_html');
$user_r->get('/settings/*rest', {rest => ''});
$user_r->get('/search');
$self->_plugins;
$self->hook(around_action => \&_around_action);
$self->hook(after_build_tx => \&_after_build_tx);
$self->hook(before_dispatch => \&_before_dispatch);
$self->core->start;
}
sub _after_build_tx {
my ($tx, $app) = @_;
$tx->req->max_message_size($ENV{CONVOS_MAX_UPLOAD_SIZE} // 40_000_000);
}
sub _around_action {
my ($next, $c, $action, $last) = @_;
my $res = $c->$next;
$c->render_later if blessed $res and $res->can('then');
return $res;
}
sub _before_dispatch {
my $c = shift;
# Handle /whatever/with%2Fslash/...
my $path = $c->req->url->path;
my $path_str = "$path";
$path_str =~ s/%([0-9a-fA-F]{2})/{my $h = hex $1; $h == 47 ? '%2F' : chr $h}/ge;
$path->leading_slash(1) if $path_str =~ s!^/!!;
$path->trailing_slash(1) if $path_str =~ s!/$!!;
$path->parts([
split '/', $path->charset ? Mojo::Util::decode($path->charset, $path_str) : $path_str
]);
# Handle mount point like /apps/convos
my ($base_url, $env_missing) = ($c->req->headers->header('X-Request-Base'), 0);
if ($base_url) {
$base_url = Mojo::URL->new($base_url);
$env_missing = !$ENV{CONVOS_REVERSE_PROXY};
$c->req->url->base($base_url) if is_true 'ENV:CONVOS_REVERSE_PROXY';
}
elsif ($ENV{CONVOS_REQUEST_BASE}) {
$base_url = Mojo::URL->new($ENV{CONVOS_REQUEST_BASE});
$c->req->url->base($base_url);
}
my $settings = $c->app->core->settings;
$base_url ||= $c->req->url->to_abs->query(Mojo::Parameters->new)->path('/');
$settings->save_p({base_url => $base_url}) if !CONVOS_GET and $settings->base_url ne $base_url;
$c->app->sessions->secure(is_true('ENV:CONVOS_SECURE_COOKIES')
|| $base_url->scheme eq 'https' ? 1 : 0);
$c->res->headers->header('X-Provider-Name', 'ConvosApp');
# Used when registering the first user
$c->stash(first_user => !$c->app->core->n_users);
# App settings
$c->stash($settings->TO_JSON);
$c->reply->exception('X-Request-Base header was seen, but CONVOS_REVERSE_PROXY is not set')
if $env_missing;
}
sub _config {
my $self = shift;
my $config = $self->config;
$config->{backend} ||= $ENV{CONVOS_BACKEND} || 'Convos::Core::Backend::File';
$config->{home} ||= $ENV{CONVOS_HOME}
||= path(File::HomeDir->my_home, qw(.local share convos))->to_string;
if ($config->{log_file} ||= $ENV{CONVOS_LOG_FILE}) {
$self->log->path($config->{log_file});
delete $self->log->{handle};
}
my $access_log = $ENV{CONVOS_ACCESS_LOG} // 'v2';
my $enabled = is_true 'ENV:CONVOS_SYSLOG';
$self->plugin(Syslog => {access_log => $access_log, enable => $enabled, only_syslog => $enabled});
$self->log->level($ENV{CONVOS_LOG_LEVEL});
$self->log->info(qq(CONVOS_HOME="$config->{home}" # https://convos.chat/doc/config#convos_home"));
my $settings = $self->core->settings;
$settings->load_p->wait;
$self->secrets($settings->session_secrets);
$settings->save_p->wait;
return $config;
}
sub _home_in_share {
my $self = shift;
my $rel = path(qw(auto share dist Convos))->to_string;
for my $inc (@INC) {
next if ref $inc or !$inc;
my $share = path($inc, $rel);
next unless -d $share and -r _;
${$self->home} = $share->to_string;
$self->static->paths->[0] = $share->child('public')->to_string;
return $self;
}
die "Unable to find $rel in @INC";
}
sub _patch_mojo_promise {
require Mojo::Promise;
Mojo::Util::monkey_patch(
'Mojo::Promise',
AWAIT_GET => sub {
my $self = shift;
my @results = @{$self->{results} // []};
croak $results[0] unless $self->{status} eq 'resolve';
return wantarray ? @results : $results[0];
}
);
}
sub _plugins {
my $self = shift;
unshift @{$self->plugins->namespaces}, 'Convos::Plugin';
my @plugins = (
qw(Convos::Plugin::Auth Convos::Plugin::Bot Convos::Plugin::Cms),
qw(Convos::Plugin::I18N Convos::Plugin::Helpers Convos::Plugin::Paste),
qw(Convos::Plugin::Themes),
);
push @plugins, split /,/, $ENV{CONVOS_PLUGINS} if $ENV{CONVOS_PLUGINS};
for (@plugins) {
my ($name, $config) = split '\?', $_, 2;
$self->plugin($name => Mojo::Parameters->new($config // '')->to_hash);
}
}
1;
=encoding utf8
=head1 NAME
Convos - Multiuser chat application
=head1 VERSION
8.07
=head1 DESCRIPTION
L<Convos> is a multiuser chat application built with L<Mojolicious>.
It currently support the IRC protocol, but can be extended to support
other protocols as well. Below is a list of the main documentation
starting points for Convos:
=over 2
=item * L<Documentation index|https://convos.chat/doc/>
=item * L<Getting started|https://convos.chat/doc/start>
=item * L<Development guide|https://convos.chat/doc/develop>
=item * L<REST API reference|https://convos.chat/api>
=back
=head2 Reference
This is the module and documentation structure of L<Convos>:
=over 2
=item * L<Convos::Core>
=over 2
=item * L<Convos::Core::Backend>
=over 2
=item * L<Convos::Core::Backend::File>
=back
=item * L<Convos::Core::Connection>
=over 2
=item * L<Convos::Core::Connection::Irc>
=back
=item * L<Convos::Core::Conversation>
=item * L<Convos::Core::User>
=back
=item * I<Convos::Controller>
=over 2
=item * L<Convos::Controller::Connection>
=item * L<Convos::Controller::Conversation>
=item * L<Convos::Controller::Events>
=item * L<Convos::Controller::Notifications>
=item * L<Convos::Controller::User>
=back
=item * I<Convos::Plugin>
=over 2
=item * I<Convos::Plugin::Auth>
=item * I<Convos::Plugin::Auth::Header>
=item * I<Convos::Plugin::Auth::LDAP>
=item * I<Convos::Plugin::Bot>
=item * I<Convos::Plugin::Cms>
=item * I<Convos::Plugin::Helpers>
=item * I<Convos::Plugin::I18N>
=item * I<Convos::Plugin::Paste>
=item * I<Convos::Plugin::Themes>
=back
=back
=head1 ATTRIBUTES
L<Convos> inherits all attributes from L<Mojolicious> and implements
the following new ones.
=head2 core
Holds a L<Convos::Core> object.
=head1 METHODS
L<Convos> inherits all methods from L<Mojolicious> and implements
the following new ones.
=head2 startup
This method sets up the application.
=head1 COPYRIGHT AND LICENSE
=head2 Material design icons
The icons used are provided by L<Google|https://www.google.com/design/icons>
under the L<CC-BY license|https://creativecommons.org/licenses/by/4.0/>.
=head2 Convos core and frontend
Copyright Convos Org.
This program is free software, you can redistribute it and/or modify it under
the terms of the Artistic License version 2.0.
=head1 AUTHORS
Jan Henning Thorsen - C<jhthorsen@cpan.org>
Marcus Ramberg - C<marcus@nordaaker.com>
=cut