-
Notifications
You must be signed in to change notification settings - Fork 0
/
bingaling.py
executable file
·135 lines (108 loc) · 4.32 KB
/
bingaling.py
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
#!/usr/bin/python
# Copyright (c) 2013, Ali Clark <ali@clark.gb.net>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
from __future__ import print_function
import sys
import subprocess
import datetime
import baseformat
import irccolor
# There are multiple purposes of this program:
# * To make sure that I address or am aware each and every mention of
# me, in case I need to say anything.
# Irssi does highlight channels, but could still miss an earlier bing
# if there are multiple and I only check the most recent.
# For this, mail would be a good fit.
# * To provide a more visible alert system when attention is
# needed. It shouldn't be necessary to watch IRC *all* of the time, or
# even frequently - this would detract from programming or other
# tasks.
# Instead, it should be fine to miss most of the chat for 30mins to an
# hour at a time, yet be alerted when there is a problem.
# For this, any of bubble notificaion, SMS, or mail (provided the
# unread mail is noticeable) would be a good fit.
# TODO: it would be really cool if I can hit reply and have each line
# of response go to the correct channel, with the user's nick
# prepended.
#
# First I'd have to learn how to write an IRC inserter (making it
# appear as my own nick)
#
# Then I'd have to actually be sending it to myself at something like
# ~/mail/sendirc, where a script would look for any new messages
# coming into the folder and send off an IRC message.
#
def sendmail(to, text):
xtra = ''
r = baseformat.splitter.match(text)
if r:
fromstr = 'sendirc@localhost'
channel = r.group(2)
time = baseformat.timeformat(r.group(3))
user = r.group(4)
rest = r.group(5)
dt = datetime.datetime.fromtimestamp(int(r.group(3)[:-3]))
xtra += 'Date: ' + dt.strftime('%a, %d %b %Y %H:%M:%S %z') + '\n'
(leadstr, uperm, nick) = baseformat.nameformat_strip(user)
xtra += 'X-BingFrom: ' + nick + '\n'
if leadstr:
userstr = leadstr + uperm + nick
else:
userstr = uperm + nick
if not (nick.startswith('-') and nick.endswith('-')):
userstr = '<' + userstr + '>'
if channel:
xtra += 'X-BingChan: ' + channel + '\n'
subj = baseformat.combine_parts(channel, time, userstr, rest)
body = userstr + ' ' + rest
else:
fromstr = 'noreply@localhost'
subj = text
body = text
plainsubj = ''.join(map(lambda c: c[0], irccolor.colorize(subj)))
plainbody = ''.join(map(lambda c: c[0], irccolor.colorize(body)))
# TODO: use a Python library for sending
ch = subprocess.Popen(['sendmail', to], stdin=subprocess.PIPE)
full = ('To: ' + to + '\n' +
'From: ' + fromstr + '\n' +
'Subject: ' + plainsubj + '\n' +
xtra +
'\n' +
plainbody + '\n')
ch.stdin.write(full)
ch.stdin.close()
ch.wait()
# In fact, I think I'll have the filter before this program, so that
# any lines that reach this point *will* perform a binging action.
#
# However, if the text is to be *modified* by this program, then I'll
# need to separate the bing stream from the non-bing stream and
# re-merge it after modification. This would require something like a
# "grepsplit" program, which takes a regex as its argument, and
# outputs to two files, with filenames given as arguments 2 and 3.
def bingpipe(bingcheck):
try:
line = sys.stdin.readline()
while line:
print(bingcheck(line), end='')
sys.stdout.flush()
line = sys.stdin.readline()
except KeyboardInterrupt:
pass
def nothing(line):
return line
def main():
bingpipe(nothing)
if __name__ == '__main__':
main()