aboutsummaryrefslogtreecommitdiff
path: root/pyfingerd/core.py
blob: 0cd995de956a7ce199cf01df177163ab29ad5963 (plain)
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
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
#!/usr/bin/env python3
# *****************************************************************************
# Copyright (C) 2017-2022 Thomas Touhey <thomas@touhey.fr>
# This file is part of the pyfingerd project, which is MIT-licensed.
# *****************************************************************************
""" Main classes for the finger server, interfaces and formatters.

    These classes, which behave as base returning default data,
    are bundled with base definitions for users and sessions.
"""

import asyncio as _asyncio
import copy as _copy
import multiprocessing as _multip
import signal as _signal
import string as _string

from datetime import datetime as _dt, timedelta as _td, tzinfo as _tzinfo
from errno import errorcode as _errorcode
from typing import Optional as _Optional, Sequence as _Sequence

from croniter import croniter as _croniter
from pytz import utc as _utc

from .binds import FingerBindsDecoder as _FingerBindsDecoder
from .errors import (
    HostnameError as _HostnameError,
    MalformedQueryError as _MalformedQueryError,
    NoBindsError as _NoBindsError,
)
from .utils import (
    access_logger as _access_logger,
    error_logger as _error_logger,
    logger as _logger,
)

__all__ = [
    'FingerFormatter', 'FingerInterface',
    'FingerServer', 'FingerSession', 'FingerUser', 'cron',
]

# ---
# Decorators.
# ---


def cron(spec: str):
    """ Add a cron specification to the callable.

        This decorator adds the ``__cron__`` member on the callable,
        as a ``croniter`` instance using the given specification.

        This makes the callable identifiable by the finger server when
        starting a server with an interface with such a callable,
        by checking if the attribute exists and starting a dedicated
        coroutine for running it periodically using the given specification.
    """

    spec = _croniter(spec)

    def decorator(func):
        func.__cron__ = spec
        return func

    return decorator


# ---
# Basic representations.
# ---


class FingerSession:
    """ Representation of an active session for a given user on the system.

        :param time: The start time of the given session;
                     by default, the current datetime.
    """

    __slots__ = ('_start', '_line', '_host', '_idle')

    def __init__(self, *_, time: _Optional[_dt] = None):
        self._start = _dt.now()
        self._idle = None
        self._line = None
        self._host = None

        self.start = time
        self._idle = self._start

    def __repr__(self):
        p = ('start', 'idle', 'line', 'orig')
        p = (
            f'{x}={getattr(self, x)!r}'
            for x in p if getattr(self, x) is not None)
        return f"{self._class__.__name__}({', '.join(p)})"

    @property
    def start(self) -> _dt:
        """ The timestamp at which the session has started.

            Note that when set, if no timezone is present,
            the datetime is considered UTC.
        """

        return self._start

    @start.setter
    def start(self, value):
        value = value if isinstance(value, _dt) else _dt(value)
        if value.tzinfo is None:
            value = value.replace(tzinfo=_utc)

        self._start = value

    @property
    def idle(self) -> _dt:
        """ The timestamp since which the user is idle on the session.

            Note that when set, if no timezone is present,
            the datetime is considered UTC; also, if the provided
            datetime is before the session start timestamp, it is
            set to it.
        """

        return self._idle

    @idle.setter
    def idle(self, value):
        value = value if isinstance(value, _dt) else _dt(value)
        if value.tzinfo is None:
            value = value.replace(tzinfo=_utc)

        if value < self._start:
            value = self._start

        self._idle = value

    @property
    def line(self) -> _Optional[str]:
        """ The line on which the user is. """

        return self._line

    @line.setter
    def line(self, value):
        self._line = None if value is None else str(value)

    @property
    def host(self) -> _Optional[str]:
        """ The host from which the user is connected. """

        return self._host

    @host.setter
    def host(self, value):
        self._host = None if value is None else str(value)


class FingerUser:
    """ Representation of a user on the system.

        Returned by subclasses of :py:class:`FingerInterface`,
        and used by subclasses of :py:class:`FingerFormatter`.

        :param login: The login of the user.
        :param name: The display name of the user.
        :param home: The path to the home of the user.
        :param shell: The path to the user's default shell.
    """

    __slots__ = (
        '_login', '_name', '_home', '_shell', '_office',
        '_plan', '_last_login', '_sessions',
    )

    def __init__(
        self, *_,
        login: _Optional[str] = None,
        name: _Optional[str] = None,
        home: _Optional[str] = None,
        shell: _Optional[str] = None,
    ):
        self._login = None
        self._name = ''
        self._home = None
        self._shell = None
        self._office = None
        self._plan = None
        self._last_login = None
        self._sessions = _FingerSessionManager()

        self.login = login
        self.name = name
        self.home = home
        self.shell = shell

    def __repr__(self):
        p = (
            'login', 'name', 'home', 'shell', 'office',
            'last_login', 'sessions')
        p = (
            f'{x}={getattr(self, x)!r}'
            for x in p if getattr(self, x) is not None)
        return f"{self._class__.__name__}({', '.join(p)})"

    @property
    def login(self) -> _Optional[str]:
        """ The login name of the user, e.g. 'cake' or 'gaben'. """

        return self._login

    @login.setter
    def login(self, value: _Optional[str]) -> None:
        self._login = value

    @property
    def name(self) -> _Optional[str]:
        """ The display name of the user, e.g. 'Jean Dupont'. """

        return self._name

    @name.setter
    def name(self, value: _Optional[str]) -> None:
        self._name = value

    @property
    def last_login(self) -> _Optional[str]:
        """ The last login date for the user.

            Is None if not known.
        """

        return self._last_login

    @last_login.setter
    def last_login(self, value: _Optional[str]) -> None:
        self._last_login = None if value is None else \
            value if isinstance(value, _dt) else _dt(value)

    @property
    def home(self) -> _Optional[str]:
        """ The path to the user's home on the given system.

            Is None if not known or defined.
        """

        return self._home

    @home.setter
    def home(self, value: _Optional[str]) -> None:
        self._home = None if value is None else str(value)

    @property
    def shell(self) -> _Optional[str]:
        """ The path to the user's shell on the given system.

            Is None if not known or defined.
        """

        return self._shell

    @shell.setter
    def shell(self, value: _Optional[str]) -> None:
        self._shell = None if value is None else str(value)

    @property
    def office(self) -> _Optional[str]:
        """ The display name of the user's office.

            Is None if not known or defined.
        """

        return self._office

    @office.setter
    def office(self, value: _Optional[str]) -> None:
        self._office = None if value is None else str(value)

    @property
    def plan(self) -> _Optional[str]:
        """ The plan of the user.

            Usually the content of the ``.plan`` file in the user's home
            on real (and kind of obsolete) UNIX-like systems.
        """

        return self._plan

    @plan.setter
    def plan(self, value: _Optional[str]) -> None:
        if value is None:
            self._plan = None
        else:
            value = str(value)
            self._plan = '\n'.join(value.splitlines())

    @property
    def sessions(self) -> _Sequence[FingerSession]:
        """ The current sessions array for the user, always defined. """

        return self._sessions


class _FingerSessionManager:
    """ Session manager. """

    __slots__ = ('_sessions',)

    def __init__(self):
        self._sessions = []

    def __repr__(self):
        return repr(self._sessions)

    def __bool__(self):
        return bool(self._sessions)

    def __iter__(self):
        return iter(self._sessions[::-1])

    def __len__(self):
        return len(self._sessions)

    def __delitem__(self, key):
        if key is None:
            self._sessions.pop(0)
        else:
            for i in ([
                i for i, x in enumerate(self._sessions)
                if key == x.name
            ][::-1]):
                self._sessions.pop(i)

    def __getitem__(self, key):
        if key is None:
            try:
                return self._sessions[0]
            except IndexError:
                raise KeyError('could not get latest session') from None

        if type(key) is int:
            try:
                return self._sessions[key]
            except IndexError:
                msg = f'could not get session #{key!r}'
                raise IndexError(msg) from None

        try:
            return next(x for x in self._sessions if key == x.name)
        except StopIteration:
            raise KeyError(f'could not get session {key!r}') from None

    def __setitem__(self, key, value):
        if not isinstance(value, FingerSession):
            raise TypeError('can only add sessions into a session manager')
        value = _copy.deepcopy(value)

        if key is None:
            # Check if except the first session, the key of the session,
            # if any, does not override another key.

            if value.name is not None:
                try:
                    next(
                        i for i, x in self._sessions[1:]
                        if value.name == x.name)
                except StopIteration:
                    pass
                else:
                    msg = "value.name overrides another session's key"
                    raise ValueError(msg) from None

            try:
                self._sessions[0] = value
                return
            except IndexError:
                raise KeyError('could not set latest session') from None

        if type(key) is int:
            # Check if except the key-th session, the key of the session,
            # if any, does not override another key.

            if value.name is not None:
                try:
                    next(
                        i for i, x in self._sessions
                        if i != key and value.name == x.name)
                except StopIteration:
                    pass
                else:
                    msg = "value.name overrides another session's key"
                    raise ValueError(msg) from None

            try:
                self._sessions[key] = value
                return
            except IndexError:
                msg = f'could not set session #{key!r}'
                raise IndexError(msg) from None

        value.name = key

        try:
            i = next(
                i for i, x in enumerate(self._sessions)
                if key == x.name)
        except StopIteration:
            raise KeyError(f'could not set session {key!r}') from None

        self._sessions[i] = value

    def add(self, session):
        """ Add a session. """

        if not isinstance(session, FingerSession):
            raise TypeError(
                'can only insert sessions into a session manager',
            )

        self._sessions.insert(0, session)

# ---
# Formatter base class.
# ---


class FingerFormatter:
    """ Formatter for :py:class:`FingerServer`.

        Provides text-formatted (as strings limited to ASCII)
        answers for given queries with given results as objects.

        This class must be subclassed by other formatters.
        Only methods not starting with an underscore are called by
        instances of :py:class:`FingerServer`; others are utilities
        called by these.

        Unless methods are overridden to have a different behaviour,
        this formatter aims at RFC 1288 compliance.

        :param tzinfo: Timezone used for formatting dates and times.
    """

    def __init__(self, tzinfo: _Optional[_tzinfo] = None):
        if tzinfo is None:
            tzinfo = _dt.now().astimezone().tzinfo
        self._tzinfo = tzinfo

    def __repr__(self):
        return f'{self.__class__.__name__}()'

    # ---
    # Internal formatting utilities.
    # ---

    def _format_idle(self, idle: _td) -> str:
        """ Format an idle time delta. """

        def _iter_idle(idle):
            days = int(idle.days)
            hours = int(idle.seconds / 3600)
            mins = int(idle.seconds % 3600 / 60)
            secs = int(idle.seconds % 60)

            if days:
                yield f'{days} day{("", "s")[days > 1]}'
            if hours:
                yield f'{hours} hour{("", "s")[hours > 1]}'
            if mins:
                yield f'{mins} minute{("", "s")[mins > 1]}'
            if secs:
                yield f'{secs} second{("", "s")[secs > 1]}'

        return f'{" ".join(_iter_idle(idle))} idle'

    def _format_time(self, d: _td) -> str:
        """ Format a date and time. """

        if d < _td():
            return ''

        days = int(d.days)
        hours = int(d.seconds / 3600)
        mins = int(d.seconds % 3600 / 60)

        if days:
            return f'{days}d'
        elif hours or mins:
            return f'{hours:02}:{mins:02}'

        return ''

    def _format_when(self, d: _dt) -> str:
        """ Format a date and time for 'when'. """

        return d.astimezone(self._tzinfo).strftime('%a %H:%M')

    def _format_header(self, hostname: str, raw_query: str) -> str:
        """ Return the header of the formatted answer.

            This header is used for every request,
            except when an error has occurred in the user's query.

            :param hostname: The hostname configured for the server.
            :param raw_query: The raw query given by the user.
            :return: The header of the formatted answer as text.
        """

        if raw_query:
            raw_query = ' ' + raw_query

        return (
            f'Site: {hostname}\r\n'
            f'Command line:{raw_query}\r\n'
            '\r\n'
        )

    def _format_footer(self) -> str:
        """ Return the footer of the formatted answer.

            This footer is used for every request,
            except when an error has occurred in the user's query.

            :return: The footer of the formatted answer as text.
        """

        return ''

    # ---
    # Used formatting functions.
    # ---

    def format_query_error(self, hostname: str, raw_query: str) -> str:
        """ Return the formatted answr for when an error has occurred.

            :param hostname: The hostname configured for the server.
            :param raw_query: The raw query given by the user.
            :return: The formatted answer as text.
        """

        return (
            f'Site: {hostname}\r\n'
            'You have made a mistake in your query!\r\n'
        )

    def format_short(
        self,
        hostname: str,
        raw_query: str,
        users: _Sequence[FingerUser],
    ) -> str:
        """ Return the formatted answer for a user list in the 'short' format.

            :param hostname: The hostname configured for the server.
            :param raw_query: The raw query given by the user.
            :param users: The user list.
            :return: The formatted answer as text.
        """

        if not users:
            return 'No user list available.\r\n'

        now = _dt.now().astimezone()

        lst = []
        for user in users:
            if not user.sessions:
                lst.append((user, None))
            for session in user.sessions:
                lst.append((user, session))

        def _login(u, s):
            return u.login or ''

        def _name(u, s):
            return u.name or ''

        def _line(u, s):
            return s.line if s and s.line else ''

        def _idle(u, s):
            return self._format_time(now - s.idle) if s else ''

        def _logt(u, s):
            return self._format_when(s.start) if s else ''

        def _offic(u, s):
            return (
                f'({s.host})' if s.host
                else u.office if u.office else '')

        columns = (
            ('Login',) + tuple(_login(u, s) for u, s in lst),
            ('Name',) + tuple(_name(u, s) for u, s in lst),
            ('TTY',) + tuple(_line(u, s) for u, s in lst),
            ('Idle',) + tuple(_idle(u, s) for u, s in lst),
            ('When',) + tuple(_logt(u, s) for u, s in lst),
            ('Office',) + tuple(_offic(u, s) for u, s in lst))

        sizes = tuple(max(map(len, c)) + 1 for i, c in enumerate(columns))
        align = ('<', '<', '<', '^', '^', '<')

        lines = []
        for line in range(len(columns[0])):
            lines.append(' '.join(
                f'{columns[i][line][:sizes[i]]:{align[i]}{sizes[i]}}'
                for i in range(len(columns))
            ))

        return (
            self._format_header(hostname, raw_query)
            + '\r\n'.join(lines) + '\r\n'
            + self._format_footer()
        )

    def format_long(
        self,
        hostname: str,
        raw_query: str,
        users: _Sequence[FingerUser],
    ) -> str:
        """ Return the formatted answer for a user list in the 'long' format.

            :param hostname: The hostname configured for the server.
            :param raw_query: The raw query given by the user.
            :param users: The user list.
            :return: The formatted answer as text.
        """

        if not users:
            return 'No user list available.\r\n'

        now = _dt.now().astimezone()
        res = ''

        for user in users:
            res += (
                f'Login name: {user.login[:27]:<27} '
                f'Name: {user.name if user.name else user.login}\r\n'
                f'Directory: {user.home[:28] if user.home else "":<28} '
                f'Shell: {user.shell if user.shell else ""}\r\n'
            )
            if user.office:
                res += f"Office: {user.office if user.office else ''}\r\n"

            if user.sessions:
                # List current sessions.

                for se in user.sessions:
                    since = (
                        se.start.astimezone(self._tzinfo)
                        .strftime('%a %b %e %R')
                    )
                    tz = self._tzinfo
                    res += f'On since {since} ({tz})'
                    if se.line is not None:
                        res += f' on {se.line}'
                    if se.host is not None:
                        res += f' from {se.host}'
                    res += '\r\n'

                    idle = now - se.idle
                    if idle >= _td(seconds=4):
                        res += f'   {self._format_idle(idle)}\r\n'
            elif user.last_login is not None:
                # Show last login.

                date = (
                    user.last_login.astimezone(self._tzinfo)
                    .strftime('%a %b %e %R'))
                tz = self._tzinfo
                res += f'Last login {date} ({tz}) on console\r\n'
            else:
                res += 'Never logged in.\r\n'

            if user.plan is None:
                res += 'No plan.\r\n'
            else:
                res += 'Plan:\r\n'
                res += '\r\n'.join(user.plan.splitlines())
                res += '\r\n'

            res += '\r\n'

        return (
            self._format_header(hostname, raw_query)
            + res + self._format_footer()
        )

# ---
# Interface (dummy) base class.
# ---


class FingerInterface:
    """ Data source for :py:class:`FingerServer`.

        Provides users and answers for the various queries received
        from the clients by the server.

        This class must be subclassed by other interfaces.
        Only methods not starting with an underscore are called by
        instances of :py:class:`FingerServer`; others are utilities
        called by these.

        By default, it behaves like a dummy interface.
    """

    def __repr__(self):
        return f'{self.__class__.__name__}()'

    def transmit_query(
        self,
        query: _Optional[str],
        host: str,
        verbose: bool,
    ) -> str:
        """ Transmit a user query to a foreign host.

            This function returns the answer formatted by it.

            If used directly (not overridden by subclasses), this
            method will refuse to transmit finger queries.

            :param query: The user query, set to None in case of
                          no query provided by the client.
            :param host: The distant host to which to transmit the
                         query.
            :param verbose: Whether the verbose flag (``/W``, long format)
                            has been passed by the current client
                            or not.
            :return: The answer formatted by the distant server.
        """

        return "This server won't transmit finger queries.\r\n"

    def search_users(
        self,
        query: _Optional[str],
        active: _Optional[bool],
    ) -> _Sequence[FingerUser]:
        """ Search for users on the current host using the given query.

            :param query: The user query, set to None in case of no
                          query provided by the client.
            :param active: Whether to get active users (True),
                           inactive users (False), or all users (None).
            :return: The list of users found using the query provided
                     by the client.
        """

        return []


# ---
# Finger/TCP server implementation.
# ---


class _FingerQuery:
    """ A finger query.

        Requests information about connected or specific users on a
        remote server.

        There are three types of requests recognized by RFC 1288:

         * {C} is a request for a list of all online users.
         * {Q1} is a request for a local user.
         * {Q2} is a request for a distant user (with hostname).

        /W means the RUIP (program answering the query) should be more
        verbose (this token can be ignored).
    """

    __slots__ = ('line', 'host', 'username', 'verbose')

    # "By default, this program SHOULD filter any unprintable data,
    #  leaving only printable 7-bit characters (ASCII 32 through
    #  ASCII 126), tabs (ASCII 9) and CRLFs."

    allowed_chars = (
        "\t !\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
        + _string.ascii_letters + _string.digits)

    def __init__(self, raw):
        """ Initialize the query object by decoding the data. """

        # Get a character string out of the query.

        raw = raw.decode('ascii', errors='ignore')
        raw = ''.join(c for c in raw if c in self.allowed_chars)
        self.line = raw

        # Get elements.

        self.host = None
        self.username = None
        self.verbose = False
        for element in raw.split():
            if element[0] == '/':
                if not element[1:]:
                    raise _MalformedQueryError(
                        raw, "missing feature flags after '/'",
                    )
                for letter in element[1:]:
                    if letter == 'W':
                        self.verbose = True
                    else:
                        raise _MalformedQueryError(
                            raw,
                            f'unknown feature flag {letter!r}',
                        )
                continue
            elif self.username is not None:
                raise _MalformedQueryError(raw, 'multiple query arguments')
            self.username = element

        if self.username is not None and '@' in self.username:
            self.host, *self.username = self.username.split('@')[::-1]
            self.username = '@'.join(self.username[::-1])


class FingerServer:
    """ The main finger server class.

        :param binds: The hosts and ports on which the server should
                      listen to and answer finger requests.
        :param hostname: The hostname to be included in answers sent
                         to clients.
        :param interface: The interface to use for querying
                          users and sessions.
        :param formatter: The formatter to use for formatting
                          answers sent to clients.
    """

    def __init__(
        self,
        binds: str = 'localhost:79',
        hostname: str = 'LOCALHOST',
        interface: FingerInterface = FingerInterface(),
        formatter: FingerFormatter = FingerFormatter(),
    ):
        # Check the host name, which should be simple LDH, i.e.
        # Letters, Digits, Hyphens.

        try:
            hostname = hostname.upper()
            if not all(
                c in _string.ascii_letters + _string.digits + '.-'
                for c in hostname
            ):
                raise AssertionError('Non-LDH hostname')
        except Exception:
            raise _HostnameError(hostname)

        # Check the interface and formatter classes.

        if not isinstance(interface, FingerInterface):
            raise TypeError(
                'Please base your interface '
                'on the base class provided by the pyfingerd module',
            )

        if not isinstance(formatter, FingerFormatter):
            raise TypeError(
                'Please base your formatter '
                'on the base class provided by the pyfingerd module',
            )

        # Keep the parameters.

        self._host = hostname
        self._interface = interface
        self._formatter = formatter

        # Check the binds.

        self._binds = [b for b in _FingerBindsDecoder().decode(binds)]
        if not self._binds:
            raise _NoBindsError()

        # Initialize multi-process related.

        self._p = None

    @property
    def hostname(self):
        """ The hostname configured for this server. """

        return self._host

    @property
    def interface(self):
        """ The interface configured for this server. """

        return self._interface

    @property
    def formatter(self):
        """ The formatter configured for this server. """

        return self._formatter

    def _serve(self):
        """ Start servers and serve on the current process. """

        async def handle_finger_connection(inp, outp):
            """ Handle a connection. """

            src, *_ = inp._transport.get_extra_info('peername')

            # Gather the request line.

            try:
                line = await inp.readline()
            except ConnectionResetError:
                _error_logger.info(
                    f'{src} submitted no query. (possible scan)',
                )
                outp.close()
                return

            # Decode the request.

            ans = ''

            try:
                query = _FingerQuery(line)
            except _MalformedQueryError as e:
                _error_logger.info(
                    f'{src} made a bad request: {e.msg} in {e.query!r}.',
                )
                ans = self.formatter.format_query_error(
                    self.hostname, line,
                )
            else:
                if query.host is not None:
                    if query.username:
                        _access_logger.info(
                            f'{src} requested transmitting user query for '
                            f'{query.username!r} at {query.host!r}.',
                        )
                    else:
                        _access_logger.info(
                            f'{src} requested transmitting user query '
                            f'to {query.host!r}.',
                        )

                    ans = self.interface.transmit_query(
                        query.host, query.username, query.verbose)
                else:
                    if query.username:
                        users = self.interface.search_users(
                            query.username, None)
                        _access_logger.info(
                            f'{src} requested user {query.username!r}: found '
                            + (
                                {0: 'no user', 1: '1 user'}
                                .get(len(users), f'{len(users)} users')
                            )
                            + '.',
                        )
                    else:
                        users = self.interface.search_users(None, True)
                        _access_logger.info(
                            f'{src} requested connected users: found '
                            + (
                                {0: 'no user', 1: '1 user'}
                                .get(len(users), f'{len(users)} users')
                            )
                            + '.',
                        )

                    if query.username or query.verbose:
                        ans = self.formatter.format_long(
                            self.hostname, query.line, users)
                    else:
                        ans = self.formatter.format_short(
                            self.hostname, query.line, users)

            # Write the output.

            ans = '\r\n'.join(ans.splitlines()) + '\r\n'
            outp.write(ans.encode('ascii', errors='ignore'))

        async def handle_connection(inp, outp):
            """ Handle a new incoming connection. """

            try:
                await handle_finger_connection(inp, outp)
            except Exception:
                _logger.exception('The following exception has occurred:')
                outp.write(b'An internal exception has occurred.\r\n')

            outp.close()
            await outp.wait_closed()

        async def start_server(bind):
            """ Start a given server. """

            family, host, port = bind.runserver_params

            try:
                server = await _asyncio.start_server(
                    handle_connection,
                    host=host, port=port, family=family,
                    reuse_address=True,
                )
            except (OSError, PermissionError) as exc:
                if isinstance(exc, PermissionError):
                    name = 'EACCES'
                else:
                    name = _errorcode[exc.errno]

                if name == 'EADDRINUSE':
                    _logger.error(
                        f'Could not bind to [{host}]:{port}: '
                        'address already in use.',
                    )
                    return
                elif name == 'EACCES':
                    _logger.error(
                        f'Could not bind to [{host}]:{port}: '
                        f'port {port} privileged port and process '
                        'is unprivileged.',
                    )
                else:
                    raise
            else:
                _logger.info(
                    f'Starting pyfingerd on [{host}]:{port}.',
                )

                try:
                    await server.serve_forever()
                except _asyncio.CancelledError:
                    pass

                _logger.info(
                    f'Stopping pyfingerd on [{host}]:{port}.',
                )

        async def cron_call(func, spec):
            """ Call a function periodically using a cron specification. """

            spec.set_current(_dt.now())

            while True:
                try:
                    func()
                except SystemExit:
                    return

                while True:
                    seconds = (
                        spec.get_next(_dt) - _dt.now()
                    ).total_seconds()
                    if seconds >= 0:
                        break

                await _asyncio.sleep(seconds)

        async def start_servers():
            """ Start the servers. """

            def get_coroutines():
                """ Tasks iterator. """

                for bind in self._binds:
                    yield start_server(bind)

                for key in dir(self.interface):
                    member = getattr(self.interface, key)
                    if not callable(member):
                        continue

                    try:
                        spec = member.__cron__
                    except AttributeError:
                        continue

                    if not isinstance(spec, _croniter):
                        continue

                    yield cron_call(member, spec)

            tasks = [_asyncio.create_task(co) for co in get_coroutines()]
            await _asyncio.wait(
                tasks, return_when=_asyncio.FIRST_COMPLETED)

            # If any task has set an exception, we try to catch it.

            exc = None
            for task in tasks:
                if exc is None:
                    try:
                        exc = task.exception()
                    except _asyncio.exceptions.InvalidStateError:
                        exc = None

                task.cancel()

            if exc is not None:
                raise exc

        try:
            _asyncio.run(start_servers())
        except KeyboardInterrupt:
            pass

    def start(self) -> None:
        """ Start all underlying server processes and bind all ports. """

        if self._p is not None and self._p.is_alive():
            return

        self._p = _multip.Process(target=self._serve)
        self._p.start()

    def stop(self) -> None:
        """ Stop all underlying server processes and unbind all ports. """

        if self._p is None or not self._p.is_alive():
            return

        self._p.kill()
        self._p.join()
        self._p = None

    def serve_forever(self) -> None:
        """ Start all servers and serve in a synchronous fashion.

            It starts all servers :py:meth:`FingerServer.start`, waits for
            an interrupt signal, and stops all servers using
            :py:meth:`FingerServer.stop`.
        """

        if self._p is not None:
            self.start()

            try:
                while True:
                    _signal.pause()
            except KeyboardInterrupt:
                pass

            self.stop()
        else:
            # If the server hasn't been started on another process
            # using ``.start()``, we can just start is on this process.

            self._serve()

    def shutdown(self):
        """ Shutdown the server, alias to `.stop()`. """

        self.stop()

# End of file.