forked from theli-ua/pyHoNBot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
executable file
·122 lines (96 loc) · 3.96 KB
/
__init__.py
File metadata and controls
executable file
·122 lines (96 loc) · 3.96 KB
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
#!/usr/bin/env python
"""
honbot - A Heroes Of Newerth chatserver Bot
Copyright 2011, Anton Romanov
Heavily inspired by phenny:
__init__.py - Phenny Init Module
Copyright 2008, Sean B. Palmer, inamidst.com
Licensed under the Eiffel Forum License 2.
"""
import sys, os, time, threading, signal,traceback
import bot
from hon import packets
from twitch_chat_bot import TwitchChatBot
class Watcher(object):
# Cf. http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/496735
def __init__(self):
self.child = os.fork()
if self.child != 0:
self.watch()
def watch(self):
try: os.wait()
except KeyboardInterrupt:
self.kill()
sys.exit()
def kill(self):
try: os.kill(self.child, signal.SIGKILL)
except OSError: pass
def twitch_source_to_nick(source):
return source.split('@')[0].split('!')[0]
def run_honbot(config):
if hasattr(config, 'delay'):
delay = config.delay
else: delay = 20
try: Watcher()
except Exception, e:
print >> sys.stderr, 'Warning:', e, '(in __init__.py)'
twitch_channel = config.twitch_guest_channel if hasattr(config, 'twitch_guest_channel') and config.twitch_guest_channel is not None and len(config.twitch_guest_channel) > 0 else '#'+config.twitch_nick.lower()
twitch_bot = TwitchChatBot(twitch_channel, config.twitch_nick, "irc.twitch.tv", config.twitch_oauth_password, 6667)
p = None
def post_to_twitch(msg):
twitch_bot.send_to_channel(msg)
def connect(config):
p = bot.Bot(config, post_to_twitch)
#t = threading.Thread(target=p.run)
#t.start()
p.run()
print "done with connect!"
return p
def handle_pubmsg(c, e):
print("Twitch: got pubmsg with c="+str(c)+", e="+str(e)+" with type="+e.type+", source="+e.source+", target="+e.target+", arguments="+str(e.arguments))
p.write_packet(packets.ID.HON_SC_WHISPER, p.config.owner, twitch_source_to_nick(e.source) + ": " + ";".join(e.arguments))
if hasattr(p.config, 'other_receivers') and p.config.other_receivers is not None:
for receiver in p.config.other_receivers:
p.write_packet(packets.ID.HON_SC_WHISPER, receiver, twitch_source_to_nick(e.source) + ": " + ";".join(e.arguments))
def handle_join(c, e):
# print("Twitch: got pubmsg with c="+str(c)+", e="+str(e)+" with type="+e.type+", source="+e.source+", target="+e.target+", arguments="+str(e.arguments))
p.write_packet(packets.ID.HON_SC_WHISPER, p.config.owner, twitch_source_to_nick(e.source) + " joined your channel!")
def handle_part(c, e):
# print("Twitch: got pubmsg with c="+str(c)+", e="+str(e)+" with type="+e.type+", source="+e.source+", target="+e.target+", arguments="+str(e.arguments))
p.write_packet(packets.ID.HON_SC_WHISPER, p.config.owner, twitch_source_to_nick(e.source) + " left your channel!")
twitch_bot.add_on_pubmsg(handle_pubmsg)
#twitch_bot.add_on_join(handle_join)
#twitch_bot.add_on_part(handle_part)
twitch_bot._connect()
while True:
try:
print("connecting!")
p = connect(config)
#names_sent = time.time()
while True:
#end = time.time()
#if (end - names_sent) > 10:
# twitch_bot.send_names()
# names_sent = time.time()
p.loop()
twitch_bot.reactor.process_once(timeout=1)
except KeyboardInterrupt:
sys.exit()
except:
print(sys.exc_type,sys.exc_value)
print(sys.exc_traceback)
print(sys.exc_info())
traceback.print_exc(file=sys.stdout)
pass
if not isinstance(delay, int):
break
warning = 'Warning: Disconnected. Reconnecting in %s seconds...' % delay
print >> sys.stderr, warning
time.sleep(delay)
def run(config):
t = threading.Thread(target=run_honbot, args=(config,))
if hasattr(t, 'run'):
t.run()
else: t.start()
if __name__ == '__main__':
print __doc__