import weechat, pynotify, string

WEECHAT_ICON = "/usr/share/pixmaps/gnome-irc.png"

weechat.register("wee-notifier", "0.1.0", "", "A notification system for weechat using pynotify")
weechat.add_message_handler("privmsg", "handle_message")

class WeeNotification:
	def show_notification(self,chan,message):
		pynotify.init("wee-notifier")
		wn = pynotify.Notification(chan, message, WEECHAT_ICON)
		wn.set_urgency(pynotify.URGENCY_NORMAL)
		wn.set_timeout(pynotify.EXPIRES_NEVER)
		wn.show()
	def message_irc(self,message):
		string = ''
		msg = message.split(":")
		for i in range(len(msg)):
			if i > 0:
				string += msg[i]+' '
		return string

def handle_message(server, args):
	window         = WeeNotification()
	string         = args.split('!')
	nick           = string[0].replace(':','')
	nick_say       = string[1].split("PRIVMSG")
	current_server = weechat.get_info("server")
	current_chan   = weechat.get_info("channel")
	chan           = nick_say[1].split(":")[0].strip()
	message        = window.message_irc(nick_say[1])
	#nicknames - defined in ~/.weechat/weechat.rc
	NICKNAME1 = weechat.get_server_info()[server]['nick1']
	NICKNAME2 = weechat.get_server_info()[server]['nick2']
	NICKNAME3 = weechat.get_server_info()[server]['nick3']
	if (current_server != server) and (current_chan != chan):
		if (NICKNAME1 == chan) or (NICKNAME2 == chan) or (NICKNAME3 == chan):
			window.show_notification("Messaggio privato da "+ nick, message)
		elif (NICKNAME1 or NICKNAME2 or NICKNAME3) in message:
			if "ACTION" in message:
				window.show_notification("<i>"+message.replace('ACTION','')+"</i>" ,"<b>"+ nick +" ("+chan+")</b>")
				weechat.prnt(message)
			else:
				window.show_notification(chan, "<b>"+nick+"</b>: "+message)
	return weechat.PLUGIN_RC_OK
