Skip to content

Commit

Permalink
feat(parser): add tag access fields to Message objects
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxdaemon committed Apr 17, 2024
1 parent deaf577 commit 01955f1
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions irclib/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
Backported from async-irc (https://github.com/snoonetIRC/async-irc.git)
"""

import datetime
import re
from abc import ABCMeta, abstractmethod
from typing import (
Expand Down Expand Up @@ -70,6 +71,17 @@
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").replace(
tzinfo=datetime.timezone.utc
)
else:
ts = datetime.datetime.now(datetime.timezone.utc)

return ts


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

Expand Down Expand Up @@ -579,6 +591,22 @@ 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 has_tag(self, name: str) -> bool:
if not self.tags:
return False

return name in self.tags

def get_tag_value(self, name: str) -> Optional[str]:
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 01955f1

Please sign in to comment.