<commit_before>#!/usr/bin/env python

import cgitb, cgi, re, socket, os, time, Cookie, sha, sys, urllib
from distutils import sysconfig

cgitb.enable()
last = None

class Pyrssi:
	def __init__(self, sock_path, passwd):
		self.sock_path = sock_path
		self.passwd = <PASSWORD>
		self.year = 60*60*24*365

	def send(self, what):
		try:
			self.cookie = Cookie.SimpleCookie(os.environ["HTTP_COOKIE"])
		except KeyError:
			self.cookie = Cookie.SimpleCookie()
		if 'pyrssi_network' in self.cookie.keys():
			self.network = self.cookie['pyrssi_network'].value
		else:
			self.network = None
		if 'pyrssi_refnum' in self.cookie.keys():
			self.refnum = self.cookie['pyrssi_refnum'].value
		else:
			self.refnum = None
		if 'pyrssi_channel' in self.cookie.keys():
			self.channel = self.cookie['pyrssi_channel'].value
		else:
			self.channel = None
		if isinstance(what, cgi.FieldStorage):
			self.form = what
			try:
				data = []
				if "msg_prefix" in self.form.keys():
					data.append(self.form['msg_prefix'].value)
				data.append(self.form['msg'].value)
				self.__send("".join(data))
			except KeyError:
				pass
		else:
			self.__send(what)

	def receive(self):
		self.__handlecookies()
		self.__dumpheader()
		if "pyrssi_pass" not in self.cookie.keys():
			self.__dumplogin()
		elif "pyrssi_channel" not in self.cookie.keys():
			self.__dumpwindowlist()
			self.__sysinfo()
		else:
			self.__dumpform()
			self.__dumplastlines()
		if "pyrssi_pass" in self.cookie.keys():
			self.__dumplogout()
		self.__dumpfooter()

	def __sysinfo(self):
		print "Powered by Python %s (on %s/%s)" % (sysconfig.get_python_version(), sys.platform, os.name)
		print "<br />"

	def __handlecookies(self):
		# see if we should set cookies
		if "action" in self.form.keys() and self.form['action'].value == "login":
			if sha.sha(self.form['pass'].value).hexdigest() != self.passwd:
				self.__dumpheader()
				self.__dumplogin("wrong password!<br />")
				self.__dumpfooter()
				sys.exit(0)
			self.cookie = Cookie.SimpleCookie()
			self.cookie['pyrssi_pass'] = self.form['pass'].value
			self.cookie['pyrssi_pass']['max-age'] = self.year
			print self.cookie

		if "action" in self.form.keys() and self.form['action'].value == "logout":
			self.cookie['pyrssi_pass']['max-age'] = 0
			print self.cookie
			self.cookie = {}

		if "action" in self.form.keys() and self.form['action'].value == "windowselect":
			self.cookie['pyrssi_channel'] = self.form['window'].value.lower()
			self.cookie['pyrssi_channel']['max-age'] = self.year
			self.cookie['pyrssi_network'] = self.form['network'].value
			self.cookie['pyrssi_network']['max-age'] = self.year
			self.cookie['pyrssi_refnum'] = self.form['refnum'].value
			self.cookie['pyrssi_refnum']['max-age'] = self.year
			print self.cookie
			self.channel = self.form['window'].value.lower()
			self.network = self.form['network'].value
			self.refnum = self.form['refnum'].value

		if "action" in self.form.keys() and self.form['action'].value == "windowlist":
			if 'pyrssi_channel' in self.cookie.keys():
				self.cookie['pyrssi_channel']['max-age'] = 0
				print self.cookie
				del self.cookie['pyrssi_channel']

	def __connect(self):
		self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
		self.sock.connect(self.sock_path)

	def __send(self, what):
		ret = 0
		if len(what):
			if what.startswith("/g "):
				for i in self.__recv("windowlist").split("\n"):
					refnum = re.sub(r'(.*): .*', r'\1', i)
					window = re.sub(r'.*: (.*) \(.*', r'\1', i)
					network = re.sub(r'.* \((.*)\).*', r'\1', i)
					if refnum == what[3:] or window == what[3:].lower():
						self.cookie['pyrssi_channel'] = window.lower()
						self.cookie['pyrssi_channel']['max-age'] = self.year
						self.cookie['pyrssi_network'] = network
						self.cookie['pyrssi_network']['max-age'] = self.year
						self.cookie['pyrssi_refnum'] = refnum
						self.cookie['pyrssi_refnum']['max-age'] = self.year
						print self.cookie
						self.channel = window
						self.network = network.lower()
						self.refnum = refnum
						break
			else:
				self.__connect()
				ret += self.sock.send("switch %s" % self.refnum)
				self.__connect()
				ret += self.sock.send("send %s" % what)
				time.sleep(0.5)
		return ret

	def __recv(self, what):
		ret = []
		self.__connect()
		self.sock.send(what)<commit_msg>pyrssi: /g: handle the case when the window name is lowercase<commit_after>#!/usr/bin/env python

import cgitb, cgi, re, socket, os, time, Cookie, sha, sys, urllib
from distutils import sysconfig

cgitb.enable()
last = None

class Pyrssi:
	def __init__(self, sock_path, passwd):
		self.sock_path = sock_path
		self.passwd = <PASSWORD>
		self.year = 60*60*24*365

	def send(self, what):
		try:
			self.cookie = Cookie.SimpleCookie(os.environ["HTTP_COOKIE"])
		except KeyError:
			self.cookie = Cookie.SimpleCookie()
		if 'pyrssi_network' in self.cookie.keys():
			self.network = self.cookie['pyrssi_network'].value
		else:
			self.network = None
		if 'pyrssi_refnum' in self.cookie.keys():
			self.refnum = self.cookie['pyrssi_refnum'].value
		else:
			self.refnum = None
		if 'pyrssi_channel' in self.cookie.keys():
			self.channel = self.cookie['pyrssi_channel'].value
		else:
			self.channel = None
		if isinstance(what, cgi.FieldStorage):
			self.form = what
			try:
				data = []
				if "msg_prefix" in self.form.keys():
					data.append(self.form['msg_prefix'].value)
				data.append(self.form['msg'].value)
				self.__send("".join(data))
			except KeyError:
				pass
		else:
			self.__send(what)

	def receive(self):
		self.__handlecookies()
		self.__dumpheader()
		if "pyrssi_pass" not in self.cookie.keys():
			self.__dumplogin()
		elif "pyrssi_channel" not in self.cookie.keys():
			self.__dumpwindowlist()
			self.__sysinfo()
		else:
			self.__dumpform()
			self.__dumplastlines()
		if "pyrssi_pass" in self.cookie.keys():
			self.__dumplogout()
		self.__dumpfooter()

	def __sysinfo(self):
		print "Powered by Python %s (on %s/%s)" % (sysconfig.get_python_version(), sys.platform, os.name)
		print "<br />"

	def __handlecookies(self):
		# see if we should set cookies
		if "action" in self.form.keys() and self.form['action'].value == "login":
			if sha.sha(self.form['pass'].value).hexdigest() != self.passwd:
				self.__dumpheader()
				self.__dumplogin("wrong password!<br />")
				self.__dumpfooter()
				sys.exit(0)
			self.cookie = Cookie.SimpleCookie()
			self.cookie['pyrssi_pass'] = self.form['pass'].value
			self.cookie['pyrssi_pass']['max-age'] = self.year
			print self.cookie

		if "action" in self.form.keys() and self.form['action'].value == "logout":
			self.cookie['pyrssi_pass']['max-age'] = 0
			print self.cookie
			self.cookie = {}

		if "action" in self.form.keys() and self.form['action'].value == "windowselect":
			self.cookie['pyrssi_channel'] = self.form['window'].value.lower()
			self.cookie['pyrssi_channel']['max-age'] = self.year
			self.cookie['pyrssi_network'] = self.form['network'].value
			self.cookie['pyrssi_network']['max-age'] = self.year
			self.cookie['pyrssi_refnum'] = self.form['refnum'].value
			self.cookie['pyrssi_refnum']['max-age'] = self.year
			print self.cookie
			self.channel = self.form['window'].value.lower()
			self.network = self.form['network'].value
			self.refnum = self.form['refnum'].value

		if "action" in self.form.keys() and self.form['action'].value == "windowlist":
			if 'pyrssi_channel' in self.cookie.keys():
				self.cookie['pyrssi_channel']['max-age'] = 0
				print self.cookie
				del self.cookie['pyrssi_channel']

	def __connect(self):
		self.sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
		self.sock.connect(self.sock_path)

	def __send(self, what):
		ret = 0
		if len(what):
			if what.startswith("/g "):
				for i in self.__recv("windowlist").split("\n"):
					refnum = re.sub(r'(.*): .*', r'\1', i)
					window = re.sub(r'.*: (.*) \(.*', r'\1', i)
					network = re.sub(r'.* \((.*)\).*', r'\1', i)
					if refnum == what[3:] or window.lower() == what[3:].lower():
						self.cookie['pyrssi_channel'] = window.lower()
						self.cookie['pyrssi_channel']['max-age'] = self.year
						self.cookie['pyrssi_network'] = network
						self.cookie['pyrssi_network']['max-age'] = self.year
						self.cookie['pyrssi_refnum'] = refnum
						self.cookie['pyrssi_refnum']['max-age'] = self.year
						print self.cookie
						self.channel = window
						self.network = network.lower()
						self.refnum = refnum
						break
			else:
				self.__connect()
				ret += self.sock.send("switch %s" % self.refnum)
				self.__connect()
				ret += self.sock.send("send %s" % what)
				time.sleep(0.5)
		return ret

	def __recv(self, what):
		ret = []
		self.__connect()
		self.sock.send(what)
{'fraction_non_alphanumeric': 0.1735632183908046, 'fraction_numerical': 0.004597701149425287, 'mean_word_length': 3.655430711610487, 'pattern_counts': {'":': 8, '<': 9, '<?xml version=': 0, '>': 9, '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': '1434077', 'n_tokens_mistral': 3632, 'n_tokens_neox': 3090, 'n_words': 595}