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

Source Code for Module Skype4Py.chat

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