forked from philterphactory/skypebot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathskypebot.py
216 lines (184 loc) · 8.24 KB
/
skypebot.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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import Skype4Py
import time
from commands import drinkcommand, wetcommand, baconcommand, snackcommand, \
cheesecommand, cockcommand, smokecommand, chooncommand, tvcommand, \
birthdaycommand, commandscratch, eurovisioncommand, teacommand, coffeecommand
import datetime
import json
import Queue
from hookserver import HookServerMessage, HookServerThread
import sys
import logging
from messages import housekeeping
import twitterconnector
class ChatHandler(object):
def __init__( self, Chat ):
self.chat = Chat
self.last_timestamp = datetime.datetime.now()
def update( self ):
new_messages = []
messages = self.chat.RecentMessages
for message in messages:
dt = message.Datetime
if dt > self.last_timestamp:
new_messages.append( message )
self.last_timestamp = dt
return new_messages
RUN_SKYPE=True
class BotRunner( object ):
def message_all( self, message ):
# send message to all connected chats
for chat_name in self.chat_handlers:
chat_handler = self.chat_handlers[ chat_name ]
try:
chat_handler.chat.SendMessage( message )
except Exception, e:
logging.info( e )
def run( self ):
return_code = 0
# set up http server to listen for github pushes
logging.info( "Staring up github HookServer..." )
queue = Queue.Queue()
retries = 3
hook_server = None
while retries > 0:
try:
hook_server = HookServerThread()
except Exception:
pass
retries -=1
if hook_server is None:
wait( 10 )
else:
break
if hook_server is None:
sys.exit(0)
hook_server.queue = queue
hook_server.start()
# twitter connection
logging.info( "Starting up Twitter connector..." )
tw = twitterconnector.TwitterConnector( "twitter_creds" )
# set up command handlers
self.chat_handlers = {}
command_mappings = {}
command_mappings[ "drink" ] = drinkcommand.DrinkCommand()
command_mappings[ "w3t" ] = wetcommand.WetCommand()
command_mappings[ "bacon" ] = baconcommand.BaconCommand()
command_mappings[ "snack" ] = snackcommand.SnackCommand()
command_mappings[ "cheese" ] = cheesecommand.CheeseCommand()
#command_mappings[ "cock" ] = cockcommand.CockCommand()
command_mappings[ "choon" ] = chooncommand.ChoonCommand()
command_mappings[ "smoke" ] = smokecommand.SmokeCommand()
command_mappings[ "telly" ] = tvcommand.TVCommand()
command_mappings[ "birthday" ] = birthdaycommand.BirthdayCommand()
command_mappings[ "eurovision" ] = eurovisioncommand.EurovisionCommand()
command_mappings[ "tea" ] = teacommand.TeaCommand()
command_mappings[ "tv" ] = tvcommand.TVCommand()
command_mappings[ "coffee" ] = coffeecommand.CoffeeCommand()
command_mappings[ "satan" ] = commandscratch.SatanCommand()
if RUN_SKYPE:
logging.info( "Attaching to Skype..." )
skype = Skype4Py.Skype(Transport='x11')
skype.Attach()
logging.info( "Entering main run loop..." )
_run = True
while _run:
try:
if RUN_SKYPE:
# maintain list of chats
chats = skype.Chats
for chat in chats:
chat_name = chat.Name
if chat_name not in self.chat_handlers:
logging.info( "New handler for chat: %s" % chat.FriendlyName )
self.chat_handlers[chat_name] = ChatHandler(chat)
message = housekeeping.new_chat_message()
try:
chat.SendMessage( message )
except Exception, e:
logging.info( e )
print e
# TODO: clear defunct chats
# update chats
for chat_name in self.chat_handlers:
chat_handler = self.chat_handlers[ chat_name ]
new_messages = chat_handler.update()
if len(new_messages)> 0:
print "New messages in chat: %s" % chat_handler.chat.FriendlyName
for new_message in new_messages:
body = new_message.Body
print body
try:
for commandstring in command_mappings:
commandbang = "!" + commandstring
bl = body.lower()
command = command_mappings[ commandstring ]
message_out = None
# if command is giftable
if hasattr( command, 'gift' ):
giftstring = "%s for " % commandbang
if giftstring in bl:
loc = body.find(giftstring) + len(giftstring)
spc = body.find( " ", loc )
recicpient = body[ loc : spc ]
members = chat_handler.chat.Members
for member in members:
dn = member.DisplayName
if recicpient.lower() in dn.lower():
message_out = command.gift( dn )
break
if message_out is None and commandbang in bl:
message_out = command.execute( new_message )
if message_out is not None:
chat_handler.chat.SendMessage( message_out )
tw.tweet( message_out )
except Exception, e:
logging.info( e )
print e
# check for updates from the HTTP server thread
hook_message = None
try:
hook_message = queue.get( False )
except Queue.Empty:
pass
if hook_message is not None:
if hook_message.code == HookServerMessage.RECIEVED_PUSH:
commits = hook_message.payload[ 'commits' ]
commit_author = commits[0]['author']['name']
message_out = housekeeping.update_message_for_name( commit_author )
self.message_all( message_out )
_run = False
return_code = 3
print "MESSAGE: Update recieved"
else:
time.sleep( 1 )
except KeyboardInterrupt:
_run = False
if _run is False:
# for chat_name in self.chat_handlers:
# chat_handler = self.chat_handlers[ chat_name ]
# try:
# chat_handler.chat.Leave()
# except Exception, e:
# logging.info( e )
break
hook_server.stop()
return return_code
###
# MAIN RUN
###
logging.basicConfig( filename="skypebot.log", level=logging.INFO, filemode='w' )
#logging.captureWarnings( True )
# define a Handler which writes INFO messages or higher to the sys.stderr
console = logging.StreamHandler()
console.setLevel(logging.INFO)
# add the handler to the root logger
logging.getLogger('').addHandler(console)
runner = BotRunner()
retcode = 0
try:
retcode = runner.run()
except Exception, e:
logging.info( e )
logging.shutdown()
sys.exit( retcode )