Guest User

Untitled

a guest
Jan 16th, 2018
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. import sys
  4. import os
  5. import nbxmpp
  6. # import time
  7. import logging
  8. try:
  9. from gi.repository import GObject as gobject
  10. except Exception:
  11. import gobject
  12.  
  13. consoleloghandler = logging.StreamHandler()
  14. root_log = logging.getLogger('nbxmpp')
  15. # root_log.setLevel('DEBUG')
  16. root_log.addHandler(consoleloghandler)
  17.  
  18. if len(sys.argv) < 2:
  19. print("Syntax: xsend JID text")
  20. sys.exit(0)
  21.  
  22. to_jid = sys.argv[1]
  23. text = ' '.join(sys.argv[2:])
  24.  
  25. jidparams = {}
  26. if os.access(os.environ['HOME'] + '/.xsend', os.R_OK):
  27. for ln in open(os.environ['HOME'] + '/.xsend').readlines():
  28. if not ln[0] in ('#', ';'):
  29. key, val = ln.strip().split('=', 1)
  30. jidparams[key.lower()] = val
  31. for mandatory in ['jid', 'password', 'hostname']:
  32. if mandatory not in jidparams.keys():
  33. open(os.environ['HOME'] + '/.xsend', 'w').write(
  34. '#Uncomment fields before use and type in correct credentials.\n'
  35. '#JID=romeo@montague.net/resource (/resource is optional)\n#PASSWORD=juliet\n')
  36. print('Please point ~/.xsend config file to valid JID for sending messages.')
  37. sys.exit(0)
  38.  
  39.  
  40. class Connection:
  41. def __init__(self):
  42. self.jid = nbxmpp.protocol.JID(jidparams['jid'])
  43. self.password = jidparams['password']
  44. self.hostname = jidparams['hostname']
  45. self.sm = nbxmpp.Smacks(self) # Stream Management
  46. self.client_cert = None
  47.  
  48. def on_auth(self, con, auth):
  49. if not auth:
  50. print('could not authenticate!')
  51. sys.exit()
  52. print('authenticated using ' + auth)
  53. self.send_message(to_jid, text)
  54.  
  55. def on_connected(self, con, con_type):
  56. print('connected with ' + con_type)
  57. auth = self.client.auth(self.jid.getNode(), self.password,
  58. resource=self.jid.getResource(), sasl=1, on_auth=self.on_auth)
  59.  
  60. def get_password(self, cb, mech):
  61. cb(self.password)
  62.  
  63. def on_connection_failed(self):
  64. print('could not connect!')
  65.  
  66. def _event_dispatcher(self, realm, event, data):
  67. # print("event!")
  68. pass
  69.  
  70. def connect(self):
  71. idle_queue = nbxmpp.idlequeue.get_idlequeue()
  72. self.client = nbxmpp.NonBlockingClient(self.jid.getDomain(), idle_queue, caller=self)
  73. self.con = self.client.connect(self.on_connected, self.on_connection_failed,
  74. hostname=self.hostname,
  75. secure_tuple=('tls', '', '', None, None))
  76.  
  77. def send_message(self, to_jid, text):
  78. id_ = self.client.send(nbxmpp.protocol.Message(to_jid, text, typ='chat'))
  79. print('sent message with id ' + id_)
  80. gobject.timeout_add(1000, self.quit)
  81.  
  82. def quit(self):
  83. self.disconnect()
  84. ml.quit()
  85.  
  86. def disconnect(self):
  87. self.client.start_disconnect()
  88.  
  89.  
  90. con = Connection()
  91. con.connect()
  92. ml = gobject.MainLoop()
  93. ml.run()
Add Comment
Please, Sign In to add comment