PagerBot/PagerBot.py

120 lines
3.2 KiB
Python
Raw Normal View History

2016-05-07 18:47:58 +00:00
#!/usr/bin/env python
2016-05-06 19:04:44 +00:00
# Author: Bandie Yip Kojote for TTYgap
# License: GNU-GPLv3
# Year: 2016
2016-05-07 18:47:58 +00:00
#
2016-05-06 19:04:44 +00:00
# Software is provided AS IS and so on.
# Host of the IRC server
2016-05-07 18:47:58 +00:00
HOST = "irc.example.com"
2016-05-06 19:04:44 +00:00
# TLS port of the IRC server
2016-05-07 18:47:58 +00:00
PORT = 6697
2016-05-06 19:04:44 +00:00
# Nick of the bot
2016-05-07 18:47:58 +00:00
NICK = "PagerBot"
2016-05-06 19:04:44 +00:00
# Ident of the bot
2016-05-07 18:47:58 +00:00
IDENT = "PagerBot"
2016-05-06 19:04:44 +00:00
# Realname of the bot
2016-05-07 18:47:58 +00:00
REALNAME = "PagerBot"
2016-05-06 19:04:44 +00:00
# Channel which should be joined
2016-05-07 18:47:58 +00:00
CHAN = "#supercoolchan"
2016-05-06 19:04:44 +00:00
2016-05-07 18:47:58 +00:00
# Mail address you're sending from
FROM = "ircbot@example.com"
2016-05-06 19:04:44 +00:00
import sys
import socket
import string
import ssl
import time
import smtplib
2016-05-07 18:25:57 +00:00
phonebook = {}
2016-05-06 19:04:44 +00:00
2016-05-07 18:25:57 +00:00
# CONFIGURE YOUR USERS HERE
2016-05-06 19:04:44 +00:00
2016-05-07 18:25:57 +00:00
# phonebook["someone"] = "123456"
# phonebook["someoneelse"] = "7654321"
2016-05-06 19:04:44 +00:00
2016-05-07 18:47:58 +00:00
2016-05-07 18:25:57 +00:00
def page(receiver, text, user):
2016-05-06 19:04:44 +00:00
2016-05-07 18:25:57 +00:00
if receiver in phonebook:
number = phonebook[receiver]
2016-05-06 19:04:44 +00:00
else:
return "The username you tried to page has no number saved."
2016-05-07 18:47:58 +00:00
to = number + "@ecityruf.de"
message = HOST + ":" + user + ":" + text
if(len(message) > 80):
2016-05-06 19:04:44 +00:00
return "The message \"%s\" is too big. It has to be less than 80 characters.\r\n" % (message)
m = smtplib.SMTP('smtpgw3.emessage.de')
try:
2016-05-07 18:47:58 +00:00
m.sendmail(FROM, to, "FROM: %s\nTO: %s\nSUBJECT: %s" %
(FROM, to, message))
2016-05-06 19:04:44 +00:00
except:
2016-05-07 18:47:58 +00:00
e = sys.exc_info()[0]
2016-05-06 19:04:44 +00:00
return "Error: %s" % e
m.quit()
return "Sent."
2016-05-07 18:47:58 +00:00
readbuffer = ""
2016-05-06 19:04:44 +00:00
2016-05-07 18:47:58 +00:00
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
2016-05-06 19:04:44 +00:00
s.connect((HOST, PORT))
ircsock = ssl.wrap_socket(s)
2016-05-06 21:25:41 +00:00
ircsock.send("NICK %s\r\n" % (NICK))
2016-05-06 19:04:44 +00:00
ircsock.send("USER %s %s no :%s\r\n" % (IDENT, HOST, REALNAME))
time.sleep(2)
2016-05-07 18:47:58 +00:00
ircsock.send("MODE %s +B\r\n" % (NICK))
2016-05-06 19:04:44 +00:00
ircsock.send("JOIN %s\r\n" % (CHAN))
print("OK\n")
while 1:
2016-05-07 18:47:58 +00:00
readbuffer = readbuffer + ircsock.recv(1024)
temp = string.split(readbuffer, "\n")
readbuffer = temp.pop()
2016-05-06 19:04:44 +00:00
2016-05-07 18:47:58 +00:00
pagingtext = ""
2016-05-06 19:04:44 +00:00
for line in temp:
2016-05-07 18:47:58 +00:00
line = string.rstrip(line)
line = string.split(line)
2016-05-07 18:47:58 +00:00
if(line[0] == "PING"):
2016-05-06 19:04:44 +00:00
ircsock.send("PONG %s\r\n" % line[1])
2016-05-07 18:47:58 +00:00
if(line[1] == "PRIVMSG"):
un = string.split(line[0], "!")
un2 = string.split(un[0], ":")
usernick = un2[1]
if("#" in line[2]):
if(line[3] == ":%s:" % (NICK) or line[3] == ":&pager"):
2016-05-07 18:47:58 +00:00
ircsock.send(
"PRIVMSG %s %s: I only do stuff via query.\r\n" % (line[2], usernick))
if("#" not in line[2]):
2016-05-07 18:47:58 +00:00
if(line[3] == ":help"):
ircsock.send(
"PRIVMSG %s This is a bot to use a paging service.\r\n" % (usernick))
time.sleep(1)
2016-05-07 18:47:58 +00:00
ircsock.send(
"PRIVMSG %s Use \"/msg %s &pager <Username> <Message>\" to page someone.\r\n" % (usernick, NICK))
elif(line[3] == ":&pager"):
2016-05-07 18:47:58 +00:00
pagingtext = ' '.join(line[5:])
print("%s tries to send to %s \"%s\"\n" %
(usernick, line[4], pagingtext))
ircsock.send("PRIVMSG %s %s\r\n" %
(usernick, page(line[4], pagingtext, usernick)))
elif(line[3] == ":&phonebook"):
2016-05-07 18:47:58 +00:00
ircsock.send("PRIVMSG %s %s\r\n" %
(usernick, phonebook.keys()))
2016-05-06 19:04:44 +00:00
# print(line)