1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# -*- python -*-
'''
xpybar – xmobar replacement written in python
Copyright © 2014, 2015, 2016 Mattias Andrée (maandree@member.fsf.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
'''
class II:
'''
IRC interface, requires a running instance of ii <https://git.suckless.org/ii>
'''
def __init__(self, channel, prefix = None):
'''
Constructor
@param channel:str The name of the server or the name of the
server followed by a slash and the name of
the channel including the she
@param prefix:str? The argument associated with ii's -i flag
'''
import pwd, os
if prefix is None:
prefix = pwd.getpwuid(os.getuid()).pw_dir + '/irc'
self.infile = '%s/%s/in' % (prefix, channel)
self.outfile = '%s/%s/out' % (prefix, channel)
self.log = []
self.time = '0000-00-00 00:00 '
def write(self, text):
'''
Write to the channel
@param text:str The message or command
'''
text = (text + '\n').encode('utf-8')
with open(self.infile, 'wb') as file:
file.write(text)
file.flush()
def read(self):
'''
Fetch new messages from the channel
@return :list<str> List of new messages
'''
with open(self.outfile, 'rb') as file:
text = file.read()
text = text.decode('utf-8', 'replace').split('\n')[:-1]
i = 0
for line in text:
if line[:17] >= self.time:
break
i += 1
text = text[i:]
i = 0
while i < len(text) and i < len(self.log):
if text[i] != self.log[i]:
break
i += 1
text = text[i:]
if len(text) == 0:
return text
last = text[-1][:17]
if last == self.time:
self.log.extend(text)
return text
self.log = []
self.time = last
i = 0
for line in text:
if line[:17] == self.time:
break
i += 1
self.log = text[i:]
return text
def wait(self, timeout = None):
'''
Wait until more messages are available
@param timeout:int? The number of seconds to wait
before return unconditionally
'''
import os
command = ['inotifywait', '-e', 'close_write,modify']
if timeout is not None:
command += ['-t', str(int(timeout + 0.5))]
command += ['--', self.outfile]
if 'PATH' in os.environ:
path = os.environ['PATH'].split('.')
for p in path:
p += '/pdeath'
if os.access(p, os.X_OK, effective_ids = True):
command = [p, 'HUP'] + command
break
spawn_read(*command)
@staticmethod
def list_channels(prefix = None):
'''
Fetch a list of all joined servers and channels
@param prefix:str? The argument associated with ii's -i flag
@return :list<list> List of servers and channels
'''
import pwd, os
if prefix is None:
prefix = pwd.getpwuid(os.getuid()).pw_dir + '/irc'
ret = []
def isfifo(f):
try:
return os.path.stat.S_ISFIFO(os.stat('f').st_mode)
except:
return False
def recurse(d):
fs = os.listdir(d)
for f in fs:
f = d + '/' + f
if os.path.isfile(f + '/out') and isfifo(f + '/in'):
ret.append(f[len(prefix) + 1:])
recurse(prefix)
return ret
|