PageRenderTime 56ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/emesene/e3/papylib/papyon/papyon/msnp2p/test.py

https://github.com/esmanhotto/emesene
Python | 142 lines | 112 code | 28 blank | 2 comment | 25 complexity | cac05e2c3c7c6d635a01df9e7d2086b0 MD5 | raw file
  1. #!/usr/bin/env python
  2. import papyon
  3. import papyon.event
  4. from papyon.msnp2p.session_manager import *
  5. from papyon.msnp2p.session import *
  6. from papyon.msnp2p.constants import EufGuid
  7. import papyon.util.string_io as StringIO
  8. import logging
  9. import gobject
  10. import os
  11. logging.basicConfig(level=logging.DEBUG)
  12. finished = False
  13. def get_proxies():
  14. import urllib
  15. proxies = urllib.getproxies()
  16. result = {}
  17. if 'https' not in proxies and \
  18. 'http' in proxies:
  19. url = proxies['http'].replace("http://", "https://")
  20. result['https'] = papyon.Proxy(url)
  21. for type, url in proxies.items():
  22. if type == 'no': continue
  23. if type == 'https' and url.startswith('http://'):
  24. url = url.replace('http://', 'https://', 1)
  25. result[type] = papyon.Proxy(url)
  26. return result
  27. class ClientEvents(papyon.event.ClientEventInterface):
  28. def on_client_state_changed(self, state):
  29. if state == papyon.event.ClientState.CLOSED:
  30. self._client.quit()
  31. elif state == papyon.event.ClientState.OPEN:
  32. self._client.profile.display_name = "Papyon"
  33. path = self._client.msn_object_path
  34. f = open(path, 'r')
  35. old_pos = f.tell()
  36. f.seek(0, os.SEEK_END)
  37. size = f.tell()
  38. f.seek(old_pos, os.SEEK_SET)
  39. msn_object = \
  40. papyon.p2p.MSNObject(self._client.profile,
  41. size, papyon.p2p.MSNObjectType.DISPLAY_PICTURE,
  42. 0, "lalala", data=f)
  43. self._client.profile.presence_msn_object = papyon.Presence.ONLINE, msn_object
  44. self._client.profile.personal_message_current_media = "yo!", None
  45. #gobject.timeout_add(3000, self._client.request_display_picture)
  46. def on_client_error(self, error_type, error):
  47. print "ERROR :", error_type, " ->", error
  48. class Client(papyon.Client):
  49. def __init__(self, account, msn_object_path, quit, http_mode=False):
  50. server = ('messenger.hotmail.com', 1863)
  51. self.quit = quit
  52. self.account = account
  53. self.msn_object_path = msn_object_path
  54. if http_mode:
  55. from papyon.transport import HTTPPollConnection
  56. papyon.Client.__init__(self, server, get_proxies(), HTTPPollConnection)
  57. else:
  58. papyon.Client.__init__(self, server, proxies = get_proxies())
  59. self.client_event_handler = ClientEvents(self)
  60. gobject.idle_add(self._connect)
  61. def _connect(self):
  62. self.login(*self.account)
  63. return False
  64. def request_display_picture(self):
  65. contacts = self.address_book.contacts.\
  66. search_by_presence(papyon.Presence.OFFLINE)
  67. contacts = self.address_book.contacts - contacts
  68. if len(contacts) == 0:
  69. print "No online contacts"
  70. return True
  71. else:
  72. contact = contacts[0]
  73. print "CONTACT : ", contact.account, str(contact.msn_object)
  74. if not contact.msn_object:
  75. return True
  76. self._msn_object_store.request(contact.msn_object, (self.__request_display_picture_callback,))
  77. return False
  78. def __request_display_picture_callback(self, msn_object):
  79. print "Received %s" % str(msn_object)
  80. def main():
  81. import sys
  82. import getpass
  83. import signal
  84. if "--http" in sys.argv:
  85. http_mode = True
  86. sys.argv.remove('--http')
  87. else:
  88. http_mode = False
  89. if len(sys.argv) < 2:
  90. account = raw_input('Account: ')
  91. else:
  92. account = sys.argv[1]
  93. if len(sys.argv) < 3:
  94. passwd = getpass.getpass('Password: ')
  95. else:
  96. passwd = sys.argv[2]
  97. if len(sys.argv) < 4:
  98. path = raw_input('Display picture path: ')
  99. else:
  100. path = sys.argv[3]
  101. mainloop = gobject.MainLoop(is_running=True)
  102. def quit():
  103. mainloop.quit()
  104. def sigterm_cb():
  105. gobject.idle_add(quit)
  106. signal.signal(signal.SIGTERM, sigterm_cb)
  107. n = Client((account, passwd), path, quit, http_mode)
  108. while mainloop.is_running():
  109. try:
  110. mainloop.run()
  111. except KeyboardInterrupt:
  112. quit()
  113. if __name__ == '__main__':
  114. main()