PagerBot/PagerBot.py

163 lines
5.3 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.
2016-05-07 21:04:09 +00:00
# The responsible being. (Nickname)
2016-06-13 16:07:17 +00:00
OWNERNAME = "melli17"
2016-05-06 19:04:44 +00:00
# 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 21:10:42 +00:00
REALNAME = "https://github.com/TTYgap/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-10-18 17:36:35 +00:00
# Ask the IRC network administrator what the ping frequency is. Take that and add +5
timeout = 125
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-08 23:20:30 +00:00
def page(receiver, text, user, urgent):
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:
2016-05-08 23:20:30 +00:00
return "This user doesn't exist in the phonebook."
2016-05-06 19:04:44 +00:00
2016-05-07 18:47:58 +00:00
to = number + "@ecityruf.de"
2016-05-08 23:20:30 +00:00
if(urgent):
message = "U " + HOST + ":" + user + ":" + text
else:
message = HOST + ":" + user + ":" + text
2016-05-07 18:47:58 +00:00
if(len(message) > 80):
2016-05-07 21:04:09 +00:00
return "The message \"%s\" is too big. It has to be less than 80 characters." % (message)
2016-05-06 19:04:44 +00:00
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()
2016-06-13 16:06:35 +00:00
return "Sent: \"%s\"" % (message)
2016-05-06 19:04:44 +00:00
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))
2016-10-18 17:36:35 +00:00
print("OK - Connected to %s:%s\n" % (HOST, PORT))
last_ping = time.time()
2016-05-06 19:04:44 +00:00
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-08 23:20:30 +00:00
urgence = False
2016-10-18 17:36:35 +00:00
if (time.time() - last_ping) > timeout:
raise Exception("PING timeout")
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-10-18 17:36:35 +00:00
last_ping = time.time()
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(
2016-05-07 21:04:09 +00:00
"PRIVMSG %s %s: I only do stuff via query. Try \"/msg %s help\".\r\n" % (line[2], usernick, NICK))
if("#" not in line[2]):
if(line[3].lower() == ":help"):
2016-05-07 18:47:58 +00:00
ircsock.send(
"PRIVMSG %s This is a bot to use a paging service.\r\n" % (usernick))
2016-05-07 21:04:09 +00:00
time.sleep(0.5)
ircsock.send(
"PRIVMSG %s Use \"/msg %s &phonebook\" to list all beings in the phonebook.\r\n" % (usernick, NICK))
time.sleep(0.5)
2016-05-07 18:47:58 +00:00
ircsock.send(
2016-05-08 23:20:30 +00:00
"PRIVMSG %s Use \"/msg %s &pager (urgent) <Username> <Message>\" to page someone.\r\n" % (usernick, NICK))
2016-06-18 11:18:34 +00:00
time.sleep(0.2)
ircsock.send(
"PRIVMSG %s Examples:\r\n" % (usernick))
time.sleep(0.2)
ircsock.send(
"PRIVMSG %s \t/msg %s &pager melli17 This is a not so urgent message.\r\n" % (usernick, NICK))
time.sleep(0.2)
ircsock.send(
"PRIVMSG %s \t/msg %s &pager urgent melli17 This is an urgent message!\r\n" % (usernick, NICK))
2016-05-07 21:04:09 +00:00
time.sleep(0.5)
ircsock.send(
"PRIVMSG %s Call %s if you want to add yourself to the pager phonebook.\r\n" % (usernick, OWNERNAME))
2016-05-08 23:20:30 +00:00
elif(line[3].lower() == ":&pager"):
2016-05-08 23:20:30 +00:00
if(line[4].lower() == "urgent"):
urgence = True
pagingtext = ' '.join(line[6:])
print("%s tries to send %s to \"%s\" in URGENCE.\n" %
(usernick, line[5], pagingtext))
ircsock.send("PRIVMSG %s %s\r\n" %
(usernick, page(line[5], pagingtext, usernick, urgence)))
else:
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, urgence)))
elif(line[3].lower() == ":&phonebook"):
2016-05-07 18:47:58 +00:00
ircsock.send("PRIVMSG %s %s\r\n" %
2016-05-10 23:15:24 +00:00
(usernick, phonebook.keys()))
else:
ircsock.send("PRIVMSG %s Unknown command. Type \"/msg %s help\" for more information.\r\n" %
(usernick, NICK))
2016-05-06 19:04:44 +00:00
# print(line)