tBKwtWS 6 anni fa
parent
commit
5381b601ba

+ 11 - 7
rotbot/common/queries.py

@@ -137,6 +137,10 @@ def get_channel_setting_game_commands(self, channel_id):
 def get_autojoin_channels(self):
 def get_autojoin_channels(self):
     return self.db.all('SELECT name FROM rotbot_channel WHERE network_id=%(network_id)s AND autojoin=True', network_id=self.network.id)
     return self.db.all('SELECT name FROM rotbot_channel WHERE network_id=%(network_id)s AND autojoin=True', network_id=self.network.id)
 
 
+def save_channel_key(self, channel_name, channel_key):
+    channel_id = get_channel_id(self, channel_name)     # Inperfect, make a index of the channel_name field.
+    self.db.run('UPDATE key FROM rotbot_channel WHERE channel_id=%(channel_id)', channel_id=channel_id)
+
 #def update_channel_last_event(self, channel_id, event_type, event_content):
 #def update_channel_last_event(self, channel_id, event_type, event_content):
 #    self.db.run('UPDATE rotbot_channel SET last_event_type=%(last_event_type)s, last_event_content=%(last_event_content)s WHERE channel.id=%(channel_id)s', last_event_type=event_type, last_event_content=event_content, channel_id=channel_id)
 #    self.db.run('UPDATE rotbot_channel SET last_event_type=%(last_event_type)s, last_event_content=%(last_event_content)s WHERE channel.id=%(channel_id)s', last_event_type=event_type, last_event_content=event_content, channel_id=channel_id)
 
 
@@ -208,20 +212,20 @@ def add_curseword(self, word, user_id):
 
 
 # Statistics
 # Statistics
 def update_message_statistics(self, type, channel_id, user_id):
 def update_message_statistics(self, type, channel_id, user_id):
-    if not self.db.one('SELECT id FROM rotbot_' + type + ' WHERE network_id=%(network_id)s AND channel_id=%(channel_id)s AND user_id=%(user_id)s', network_id=self.network.id, channel_id=channel_id ,user_id=user_id):  # Not on record.
+    if not self.db.one('SELECT id FROM rotbot_' + type + ' WHERE channel_id=%(channel_id)s AND user_id=%(user_id)s', channel_id=channel_id ,user_id=user_id):  # Not on record.
         self.db.run('INSERT INTO rotbot_' + type + ' (network_id, channel_id, user_id, amount) VALUES (%(network_id)s, %(channel_id)s, %(user_id)s, 1)', network_id=self.network.id, channel_id=channel_id ,user_id=user_id)   # Create record.
         self.db.run('INSERT INTO rotbot_' + type + ' (network_id, channel_id, user_id, amount) VALUES (%(network_id)s, %(channel_id)s, %(user_id)s, 1)', network_id=self.network.id, channel_id=channel_id ,user_id=user_id)   # Create record.
     else:   # On record.
     else:   # On record.
-        self.db.run('UPDATE rotbot_' + type + ' SET amount = amount +1 WHERE network_id=%(network_id)s AND channel_id=%(channel_id)s AND user_id=%(user_id)s', network_id=self.network.id, channel_id=channel_id ,user_id=user_id)  # Increment record.
+        self.db.run('UPDATE rotbot_' + type + ' SET amount = amount +1 WHERE channel_id=%(channel_id)s AND user_id=%(user_id)s', channel_id=channel_id ,user_id=user_id)  # Increment record.
 
 
 def increment_join(self, channel_id, user_id):
 def increment_join(self, channel_id, user_id):
-    if not self.db.one('SELECT id FROM rotbot_join WHERE network_id=%(network_id)s AND channel_id=%(channel_id)s AND user_id=%(user_id)s', network_id=self.network.id, channel_id=channel_id, user_id=user_id):   # No record yet
+    if not self.db.one('SELECT id FROM rotbot_join WHERE channel_id=%(channel_id)s AND user_id=%(user_id)s', channel_id=channel_id, user_id=user_id):   # No record yet
         self.db.run('INSERT INTO rotbot_join (network_id, channel_id, user_id, amount) VALUES (%(network_id)s, %(channel_id)s, %(user_id)s, 1)', network_id=self.network.id, channel_id=channel_id, user_id=user_id)   # Create record.
         self.db.run('INSERT INTO rotbot_join (network_id, channel_id, user_id, amount) VALUES (%(network_id)s, %(channel_id)s, %(user_id)s, 1)', network_id=self.network.id, channel_id=channel_id, user_id=user_id)   # Create record.
     else:
     else:
-        self.db.run('UPDATE rotbot_join SET amount = amount + 1 WHERE network_id=%(network_id)s AND channel_id=%(channel_id)s AND user_id=%(user_id)s', network_id=self.network.id, channel_id=channel_id, user_id=user_id)   # Update existing record.
+        self.db.run('UPDATE rotbot_join SET amount = amount + 1 WHERE channel_id=%(channel_id)s AND user_id=%(user_id)s', channel_id=channel_id, user_id=user_id)   # Update existing record.
 
 
 def increment_kick(self, channel_id, kicker_id, kicked_id):
 def increment_kick(self, channel_id, kicker_id, kicked_id):
-    if not self.db.one('SELECT id FROM rotbot_kick WHERE channel_id=%(channel_id)s AND kicker_id=%(kicker_id)s AND kicked_id=%(kicked_id)s', network_id=self.network.id, channel_id=channel_id, kicker_id=kicker_id, kicked_id=kicked_id): # No records for kicker channel combination.
-        self.db.run('INSERT INTO rotbot_kick (channel_id, kicker_id, kicked_id, amount) VALUES (%(channel_id)s, %(kicker_id)s, %(kicked_id)s, 1)', channel_id=channel_id, kicker_id=kicker_id, kicked_id=kicked_id)  # Create record.
+    if not self.db.one('SELECT id FROM rotbot_kick WHERE channel_id=%(channel_id)s AND kicker_id=%(kicker_id)s AND kicked_id=%(kicked_id)s', channel_id=channel_id, kicker_id=kicker_id, kicked_id=kicked_id): # No records for kicker channel combination.
+        self.db.run('INSERT INTO rotbot_kick (network_id, channel_id, kicker_id, kicked_id, amount) VALUES (%(network_id)s, %(channel_id)s, %(kicker_id)s, %(kicked_id)s, 1)', network_id=self.network.id, channel_id=channel_id, kicker_id=kicker_id, kicked_id=kicked_id)  # Create record.
     else:
     else:
         self.db.run('UPDATE rotbot_kick SET amount = amount + 1 WHERE channel_id=%(channel_id)s AND kicker_id=%(kicker_id)s AND kicked_id=%(kicked_id)s', channel_id=channel_id, kicker_id=kicker_id, kicked_id=kicked_id) # Update record.
         self.db.run('UPDATE rotbot_kick SET amount = amount + 1 WHERE channel_id=%(channel_id)s AND kicker_id=%(kicker_id)s AND kicked_id=%(kicked_id)s', channel_id=channel_id, kicker_id=kicker_id, kicked_id=kicked_id) # Update record.
 
 
@@ -242,7 +246,7 @@ def punish_user(self, user_id, coin, karma):
     self.db.run('UPDATE website_users SET coin=coin-%(coin)s, karma_correction=karma_correction-%(karma)s WHERE id=%(user_id)s', coin=coin, karma=karma, user_id=user_id)
     self.db.run('UPDATE website_users SET coin=coin-%(coin)s, karma_correction=karma_correction-%(karma)s WHERE id=%(user_id)s', coin=coin, karma=karma, user_id=user_id)
 
 
 def payday(self, coin):
 def payday(self, coin):
-    self.db.run('UPDATE rotbot_user SET coin=coin+%(coin)s WHERE level>0', coin=coin)
+    self.db.run('UPDATE rotbot_user SET coin=coin+%(coin)s WHERE network_id=%(network_id)s AND level>0', coin=coin, network_id=self.network.id)
 
 
 def cointransfer(self, sender_id, receiver_id, coin):
 def cointransfer(self, sender_id, receiver_id, coin):
     self.db.run('UPDATE rotbot_user SET coin=coin-%(coin)s, coin_spent=coin_spent+%(coin)s, coin_given=coin_given+%(coin)s, ap_spent=ap_spent+1 WHERE id=%(sender_id)s', coin=coin, sender_id=sender_id)
     self.db.run('UPDATE rotbot_user SET coin=coin-%(coin)s, coin_spent=coin_spent+%(coin)s, coin_given=coin_given+%(coin)s, ap_spent=ap_spent+1 WHERE id=%(sender_id)s', coin=coin, sender_id=sender_id)

+ 8 - 2
rotbot/common/userstatus.py

@@ -1,10 +1,16 @@
+def is_owner(self, user_name, channel_name):
+    if channel_name not in self.channels:
+        return False
+    if self.channels[channel_name].is_owner(user_name):
+        return True
+    return False
+
 def atleast_admin(self, user_name, channel_name):
 def atleast_admin(self, user_name, channel_name):
     if channel_name not in self.channels:
     if channel_name not in self.channels:
         return False
         return False
     if self.channels[channel_name].is_owner(user_name) or self.channels[channel_name].is_admin(user_name):
     if self.channels[channel_name].is_owner(user_name) or self.channels[channel_name].is_admin(user_name):
         return True
         return True
-    else:
-        return False
+    return False
 
 
 def atleast_oper(self, user_name, channel_name):
 def atleast_oper(self, user_name, channel_name):
     if channel_name not in self.channels:
     if channel_name not in self.channels:

+ 0 - 2
rotbot/events/common.py

@@ -4,13 +4,11 @@ from common import queries, userstatus, log
 
 
 class Inform():
 class Inform():
     def owners(self, connection, message):
     def owners(self, connection, message):
-        log.notice('Message: %s' % (message))
         if self.network.home_channel in self.channels:
         if self.network.home_channel in self.channels:
             for owner in self.channels[self.network.home_channel].owners():
             for owner in self.channels[self.network.home_channel].owners():
                 connection.privmsg(owner, message)
                 connection.privmsg(owner, message)
 
 
     def notice_owners(self, connection, message):
     def notice_owners(self, connection, message):
-        log.notice('Message: %s' % (message))
         if self.network.home_channel in self.channels:
         if self.network.home_channel in self.channels:
             for owner in self.channels[self.network.home_channel].owners():
             for owner in self.channels[self.network.home_channel].owners():
                 connection.notice(owner, message)
                 connection.notice(owner, message)

+ 19 - 12
rotbot/events/on_join.py

@@ -1,4 +1,4 @@
-from common import log, queries, font
+from common import log, queries, font, userstatus
 
 
 def process_event(self, connection, event):
 def process_event(self, connection, event):
     log.info(event) # Log to console.
     log.info(event) # Log to console.
@@ -8,26 +8,33 @@ def process_event(self, connection, event):
     user = queries.create_or_get_and_update_last_event(self, 'user', 'cj', channel_name=event.target, user_name=event.source.nick)
     user = queries.create_or_get_and_update_last_event(self, 'user', 'cj', channel_name=event.target, user_name=event.source.nick)
     queries.increment_join(self, channel.id, user.id)
     queries.increment_join(self, channel.id, user.id)
 
 
+    # Stop if the bot joined the channel.
+    if event.source == self.connection.get_nickname():
+        return
+
+    # Promote user if usermask is in database.
     owners = queries.get_owners(self)
     owners = queries.get_owners(self)
+    print('Event source: ', event.source)
     if event.source in owners:
     if event.source in owners:
-        connection.mode(channel, "+aohv %s %s %s %s" % event.source.nick, event.source.nick, event.source.nick, event.source.nick)
+        connection.mode(event.target, "+aohv %s %s %s %s" % (event.source.nick, event.source.nick, event.source.nick, event.source.nick))
         curse = queries.random_curse(self)
         curse = queries.random_curse(self)
         if curse:
         if curse:
-            connection.privmsg(channel, 'Welcome back %s%s%s, you %s.' % (font.red, event.source, font.reset, curse))
+            connection.privmsg(event.target, 'Welcome back %s%s%s, you %s.' % (font.red, event.source, font.reset, curse))
+
+    # Promote user if owner in home channel.
+    if userstatus.is_owner(self, event.source.nick, self.network.home_channel):
+        connection.mode(event.target, '+aohv %s %s %s %s' % (event.source.nick, event.source.nick, event.source.nick, event.source.nick))
+
 
 
-    # if event.source.nick == connection.get_nickname():  # The bot joined a channel.
+    if event.source.nick == connection.get_nickname():  # The bot joined a channel.
         # connection.who(self.homechannel)    # Get whoreplies for users of homechannel.
         # connection.who(self.homechannel)    # Get whoreplies for users of homechannel.
-        # if self.channels[event.target].has_key():
-        #     if event.target in self.channelkeys:    # New key used to join channel.
-        #         self.db.run("UPDATE channels SET key='" + self.channelkeys[event.target] + "' WHERE name='" + event.target + "' AND network='" + self.network + "'")    # Save new key to DB.
-        #         del self.channelkeys[event.target]  # Delete entry.
+        if self.channels[event.target].has_key():   # Passworded channel.
+            if event.target in self.channelkeys:    # New key used to join channel.
+                self.db.run('UPDATE rotbot_channels SET key=%(key)s WHERE id=%(id)s', key=self.channelkeys[event.target], channel.id)    # Save new key to DB.
         # if event.target == self.homechannel:    # Home channel.
         # if event.target == self.homechannel:    # Home channel.
         #     connection.who(connection.get_nickname())   # get whoreply to add bot to protectees.
         #     connection.who(connection.get_nickname())   # get whoreply to add bot to protectees.
-    #    return  # Do not greet myself.
+       # return  # Do not greet myself.
 
 
-    # # Promote owners.
-    # if event.source.nick in self.channels[self.network.home_channel].owners():
-    #     connection.mode(event.target, '+vhoa %s %s %s %s' % (event.source.nick, event.source.nick, event.source.nick, event.source.nick))
 
 
     # # Stop if greeting is not wanted.
     # # Stop if greeting is not wanted.
     # joingreeting = self.db.one("SELECT join_greeting FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'")
     # joingreeting = self.db.one("SELECT join_greeting FROM channels WHERE name='" + event.target + "' AND network='" + self.network + "'")

+ 21 - 14
rotbot/events/on_privnotice.py

@@ -1,3 +1,4 @@
+import asyncio, time
 import commands.public, commands.admin, commands.games, commands.statistics
 import commands.public, commands.admin, commands.games, commands.statistics
 from common import log, queries, font
 from common import log, queries, font
 from events.common import Inform
 from events.common import Inform
@@ -21,9 +22,9 @@ def process_event(self, connection, event):
 
 
     if event.source.nick == connection.get_nickname():  # Message came from myself.
     if event.source.nick == connection.get_nickname():  # Message came from myself.
         return
         return
-    elif event.source.nick == "NickServ":
+    elif event.source.nick == "NickServ":   # Message from NickServ.
         if event.arguments[0].startswith("This nickname is registered"):
         if event.arguments[0].startswith("This nickname is registered"):
-            connection.privmsg('NickServ', 'identify %s %s' % (self.network.nickname, self.network.password)) # Identify with NickServ.
+            connection.privmsg('NickServ', 'identify %s %s' % (self.network.nickname, self.network.password)) # Identify with NickServ. Also doing it on_welcome, but if there is a netsplit or so and there is no welcome, but a need to login, this is needed.
         if event.arguments[0].startswith("You are already identified."):
         if event.arguments[0].startswith("You are already identified."):
             return
             return
 
 
@@ -34,17 +35,23 @@ def process_event(self, connection, event):
             log.info('Registerring with NickServ.')
             log.info('Registerring with NickServ.')
         if event.arguments[0].startswith('Nickname ') and event.arguments[0].endswith(' registered.'):
         if event.arguments[0].startswith('Nickname ') and event.arguments[0].endswith(' registered.'):
             Inform.home_channel(seld, connection, 'Registerred nickname %s%s%s with NickServ.' % font.red, self.network.nickname, font.reset)
             Inform.home_channel(seld, connection, 'Registerred nickname %s%s%s with NickServ.' % font.red, self.network.nickname, font.reset)
-    # elif event.source.nick == "ChanServ":
-    #     if event.arguments[0].startswith("Key for channel ") and len(event.arguments[0]) > 5:   # Received channel key.
-    #         self.channelkeys[event.arguments[0].split(' ')[3]] = event.arguments[0].split(' ')[5][:-1]
-    #         connection.join(event.arguments[0].split(' ')[3], event.arguments[0].split(' ')[5][:-1])
-    #         Inform.owners(self, connection, "Received " + red + event.arguments[0].split(" ")[3] + reset + " key: " + event.arguments.split(" ")[5][:-1])
-    #     if event.arguments[0] == "Password authentication required for that command.":  # Not authenticated with NisckServ.
-    #         Inform.notice_owners(self, connection, "Not authenticated with NickServ. See " + blue + self.helpchar + "recovernick " + reset + "and " + blue + self.helpchar + "registernick" + reset + ".")
-    #         return
-    #     if event.arguments[0].startswith("You have been unbanned from ") or event.arguments[0].endswith(" autokick list is empty.") or event.arguments[0].startswith("You are already in ") or event.arguments[0] == "Syntax: UNBAN channel [nick]" or event.arguments[0] == "/msg ChanServ HELP UNBAN for more information":
-    #         return
-    #     if event.arguments[0].startswith("Channel ") and event.arguments[0].endswith(" has no key."):
-    #         return
+        if event.arguments[0].startswith('You must have been using this nick for at least 30 seconds to register.'):
+            asyncio.run(register_later(self.connection, 31))
+    elif event.source.nick == "ChanServ":
+        if event.arguments[0].startswith("Key for channel ") and len(event.arguments[0]) > 5:   # Received channel key.
+            queries.save_channel_key(self, event.arguments[0].split(' ')[3], event.arguments[0].split(' ')[5][:-1])
+            connection.join(event.arguments[0].split(' ')[3], event.arguments[0].split(' ')[5][:-1])
+            Inform.owners(self, connection, "Received " + red + event.arguments[0].split(" ")[3] + reset + " key: " + event.arguments.split(" ")[5][:-1])
+        if event.arguments[0] == "Password authentication required for that command.":  # Not authenticated with NisckServ.
+            Inform.notice_owners(self, connection, "Not authenticated with NickServ. See " + blue + self.helpchar + "recovernick " + reset + "and " + blue + self.helpchar + "registernick" + reset + ".")
+            return
+        if event.arguments[0].startswith("You have been unbanned from ") or event.arguments[0].endswith(" autokick list is empty.") or event.arguments[0].startswith("You are already in ") or event.arguments[0] == "Syntax: UNBAN channel [nick]" or event.arguments[0] == "/msg ChanServ HELP UNBAN for more information":
+            return
+        # if event.arguments[0].startswith("Channel ") and event.arguments[0].endswith(" has no key."):
+        #     return
     if event.source.nick != "Global":
     if event.source.nick != "Global":
         Inform.notice_owners(self, connection, 'Notice from %s %s %s %s: %s %s' % (font.red, font.red, event.source.nick, font.grey, font.reset, event.arguments[0]))
         Inform.notice_owners(self, connection, 'Notice from %s %s %s %s: %s %s' % (font.red, font.red, event.source.nick, font.grey, font.reset, event.arguments[0]))
+
+async def say_after(connection, seconds):
+    await asyncio.sleep(seconds)
+    connection.privmsg('NickServ', 'identify %s %s' % (self.network.nickname, self.network.password))