__init__.py (10304B)
1 #Embedded file name: C:\BuildAgent\work\20d73f1b24eed06e\eve\release\V24.01\packages\chatutil\__init__.py 2 from __future__ import absolute_import 3 import hashlib 4 import re 5 from .filter import CleanAndHighlightText, UpdateChatFilterVariables, GetHighlightedText, ShouldHightlightBlink 6 from .urls import LinkURLs 7 import inventorycommon.const as invconst 8 import chatutil.const as chatUtilConst 9 import datetimeutils 10 MAX_TEXT_LEN = 1024 11 MAX_TEXT_LEN_SAFEGUARD = 2048 12 showInfoComplete = re.compile("(?P<pretext>.*?<loc><url=showinfo:) # Non-greedy consomption of all text up to shoinfo tag\n (?P<typeID>\\d*) # typeID, always present\n (?P<seperator>/*) # Optional seperator, only present when itemID is\n (?P<itemID>\\d*) # optional itemID\n > # close bracket for show info anchor\n (?P<itemName>.*?) # Supplied item name\n (?P<posttext></url>) # Close anchor. Only here to ensure the link isn't mangled.\n </loc>", re.X) 13 14 def StripBreaks(txt): 15 txt = txt.strip() 16 while txt.endswith('<br>'): 17 txt = txt[:-4] 18 txt = txt.strip() 19 20 while txt.startswith('<br>'): 21 txt = txt[4:] 22 txt = txt.strip() 23 24 return txt 25 26 27 def GetBodyText(response): 28 text = '' 29 for child in response.children: 30 if child.tag == 'body': 31 text = child.text 32 33 return text 34 35 36 def IsInvite(response): 37 xElem = response.find_child_with_tag('x') 38 if xElem: 39 if 'xmlns' in xElem.attributes and xElem.attributes['xmlns'] == 'http://jabber.org/protocol/muc#user': 40 inviteElem = xElem.find_child_with_tag('invite') 41 if inviteElem: 42 return True 43 return False 44 45 46 def IsDecline(response): 47 for child in response.children: 48 if child.tag == 'x': 49 if 'xmlns' in child.attributes and child.attributes['xmlns'] == 'http://jabber.org/protocol/muc#user': 50 if child.children and child.children[0].tag == 'decline': 51 return True 52 53 return False 54 55 56 def GetInviteSender(response): 57 xElem = response.find_child_with_tag('x') 58 if xElem: 59 if 'xmlns' in xElem.attributes and xElem.attributes['xmlns'] == 'http://jabber.org/protocol/muc#user': 60 inviteElem = xElem.find_child_with_tag('invite') 61 if inviteElem: 62 user = inviteElem.attributes.get('from', None) 63 if user: 64 return user.split('@')[0] 65 66 67 def GetInviteReason(response): 68 xElem = response.find_child_with_tag('x') 69 if xElem: 70 if 'xmlns' in xElem.attributes and xElem.attributes['xmlns'] == 'http://jabber.org/protocol/muc#user': 71 inviteElem = xElem.find_child_with_tag('invite') 72 if inviteElem: 73 reasonElem = inviteElem.find_child_with_tag('reason') 74 if reasonElem: 75 return reasonElem.text 76 77 78 def GetInvitePassword(response): 79 xElem = response.find_child_with_tag('x') 80 if xElem: 81 if 'xmlns' in xElem.attributes and xElem.attributes['xmlns'] == 'http://jabber.org/protocol/muc#user': 82 passwordElem = xElem.find_child_with_tag('password') 83 if passwordElem: 84 return passwordElem.text 85 86 87 def GetDeclineReason(response): 88 for child in response.children: 89 if child.tag == 'x': 90 if 'xmlns' in child.attributes and child.attributes['xmlns'] == 'http://jabber.org/protocol/muc#user': 91 if child.children and child.children[0].tag == 'decline': 92 invite = child.children[0] 93 if invite.children and invite.children[0].tag == 'reason': 94 return invite.children[0].text 95 96 97 def GetDeclineSender(response): 98 for child in response.children: 99 if child.tag == 'x': 100 if 'xmlns' in child.attributes and child.attributes['xmlns'] == 'http://jabber.org/protocol/muc#user': 101 if child.children and child.children[0].tag == 'decline': 102 user = child.children[0].attributes.get('from', None) 103 if user: 104 return user.split('@')[0] 105 106 107 def ParseFeatureSet(response): 108 result = [] 109 query = response.children[0] 110 for child in query.children: 111 if child.tag == 'feature': 112 feature = child.attributes['var'] 113 result.append(feature) 114 115 return result 116 117 118 def IsChannelPrivate(channelName): 119 category = channelName.split('_')[0] 120 isPrivate = category in ('private', 'player') 121 return isPrivate 122 123 124 def GetStatusCodes(response): 125 statusCodes = [] 126 x = response.find_child_with_tag('x') 127 if x and x.attributes.get('xmlns', '') == 'http://jabber.org/protocol/muc#user': 128 for c in x.children: 129 if c.tag == 'status': 130 code = c.attributes.get('code') 131 statusCodes.append(code) 132 133 return statusCodes 134 135 136 def ParseChannelName(channelName): 137 category = 'public' 138 count = 0 139 if not channelName: 140 channelName = '' 141 trimmedChannelName = channelName.strip() 142 countAsString = '' 143 if trimmedChannelName.endswith(')'): 144 ix = trimmedChannelName.rfind('(') 145 if ix != -1: 146 suffix = trimmedChannelName[ix + 1:-1] 147 trimmedChannelName = trimmedChannelName[0:ix - 1] 148 parts = suffix.split(',') 149 if len(parts) == 1: 150 countAsString = parts[0] 151 elif len(parts) == 2: 152 category = parts[0] 153 countAsString = parts[1] 154 else: 155 countAsString = '' 156 try: 157 count = int(countAsString) 158 except ValueError: 159 trimmedChannelName = channelName 160 category = 'public' 161 162 return (trimmedChannelName, category, count) 163 164 165 def GetChannelId(channelName): 166 normalized = channelName.strip().lower() 167 m = hashlib.md5() 168 m.update(normalized) 169 return m.hexdigest() 170 171 172 def IsAuthError(response): 173 isAuthError = False 174 errorElement = response.find_child_with_tag('error') 175 if errorElement: 176 if errorElement.attributes['code'] == '401' and errorElement.attributes['type'] == 'auth': 177 isAuthError = True 178 return isAuthError 179 180 181 def GetCharacterBannnedExpiry(expiry_text): 182 expiry = int(expiry_text) 183 if expiry > 0: 184 expiry = datetimeutils.unix_to_blue(expiry) 185 return expiry 186 187 188 def GetErrorTextElement(response): 189 errorElement = response.find_child_with_tag('error') 190 if errorElement: 191 return errorElement.find_child_with_tag('text') 192 193 194 def GetErrorType(response): 195 errorElement = response.find_child_with_tag('error') 196 if errorElement: 197 return errorElement.attributes.get('type') 198 199 200 def GetErrorCode(response): 201 errorElement = response.find_child_with_tag('error') 202 if errorElement: 203 return errorElement.attributes['code'] 204 205 206 def GetUsersFromResponse(response): 207 result = [] 208 queryElement = response.children[0] 209 for child in queryElement.children: 210 if child.tag == 'item': 211 jid = child.attributes['jid'] 212 charid = jid.split('@')[0] 213 try: 214 charid = int(charid) 215 except ValueError: 216 pass 217 218 result.append(charid) 219 220 return result 221 222 223 def CheckAllowedAccess(charid, corpid, allianceid, allowed, blocked): 224 allow = None 225 set_charid = 'set_{0}'.format(charid) 226 set_corpid = 'set_{0}'.format(corpid) 227 set_allianceid = 'set_{0}'.format(allianceid) 228 if 'all' in allowed: 229 allow = 'all' 230 elif 'all' in blocked: 231 allow = None 232 if set_allianceid in allowed: 233 allow = 'allianceid' 234 elif set_allianceid in blocked: 235 allow = None 236 if set_corpid in allowed: 237 allow = 'corpid' 238 elif set_corpid in blocked: 239 allow = None 240 if set_charid in allowed: 241 allow = 'charid' 242 elif set_charid in blocked: 243 allow = None 244 return allow 245 246 247 def GetChannelCategory(channel): 248 return channel.split('_')[0] 249 250 251 def GetChannelDifferentiator(channel): 252 return channel.split('_', 1)[1] 253 254 255 def CompleteAutoLinks(text): 256 import localization 257 import evetypes 258 filledText = '' 259 match = showInfoComplete.search(text) 260 if match is None: 261 return text 262 while match is not None: 263 pretext = match.group('pretext') 264 typeID = match.group('typeID') 265 seperator = match.group('seperator') 266 itemID = match.group('itemID') 267 itemName = match.group('itemName') 268 posttext = match.group('posttext') 269 groupID = evetypes.GetGroupID(typeID) 270 if itemID == '' and typeID != '': 271 filledName = evetypes.GetName(typeID) 272 elif groupID in [invconst.groupCharacter, invconst.groupCorporation]: 273 filledName = cfg.eveowners.Get(itemID).name 274 elif groupID == invconst.groupSolarSystem: 275 filledName = cfg.evelocations.Get(itemID).name 276 elif groupID == invconst.groupStation: 277 orbitName = cfg.evelocations.Get(cfg.stations.Get(itemID).orbitID).name 278 longMoon = localization.GetByLabel('UI/Locations/LocationMoonLong') 279 shortMoon = localization.GetByLabel('UI/Locations/LocationMoonShort') 280 orbitName = orbitName.replace(longMoon, shortMoon).replace(longMoon.lower(), shortMoon.lower()) 281 filledName = localization.GetByLabel('UI/Chat/StationAutoLink', orbitName=orbitName) 282 else: 283 filledName = match.group('itemName') 284 filledText += '%s%s%s%s>%s%s' % (pretext, 285 typeID, 286 seperator, 287 itemID, 288 filledName, 289 posttext) 290 text = text[match.span()[1]:] 291 match = showInfoComplete.search(text) 292 293 filledText = filledText + text 294 return filledText 295 296 297 def TruncateVeryLongText(text): 298 if len(text) > MAX_TEXT_LEN_SAFEGUARD: 299 return text[:MAX_TEXT_LEN_SAFEGUARD - 5] + ' ...' 300 return text 301 302 303 class InvalidChatChannelNameError(RuntimeError): 304 pass 305 306 307 class InvalidSessionChatChannelNameError(InvalidChatChannelNameError): 308 pass