Guest User

Untitled

a guest
May 23rd, 2018
212
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.63 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2.  
  3. # This file is part of emesene.
  4. #
  5. # emesene is free software; you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation; either version 3 of the License, or
  8. # (at your option) any later version.
  9. #
  10. # emesene is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with emesene; if not, write to the Free Software
  17. # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  18. import os
  19. import extension
  20. from plugin_base import PluginBase
  21. import e3
  22. import gui
  23. import subprocess
  24. import urllib2
  25. try:
  26. import tinyurl #you need to install tinyurl "easy_install tinyurl" before starting this plugin
  27. except ImportError:
  28. pass
  29.  
  30. class Plugin(PluginBase):
  31. _description = 'Run some commands from window chat'
  32. _authors = { 'James Axl' : 'axlrose112@gmail.com' }
  33.  
  34. def __init__(self):
  35. PluginBase.__init__(self)
  36.  
  37. def start(self, session):
  38. self.session = session
  39. self.session.signals.conv_message_send_succeed.subscribe(self.send_message)
  40. self.session.signals.conv_started.subscribe(self.open_conv)
  41. self.soundPlayer = extension.get_and_instantiate('sound', session)
  42. self.homedir = os.path.expanduser('~/')
  43. return True
  44.  
  45. def stop(self):
  46. self.session.signals.conv_started.unsubscribe(self.open_conv)
  47. self.session.signals.conv_message_send_succeed.unsubscribe(self.send_message)
  48. return False
  49.  
  50. def config(self, session):
  51. pass
  52.  
  53. def open_conv(self, cid, account):
  54. self.conversation = self.session.get_conversation(cid)
  55. self.conversation.input.on_send_message = self.custom_on_send_message
  56.  
  57.  
  58. def custom_on_send_message(self, cid, message):
  59. cmds=["/help","/clear","/nudge","/run","/tiny"]
  60. chck=message.body.split('[')
  61. message.body=message.body.split(' ')
  62. if message.body[0] in cmds:
  63. if message.body[0]=="/help":
  64. help="""<ul><strong><li>/clean for cleaning chat window.</li></strong>
  65. <strong><li>/nudge for sending nudge to user.</li></strong>
  66. <strong><li>/run for running command e.g /run mplayer [fatman.mp3], /run firefox or /run mplayer [james axl.flv]
  67. <span>DEFAULT PATH IS YOUR HOME DIRECTORY</span>.</li></strong>
  68. <strong><li>/tiny for sending large URL e.g /tiny large_url.</li></strong<ul>"""
  69. message=e3.Message(e3.Message.TYPE_INFO, help,'',timestamp=None)
  70. self.session.gui_message(cid,'',message)
  71. elif message.body[0]=="/clear":
  72. self.conversation.output.clear()
  73. elif message.body[0]=="/nudge":
  74. self.session.request_attention(cid)
  75. message = e3.Message(e3.Message.TYPE_INFO, '', None, None)
  76. message.body = _('You just sent a nudge!')
  77. self.session.gui_message(cid,'',message)
  78. self.soundPlayer.play(gui.theme.sound_theme.sound_nudge)
  79. elif message.body[0]=="/run":
  80. if len(chck)==2:
  81. try:
  82. c=' '.join(message.body[1:])
  83. message.body=c.split('[')
  84. subprocess.Popen([message.body[0][:-1],self.homedir+message.body[1][:-1]])
  85. except NameError,OSError:
  86. message = e3.Message(e3.Message.TYPE_INFO, '', None, None)
  87. message.body="""<B>Command not found</B>"""
  88. self.session.gui_message(cid,'',message)
  89. elif len(message.body)==2:
  90. try:
  91. subprocess.Popen([message.body[1]])
  92. except NameError,OSError:
  93. message = e3.Message(e3.Message.TYPE_INFO, '', None, None)
  94. message.body="""<B>Command not found</B>"""
  95. self.session.gui_message(cid,'',message)
  96. elif len(message.body)==1:
  97. message = e3.Message(e3.Message.TYPE_INFO, '', None, None)
  98. message.body="""<B>Please type /help for more help</B>"""
  99. self.session.gui_message(cid,'',message)
  100. elif message.body[0]=="/tiny":
  101. if len(message.body)!=2:
  102. message = e3.Message(e3.Message.TYPE_INFO, '', None, None)
  103. message.body="""<B>Please type /help for more help</B>"""
  104. self.session.gui_message(cid,'',message)
  105. else:
  106. req = urllib2.Request(message.body[1])
  107. try:
  108. urllib2.urlopen(req)
  109. url=tinyurl.create_one(message.body[1])
  110. message = e3.Message(e3.Message.TYPE_MESSAGE,url,None, None) #It is look ilogical but i need it :).
  111. self.session.gui_message(cid,'',message)
  112. message = e3.Message(e3.Message.TYPE_INFO, '', None, None)
  113. message.body="""<B><a href="%s">%s</a> was succeful generated and sent :)</B>"""%(url,url)
  114. self.session.gui_message(cid,'',message)
  115. except NameError:
  116. message = e3.Message(e3.Message.TYPE_INFO, '', None, None)
  117. message.body="""<b>Please install tinyurl, easy_install tinyurl</b>"""
  118. self.session.gui_message(cid,'',message)
  119. except ValueError:
  120. message = e3.Message(e3.Message.TYPE_INFO, '', None, None)
  121. message.body="""<b>Check your URL</b>"""
  122. self.session.gui_message(cid,'',message)
  123.  
  124. else:
  125. message = e3.Message(e3.Message.TYPE_INFO, '', None, None)
  126. message.body = """<B>Please type /help for more help</B>"""
  127. self.session.gui_message(cid,'',message)
Add Comment
Please, Sign In to add comment