Package Skype4Py :: Module chat
[frames] | no frames]

Source Code for Module Skype4Py.chat

  1  '''Chats. 
  2  ''' 
  3   
  4  from utils import * 
  5  from user import * 
  6  from enums import * 
  7  from errors import * 
  8   
  9   
10 -class IChat(Cached):
11 '''Represents a Skype chat. 12 ''' 13
14 - def __repr__(self):
15 return '<%s with Name=%s>' % (Cached.__repr__(self)[1:-1], repr(self.Name))
16
17 - def _Alter(self, AlterName, Args=None):
18 return self._Skype._Alter('CHAT', self._Name, AlterName, Args, 'ALTER CHAT %s' % AlterName)
19
20 - def _Init(self, Name, Skype):
21 self._Skype = Skype 22 self._Name = Name
23
24 - def _Property(self, PropName, Value=None, Cache=True):
25 return self._Skype._Property('CHAT', self._Name, PropName, Value, Cache)
26
27 - def AcceptAdd(self):
28 '''Accepts a shared group add request. 29 ''' 30 self._Alter('ACCEPTADD')
31
32 - def AddMembers(self, *Members):
33 '''Adds new members to the chat. 34 35 @param Members: One or more users to add. 36 @type Members: L{IUser} 37 ''' 38 self._Alter('ADDMEMBERS', ', '.join(x.Handle for x in Members))
39
40 - def Bookmark(self):
41 '''Bookmarks the chat in Skype client. 42 ''' 43 self._Alter('BOOKMARK')
44
45 - def ClearRecentMessages(self):
46 '''Clears recent chat messages. 47 ''' 48 self._Alter('CLEARRECENTMESSAGES')
49
50 - def Disband(self):
51 '''Ends the chat. 52 ''' 53 self._Alter('DISBAND')
54
55 - def EnterPassword(self, Password):
56 '''Enters chat password. 57 58 @param Password: Password 59 @type Password: unicode 60 ''' 61 self._Alter('ENTERPASSWORD', Password)
62
63 - def Join(self):
64 '''Joins the chat. 65 ''' 66 self._Alter('JOIN')
67
68 - def Kick(self, Handle):
69 '''Kicks a member from chat. 70 71 @param Handle: Handle 72 @type Handle: unicode 73 ''' 74 self._Alter('KICK', Handle)
75
76 - def KickBan(self, Handle):
77 '''Kicks and bans a member from chat. 78 79 @param Handle: Handle 80 @type Handle: unicode 81 ''' 82 self._Alter('KICKBAN', Handle)
83
84 - def Leave(self):
85 '''Leaves the chat. 86 ''' 87 self._Alter('LEAVE')
88
89 - def OpenWindow(self):
90 '''Opens the chat window. 91 ''' 92 self._Skype.Client.OpenDialog('CHAT', self._Name)
93
94 - def SendMessage(self, MessageText):
95 '''Sends a chat message. 96 97 @param MessageText: Message text 98 @type MessageText: unicode 99 @return: Message object 100 @rtype: L{IChatMessage} 101 ''' 102 return IChatMessage(chop(self._Skype._DoCommand('CHATMESSAGE %s %s' % (self._Name, MessageText)), 2)[1], self._Skype)
103
104 - def SetPassword(self, Password, Hint=''):
105 '''Sets the chat password. 106 107 @param Password: Password 108 @type Password: unicode 109 @param Hint: Password hint 110 @type Hint: unicode 111 ''' 112 if ' ' in Password: 113 raise ValueError('Password mut be one word') 114 self._Alter('SETPASSWORD', '%s %s' % (Password, Hint))
115
116 - def Unbookmark(self):
117 '''Unbookmarks the chat. 118 ''' 119 self._Alter('UNBOOKMARK')
120
121 - def _GetActiveMembers(self):
122 return tuple(IUser(x, self._Skype) for x in esplit(self._Property('ACTIVEMEMBERS', Cache=False)))
123 124 ActiveMembers = property(_GetActiveMembers, 125 doc='''Active members of a chat. 126 127 @type: tuple of L{IUser} 128 ''') 129
130 - def _GetActivityDatetime(self):
131 from datetime import datetime 132 return datetime.fromtimestamp(self.ActivityTimestamp)
133 134 ActivityDatetime = property(_GetActivityDatetime, 135 doc='''Returns chat activity timestamp as datetime. 136 137 @type: datetime.datetime 138 ''') 139
140 - def _GetActivityTimestamp(self):
141 return float(self._Property('ACTIVITY_TIMESTAMP'))
142 143 ActivityTimestamp = property(_GetActivityTimestamp, 144 doc='''Returns chat activity timestamp. 145 146 @type: float 147 @see: L{ActivityDatetime} 148 ''') 149
150 - def _GetAdder(self):
151 return IUser(self._Property('ADDER'), self._Skype)
152 153 Adder = property(_GetAdder, 154 doc='''Returns the user that added current user to the chat. 155 156 @type: L{IUser} 157 ''') 158
159 - def _SetAlertString(self, value):
160 self._Alter('SETALERTSTRING', quote('=%s' % value))
161 162 AlertString = property(fset=_SetAlertString, 163 doc='''Chat alert string. Only messages containing words from this string will cause a 164 notification to pop up on the screen. 165 166 @type: unicode 167 ''') 168
169 - def _GetApplicants(self):
170 return tuple(IUser(x, self._Skype) for x in esplit(self._Property('APPLICANTS')))
171 172 Applicants = property(_GetApplicants, 173 doc='''Chat applicants. 174 175 @type: tuple of L{IUser} 176 ''') 177
178 - def _GetBlob(self):
179 return self._Property('BLOB')
180 181 Blob = property(_GetBlob, 182 doc='''Chat blob. 183 184 @type: unicode 185 ''') 186
187 - def _GetBookmarked(self):
188 return self._Property('BOOKMARKED') == 'TRUE'
189 190 Bookmarked = property(_GetBookmarked, 191 doc='''Tells if this chat is bookmarked. 192 193 @type: bool 194 ''') 195
196 - def _GetDatetime(self):
197 from datetime import datetime 198 return datetime.fromtimestamp(self.Timestamp)
199 200 Datetime = property(_GetDatetime, 201 doc='''Chat timestamp as datetime. 202 203 @type: datetime.datetime 204 ''') 205
206 - def _GetDescription(self):
207 return self._Property('DESCRIPTION')
208
209 - def _SetDescription(self, value):
210 self._Property('DESCRIPTION', value)
211 212 Description = property(_GetDescription, _SetDescription, 213 doc='''Chat description. 214 215 @type: unicode 216 ''') 217
218 - def _GetDialogPartner(self):
219 return self._Property('DIALOG_PARTNER')
220 221 DialogPartner = property(_GetDialogPartner, 222 doc='''Skypename of the chat dialog partner. 223 224 @type: unicode 225 ''') 226
227 - def _GetFriendlyName(self):
228 return self._Property('FRIENDLYNAME')
229 230 FriendlyName = property(_GetFriendlyName, 231 doc='''Friendly name of the chat. 232 233 @type: unicode 234 ''') 235
236 - def _GetGuideLines(self):
237 return self._Property('GUIDELINES')
238
239 - def _SetGuideLines(self, value):
240 self._Alter('SETGUIDELINES', value)
241 242 GuideLines = property(_GetGuideLines, _SetGuideLines, 243 doc='''Chat guidelines. 244 245 @type: unicode 246 ''') 247
248 - def _GetMemberObjects(self):
249 return tuple(IChatMember(x, self._Skype) for x in esplit(self._Property('MEMBEROBJECTS'), ', '))
250 251 MemberObjects = property(_GetMemberObjects, 252 doc='''Chat members as member objects. 253 254 @type: tuple of L{IChatMember} 255 ''') 256
257 - def _GetMembers(self):
258 return tuple(IUser(x, self._Skype) for x in esplit(self._Property('MEMBERS')))
259 260 Members = property(_GetMembers, 261 doc='''Chat members. 262 263 @type: tuple of L{IUser} 264 ''') 265
266 - def _GetMessages(self):
267 return tuple(IChatMessage(x ,self._Skype) for x in esplit(self._Property('CHATMESSAGES', Cache=False), ', '))
268 269 Messages = property(_GetMessages, 270 doc='''All chat messages. 271 272 @type: tuple of L{IChatMessage} 273 ''') 274
275 - def _GetMyRole(self):
276 return self._Property('MYROLE')
277 278 MyRole = property(_GetMyRole, 279 doc='''My chat role in a public chat. 280 281 @type: L{Chat member role<enums.chatMemberRoleUnknown>} 282 ''') 283
284 - def _GetMyStatus(self):
285 return self._Property('MYSTATUS')
286 287 MyStatus = property(_GetMyStatus, 288 doc='''My status in a public chat. 289 290 @type: L{My chat status<enums.chatStatusUnknown>} 291 ''') 292
293 - def _GetName(self):
294 return self._Name
295 296 Name = property(_GetName, 297 doc='''Chat name as used by Skype to identify this chat. 298 299 @type: unicode 300 ''') 301
302 - def _GetOptions(self):
303 return int(self._Property('OPTIONS'))
304
305 - def _SetOptions(self, value):
306 self._Alter('SETOPTIONS', value)
307 308 Options = property(_GetOptions, _SetOptions, 309 doc='''Chat options. A mask. 310 311 @type: L{Chat options<enums.chatOptionJoiningEnabled>} 312 ''') 313
314 - def _GetPasswordHint(self):
315 return self._Property('PASSWORDHINT')
316 317 PasswordHint = property(_GetPasswordHint, 318 doc='''Chat password hint. 319 320 @type: unicode 321 ''') 322
323 - def _GetPosters(self):
324 return tuple(IUser(x, self._Skype) for x in esplit(self._Property('POSTERS')))
325 326 Posters = property(_GetPosters, 327 doc='''Users who have posted messages to this chat. 328 329 @type: tuple of L{IUser} 330 ''') 331
332 - def _GetRecentMessages(self):
333 return tuple(IChatMessage(x, self._Skype) for x in esplit(self._Property('RECENTCHATMESSAGES', Cache=False), ', '))
334 335 RecentMessages = property(_GetRecentMessages, 336 doc='''Most recent chat messages. 337 338 @type: tuple of L{IChatMessage} 339 ''') 340
341 - def _GetStatus(self):
342 return self._Property('STATUS')
343 344 Status = property(_GetStatus, 345 doc='''Status. 346 347 @type: L{Chat status<enums.chsUnknown>} 348 ''') 349
350 - def _GetTimestamp(self):
351 return float(self._Property('TIMESTAMP'))
352 353 Timestamp = property(_GetTimestamp, 354 doc='''Chat timestamp. 355 356 @type: float 357 @see: L{Datetime} 358 ''') 359
360 - def _GetTopic(self):
361 try: 362 topicxml = self._Property('TOPICXML') 363 if topicxml: 364 return topicxml 365 except ISkypeError: 366 pass 367 return self._Property('TOPIC')
368
369 - def _SetTopic(self, value):
370 try: 371 self._Alter('SETTOPICXML', value) 372 except ISkypeError: 373 self._Alter('SETTOPIC', value)
374 375 Topic = property(_GetTopic, _SetTopic, 376 doc='''Chat topic. 377 378 @type: unicode 379 ''') 380
381 - def _GetTopicXML(self):
382 return self._Property('TOPICXML')
383
384 - def _SetTopicXML(self, value):
385 self._Property('TOPICXML', value)
386 387 TopicXML = property(_GetTopicXML, _SetTopicXML, 388 doc='''Chat topic in XML format. 389 390 @type: unicode 391 ''') 392
393 - def _GetType(self):
394 return self._Property('TYPE')
395 396 Type = property(_GetType, 397 doc='''Chat type. 398 399 @type: L{Chat type<enums.chatTypeUnknown>} 400 ''')
401 402
403 -class IChatMessage(Cached):
404 '''Represents a single chat message. 405 ''' 406
407 - def __repr__(self):
408 return '<%s with Id=%s>' % (Cached.__repr__(self)[1:-1], repr(self.Id))
409
410 - def _Init(self, Id, Skype):
411 self._Skype = Skype 412 self._Id = int(Id)
413
414 - def _Property(self, PropName, Value=None, Cache=True):
415 return self._Skype._Property('CHATMESSAGE', self._Id, PropName, Value, Cache)
416
417 - def MarkAsSeen(self):
418 '''Marks a missed chat message as seen. 419 ''' 420 self._Skype._DoCommand('SET CHATMESSAGE %d SEEN' % self._Id, 'CHATMESSAGE %d STATUS READ' % self._Id)
421
422 - def _GetBody(self):
423 return self._Property('BODY')
424
425 - def _SetBody(self, value):
426 self._Property('BODY', value)
427 428 Body = property(_GetBody, _SetBody, 429 doc='''Chat message body. 430 431 @type: unicode 432 ''') 433
434 - def _GetChat(self):
435 return IChat(self.ChatName, self._Skype)
436 437 Chat = property(_GetChat, 438 doc='''Chat this message was posted on. 439 440 @type: L{IChat} 441 ''') 442
443 - def _GetChatName(self):
444 return self._Property('CHATNAME')
445 446 ChatName = property(_GetChatName, 447 doc='''Name of the chat this message was posted on. 448 449 @type: unicode 450 ''') 451
452 - def _GetDatetime(self):
453 from datetime import datetime 454 return datetime.fromtimestamp(self.Timestamp)
455 456 Datetime = property(_GetDatetime, 457 doc='''Chat message timestamp as datetime. 458 459 @type: datetime.datetime 460 ''') 461
462 - def _GetEditedBy(self):
463 return self._Property('EDITED_BY')
464 465 EditedBy = property(_GetEditedBy, 466 doc='''Skypename of the user who edited this message. 467 468 @type: unicode 469 ''') 470
471 - def _GetEditedDatetime(self):
472 from datetime import datetime 473 return datetime.fromtimestamp(self.EditedTimestamp)
474 475 EditedDatetime = property(_GetEditedDatetime, 476 doc='''Message editing timestamp as datetime. 477 478 @type: datetime.datetime 479 ''') 480
481 - def _GetEditedTimestamp(self):
482 return float(self._Property('EDITED_TIMESTAMP'))
483 484 EditedTimestamp = property(_GetEditedTimestamp, 485 doc='''Message editing timestamp. 486 487 @type: float 488 ''') 489
490 - def _GetFromDisplayName(self):
491 return self._Property('FROM_DISPNAME')
492 493 FromDisplayName = property(_GetFromDisplayName, 494 doc='''DisplayName of the message sender. 495 496 @type: unicode 497 ''') 498
499 - def _GetFromHandle(self):
500 return self._Property('FROM_HANDLE')
501 502 FromHandle = property(_GetFromHandle, 503 doc='''Skypename of the message sender. 504 505 @type: unicode 506 ''') 507
508 - def _GetId(self):
509 return self._Id
510 511 Id = property(_GetId, 512 doc='''Chat message Id. 513 514 @type: int 515 ''') 516
517 - def _GetIsEditable(self):
518 return self._Property('IS_EDITABLE') == 'TRUE'
519 520 IsEditable = property(_GetIsEditable, 521 doc='''Tells if message body is editable. 522 523 @type: bool 524 ''') 525
526 - def _GetLeaveReason(self):
527 return self._Property('LEAVEREASON')
528 529 LeaveReason = property(_GetLeaveReason, 530 doc='''LeaveReason. 531 532 @type: L{Chat leave reason<enums.leaUnknown>} 533 ''') 534
535 - def _SetSeen(self, value):
536 from warnings import warn 537 warn('IChatMessage.Seen = x: Use IChatMessage.MarkAsSeen() instead.', DeprecationWarning, stacklevel=2) 538 if value: 539 self.MarkAsSeen() 540 else: 541 raise ISkypeError(0, 'Seen can only be set to True')
542 543 Seen = property(fset=_SetSeen, 544 doc='''Marks a missed chat message as seen. 545 546 @type: bool 547 @deprecated: Unpythonic, use L{MarkAsSeen} instead. 548 ''') 549
550 - def _GetSender(self):
551 return IUser(self.FromHandle, self._Skype)
552 553 Sender = property(_GetSender, 554 doc='''Sender of the chat message. 555 556 @type: L{IUser} 557 ''') 558
559 - def _GetStatus(self):
560 return self._Property('STATUS')
561 562 Status = property(_GetStatus, 563 doc='''Status of the chat messsage. 564 565 @type: L{Chat message status<enums.cmsUnknown>} 566 ''') 567
568 - def _GetTimestamp(self):
569 return float(self._Property('TIMESTAMP'))
570 571 Timestamp = property(_GetTimestamp, 572 doc='''Chat message timestamp. 573 574 @type: float 575 @see: L{Datetime} 576 ''') 577
578 - def _GetType(self):
579 return self._Property('TYPE')
580 581 Type = property(_GetType, 582 doc='''Type of chat message. 583 584 @type: L{Chat message type<enums.cmeUnknown>} 585 ''') 586
587 - def _GetUsers(self):
588 return tuple(IUser(self._Skype, x) for x in esplit(self._Property('USERS')))
589 590 Users = property(_GetUsers, 591 doc='''Users added to the chat. 592 593 @type: tuple of L{IUser} 594 ''')
595 596
597 -class IChatMember(Cached):
598 '''Represents a member of a public chat. 599 ''' 600
601 - def __repr__(self):
602 return '<%s with Id=%s>' % (Cached.__repr__(self)[1:-1], repr(self.Id))
603
604 - def _Alter(self, AlterName, Args=None):
605 return self._Skype._Alter('CHATMEMBER', self._Id, AlterName, Args, 'ALTER CHATMEMBER %s' % AlterName)
606
607 - def _Init(self, Id, Skype):
608 self._Skype = Skype 609 self._Id = int(Id)
610
611 - def _Property(self, PropName, Value=None, Cache=True):
612 return self._Skype._Property('CHATMEMBER', self._Id, PropName, Value, Cache)
613
614 - def CanSetRoleTo(self, Role):
615 '''Checks if the new role can be applied to the member. 616 617 @param Role: New chat member role. 618 @type Role: L{Chat member role<enums.chatMemberRoleUnknown>} 619 @return: True if the new role can be applied, False otherwise. 620 @rtype: bool 621 ''' 622 return self._Alter('CANSETROLETO', Role) == 'TRUE'
623
624 - def _GetChat(self):
625 return IChat(self._Property('CHATNAME'), self._Skype)
626 627 Chat = property(_GetChat, 628 doc='''Chat this member belongs to. 629 630 @type: L{IChat} 631 ''') 632
633 - def _GetHandle(self):
634 return self._Property('IDENTITY')
635 636 Handle = property(_GetHandle, 637 doc='''Member Skypename. 638 639 @type: unicode 640 ''') 641
642 - def _GetId(self):
643 return self._Id
644 645 Id = property(_GetId, 646 doc='''Chat member Id. 647 648 @type: int 649 ''') 650
651 - def _GetIsActive(self):
652 return self._Property('IS_ACTIVE') == 'TRUE'
653 654 IsActive = property(_GetIsActive, 655 doc='''Member activity status. 656 657 @type: bool 658 ''') 659
660 - def _GetRole(self):
661 return self._Property('ROLE')
662
663 - def _SetRole(self, value):
664 self._Alter('SETROLETO', value)
665 666 Role = property(_GetRole, _SetRole, 667 doc='''Chat Member role. 668 669 @type: L{Chat member role<enums.chatMemberRoleUnknown>} 670 ''')
671