Guest User

Untitled

a guest
Jul 18th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 8.75 KB | None | 0 0
  1. import re
  2. import socket
  3.  
  4. import MySQLdb as mdb
  5.  
  6. global run, dbhost, dbuser, dbpass, dbname, table, db, dbc
  7.  
  8. server = "irc.freenode.net"
  9. channel = "#reddit-r4r-Florida"
  10. botnick = "nb1"
  11. run = 1
  12. dbhost = 'localhost'
  13. dbuser = 'root'
  14. dbpass = 'Ntk5927654'
  15. dbname = 'nbot'
  16. dbtable = 'users'
  17.  
  18. def ping():
  19.     ircsock.send("PONG :pingis\n")
  20.  
  21. def sendmsg(chan, msg):
  22.     ircsock.send("PRIVMSG " + chan + " :" + msg + "\n")
  23.  
  24. def joinchan(chan):
  25.     ircsock.send("JOIN " + chan + "\n")
  26.  
  27. def stoprun():
  28.     global run
  29.     run = 0
  30.     return run
  31.  
  32. def cities(nick, chan):
  33.     query = buildquery('SELECT', 'cities', '', '', '`name` ASC', '')
  34.     c = gdbc(query)
  35.     data = c.fetchall()
  36.     rc = c.rowcount
  37.     counter = 1
  38.     if rc > 0:
  39.         s = 'We have users registered in: '
  40.         for row in data:
  41.             city = row[1]
  42.             num = row[2]
  43.             if counter == rc:
  44.                 s += ' and '
  45.             elif counter > 1:
  46.                 s += ', '
  47.             s += '%s (%s)' % (city, num)
  48.             counter += 1
  49.     else:
  50.         s = 'No registered users have specified their location'
  51.     sendmsg(chan, s)
  52.  
  53. def gdbc(query):
  54.     global db, dbc, channel
  55.     try:
  56.         db = mdb.connect(host=dbhost, user=dbuser, passwd=dbpass, db=dbname)
  57.         dbc = db.cursor()
  58.         dbc.execute(query)
  59.     except mdb.Error, e:
  60.         sendmsg(channel, "Error %d: %s" % (e.args[0], e.args[1]))
  61.     finally:
  62.         if db:
  63.             db.close()
  64.     return dbc
  65.  
  66. def rowcount(qstring):
  67.     c = gdbc(qstring)
  68.     rc = c.rowcount
  69.     c.close
  70.     return rc
  71.  
  72. def getuser(nstring):
  73.     c = gdbc(buildquery('SELECT', 'users', '', 'name=%s' % nstring, '', '1'))
  74.     data = c.fetchall()
  75.     c.close
  76.     return data[0]
  77.  
  78. def register(ps):
  79.     global dbc
  80.     dbc.execute(buildquery('INSERT', '', 'users', ps, '', '', ''))
  81.  
  82. def unregister():
  83.     return ''
  84.  
  85. def info(ps):
  86.     global channel
  87.     if len(ps) == 1:
  88.         nstring = ps[0]
  89.     else:
  90.         nstring = ps[1]
  91.     user = getuser(nstring)
  92.     sendmsg(channel, '%s' % user[6])
  93.  
  94. def set(ps):
  95.     global dbc
  96.     dbc.execute(buildquery('UPDATE', '', 'users', ps, '', '', ''))
  97.  
  98. def askball(str):
  99.     return "You asked: %s" % (str)
  100.  
  101. def who(nick):
  102.     return 'Who is %s' % (nick)
  103.  
  104. def locals(nick, chan, ps):
  105.     pstring = buildpstring(ps)
  106.     if pstring.find("location=") == -1:
  107.         dl = getuser(nick)[5]
  108.         if pstring != '':
  109.             pstring += ';'
  110.         pstring += "location=%s" % dl
  111.         location = dl
  112.     else:
  113.         location = pstring.split("location=")[-1].split(";")[0]
  114.     query = buildquery('SELECT', 'users', '', pstring, '`id` ASC', '')
  115.     c = gdbc(query)
  116.     data = c.fetchall()
  117.     rc = c.rowcount
  118.     c.close()
  119.     if rc != 0:
  120.         if rc == 1:
  121.             s = '%s is the only user in %s' % (data[0][6], location)
  122.         else:
  123.             s = '%s users in %s: ' % (rc, location)
  124.             for i in range(len(data)):
  125.                 if i > 0:
  126.                     s += ', '
  127.                 s += data[i][6]
  128.     else:
  129.         rc = rowcount(buildquery('SELECT' , 'users', '', 'location=%s' % location, '`id` ASC', ''))
  130.         if rc != 0:
  131.             s = 'No users in %s match criteria.' % location
  132.         else:
  133.             s = 'No registered user in %s.' % location
  134.     sendmsg(chan, s)
  135.  
  136. def buildpstring(ps):
  137.     counter = 0
  138.     age = ''
  139.     ages = ''
  140.     gender = ''
  141.     seeking = ''
  142.     location = ''
  143.     s = ''
  144.     for param in ps:
  145.         if re.match("^[0-9]+$", param):
  146.             age = param
  147.         elif re.match("^m|f$", param):
  148.             gender = param
  149.         elif re.match("^[\S]+:[\S]+$", param):
  150.             if counter > 0:
  151.                 s += ';'
  152.             kvp = param.split(':')
  153.             key = kvp[0]
  154.             value = kvp[1]
  155.             s += '%s=%s' % (key, value)
  156.             counter += 1
  157.         elif re.match("^4m|4f|4g|4a|4t|4n$", param):
  158.             seeking = param
  159.         elif re.match("^[0-9]*-[0-9]*$", param):
  160.             ages = param
  161.         else:
  162.             if location == '':
  163.                 location = param
  164.             else:
  165.                 location += ' %s' % param
  166.     if age != '':
  167.         if s != '':
  168.             s += ';'
  169.         s += 'age=%s' % age
  170.     if ages != '':
  171.         if s != '':
  172.             s += ';'
  173.         s += 'ages=%s' % ages
  174.     if gender != '':
  175.         if s != '':
  176.             s += ';'
  177.         s += 'gender=%s' % gender
  178.     if seeking != '':
  179.         if s != '':
  180.             s += ';'
  181.         s += 'seeking=%s' % seeking
  182.     if location != '':
  183.         if s != '':
  184.             s += ';'
  185.         s += 'location=%s' % location
  186.     return s
  187.  
  188. def buildquery(qt, tn, st, ps, ob, ls):
  189.     params = ps.split(";")
  190.     counter = 0
  191.     if qt == 'SELECT':
  192.         s = 'SELECT * FROM `' + tn + '`'
  193.         if ps != '':
  194.             s += ' WHERE'
  195.     elif qt == 'UPDATE':
  196.         s = "UPDATE `" + tn + "` SET"
  197.     else:
  198.         s = 'INSERT INTO `' + tn + '` ('
  199.     if ps != '':
  200.         for param in params:
  201.             if counter > 0:
  202.                 if qt == 'UPDATE' or qt == 'INSERT':
  203.                     s += ', '
  204.                 else:
  205.                     s += " && "
  206.             else:
  207.                 s += " "
  208.             vals = param.split("=")
  209.             key = vals[0]
  210.             value = vals[-1]
  211.             if qt == 'SELECT' and key == 'ages':
  212.                 ages = value.split('-')
  213.                 minage = ages[0]
  214.                 maxage = ages[1]
  215.                 if minage != '':
  216.                     s += "`age` >= '" + minage + "'"
  217.                 if maxage != '':
  218.                     if minage != '':
  219.                         s += " && "
  220.                     s += "`age` <= '" + maxage + "'"
  221.             elif qt == 'SELECT' and key == 'seeking':
  222.                 s += "(`seeking` = '%s' || `seeking` = '4a')" % value
  223.             else:
  224.                 s += "`%s`" % key
  225.                 if qt != 'INSERT':
  226.                     s += " = '%s'" % value
  227.             counter += 1
  228.     if qt == 'INSERT':
  229.         s += ') VALUES ('
  230.         vals = param.split("=")
  231.         if counter != 0:
  232.             s += ', '
  233.         value = vals[-1]
  234.         s += "'$s'"
  235.     if qt == 'SELECT' and ob != '':
  236.             s += ' ORDER BY ' + ob
  237.     if ls != '' and qt != 'INSERT':
  238.         s += ' LIMIT ' + ls
  239.     return s
  240.  
  241. def help(ps):
  242.     return "This will be the help"
  243.  
  244. def handle(nick, chan, msg):
  245.     global botnick
  246.     parts = msg.split()
  247.     cmd = parts[0]
  248.     if chan == botnick:
  249.         chan = nick
  250.     if re.match('^dismiss$', cmd) or re.match('^bye$', cmd):
  251.         if nick == "N___":
  252.             sendmsg(chan, '%s: I am dismissed!' % (nick))
  253.             stoprun()
  254.         else:
  255.             sendmsg(chan, '%s: You do not have permission to dismiss me.' % (nick))
  256.     elif re.match('^locals$', cmd):
  257.         locals(nick, chan, parts[1:])
  258.     elif re.match('^cities$', cmd):
  259.         cities(nick, chan)
  260.     elif re.match('^info$', cmd):
  261.         parts[0] = nick
  262.         info(parts)
  263.     elif re.match('^register$', cmd):
  264.         parts[0] = "name:" + nick
  265.         register(parts)
  266.     elif re.match('^set$', cmd):
  267.         parts[0] = "name:" + nick
  268.         sendmsg(chan, set(parts))
  269.     elif re.match('^help$', cmd):
  270.         sendmsg(chan, help(ps))
  271.     elif re.match('[\S\s]+?$', msg):
  272.         sendmsg(chan, askball(msg))
  273.     elif re.match('^ask$', cmd):
  274.         sendmsg(chan, askball(msg))
  275.     else:
  276.         sendmsg(chan, '%s: I do not understand the command "%s"' % (nick, cmd))
  277.  
  278. ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  279. ircsock.connect((server, 6667))
  280. ircsock.send("USER " + botnick + " " + botnick + " " + botnick + " :This is nBot___\n")
  281. ircsock.send("NICK " + botnick + "\n")
  282.  
  283. joinchan(channel)
  284.  
  285. while run == 1:
  286.     ircmsg = ircsock.recv(2048)
  287.     ircmsg = ircmsg.strip('\n\r')
  288.     print(ircmsg)
  289.  
  290.     if ircmsg.find(' PRIVMSG ') != -1 or ircmsg.find(' JOIN ') != -1 or ircmsg.find(' PART ') != -1:
  291.         mtype = ' PRIVMSG '
  292.         nick = ircmsg.split('!')[0][1:]
  293.         host = ircmsg.split('!')[-1].split(' ')[0]
  294.         msg = ''
  295.         if ircmsg.find(' JOIN ') != -1:
  296.             mtype = ' JOIN '
  297.         elif ircmsg.find(' PART ') != -1:
  298.             mtype = ' PART '
  299.         if mtype == ' JOIN ':
  300.             chan = ircmsg.split(' JOIN ')[-1]
  301.             if nick != botnick:
  302.                 sendmsg(channel, 'Welcome %s!' % nick)
  303.         elif mtype == ' PART ':
  304.             chan = ircmsg.split(' PART ')[-1]
  305.         elif mtype == ' PRIVMSG ' and ircmsg.find(':' + botnick + ':') != -1:
  306.             chan = ircmsg.split(mtype)[-1].split(' :')[0]
  307.             msg = ircmsg.split(' PRIVMSG ' + chan + ' :' + botnick + ':')[-1]
  308.             handle(nick, chan, msg)
  309.  
  310.     if ircmsg.find("PING :") != -1:
  311.         ping()
Add Comment
Please, Sign In to add comment