/
kittybot2.py
133 lines (112 loc) · 3.78 KB
/
kittybot2.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
"""
This is kittybot v2.0, now in Python!
Code is released under GPLv3. See the file named copying for more details
TODO: random kitties every hour
TODO: geohash!
TODO: fix weather to take farhenhite
TODO: mouthpiece mode
TODO Kitties on startup
"""
import irclib
import re
import socket
from lxml import etree
import urllib
import time
from config import *
import random
import feedparser
import urlshortener
import BeautifulSoup
import commands
halp = """you can try !weather, !thefuckingweather, !wtf, !kitty, !tag [tag name for flickr], !locate [username]. Check out my source code at https://github.com/tahnok/kittybot2"""
#regexes
locatere = re.compile('!locate ([\x5b-\x60\x7b-\x7d]|[\\w-]+$)')
#this is super hacky, find a way to fix it
reply = "Not Set"
def parse(msg, sender, connection):
for command in commands.registered:
result = command.execute(msg, sender, connection)
if result is not None:
return result
print "No match for " + msg
return None
# IRC related things
def debug(connection, event):
print('---------Event')
print("event type: " + event.eventtype())
print("event target: " + event.target())
print("event source: " + event.source())
print("event arguments: " + str(event.arguments()))
def pubmsg(connection, event):
global reply
def say(msg):
connection.privmsg(event.target(), msg)
#This is where the parsing / magic happens
message = event.arguments()[0]
print message
m = locatere.match(message)
if m is not None:
print "locating..."
reply = event.target()
a = connection.whois([m.group(1)])
elif message == "!halp":
connection.privmsg(irclib.nm_to_n(event.source()), halp)
elif message == "!kthxbai":
print "bai"
if event.source() == owner:
say("going to sleep now")
say("bai")
connection.quit()
exit()
else:
say("har har nice try")
else:
reply = parse(message, event.source(), connection)
if reply is not None:
say(reply)
def privmsg(connection, event):
print("it's a private msg!")
def bounce(connection, event):
connection.join(event.target())
def handlewho(connection, event):
print("whois handler")
global reply
try:
ip = event.arguments()[2]
m = re.match('\\w+/', ip)
if m is not None:
msg = "Error: User has a hostmask"
else:
m = re.match('^\\d{1,3}\\.\\d{1,3}\\.\\d{1,3}$', ip)
if m is None:
ip = socket.gethostbyname(ip)
root = etree.parse('http://api.ipinfodb.com/v2/ip_query.php?key=' + ipinfodbkey + '&ip=' + ip + '&timezone=false').getroot()
lon = root.find("Longitude").text
lat = root.find("Latitude").text
city = root.find("City").text
long_url = "http://maps.google.com/maps?f=d&source=s_d&saddr=%s,%s" % (lat, lon)
short_url = urlshortener.shorten(long_url)
msg = "City: %s. Map: %s" % (city, short_url)
except:
msg = "DAMN YOU MITHORIUM"
connection.privmsg(reply, msg)
def handledisconnect(connection, event):
print ("disconencted")
def main ():
irc = irclib.IRC()
irc.add_global_handler('privmsg', privmsg)
irc.add_global_handler('pubmsg', pubmsg)
irc.add_global_handler('whoisuser', handlewho)
irc.add_global_handler('kick', bounce)
irc.add_global_handler('disconnect', debug)
irc.add_global_handler('error', debug)
server = irc.server()
server.connect(network, port, nick, ircname=name)
if freenode:
server.privmsg("NickServ", "identify " + nickservpwd)
for channel in channels:
server.join(channel)
irc.process_forever(0.5)
if __name__ == '__main__':
main()