mirror of
https://git.ugnet.gay/CrossTalk/azul.git
synced 2026-11-25 08:30:04 +00:00
0.6 final
This commit is contained in:
1 parent
7c0581eb97
commit
b89e1932bb
160 files changed
+20534
-4664
No files matched your search
+142
-29
@@ -1,24 +1,29 @@
|
||||
import os
|
||||
import struct
|
||||
import time
|
||||
import settings
|
||||
|
||||
from array import array
|
||||
from core import event
|
||||
from core.backend import Chat, ChatSession, BackendSession
|
||||
from core.models import Contact, Substatus, Circle, CircleRole, User, TextWithData, OIM, LoginOption
|
||||
from core.alerts import Alert
|
||||
from dataclasses import dataclass, field
|
||||
from enum import IntEnum
|
||||
from util.misc import Logger
|
||||
from util.misc import Logger, is_valid_oscar_screenname
|
||||
from urllib.parse import quote
|
||||
from typing import Optional, Any, Dict
|
||||
from typing import Optional, Any, Dict, List
|
||||
|
||||
from ..foodgroups.buddy import build_presence_notif
|
||||
from ..foodgroups.buddy import build_presence_notif, watching
|
||||
from ..foodgroups.feedbag import build_buddy_added_notif
|
||||
from ..foodgroups.popup import popup_display
|
||||
from ..proto.snac import OSCARContext
|
||||
from ..proto.tlv import TLV, marshal_tlvs, find_tlv
|
||||
from ..proto.chat import ChatEventHandler
|
||||
from . import names
|
||||
from .delivery import oscar_sessions
|
||||
|
||||
# TODO(subpurple): move this to a config of some sort
|
||||
# TODO: move this to a config of some sort
|
||||
FOODGROUP_VERSIONS: {int, int} = {
|
||||
0x0001: 4, # OSERVICE
|
||||
0x0002: 1, # LOCATE
|
||||
@@ -30,25 +35,73 @@ FOODGROUP_VERSIONS: {int, int} = {
|
||||
0x000A: 1, # USER_LOOKUP
|
||||
0x000B: 1, # STATS
|
||||
0x000C: 1, # TRANSLATE
|
||||
0x000F: 1, # DIRECTORY
|
||||
0x0013: 6, # FEEDBAG
|
||||
0x0015: 2, # ICQ
|
||||
0x0018: 1, # EMAIL
|
||||
0x0022: 1, # PLUGIN
|
||||
0x0024: 1, # UNNAMED (possibly NACHOS?)
|
||||
0x0025: 1 # MDIR
|
||||
}
|
||||
|
||||
SERVICE_BOS = 0x0000
|
||||
|
||||
SERVICE_FOODGROUPS: Dict[int, List[int]] = {
|
||||
SERVICE_BOS: [0x0018, 0x0010, 0x0003, 0x0013, 0x0004, 0x0015, 0x0002, 0x0001, 0x0009, 0x000A, 0x0006, 0x0008, 0x000B],
|
||||
0x0007: [0x0001, 0x0007], # ADMIN
|
||||
0x000D: [0x000D, 0x0001], # CHAT_NAV
|
||||
0x000E: [0x0001, 0x000E], # CHAT
|
||||
0x000F: [0x000F, 0x0001], # ODIR
|
||||
0x0010: [0x0010, 0x0001], # BART
|
||||
0x0018: [0x0018, 0x0001], # ALERT
|
||||
}
|
||||
|
||||
ERROR_URLS: {int, str} = {
|
||||
0x0001: 'http://www.aim.aol.com/errors/UNREGISTERED_SCREENNAME.html', # Unregistered screen name
|
||||
0x0005: 'http://www.aim.aol.com/errors/MISMATCH_PASSWD.html', # Incorrect password
|
||||
0x0009: '', # Not a tester (inaccurate)
|
||||
0x0011: 'http://www.aim.aol.com/errors/SUSPENDED.html', # Suspended
|
||||
0x0006: '' # Unverified E-mail (inaccurate)
|
||||
0x0006: '', # Unverified E-mail (inaccurate)
|
||||
0x0008: '', # ICQ user doesn't exist
|
||||
}
|
||||
|
||||
PW_CHANGE_URL_FORMAT = 'http://aim.aol.com/redirects/password/change_password.adp?ScreenName={}&ccode={}&lang={}'
|
||||
|
||||
DISCONNECT_NEW_LOGIN = 0x0001
|
||||
|
||||
bos_cookies: array[{bytes, OSCARContext}] = []
|
||||
COOKIE_LIFETIME = 300
|
||||
|
||||
|
||||
@dataclass
|
||||
class ServiceCookie:
|
||||
uuid: Optional[str] = None
|
||||
version: str = '?'
|
||||
program: str = 'aim'
|
||||
screen_name: Optional[str] = None
|
||||
icq: bool = False
|
||||
client_id: str = ''
|
||||
multi_conn: Optional[int] = None
|
||||
bs: Optional[BackendSession] = None
|
||||
service: int = SERVICE_BOS
|
||||
room: Optional[Dict[str, Any]] = None
|
||||
issued: float = field(default_factory = time.time)
|
||||
|
||||
|
||||
bos_cookies: Dict[bytes, ServiceCookie] = {}
|
||||
|
||||
def issue_cookie(entry: ServiceCookie) -> bytes:
|
||||
now = time.time()
|
||||
for stale in [cookie for cookie, other in bos_cookies.items() if now - other.issued > COOKIE_LIFETIME]:
|
||||
del bos_cookies[stale]
|
||||
cookie = os.urandom(256)
|
||||
bos_cookies[cookie] = entry
|
||||
return cookie
|
||||
|
||||
def take_cookie(cookie: bytes) -> Optional[ServiceCookie]:
|
||||
entry = bos_cookies.pop(cookie, None)
|
||||
if entry is None or time.time() - entry.issued > COOKIE_LIFETIME:
|
||||
return None
|
||||
return entry
|
||||
|
||||
|
||||
class BackendEventHandler(event.BackendEventHandler):
|
||||
@@ -61,20 +114,33 @@ class BackendEventHandler(event.BackendEventHandler):
|
||||
|
||||
def on_maintenance_message(self, *args: Any, **kwargs: Any) -> None:
|
||||
self.ctrl.logger.info("on_maintenance message dispatched")
|
||||
message = "CrossTalk will be down for maintenance soon. Now is a good time to wrap up any conversations."
|
||||
if args[1] is not None and args[1] > 0:
|
||||
message = """CrossTalk will be down for maintenance in around {} minute(s). \
|
||||
Now is a good time to wrap up any conversations.""".format(args[1])
|
||||
self.ctrl.send_snac(popup_display(self, "https://crosstalk.im/maintenance", message))
|
||||
|
||||
def on_client_alert(self, icon_url: Optional[str], url: Optional[str], message: str = '') -> None:
|
||||
def on_client_alert(self, alert: Alert) -> bool:
|
||||
self.ctrl.logger.info("on_client_alert dispatched")
|
||||
self.ctrl.send_snac(popup_display(self, url, message))
|
||||
self.ctrl.send_snac(popup_display(self, alert.url or '', alert.plain()))
|
||||
return True
|
||||
|
||||
def on_maintenance_boot(self) -> None:
|
||||
# TODO(HIDEN): can we pass an informative message to the user?
|
||||
self.ctrl.close()
|
||||
return
|
||||
|
||||
def on_warning_level_changed(self, level: int, warner: Optional[User]) -> None:
|
||||
# a warning (from AIM, TOC or an MSN spam report) gets OSERVICE__EVIL_NOTIFICATION; any change, wearing off too, a
|
||||
# fresh OSERVICE__USER_INFO_UPDATE, since the level is part of the user's own info
|
||||
from ..foodgroups.icbm import build_evil_notification
|
||||
from ..foodgroups.oservice import build_self_info
|
||||
previous = self.bs.front_data.get('oscar0_evil', 0)
|
||||
self.bs.front_data['oscar0_evil'] = level
|
||||
if level > previous:
|
||||
self.ctrl.send_snac(build_evil_notification(self.bs, level, warner))
|
||||
self.ctrl.send_snac(build_self_info(self.bs))
|
||||
|
||||
def on_presence_notification(
|
||||
self, ctc: Contact, on_contact_add: bool, old_substatus: Substatus, *,
|
||||
trid: Optional[str] = None, update_status: bool = True, update_info_other: bool = True,
|
||||
@@ -86,18 +152,27 @@ Now is a good time to wrap up any conversations.""".format(args[1])
|
||||
self.ctrl.logger.info('update_info_other:', update_info_other)
|
||||
self.ctrl.logger.info('status:', ctc.status.substatus.name)
|
||||
|
||||
# don't want updates from users with invalid usernames
|
||||
if not names.representable(ctc.head):
|
||||
return
|
||||
|
||||
# don't want any non-status updates
|
||||
if not update_status:
|
||||
self.ctrl.logger.info('fell into update_status check')
|
||||
return
|
||||
|
||||
# for now, we only send presence notifications if the buddy changing status is offline(-ish) or online
|
||||
if not ctc.status.is_offlineish() and ctc.status.substatus != Substatus.Online:
|
||||
self.ctrl.logger.info('fell into status check')
|
||||
# presence only starts flowing once the client says it's ready for it (OSERVICE__CLIENT_READY), and only for
|
||||
# people on the buddy list the client actually runs on (its own one, unless it uses the feedbag)
|
||||
if not self.bs.front_data.get('oscar0_ready') or not watching(self.bs, ctc.head):
|
||||
return
|
||||
|
||||
self.ctrl.logger.info('sending presence notif')
|
||||
self.ctrl.send_snac(build_presence_notif(ctc))
|
||||
try:
|
||||
ctc.compute_visible_status(self.bs.user)
|
||||
|
||||
self.ctrl.logger.info('sending presence notif')
|
||||
self.ctrl.send_snac(build_presence_notif(ctc, self.ctrl.backend, self.bs))
|
||||
except Exception as ex:
|
||||
self.ctrl.logger.error(ex)
|
||||
|
||||
def on_presence_self_notification(self, old_substatus: Substatus, *, update_status: bool = True,
|
||||
update_info: bool = True) -> None:
|
||||
@@ -110,12 +185,17 @@ Now is a good time to wrap up any conversations.""".format(args[1])
|
||||
) -> None:
|
||||
self.ctrl.logger.info('on_chat_invite')
|
||||
if chat is None:
|
||||
chat = self.backend.chat_create()
|
||||
chat = self.ctrl.backend.chat_create()
|
||||
chat.add_id('oscar0', chat.ids['main'])
|
||||
chats = self.bs.front_data.setdefault('oscar0_chats', {})
|
||||
# a conversation with the same person moves to the newest chat they started
|
||||
previous = chats.pop(inviter.uuid, None)
|
||||
if previous is not None and previous[0].chat is not chat:
|
||||
previous[0].close()
|
||||
evt = ChatEventHandler(self.ctrl.backend.loop, self.ctrl, self.bs)
|
||||
cs = chat.join('oscar0', self.bs, evt)
|
||||
chat.send_participant_joined(cs)
|
||||
self.bs.front_data['oscar0_chats'][inviter.uuid] = (cs, evt)
|
||||
chats[inviter.uuid] = (cs, evt)
|
||||
return
|
||||
|
||||
def on_declined_chat_invite(self, chat: Chat, circle: bool = False) -> None:
|
||||
@@ -126,7 +206,20 @@ Now is a good time to wrap up any conversations.""".format(args[1])
|
||||
message: Optional[TextWithData] = None) -> None:
|
||||
self.ctrl.logger.info('on_added_me')
|
||||
|
||||
self.ctrl.send_snac(build_buddy_added_notif(user.username))
|
||||
if not names.representable(user):
|
||||
return
|
||||
|
||||
# an add still waiting on our authorization isn't announced until it's granted
|
||||
from ..foodgroups.buddy import buddy_pending
|
||||
if buddy_pending(user, self.bs.user):
|
||||
return
|
||||
|
||||
from ..foodgroups.icbm import send_icq_system_message, ICQMessageType
|
||||
if self.bs.front_data.get('oscar0_feedbag'):
|
||||
self.ctrl.send_snac(build_buddy_added_notif(names.name_for(self.bs, user)))
|
||||
elif names.is_icq_session(self.bs):
|
||||
# ICQ clients without a feedbag only learn this from a "you were added" message (MSG_TYPE_ADDED)
|
||||
send_icq_system_message(self.ctrl, user, ICQMessageType.Added, '')
|
||||
|
||||
def on_removed_me(self, user: User) -> None:
|
||||
self.ctrl.logger.info('on_removed_me')
|
||||
@@ -139,13 +232,25 @@ Now is a good time to wrap up any conversations.""".format(args[1])
|
||||
|
||||
def on_oim_sent(self, oim: OIM) -> None:
|
||||
self.ctrl.logger.info('on_oim_sent')
|
||||
pass
|
||||
# an offline message only gets stored while its recipient can't be reached, so a session that hears about one
|
||||
# (signed on invisible, say) gets it straight away, like the YMSG frontend does
|
||||
from ..foodgroups.icbm import deliver_offline_messages
|
||||
deliver_offline_messages(self.ctrl, [oim])
|
||||
|
||||
def on_login_elsewhere(self, option: LoginOption) -> None:
|
||||
self.ctrl.logger.info('on_login_elsewhere')
|
||||
self.ctrl.signoff(DISCONNECT_NEW_LOGIN)
|
||||
self.ctrl.close()
|
||||
return
|
||||
|
||||
def oscar_on_icbm(self, msg: Any) -> None:
|
||||
# an ICBM (rendezvous, ICQ message, client error/ack) passed straight between OSCAR clients, bypassing the chat
|
||||
# machinery the other frontends message through
|
||||
self.ctrl.send_snac(msg)
|
||||
|
||||
def oscar_on_snac(self, msg: Any) -> None:
|
||||
self.ctrl.send_snac(msg)
|
||||
|
||||
# circles are not a thing in AIM
|
||||
def on_circle_invite_revoked(self, chat_id: str) -> None:
|
||||
pass
|
||||
@@ -174,52 +279,60 @@ class LoginError(IntEnum):
|
||||
NotATester = 0x0009,
|
||||
Suspended = 0x0011,
|
||||
EmailNotVerified = 0x0006
|
||||
ICQUserError = 0x0008
|
||||
|
||||
|
||||
def login(logger: Logger,
|
||||
context: OSCARContext,
|
||||
tlvs: array[TLV],
|
||||
uuid: Optional[str],
|
||||
error_code: Optional[int]) -> bytes:
|
||||
error_code: Optional[int],
|
||||
*,
|
||||
icq: bool = False,
|
||||
multi_conn: Optional[int] = None) -> bytes:
|
||||
|
||||
# get user if found
|
||||
user = context.backend.user_service.get(uuid) if uuid else None
|
||||
|
||||
# find screen name TLV and use it if not found in DB
|
||||
screen_name_tlv = find_tlv(tlvs, 0x0001)
|
||||
screen_name = user.username if user else screen_name_tlv.data.decode()
|
||||
given_name = screen_name_tlv.data.decode('latin-1') if screen_name_tlv else ''
|
||||
# an ICQ client gets its UIN back, an AIM one the screen name's own formatting
|
||||
screen_name = (given_name.replace(' ', '') if icq else user.username) if user else given_name
|
||||
|
||||
# additionally check if user is suspended here
|
||||
if error_code is None and user.suspended:
|
||||
error_code = LoginError.Suspended
|
||||
|
||||
logger.info(screen_name, 'tried to sign on but is suspended!')
|
||||
|
||||
|
||||
# and check if the user has verified their e-mail address
|
||||
#if error_code is None and not user.verified_to_login:
|
||||
# error_code = LoginError.EmailNotVerified
|
||||
#
|
||||
# logger.info(screen_name, 'tried to sign on but has not verified their e-mail address!')
|
||||
|
||||
|
||||
if error_code is None:
|
||||
logger.info(screen_name, 'signed on successfully!')
|
||||
|
||||
# generate BOS cookie and add it to array
|
||||
bos_cookie = os.urandom(256)
|
||||
bos_cookies.append({
|
||||
bos_cookie: {'uuid': uuid, 'version': context.client.version}
|
||||
})
|
||||
client_id_tlv = find_tlv(tlvs, 0x0003)
|
||||
bos_cookie = issue_cookie(ServiceCookie(
|
||||
uuid = uuid, version = context.client.version, program = 'icq' if icq else 'aim',
|
||||
screen_name = screen_name, icq = icq,
|
||||
client_id = client_id_tlv.data.decode('latin-1') if client_id_tlv else '', multi_conn = multi_conn,
|
||||
))
|
||||
|
||||
# get user email
|
||||
email = user.email
|
||||
|
||||
# find client's country TLV for use in password change URL
|
||||
country_tlv = find_tlv(tlvs, 0x000E)
|
||||
country = country_tlv.data if country_tlv else 'us'
|
||||
country = country_tlv.data.decode('latin-1') if country_tlv else 'us'
|
||||
|
||||
# find client's language TLV for use in password change URL
|
||||
language_tlv = find_tlv(tlvs, 0x000F)
|
||||
language = language_tlv.data if language_tlv else 'en'
|
||||
language = language_tlv.data.decode('latin-1') if language_tlv else 'en'
|
||||
|
||||
# format password change URL with found client country and language
|
||||
pw_change_url = PW_CHANGE_URL_FORMAT.format(
|
||||
@@ -241,5 +354,5 @@ def login(logger: Logger,
|
||||
return marshal_tlvs([
|
||||
TLV(0x0001, screen_name), # Screen name
|
||||
TLV(0x0008, struct.pack('>H', error_code)), # Error code
|
||||
TLV(0x0004, ERROR_URLS[error_code]), # Error URL
|
||||
])
|
||||
TLV(0x0004, ERROR_URLS.get(int(error_code), '')), # Error URL
|
||||
])
|
||||
Reference in new issue
Block a user