Advertisement
Guest User

Untitled

a guest
Feb 13th, 2016
190
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.52 KB | None | 0 0
  1. import gi
  2. gi.require_version('Gtk', '3.0')
  3. from gi.repository import Gtk, GLib, Gdk
  4. import threading
  5. import logging
  6. import os
  7. import cairo
  8. import irc.client
  9.  
  10. logger = logging.getLogger(__name__)
  11. VERSION = "0.1-a"
  12.  
  13. CHANNEL = '#sebdevsdk48jf3'
  14. SERVER = 'irc.freenode.net'
  15. PORT = 6667
  16. NICK = 'naughtyboy_cl'
  17.  
  18. COLOUR = 0, 0, 0, .5
  19. INFO_COLOUR = .3, .1, .2, .5
  20.  
  21. class Irc(irc.client.SimpleIRCClient):
  22.     def __init__(self, window):
  23.         irc.client.SimpleIRCClient.__init__(self)
  24.         self.window = window
  25.  
  26.     def on_welcome(self, connection, event):
  27.         logger.info('Connection complete, Joining channel')
  28.         GLib.idle_add(self.window.show_info_wait, 'Connection complete, Joining channel {0} ...'.format(CHANNEL))
  29.         connection.join(CHANNEL)
  30.  
  31.     def on_join(self, c, e):
  32.         logger.info("Joined {0}".format(e.target))
  33.         GLib.idle_add(self.window.joined_channel, e.target)
  34.  
  35.     def on_pubmsg(self, c, e):
  36.         logger.info("ChanMSG: {0} {1} {2} {3}".format(e.arguments, e.source, e.target, e.type))
  37.         GLib.idle_add(self.window.update_channel, e.arguments[0], e.source.split('!')[0])
  38.  
  39.     def say_on_channel(self, message):
  40.         self.connection.privmsg(CHANNEL, message)
  41.  
  42.  
  43. class IrcWindow(Gtk.Window):
  44.  
  45.     def __init__(self):
  46.         Gtk.Window.__init__(self, title='NaughtyBoy IRC Client (v{0})'.format(VERSION))
  47.         self.set_default_size(800, 600)
  48.         box = Gtk.VBox(spacing=2)
  49.         self.add(box)
  50.  
  51.         # Can we composite?
  52.         self.screen = self.get_screen()
  53.         self.visual = self.screen.get_rgba_visual()
  54.         if self.visual != None and self.screen.is_composited():
  55.             logger.info("Compositing support detected!")
  56.             self.set_visual(self.visual)
  57.  
  58.         # Add info bar at top
  59.         self.info_bar = Gtk.InfoBar()
  60.         self.info_bar.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(*INFO_COLOUR))
  61.         self.spinner = Gtk.Spinner()
  62.         self.wait_label = Gtk.Label("Connecting to {0} ...".format(SERVER))
  63.         self.info_bar.get_content_area().pack_start(self.wait_label, True, True, 5)
  64.         self.info_bar.get_action_area().pack_start(self.spinner, True, True, 5)
  65.         box.pack_start(self.info_bar, False, False, 0)
  66.  
  67.         # Add channel text view to box
  68.         self.channel_view = Gtk.TextView()
  69.         self.channel_view.set_editable(False)
  70.         self.channel_view.set_cursor_visible(False)
  71.         self.channel_view.override_background_color(Gtk.StateFlags.NORMAL, Gdk.RGBA(*COLOUR))
  72.         box.pack_start(self.channel_view, True, True, 0)
  73.  
  74.         # Add Text input view to box
  75.         self.entry = Gtk.Entry()
  76.         self.entry.set_text('SPEAK!')
  77.         self.entry.connect("activate", self.on_message_entered)
  78.         self.entry.set_opacity(0.5)
  79.         self.entry.set_editable(False)
  80.         box.pack_end(self.entry, False, False, 0)
  81.  
  82.         # Set up IRC Connection (will probably hang UI, should be on own thread)
  83.         self.client = Irc(self)
  84.         logger.info("Connecting to irc server {0}:{1} with nick {2}".format(SERVER, PORT, NICK))
  85.         try:
  86.             self.client.connect(SERVER, PORT, NICK)
  87.         except irc.client.ServerConnectionError as x:
  88.             self.channel_view.get_buffer.set_text("Failed connecting: {0}".format(x.message))
  89.  
  90.         self.spinner.start()
  91.         self.info_bar.show()
  92.  
  93.         self.irc_thread = threading.Thread(target=self.client.start)
  94.         self.irc_thread.setDaemon(True)
  95.         self.irc_thread.start()
  96.  
  97.         self.set_app_paintable(True)
  98.         self.connect("draw", self.area_draw)
  99.  
  100.         self.connect("delete-event", self.close_and_quit)
  101.  
  102.     def joined_channel(self, channel_name):
  103.         """
  104.        Callback used by the irc client when joined
  105.        :param channel_name: The name of the channel that has just been joined
  106.        """
  107.         self.hide_info()
  108.         self.channel_view.get_buffer().set_text("** JOINED {0} **".format(channel_name))
  109.         self.entry.set_opacity(1)
  110.         self.entry.set_editable(True)
  111.  
  112.     def on_message_entered(self, entry):
  113.         self.client.say_on_channel(entry.get_text())
  114.         self.update_channel('\t"{0}"'.format(entry.get_text()), 'You')
  115.         self.entry.set_text("")
  116.  
  117.     def replace_text_view(self, text):
  118.         self.channel_view.get_buffer().set_text(text)
  119.  
  120.     def update_channel(self, message, who):
  121.         self.channel_view.get_buffer().insert_at_cursor('\n')
  122.         self.channel_view.get_buffer().insert_at_cursor('{0}: {1}'.format(who, message))
  123.  
  124.     def close_and_quit(self, window, event):
  125.         Gtk.main_quit()
  126.         logger.info("Quitting!")
  127.         print("Terminating connections...")
  128.         self.client.connection.disconnect(message='Byeeee!')
  129.  
  130.  
  131.     def show_info_wait(self, wait_message):
  132.         self.wait_label.set_text(wait_message)
  133.         self.spinner.start()
  134.         self.info_bar.show()
  135.  
  136.     def hide_info(self):
  137.         self.spinner.stop()
  138.         self.info_bar.hide()
  139.  
  140.     def area_draw(self, widget, cr):
  141.         cr.set_source_rgba(*COLOUR)
  142.         cr.set_operator(cairo.OPERATOR_SOURCE)
  143.         cr.paint()
  144.         cr.set_operator(cairo.OPERATOR_OVER)
  145.  
  146. if __name__ == "__main__":
  147.     user_home = os.path.expanduser('~')
  148.     config_dir = os.path.join(user_home, '.naughtyboy')
  149.     if not os.path.exists(config_dir):
  150.         os.mkdir(config_dir)
  151.  
  152.     logging.basicConfig(filename=os.path.join(config_dir, 'debug.log'), level=logging.INFO)
  153.     logger.info("Starting up...")
  154.     win = IrcWindow()
  155.     win.show_all()
  156.     Gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement