An IRC bot written in python for paging someone over e*cityruf using mail.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PagerBot.py 5.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. #!/usr/bin/env python
  2. # Author: Bandie Yip Kojote for TTYgap
  3. # License: GNU-GPLv3
  4. # Year: 2016
  5. #
  6. # Software is provided AS IS and so on.
  7. # The responsible being. (Nickname)
  8. OWNERNAME = "melli17"
  9. # Host of the IRC server
  10. HOST = "irc.example.com"
  11. # TLS port of the IRC server
  12. PORT = 6697
  13. # Nick of the bot
  14. NICK = "PagerBot"
  15. # Ident of the bot
  16. IDENT = "PagerBot"
  17. # Realname of the bot
  18. REALNAME = "https://github.com/TTYgap/PagerBot/"
  19. # Channel which should be joined
  20. CHAN = "#supercoolchan"
  21. # Ask the IRC network administrator what the ping frequency is. Take that and add +5
  22. timeout = 125
  23. # Mail address you're sending from
  24. FROM = "ircbot@example.com"
  25. import sys
  26. import socket
  27. import string
  28. import ssl
  29. import time
  30. import smtplib
  31. phonebook = {}
  32. # CONFIGURE YOUR USERS HERE
  33. # phonebook["someone"] = "123456"
  34. # phonebook["someoneelse"] = "7654321"
  35. def page(receiver, text, user, urgent):
  36. if receiver in phonebook:
  37. number = phonebook[receiver]
  38. else:
  39. return "This user doesn't exist in the phonebook."
  40. to = number + "@ecityruf.de"
  41. if(urgent):
  42. message = "U " + HOST + ":" + user + ":" + text
  43. else:
  44. message = HOST + ":" + user + ":" + text
  45. if(len(message) > 80):
  46. return "The message \"%s\" is too big. It has to be less than 80 characters." % (message)
  47. m = smtplib.SMTP('smtpgw3.emessage.de')
  48. try:
  49. m.sendmail(FROM, to, "FROM: %s\nTO: %s\nSUBJECT: %s" %
  50. (FROM, to, message))
  51. except:
  52. e = sys.exc_info()[0]
  53. return "Error: %s" % e
  54. m.quit()
  55. return "Sent: \"%s\"" % (message)
  56. readbuffer = ""
  57. s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  58. s.connect((HOST, PORT))
  59. ircsock = ssl.wrap_socket(s)
  60. ircsock.send("NICK %s\r\n" % (NICK))
  61. ircsock.send("USER %s %s no :%s\r\n" % (IDENT, HOST, REALNAME))
  62. time.sleep(2)
  63. ircsock.send("MODE %s +B\r\n" % (NICK))
  64. ircsock.send("JOIN %s\r\n" % (CHAN))
  65. print("OK - Connected to %s:%s\n" % (HOST, PORT))
  66. last_ping = time.time()
  67. while 1:
  68. readbuffer = readbuffer + ircsock.recv(1024)
  69. temp = string.split(readbuffer, "\n")
  70. readbuffer = temp.pop()
  71. pagingtext = ""
  72. urgence = False
  73. if (time.time() - last_ping) > timeout:
  74. raise Exception("PING timeout")
  75. for line in temp:
  76. line = string.rstrip(line)
  77. line = string.split(line)
  78. if(line[0] == "PING"):
  79. ircsock.send("PONG %s\r\n" % line[1])
  80. last_ping = time.time()
  81. if(line[1] == "PRIVMSG"):
  82. un = string.split(line[0], "!")
  83. un2 = string.split(un[0], ":")
  84. usernick = un2[1]
  85. if("#" in line[2]):
  86. if(line[3] == ":%s:" % (NICK) or line[3] == ":&pager"):
  87. ircsock.send(
  88. "PRIVMSG %s %s: I only do stuff via query. Try \"/msg %s help\".\r\n" % (line[2], usernick, NICK))
  89. if("#" not in line[2]):
  90. if(line[3].lower() == ":help"):
  91. ircsock.send(
  92. "PRIVMSG %s This is a bot to use a paging service.\r\n" % (usernick))
  93. time.sleep(0.5)
  94. ircsock.send(
  95. "PRIVMSG %s Use \"/msg %s &phonebook\" to list all beings in the phonebook.\r\n" % (usernick, NICK))
  96. time.sleep(0.5)
  97. ircsock.send(
  98. "PRIVMSG %s Use \"/msg %s &pager (urgent) <Username> <Message>\" to page someone.\r\n" % (usernick, NICK))
  99. time.sleep(0.2)
  100. ircsock.send(
  101. "PRIVMSG %s Examples:\r\n" % (usernick))
  102. time.sleep(0.2)
  103. ircsock.send(
  104. "PRIVMSG %s \t/msg %s &pager melli17 This is a not so urgent message.\r\n" % (usernick, NICK))
  105. time.sleep(0.2)
  106. ircsock.send(
  107. "PRIVMSG %s \t/msg %s &pager urgent melli17 This is an urgent message!\r\n" % (usernick, NICK))
  108. time.sleep(0.5)
  109. ircsock.send(
  110. "PRIVMSG %s Call %s if you want to add yourself to the pager phonebook.\r\n" % (usernick, OWNERNAME))
  111. elif(line[3].lower() == ":&pager"):
  112. if(line[4].lower() == "urgent"):
  113. urgence = True
  114. pagingtext = ' '.join(line[6:])
  115. print("%s tries to send %s to \"%s\" in URGENCE.\n" %
  116. (usernick, line[5], pagingtext))
  117. ircsock.send("PRIVMSG %s %s\r\n" %
  118. (usernick, page(line[5], pagingtext, usernick, urgence)))
  119. else:
  120. pagingtext = ' '.join(line[5:])
  121. print("%s tries to send to %s \"%s\"\n" %
  122. (usernick, line[4], pagingtext))
  123. ircsock.send("PRIVMSG %s %s\r\n" %
  124. (usernick, page(line[4], pagingtext, usernick, urgence)))
  125. elif(line[3].lower() == ":&phonebook"):
  126. ircsock.send("PRIVMSG %s %s\r\n" %
  127. (usernick, phonebook.keys()))
  128. else:
  129. ircsock.send("PRIVMSG %s Unknown command. Type \"/msg %s help\" for more information.\r\n" %
  130. (usernick, NICK))
  131. # print(line)