Python Inside
#48
since i'm very familiar with irc (i've been on irc in one form or another for almost ten years, now,) it was really simple for me to write my own code. i thought that there might be an existing irclib for python, but i didn't know enough about the language and how its module system worked to try to use it. without further ado, here's the basics of it, although excuse any sloppy/incorrect code, as it's my first attempt at a python script. i've stripped out some code as to keep the post short, but, it's obvious as to how it should work. i've added comments at the bottom of the post.

Quote:#
# xbmc irc bot by randolph e. sommerfeld <[email protected]>
# permission to copy granted so long as this copyright notice remains.
# email the author <[email protected]> with any changes or custom hacks.
# thanks!
#
from socket import *
import string
import time

##### "consts #####
version_notice = "xbox bot v0.1 by randy sommerfeld, running on a real, live xbox!"

##### functions #####

def servercommands(nick,user,host,str):
command = string.upper(str[0:string.find(str, " ")])
cmd = string.split(str," ")
irc_string = str[string.find(str,":")+1:9999999]
if command == "ping":
irc_send("pong " + cmd[1])
elif command == "privmsg":
if string.upper(cmd[2]) == ":xb":
botstr = irc_string[string.find(irc_string," ")+1:9999]
botcommands(nick,user,host,cmd[1],botstr)
elif string.upper(cmd[2]) == ":^aversion^a":
irc_send("notice " + nick + " :^aversion " + version_notice + "^a")

def botcommands(nick,user,host,target,botstr):
tmp_find_string = string.find(botstr," ")
if tmp_find_string == -1:
tmp_find_string = 999999999999
bot_cmd = string.upper(botstr[0:tmp_find_string])
if bot_cmd == "die":
done=1
elif bot_cmd == "version":
privmsg(target,version_notice)
elif bot_cmd == "help":
privmsg(target,help)
#.....etc, each bot command goes here.....

def privmsg(target,msg):
irc_send("privmsg " + target + " :" + msg)

def irc_send(msg):
print("--> " + msg)
s.send(msg + "\r\n")

def irc_getnick(str):
return str[0:string.find(str, "!")]

def irc_getuser(str):
return str[string.find(str, "!")+1:string.find(str, "@")]

def irc_gethost(str):
return str[string.find(str, "@")+1:999]

##### main() #####

s = socket(af_inet, sock_stream)
s.connect(("irc.synchro.net", 6667))
print "connecting to irc..."

irc_send("user xbox * * xbox :i'm running on cyan's xbox :")
irc_send("nick xbox")
irc_send("join #xbox")

done=0
remainder=""
while not done:
data = ""
test_remainder = string.find(remainder, "\r\n")
if test_remainder != -1:
data = remainder[0:test_remainder]
remainder = remainder[test_remainder+2:9999999999999]
else:
time.sleep(1)
remainder = remainder + s.recv(512)
if data:
print("<-- " + data)
strindex=1
strindex = string.find(data, ":")
if strindex == 0:
origin_str = data[1:string.index(data, " ")]
cmd_str = data[string.index(data, " ")+1:999]
is_nuh = string.find(origin_str, "!")
if is_nuh == 1:
servercommands(origin_str,0,0,cmd_str)
else:
nick = irc_getnick(origin_str)
user = irc_getuser(origin_str)
host = irc_gethost(origin_str)
servercommands(nick,user,host,cmd_str)
else:
servercommands(0,0,0,data)

* i know the '999999' thing is lame, and there must be some other way to mean "all the way to the end of the string," but i couldn't find it.
* the ^a is an actual control-a character. i wasn't sure how to insert it into python (i.e. by \0x01, \001, \1 or otherwise) this is used for ctcp.
* i can typically be found on irc.synchro.net as "cyan" in #rrx
* commands in the channel are sent like this: "xb version", and the response is thus put into the channel. commands in msg are sent like this: "/msg xbox version" and then the response is msg'd back to you.
* please email me or talk to me on irc if you make any changes or if you're using this code. it's just a skeleton for now, but it should be enough to get everyone going :)


Messages In This Thread
[No subject] - by Blackbelt - 2003-10-07, 19:17
[No subject] - by Hullebulle - 2003-10-08, 00:51
[No subject] - by Zealot - 2003-10-08, 18:51
[No subject] - by Hullebulle - 2003-10-08, 20:36
[No subject] - by burriko - 2003-10-09, 12:33
[No subject] - by darkie - 2003-10-09, 16:19
[No subject] - by Gamester17 - 2003-10-10, 12:12
[No subject] - by windragz - 2003-10-10, 21:14
[No subject] - by Frodo - 2003-10-11, 10:46
[No subject] - by Gamester17 - 2003-10-11, 13:57
[No subject] - by Frodo - 2003-10-11, 15:13
[No subject] - by Gamester17 - 2003-10-11, 17:35
[No subject] - by Frodo - 2003-10-11, 18:33
[No subject] - by darkie - 2003-10-13, 16:43
[No subject] - by burriko - 2003-10-15, 00:01
[No subject] - by Frodo - 2003-10-15, 08:20
[No subject] - by Gamester17 - 2003-10-15, 13:50
[No subject] - by windragz - 2003-10-16, 19:47
[No subject] - by darkie - 2003-10-17, 23:59
[No subject] - by burriko - 2003-10-18, 20:10
[No subject] - by darkie - 2003-10-18, 20:54
[No subject] - by burriko - 2003-10-22, 01:30
[No subject] - by darkie - 2003-10-22, 18:03
[No subject] - by burriko - 2003-10-22, 18:48
[No subject] - by darkie - 2003-10-22, 20:30
[No subject] - by burriko - 2003-10-22, 20:39
[No subject] - by darkie - 2003-10-23, 21:29
[No subject] - by fonzi - 2003-10-29, 09:40
[No subject] - by Gamester17 - 2003-10-29, 14:37
[No subject] - by cyberplague - 2003-11-08, 00:23
[No subject] - by rjm2k - 2003-11-08, 01:40
[No subject] - by darkie - 2003-11-08, 01:43
[No subject] - by burriko - 2003-11-08, 01:54
[No subject] - by cyberplague - 2003-11-08, 10:20
[No subject] - by rjm2k - 2003-11-08, 17:05
[No subject] - by darkie - 2003-11-08, 18:10
[No subject] - by cyberplague - 2003-11-08, 19:34
[No subject] - by burriko - 2003-11-18, 19:16
[No subject] - by darkie - 2003-11-19, 00:07
[No subject] - by burriko - 2003-11-19, 01:04
[No subject] - by Gamester17 - 2003-11-19, 13:42
[No subject] - by kikiller - 2003-11-19, 14:02
[No subject] - by Cyan - 2003-11-28, 11:12
[No subject] - by febs - 2003-11-28, 17:34
[No subject] - by darkie - 2003-11-30, 21:21
[No subject] - by Nickman - 2003-11-30, 22:20
[No subject] - by fonzi - 2003-12-01, 12:55
[No subject] - by Cyan - 2003-12-02, 08:28
[No subject] - by Hullebulle - 2003-12-03, 01:26
[No subject] - by kraqh3d - 2003-12-03, 17:00
[No subject] - by darkie - 2003-12-03, 17:20
[No subject] - by cyberplague - 2003-12-03, 20:20
[No subject] - by burriko - 2003-12-10, 00:44
[No subject] - by darkie - 2003-12-10, 18:52
[No subject] - by adrianmak - 2003-12-21, 11:59
[No subject] - by Hullebulle - 2003-12-21, 20:47
[No subject] - by Kuranes - 2003-12-22, 19:27
[No subject] - by gremlin - 2004-01-03, 15:06
[No subject] - by adrianmak - 2004-01-09, 11:18
[No subject] - by darkie - 2004-01-10, 19:47
[No subject] - by alx5962 - 2004-01-11, 20:05
[No subject] - by darkie - 2004-01-11, 20:48
[No subject] - by alx5962 - 2004-01-11, 22:56
[No subject] - by rayban - 2004-01-12, 10:05
[No subject] - by alx5962 - 2004-01-13, 11:02
[No subject] - by darkie - 2004-01-13, 20:58
[No subject] - by alx5962 - 2004-01-14, 00:25
[No subject] - by fonzi - 2004-01-16, 09:42
[No subject] - by darkie - 2004-01-16, 17:11
[No subject] - by Hullebulle - 2004-01-16, 17:31
[No subject] - by darkie - 2004-01-16, 19:16
[No subject] - by alx5962 - 2004-01-19, 15:28
[No subject] - by cyberplague - 2004-01-19, 18:38
[No subject] - by darkie - 2004-01-20, 01:38
[No subject] - by alx5962 - 2004-01-20, 10:00
[No subject] - by fonzi - 2004-01-23, 10:11
[No subject] - by darkie - 2004-01-23, 12:50
[No subject] - by XDX - 2004-01-25, 02:55
[No subject] - by san - 2004-01-25, 14:47
[No subject] - by alx5962 - 2004-01-29, 12:14
[No subject] - by alx5962 - 2004-01-30, 12:53
[No subject] - by alx5962 - 2004-02-01, 13:10
[No subject] - by darkie - 2004-02-01, 15:41
[No subject] - by alx5962 - 2004-02-01, 16:38
[No subject] - by alx5962 - 2004-02-02, 00:25
[No subject] - by alx5962 - 2004-02-03, 21:39
[No subject] - by darkie - 2004-02-03, 22:08
[No subject] - by alx5962 - 2004-02-03, 22:54
[No subject] - by kexman - 2004-02-08, 01:04
[No subject] - by darkie - 2004-02-08, 01:42
[No subject] - by alx5962 - 2004-02-08, 02:03
[No subject] - by darkie - 2004-02-08, 02:36
[No subject] - by alx5962 - 2004-02-08, 11:50
[No subject] - by alx5962 - 2004-02-10, 16:59
[No subject] - by alx5962 - 2004-02-13, 18:15
[No subject] - by darkie - 2004-02-14, 19:51
[No subject] - by alx5962 - 2004-02-15, 01:13
[No subject] - by alx5962 - 2004-02-15, 23:17
[No subject] - by darkie - 2004-02-16, 00:13
[No subject] - by Kieren - 2004-02-18, 19:51
[No subject] - by alx5962 - 2004-02-18, 22:54
[No subject] - by alx5962 - 2004-02-19, 16:24
[No subject] - by darkie - 2004-02-19, 20:19
[No subject] - by Kieren - 2004-02-19, 20:42
[No subject] - by alx5962 - 2004-02-19, 21:32
[No subject] - by Gamester17 - 2004-02-20, 12:39
[No subject] - by alx5962 - 2004-02-20, 12:44
[No subject] - by bobrooney_78 - 2004-02-20, 14:17
[No subject] - by alx5962 - 2004-02-20, 14:29
[No subject] - by Gamester17 - 2004-02-20, 14:41
[No subject] - by alx5962 - 2004-02-20, 15:16
[No subject] - by alx5962 - 2004-02-22, 13:10
[No subject] - by Zinger - 2004-02-22, 17:26
[No subject] - by Kieren - 2004-02-22, 19:19
[No subject] - by Zinger - 2004-02-23, 04:21
[No subject] - by Kieren - 2004-02-23, 15:15
[No subject] - by darkie - 2004-02-23, 19:04
[No subject] - by burriko - 2004-02-26, 15:27
[No subject] - by alx5962 - 2004-02-26, 15:35
[No subject] - by darkie - 2004-02-26, 15:55
[No subject] - by burriko - 2004-02-26, 16:14
[No subject] - by Zinger - 2004-02-28, 01:53
[No subject] - by darkie - 2004-03-02, 16:15
[No subject] - by Zinger - 2004-03-03, 08:22
[No subject] - by bobrooney_78 - 2004-03-03, 17:13
[No subject] - by alx5962 - 2004-03-03, 17:51
[No subject] - by bobrooney_78 - 2004-03-03, 19:01
[No subject] - by Zinger - 2004-03-04, 01:00
[No subject] - by burriko - 2004-03-04, 01:42
[No subject] - by alx5962 - 2004-03-05, 21:09
[No subject] - by alx5962 - 2004-03-07, 15:52
[No subject] - by burriko - 2004-03-07, 17:11
[No subject] - by gaogao - 2004-03-08, 11:16
[No subject] - by alx5962 - 2004-03-08, 21:07
[No subject] - by contrast - 2004-03-09, 12:06
[No subject] - by bobrooney_78 - 2004-03-09, 13:44
[No subject] - by alx5962 - 2004-03-09, 15:05
[No subject] - by silentyl - 2004-03-11, 02:15
[No subject] - by burriko - 2004-03-11, 12:21
[No subject] - by alx5962 - 2004-03-11, 15:36
Logout Mark Read Team Forum Stats Members Help
Python Inside0