Compare commits

...

3 Commits

Author SHA1 Message Date
64f5cf2c30 lol forgot to update this for ages 2023-12-02 16:57:04 +11:00
667fb2bd4f I don't need that anymore 2022-08-18 03:46:07 +10:00
9dd3b82bf8 Still splitting matrix out from corelib 2022-08-10 00:00:55 +10:00
16 changed files with 249 additions and 155 deletions

View File

@@ -1,58 +0,0 @@
import aiohttp as ah
import time
import asyncio
import datetime
import traceback
from .utils import Event,Listener
func=type(lambda:1) # Gross, but I can't actually find it.
# And yes, lamdas are <class 'function'> too.
tracing=False
def trace(*msg):
if tracing: print(datetime.datetime.now(),*msg)
class MatrixClient():
def __init__(self,homeserver,token):
self.event_queue=asyncio.Queue() #Contains Events. Everything in here must be an Event.
self.listeners=[]
self.timers=[]
async def request(self,endpoint='sync',method='GET', ver=0,headers={} *args,**kwargs):
async with self.session.request(method, f'{self.baseurl}/r{ver}/{endpoint}', headers=headers|{'Authorization':f'Bearer {self.token}'}, *args,**kwargs) as fetched:
if fetched.status_code!=200: raise Exception('fix ur shit')
try: return await fetched.json()
except JSONDecodeError: pass # TODO: Figure out what this is called
# This function just dumps /sync blobs in the event queue as a raw event.
# ALL handling is deferred to handlers.
# ... Except updating the since token. That's important to do here to guarantee that it never calls with the same token twice.
async def sync(self):
blob=await self.request(params={'timeout':30000,'since':self.since})
self.since=blob['next_batch']
self.event_queue.put(Event('m.sync',None,blob))
def addlistener(self,name=None,match=None):
if isinstance(name,func) and match==None: match=name; name=None
def __wrap__(funky):
self.listeners.append(Listener(name,match,funky))
return funky
def loop(timer=0):
def _loop(funky):
@functools.wraps(funky)
async def _wrapper(*args,**kwargs):
while True:
try: await funky(*args,**kwargs)
except Exception: open('error '+str(datetime.datetime.now()),'w').write(traceback.format_exc())
await asyncio.sleep(timer)
self.timers.append(_wrapper)
return _wrapper
return _loop
async def process_queue(self):
item=await self.event_queue.get():
for listener in self.listeners:
if listener==item:
await listener(item)

53
chatlib/chatlib.py Normal file
View File

@@ -0,0 +1,53 @@
import asyncio
from traceback import format_exc
from .listener import Listener
from .timer import Timer
from . import utils
from .event import Event
event_queue=asyncio.Queue() #Contains Events. Everything in here must be an Event.
listeners=[] # These respond to events. They're executed by workers processing the event queue. They should all be Listeners
timers=[] # These just execute on a schedule. That schedule can be "as soon as it exits". Should all be Timers, which include the property of whether to wait for exit before starting the next call or not (rarely relevant, so defaults to "no" unless schedule is 0).
# Queue management
async def process_queue():
while True:
item=await event_queue.get()
asyncio.create_task(process_item(item))
async def process_item(item):
for listener in listeners:
terminate=False
if listener==item:
try: terminate=await listener(item)
except: utils.trace('explosions in',listener); format_exc()
if terminate: break
# Convenience functions for initialising.
def addlistener(match):
def __wrap__(funky):
listeners.append(Listener(match,funky))
return funky
return __wrap__
def addtimer(interval,start=None,block=None):
def __wrap__(funky):
a=Timer(funky,interval,block,start)
timers.append(a)
try: # Timers added after start will get run here, but timers added before start won't
asyncio.create_task(a.run())
except RuntimeError: pass
return funky
return __wrap__
def addevent(*args,**kwargs):
asyncio.create_task(event_queue.put(Event(*args,**kwargs)))
async def main():
# Any time that didn't get run before occurs here.
for timer in timers: asyncio.create_task(timer.run())
await process_queue()
def run():
asyncio.run(main())

8
chatlib/event.py Normal file
View File

@@ -0,0 +1,8 @@
from dataclasses import dataclass
@dataclass
class Event():
event_type:str=''
data:object=None
raw_data:dict=None
outbound:bool=False # This should be remapped as a data piece so it could instead be a direction/destination

26
chatlib/listener.py Normal file
View File

@@ -0,0 +1,26 @@
from asyncio import iscoroutinefunction
from .event import Event
class Listener():
def __init__(self,match,function):
self.match=match
self.function=function
@property
def matchstr(self):
return isinstance(self.match,str)
@property
def isasync(self):
return iscoroutinefunction(self.function)
async def __call__(self,*args,**kwargs):
if not self.isasync: return self.function(*args,**kwargs)
return await self.function(*args,**kwargs)
def __eq__(self,other):
if isinstance(other,Event):
if self.matchstr: return self.match==other.event_type
return self.match(other) # If it's not a string, assume it's a callable.
else: return super.__eq__(self,other)
def __str__(self): return self.function.__name_

54
chatlib/timer.py Normal file
View File

@@ -0,0 +1,54 @@
from traceback import format_exc
from asyncio import sleep,create_task
from datetime import datetime
from asyncio import iscoroutinefunction,get_event_loop
from . import utils
class Timer():
def __init__(self,function,interval,block=None,start=None,*args,**kwargs):
self.function=function
self.interval=interval
self.args=args
self.kwargs=kwargs
self.block=block
if block is None: self.block=(interval == 0)
self.start=start
self.scheduled=None
@property
def isasync(self):
return iscoroutinefunction(self.function)
"""
This mess of functions...
Operate and enqueue are just wrappers that do some useful things that I couldn't fit elsewhere. Iterate is the core that runs them in the right order according to blocking.
"""
async def enqueue(self,interval=None):
self.scheduled=create_task(self.iterate(interval))
async def operate(self,*args,**kwargs):
if not args: args=self.args
if not kwargs: kwargs=self.kwargs
try:
if not self.isasync: return self.function(*args,**kwargs)
return await self.function(*args,**kwargs)
except: utils.trace('explosions in',self); format_exc()
async def iterate(self,interval=None):
if interval is None: interval=self.interval
await sleep(interval)
await (self.operate,self.enqueue)[not self.block]()
await (self.operate,self.enqueue)[self.block]()
async def run(self):
slop=0
if self.start is not None:
ima=datetime.utcnow()
slop=(self.start-ima).total_seconds() # It'll be very slightly wrong but whatever
await self.enqueue(slop)
def __str__(self):
return self.function.__name__
def __del__(self):
self.scheduled.cancel()

5
chatlib/utils.py Normal file
View File

@@ -0,0 +1,5 @@
from datetime import datetime
tracing=False
def trace(*msg):
if tracing: print(datetime.now(),*msg)

View File

@@ -8,3 +8,6 @@ Raw events will not be exposed, only processed events. This is not a "you have f
https://spec.matrix.org
https://www.matrix.org/docs/develop/
Main chat lib defines API - is NOT a class, the module itself is a singleton
Specific proto libs are classes, instansiate with the module + account info, it'll hook itself into the module. For convenience, if the module isn't provided they'll do it themselves, and provide a .run() wrapper. If that's used with a provided module (set a flag), they emit a warning.

View File

@@ -1,5 +1,6 @@
from .utils import Listener as _l, Event as _e, get_or_create as goc
from .utils import get_or_create as goc
import .models
from .chatlib import Listener as _l, Event as _e
# Default handlers will be given two parts each:
# 1) Mould the raw event into a parsed event (update internal state)
@@ -9,10 +10,11 @@ import .models
def sync_f(client,event):
blob=event.raw_data
# This event never really gets any processed data in it.
rooms=blob['rooms']['join']
for rid,room in rooms.items():
client.event_queue.add(_e('m.room',{'id':rid},room))
sync=_l('sync','m.sync',sync_f)
for status in ['join','leave','invite']:
roomlist=blob.get('rooms',{status:{}}).get(status,{})
for rid,room in roomlist.items():
client.event_queue.add(_e('m.room',{'id':rid,'state':status},room))
sync=_l('m.sync',sync_f)
def room_f(client,event):
blob=event.raw_data
@@ -20,4 +22,4 @@ def room_f(client,event):
room=goc(lambda x:x.id==rid,client.account.rooms,models.Room(rid))
event.data=room
room.name=
room=_l('room','m.room',room_f)
room=_l('m.room',room_f)

18
matrix/matrixapi.py Normal file
View File

@@ -0,0 +1,18 @@
class MatrixLibHttp():
def __init__(self,instance,token=None,username=None,password=None,chatlib=None):
self.instance=instance
self.https=True
self.token=token
if chatlib is None:
import chatlib
chatlib.addlistener() # TODO: probably should make that a decorator actually. Lol, it already is.
@property
def baseurl(self):
return 'http'+['','s'][self.https]+'://'+self.instance+'/_matrix/client'
async def request(self,endpoint='sync',method='GET', ver=0,headers={}, *args,**kwargs):
async with self.session.request(method, f'{self.baseurl}/r{ver}/{endpoint}', headers=headers|{'Authorization':f'Bearer {self.token}'}, *args,**kwargs) as fetched:
if fetched.status_code!=200: raise Exception('fix ur shit')
try: return await fetched.json()
except JSONDecodeError: pass # TODO: Figure out what this is called

54
matrix/models.py Normal file
View File

@@ -0,0 +1,54 @@
from dataclasses import dataclass, field
from datetime import datetime
@dataclass
class User():
@property
def mxid(self): return f'@{self.username}:{self.homeserver}'
@mxid.setter
def set_mxid(self,value):
a=value.lstrip('@').split(':')
self.username=a[0]; self.homeserver=a[1]
username:str
homeserver:str
nick:str=''
avatar:str=''
@dataclass
class Account(User):
rooms:List[Room]=field(default_factory=list)
@dataclass
class Room():
room_id:str
name:str=''
viewer:Account=None
members:List[Member]=field(default_factory=list)
@property
def ismember(self): return self.viewer in self.members
parents:List[Space]=field(default_factory=list)
messages:List[Message]=field(default_factory=list)
@dataclass
class Space(Room):
children:List[Room]=field(default_factory=list)
@dataclass
class Event():
event_id:str
author:Member
timestamp:datetime
contents:str=''
@dataclass
class Message(Event):
format:str=''
@dataclass
class State(Event):
key:str
@dataclass
class Member(User):
room_av:str=''
room_nick:str=''

0
matrix/outbound.py Normal file
View File

10
matrix/timers.py Normal file
View File

@@ -0,0 +1,10 @@
from chatlib import addtimer
# This function just dumps /sync blobs in the event queue as a raw event.
# ALL handling is deferred to handlers.
# ... Except updating the since token. That's important to do here to guarantee that it never calls with the same token twice.
@addtimer(0)
async def sync(self):
blob=await self.request(params={'timeout':30000,'since':self.since})
self.since=blob['next_batch']
self.event_queue.put(Event('m.sync',None,blob))

9
matrix/utils.py Normal file
View File

@@ -0,0 +1,9 @@
def get_or_create(needle,haystack,default):
"""
This is a wrapper for filter that can add stuff. Nothing special. Needle is a function, default isn't. Haystack is a list. I might fix that later.
"""
f=filter(needle,haystack)
try: return next(f)
except StopIteration: pass
haystack.append(default)
return default

View File

@@ -1,13 +0,0 @@
from .matrix import *
import .listeners
real_listeners=dict(filter(lambda x:isinstance(x,Listener),listeners.__dict__.items()))
class MatrixClient(MatrixClient):
def __init__(self,*args,**kwargs):
super().__init__(*args,**kwargs)
self.listeners=real_listeners
self.homeserver=homeserver
self.token=token # Deal with login stuff later
self.baseurl=f'https://{self.homeserver}/_matrix/client'
self.session=ah.ClientSession()
self.since=None

View File

@@ -1,39 +0,0 @@
from .utils import redc
@redc
class Account():
mxid:str
username:str
homeserver:str
avatar_url:str=''
nickname:str=''
rooms:List[Room]=field(default_factory=list)
@redc
class Room():
id:str
name:str=''
members:List[Member]=field(default_factory=list)
parents:List[Space]=field(default_factory=list)
messages:List[Message]=field(default_factory=list)
@redc
class Space(Room):
children:List[Room]=field(default_factory=list)
@redc
class Message():
id:str
contents:str
author:Member
@redc
class User():
mxid:str
nick:str
avatar:str
@redc
class Member(User):
room_av:str
room_nick:str

View File

@@ -1,38 +0,0 @@
from dataclasses import dataclass, field, _MISSING_TYPE as mt
from asyncio import iscoroutinefunction
@dataclass
class Event():
event_type:str
data:Object=None
raw_data:dict=None
outbound:bool=False
class Listener():
def __init__(self,name,match,function):
self.name=name or function.__name__
self.match=match
self._matchstr=isinstance(match,str)
self.function=function
self._async=iscoroutinefunction(function)
async def __call__(self,*args,**kwargs):
if not self._async: return self.function(*args,**kwargs)
return await self.function(*args,**kwargs)
def __eq__(self,other):
if isinstance(other,Event):
if self._matchstr: return self.match==other.event_type
return self.match(other) # If it's not a string, assume it's a callable.
else: return super.__eq__(self,other)
def __str__(self): return self.name
def get_or_create(needle,haystack,default):
"""
This is a wrapper for filter that can add stuff. Nothing special. Needle is a function, default isn't. Haystack is a list. I might fix that later.
"""
f=filter(needle,haystack)
try: return next(f)
except StopIteration: pass
haystack.append(default)
return default