Skip to content

Commit

Permalink
parser: Add tag access fields to Message objects
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdaemon committed Feb 9, 2023
1 parent ff23b8a commit d53a51a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions irclib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
Union,
cast,
)
import datetime

from irclib.errors import ParseError

Expand Down Expand Up @@ -69,6 +70,15 @@
SelfT = TypeVar("SelfT")


def parse_server_time(value: Optional[str]) -> datetime.datetime:
if value:
ts = datetime.datetime.strptime(value, '%Y-%m-%dT%H:%M:%S.%fZ')
else:
ts = datetime.datetime.utcnow()

return ts.replace(tzinfo=datetime.timezone.utc)


class Parseable(metaclass=ABCMeta):
"""Abstract class for parseable objects"""

Expand Down Expand Up @@ -582,6 +592,16 @@ def __init__(
self._command = command
self._parameters = _parse_params(parameters)

self.time = parse_server_time(self.get_tag_value('time'))
self.message_id = self.get_tag_value('msgid')
self.batch_id = self.get_tag_value('batch')

def get_tag_value(self, name):
if self.tags and name in self.tags:
return self.tags[name].value

return None

@property
def tags(self) -> MsgTagList:
"""IRC tag list"""
Expand Down

0 comments on commit d53a51a

Please sign in to comment.