Bump to v0.2
[ipython-irc.git] / ipythonirc / __init__.py
1 # Copyright (C) 2013 W. Trevor King <wking@tremily.us>
2 #
3 # This file is part of ipython-irc.
4 #
5 # ipython-irc is free software: you can redistribute it and/or modify it under
6 # the terms of the GNU General Public License as published by the Free Software
7 # Foundation, either version 3 of the License, or (at your option) any later
8 # version.
9 #
10 # ipython-irc is distributed in the hope that it will be useful, but WITHOUT
11 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 # FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
13 # details.
14 #
15 # You should have received a copy of the GNU General Public License along with
16 # ipython-irc.  If not, see <http://www.gnu.org/licenses/>.
17
18 """An IRC iframe using webchat.freenode.net
19 """
20
21 import urllib.parse as _urllib_parse
22
23 import IPython.display as _IPython_display
24
25
26 __version__ = '0.2'
27
28 __all__ = ['IRC']
29
30
31 class IRC (object):
32     """An IRC iframe using webchat clients
33
34     Currently only supports freenode (via webchat.freenode.net).
35
36     Use with::
37
38       IRC(nick='you', channels=('#ipython', ...)).show()
39     """
40     def __init__(self, server='chat.freenode.net', nick=None,
41                  channels=('#ipython','#rogue'), width=647, height=400):
42         self.server = server
43         self.nick = nick
44         self.channels = channels
45         self.width = width
46         self.height = height
47         if self.server not in [
48                 'chat.freenode.net',
49                 ]:
50             raise NotImplementedError(self.server)
51
52     def _html(self):
53         data = {}
54         for attr in ['nick', 'channels']:
55             value = getattr(self, attr)
56             if value:
57                 if attr in ['channels']:
58                     value = ','.join(chan.lstrip('#') for chan in value)
59                 data[attr] = value
60             else:
61                 data['prompt'] = 1
62         url = '{}?{}'.format(
63             'http://webchat.freenode.net',
64             _urllib_parse.urlencode(data)
65             )
66         return '<iframe src="{}" width="{}" height="{}"></iframe>'.format(
67             url, self.width, self.height)
68
69     def show(self):
70         _IPython_display.display(_IPython_display.HTML(self._html()))