<commit_before>/**
 * ZNC Cmd Notify
 *
 * Fowards a notify to a command of your choice
 *
 * Forked credits:
 * Copyright (c) 2011 <NAME>
 * Licensed under the MIT license
 */

#include "znc.h"
#include "Chan.h"
#include "User.h"
#include "Modules.h"
#include "time.h"

#if (!defined(VERSION_MAJOR) || !defined(VERSION_MINOR) || (VERSION_MAJOR == 0 && VERSION_MINOR < 72))
#error This module needs ZNC 0.072 or newer.
#endif

// Handle versions of ZNC older than 0.090 by disabling the away_only condition
#if VERSION_MAJOR == 0 && VERSION_MINOR >= 90
#define NOTIFO_AWAY
#endif

// Debug output
#define NOTIFO_DEBUG 0

#if NOTIFO_DEBUG
#define PutDebug(s) PutModule(s)
#else
#define PutDebug(s) //s
#endif

class CNotifoMod : public CModule
{
	protected:
		// Application name
		CString app;

		// Too lazy to add CString("\r\n\") everywhere
		CString crlf;

		// User agent to use
		CString user_agent;

		// Time last notification was sent for a given context
		map <CString, unsigned int> last_notification_time;

		// Time of last message by user to a given context
		map <CString, unsigned int> last_reply_time;

		// Time of last activity by user for a given context
		map <CString, unsigned int> last_active_time;

		// Time of last activity by user in any context
		unsigned int idle_time;

		// User object
		CUser *user;

		// Configuration options
		MCString options;
		MCString defaults;

	public:

		MODCONSTRUCTOR(CNotifoMod) {
			app = "ZNC";
			crlf = "\r\n";

			idle_time = time(NULL);
			user_agent = "ZNC To Notifo";

			// Current user
			user = GetUser();

			// Notifo user account and secret
			defaults["username"] = "";
			defaults["secret"] = "";

			// Condition strings
			defaults["channel_conditions"] = "all";
			defaults["query_conditions"] = "all";

			// Notification conditions
#ifdef NOTIFO_AWAY
			defaults["away_only"] = "no";
#endif
			defaults["client_count_less_than"] = "1";
			defaults["highlight"] = "benwa";
			defaults["idle"] = "0";
			defaults["last_active"] = "0";
			defaults["last_notification"] = "0";
			defaults["nick_blacklist"] = "";
			defaults["replied"] = "no";

			// Notification settings
			defaults["message_length"] = "1024";
			defaults["message_uri"] = "";
			
			// Shell command settings
			defaults["cmd"] = "/etc/znc_notify.sh";
		}
		virtual ~CNotifoMod() {}

	protected:

		/**
		 * Shorthand for encoding a string for a URL.
		 *
		 * @param str String to be encoded
		 * @return Encoded string
		 */
		CString urlencode(const CString& str)
		{
			return str.Escape_n(CString::EASCII, CString::EURL);
		}

		/**
		 * Performs string expansion on a set of keywords.
		 * Given an initial string and a dictionary of string replacments,
		 * iterate over the dictionary, expanding keywords one-by-one.
		 *
		 * @param content String contents
		 * @param replace Dictionary of string replacements
		 * @return Result of string replacements
		 */
		CString expand(const CString& content, MCString& replace)
		{
			CString result = content.c_str();

			for(MCString::iterator i = replace.begin(); i != replace.end(); i++)
			{
				result.Replace(i->first, i->second);
			}

			return result;
		}

		/**
		 * Send a message to the currently-configured Notifo account.
		 * Requires (and assumes) that the user has already configured their
		 * username and API secret using the 'set' command.
		 *
		 * @param message Message to be sent to the user
		 * @param title Message title to use
		 * @param context Channel or nick context
		 */
		bool send_message(const CString& message, const CString& title="New Message", const CString& context="*notifo", const CNick& nick=CString("*notifo"))
		{
			// Set the last notification time
			last_notification_time[context] = time(NULL);

			// Shorten message if needed
			unsigned int message_length = options["message_length"].ToUInt();
			CString short_message = message;
			if (message_length > 0)
			{
				short_message = message.Ellipsize(message_length);
			}

			// Generate an ISO8601 date string
			time_t rawtime;
			struct tm * timeinfo;
			time(&rawtime);
			timeinfo = localtime(&rawtime);
			char iso8601 [20];
			strftime(iso8601, 20, "%Y-%m-%d %H:%M:%S", timeinfo);

			CString cmd = message;
			int pid;
			pid = fork();

			if(pid < 0) /* process creation failed */
			{
				return false;
			}
			else if(pid == 0) /* child executes this */
			{
				execl("/bin/bash", "-c", options["cmd"], cmd.c_str(), NULL); /* tells the child to stop executing this code */
				PutIRC("PRIVMSG " + nick.GetNick() + " : Send fail.");
				exit(0);
				/* and start executing pidgin..or w/e you want*/
			}
			else /* parent executes this */
			{  }
			return true;  
		}
<commit_msg>Fix c-string error on cmd pass<commit_after>/**
 * ZNC Cmd Notify
 *
 * Fowards a notify to a command of your choice
 *
 * Forked credits:
 * Copyright (c) 2011 <NAME>
 * Licensed under the MIT license
 */

#include "znc.h"
#include "Chan.h"
#include "User.h"
#include "Modules.h"
#include "time.h"

#if (!defined(VERSION_MAJOR) || !defined(VERSION_MINOR) || (VERSION_MAJOR == 0 && VERSION_MINOR < 72))
#error This module needs ZNC 0.072 or newer.
#endif

// Handle versions of ZNC older than 0.090 by disabling the away_only condition
#if VERSION_MAJOR == 0 && VERSION_MINOR >= 90
#define NOTIFO_AWAY
#endif

// Debug output
#define NOTIFO_DEBUG 0

#if NOTIFO_DEBUG
#define PutDebug(s) PutModule(s)
#else
#define PutDebug(s) //s
#endif

class CNotifoMod : public CModule
{
	protected:
		// Application name
		CString app;

		// Too lazy to add CString("\r\n\") everywhere
		CString crlf;

		// User agent to use
		CString user_agent;

		// Time last notification was sent for a given context
		map <CString, unsigned int> last_notification_time;

		// Time of last message by user to a given context
		map <CString, unsigned int> last_reply_time;

		// Time of last activity by user for a given context
		map <CString, unsigned int> last_active_time;

		// Time of last activity by user in any context
		unsigned int idle_time;

		// User object
		CUser *user;

		// Configuration options
		MCString options;
		MCString defaults;

	public:

		MODCONSTRUCTOR(CNotifoMod) {
			app = "ZNC";
			crlf = "\r\n";

			idle_time = time(NULL);
			user_agent = "ZNC To Notifo";

			// Current user
			user = GetUser();

			// Notifo user account and secret
			defaults["username"] = "";
			defaults["secret"] = "";

			// Condition strings
			defaults["channel_conditions"] = "all";
			defaults["query_conditions"] = "all";

			// Notification conditions
#ifdef NOTIFO_AWAY
			defaults["away_only"] = "no";
#endif
			defaults["client_count_less_than"] = "1";
			defaults["highlight"] = "benwa";
			defaults["idle"] = "0";
			defaults["last_active"] = "0";
			defaults["last_notification"] = "0";
			defaults["nick_blacklist"] = "";
			defaults["replied"] = "no";

			// Notification settings
			defaults["message_length"] = "1024";
			defaults["message_uri"] = "";
			
			// Shell command settings
			defaults["cmd"] = "/etc/znc_notify.sh";
		}
		virtual ~CNotifoMod() {}

	protected:

		/**
		 * Shorthand for encoding a string for a URL.
		 *
		 * @param str String to be encoded
		 * @return Encoded string
		 */
		CString urlencode(const CString& str)
		{
			return str.Escape_n(CString::EASCII, CString::EURL);
		}

		/**
		 * Performs string expansion on a set of keywords.
		 * Given an initial string and a dictionary of string replacments,
		 * iterate over the dictionary, expanding keywords one-by-one.
		 *
		 * @param content String contents
		 * @param replace Dictionary of string replacements
		 * @return Result of string replacements
		 */
		CString expand(const CString& content, MCString& replace)
		{
			CString result = content.c_str();

			for(MCString::iterator i = replace.begin(); i != replace.end(); i++)
			{
				result.Replace(i->first, i->second);
			}

			return result;
		}

		/**
		 * Send a message to the currently-configured Notifo account.
		 * Requires (and assumes) that the user has already configured their
		 * username and API secret using the 'set' command.
		 *
		 * @param message Message to be sent to the user
		 * @param title Message title to use
		 * @param context Channel or nick context
		 */
		bool send_message(const CString& message, const CString& title="New Message", const CString& context="*notifo", const CNick& nick=CString("*notifo"))
		{
			// Set the last notification time
			last_notification_time[context] = time(NULL);

			// Shorten message if needed
			unsigned int message_length = options["message_length"].ToUInt();
			CString short_message = message;
			if (message_length > 0)
			{
				short_message = message.Ellipsize(message_length);
			}

			// Generate an ISO8601 date string
			time_t rawtime;
			struct tm * timeinfo;
			time(&rawtime);
			timeinfo = localtime(&rawtime);
			char iso8601 [20];
			strftime(iso8601, 20, "%Y-%m-%d %H:%M:%S", timeinfo);

			CString cmd = message;
			int pid;
			pid = fork();

			if(pid < 0) /* process creation failed */
			{
				return false;
			}
			else if(pid == 0) /* child executes this */
			{
				execl("/bin/bash", "-c", options["cmd"].c_str(), cmd.c_str(), NULL); /* tells the child to stop executing this code */
				PutIRC("PRIVMSG " + nick.GetNick() + " : Send fail.");
				exit(0);
				/* and start executing pidgin..or w/e you want*/
			}
			else /* parent executes this */
			{  }
			return true;  
		}

{'fraction_non_alphanumeric': 0.12625994694960213, 'fraction_numerical': 0.009973474801061009, 'mean_word_length': 3.2767695099818512, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 19, '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': '3772268', 'n_tokens_mistral': 3548, 'n_tokens_neox': 2967, 'n_words': 955}