<commit_before>#!/usr/bin/env python3
import inspect
import os
import imp
import logging
import time
import json
import getpass

from sleekxmpp import ClientXMPP
from sleekxmpp.exceptions import IqError, IqTimeout
from module import XMPPModule
from collections import OrderedDict


import sys
sys.path.append(".")
import module


class Bot(ClientXMPP):

	jid = ""
	rooms = []
	modules = None
	modavail = []
	config = None
	mucusers = {}

	def __init__(self, pwd):
		self.load_config()

		ClientXMPP.__init__(self,self.jid,pwd)

		self.add_event_handler("session_start", self.session_start)
		self.add_event_handler("message", self.message)
		self.add_event_handler("groupchat_message", self.groupmsg)
		self.add_event_handler("groupchat_presence", self.muc_presence)


		self.load_modules()


	def load_config(self):
		with open("config.json","r") as f:
			self.config = json.loads(f.read())

		self.jid = self.config["jid"] if "jid" in self.config.keys() else None # TODO: Make this an error

		self.rooms = []
		if "muc" in self.config.keys():
			for r in self.config["muc"]:
				self.rooms.append((r["room"],r["nick"]))
				self.mucusers[r["room"]] = {}

	def write_config(self):
		with open("config.json", "w") as f:
			f.write(json.dumps(self.config, indent=4, sort_keys=True))

	def load_modules(self):
		# TODO: Put this dir in config?
		self.modules = None
		self.modavail = []
		mods = []
		modules = []
		for f in os.listdir("./modules"):
			if f[-2:] != "py":
				continue
			file,pathname,description = imp.find_module("./modules/" + f[:-3])
			try:
				mods.append(imp.load_module(f[:-3],file,pathname,description))
			except Exception as e:
				print(e)

		for m in mods:
			for name, obj in inspect.getmembers(m):
				if inspect.isclass(obj) and issubclass(obj,XMPPModule) and name != "XMPPModule":
					if name in self.config["modules"]:
						modules.append( (name, obj(self)) )
					self.modavail.append(name)
		self.modules = OrderedDict(sorted(modules, key=lambda x: x[1].priority))

		return self.modules.keys()

	def muc_presence(self, presence):
		if presence['muc']['jid'] == self.jid:
			return
		self.mucusers[presence['muc']['room']][presence['muc']['nick']] = presence['muc']['jid'].bare
		
		for m in self.modules.values():
			try:
				m.handleMucPresence(presence)
			except Exception as e:
				print(e)

	def session_start(self, event):
		self.send_presence()
		for r in self.rooms:
			self.plugin['xep_0045'].joinMUC(r[0], r[1], wait=True)

		# self.get_roster()

	def message(self, msg):
		if msg['type'] not in ('chat', 'normal'):
			return

		for m in self.modules.values():
			try:
				m.recvMsg(msg)
				if m.terminate:
					m.terminate = False
					return
			except Exception as e:
				print(e)

	def groupmsg(self, msg):
		if msg['type'] not in ("groupchat") or msg['mucnick'] in [n[1] for n in self.rooms]:
			return

		for m in self.modules.values():
			try:
				m.recvGroupMsg(msg)
				if m.terminate:
					m.terminate = False
					return
			except Exception as e:
				print(e)

	def sendMsg(self, to, text):
		self.send_message(mto=to, mbody=text, mtype="chat")

	def sendGroupMsg(self, to, text):
		self.send_message(mto=to, mbody=text, mtype="groupchat")

	def reply(self, msg, string):
		if msg['type'] in ("chat", "normal"):
			self.sendMsg(msg['from'].bare, string)
		elif msg['type'] in ("groupchat"):
			self.sendGroupMsg(msg['from'].bare, string)
		else:
			print("Error replying")

	def isadmin(self, jid=None, nick=None, room=None, msg=None):
		if msg:
			if msg['type'] == "groupchat":
				name = self.mucusers[msg['mucroom']][msg["mucnick"]]
			else:
				name = msg["from"].bare
	
		elif jid:
			name = jid
		elif nick and room:
			if room not in self.mucusers.keys():
				print("room does not exist!!")
				raise Exception("Ya dun goofed the isadmin")
			name = self.mucusers[room][nick]
		else:
			print("isadmin check failed")
			raise Exception("Improper use of isadmin")<commit_msg>Prevented NULL name checks<commit_after>#!/usr/bin/env python3
import inspect
import os
import imp
import logging
import time
import json
import getpass

from sleekxmpp import ClientXMPP
from sleekxmpp.exceptions import IqError, IqTimeout
from module import XMPPModule
from collections import OrderedDict


import sys
sys.path.append(".")
import module


class Bot(ClientXMPP):

	jid = ""
	rooms = []
	modules = None
	modavail = []
	config = None
	mucusers = {}

	def __init__(self, pwd):
		self.load_config()

		ClientXMPP.__init__(self,self.jid,pwd)

		self.add_event_handler("session_start", self.session_start)
		self.add_event_handler("message", self.message)
		self.add_event_handler("groupchat_message", self.groupmsg)
		self.add_event_handler("groupchat_presence", self.muc_presence)


		self.load_modules()


	def load_config(self):
		with open("config.json","r") as f:
			self.config = json.loads(f.read())

		self.jid = self.config["jid"] if "jid" in self.config.keys() else None # TODO: Make this an error

		self.rooms = []
		if "muc" in self.config.keys():
			for r in self.config["muc"]:
				self.rooms.append((r["room"],r["nick"]))
				self.mucusers[r["room"]] = {}

	def write_config(self):
		with open("config.json", "w") as f:
			f.write(json.dumps(self.config, indent=4, sort_keys=True))

	def load_modules(self):
		# TODO: Put this dir in config?
		self.modules = None
		self.modavail = []
		mods = []
		modules = []
		for f in os.listdir("./modules"):
			if f[-2:] != "py":
				continue
			file,pathname,description = imp.find_module("./modules/" + f[:-3])
			try:
				mods.append(imp.load_module(f[:-3],file,pathname,description))
			except Exception as e:
				print(e)

		for m in mods:
			for name, obj in inspect.getmembers(m):
				if inspect.isclass(obj) and issubclass(obj,XMPPModule) and name != "XMPPModule":
					if name in self.config["modules"]:
						modules.append( (name, obj(self)) )
					self.modavail.append(name)
		self.modules = OrderedDict(sorted(modules, key=lambda x: x[1].priority))

		return self.modules.keys()

	def muc_presence(self, presence):
		if presence['muc']['jid'] == self.jid:
			return
		self.mucusers[presence['muc']['room']][presence['muc']['nick']] = presence['muc']['jid'].bare
		
		for m in self.modules.values():
			try:
				m.handleMucPresence(presence)
			except Exception as e:
				print(e)

	def session_start(self, event):
		self.send_presence()
		for r in self.rooms:
			self.plugin['xep_0045'].joinMUC(r[0], r[1], wait=True)

		# self.get_roster()

	def message(self, msg):
		if msg['type'] not in ('chat', 'normal'):
			return

		for m in self.modules.values():
			try:
				m.recvMsg(msg)
				if m.terminate:
					m.terminate = False
					return
			except Exception as e:
				print(e)

	def groupmsg(self, msg):
		if msg['type'] not in ("groupchat") or msg['mucnick'] in [n[1] for n in self.rooms]:
			return

		for m in self.modules.values():
			try:
				m.recvGroupMsg(msg)
				if m.terminate:
					m.terminate = False
					return
			except Exception as e:
				print(e)

	def sendMsg(self, to, text):
		self.send_message(mto=to, mbody=text, mtype="chat")

	def sendGroupMsg(self, to, text):
		self.send_message(mto=to, mbody=text, mtype="groupchat")

	def reply(self, msg, string):
		if msg['type'] in ("chat", "normal"):
			self.sendMsg(msg['from'].bare, string)
		elif msg['type'] in ("groupchat"):
			self.sendGroupMsg(msg['from'].bare, string)
		else:
			print("Error replying")

	def isadmin(self, jid=None, nick=None, room=None, msg=None):
		if msg:
			if msg['type'] == "groupchat":
				name = self.mucusers[msg['mucroom']][msg["mucnick"]]
			else:
				name = msg["from"].bare
	
		elif jid:
			name = jid
		elif nick and room:
			if room not in self.mucusers.keys():
				print("room does not exist!!")
				raise Exception("Ya dun goofed the isadmin")
			name = self.mucusers[room][nick]
		else:
			print("isadmin check failed")
			raise Exception("Improper use of isadmin")

		if name == '':
			print("Received NULL name...")
			return False

{'fraction_non_alphanumeric': 0.15415515942756716, 'fraction_numerical': 0.003263871453678132, 'mean_word_length': 3.8937346437346436, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3530713', 'n_tokens_mistral': 3303, 'n_tokens_neox': 2828, 'n_words': 691}