/* X-Chat * Copyright (C) 1998 Peter Zelezny. * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA */ #include #include #include #include #include #include #include #include #ifdef WIN32 #include #else #include #include #endif #include "hexchat.h" #include "cfgfiles.h" #include "chanopt.h" #include "plugin.h" #include "fe.h" #include "server.h" #include "util.h" #include "outbound.h" #include "hexchatc.h" #include "text.h" #include "typedef.h" #ifdef WIN32 #include #endif struct pevt_stage1 { int len; char *data; struct pevt_stage1 *next; }; static void mkdir_p (char *filename); static char *log_create_filename (char *channame); static char * scrollback_get_filename (session *sess) { char *net, *chan, *buf; net = server_get_network (sess->server, FALSE); if (!net) return NULL; buf = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "scrollback" G_DIR_SEPARATOR_S "%s" G_DIR_SEPARATOR_S "%s.txt", get_xdir (), net, ""); mkdir_p (buf); g_free (buf); chan = log_create_filename (sess->channel); buf = g_strdup_printf ("%s" G_DIR_SEPARATOR_S "scrollback" G_DIR_SEPARATOR_S "%s" G_DIR_SEPARATOR_S "%s.txt", get_xdir (), net, chan); free (chan); return buf; } #if 0 static void scrollback_unlock (session *sess) { char buf[1024]; if (scrollback_get_filename (sess, buf, sizeof (buf) - 6) == NULL) return; strcat (buf, ".lock"); unlink (buf); } static gboolean scrollback_lock (session *sess) { char buf[1024]; int fh; if (scrollback_get_filename (sess, buf, sizeof (buf) - 6) == NULL) return FALSE; strcat (buf, ".lock"); if (access (buf, F_OK) == 0) return FALSE; /* can't get lock */ fh = open (buf, O_CREAT | O_TRUNC | O_APPEND | O_WRONLY, 0644); if (fh == -1) return FALSE; return TRUE; } #endif void scrollback_close (session *sess) { if (sess->scrollfd != -1) { close (sess->scrollfd); sess->scrollfd = -1; } } /* shrink the file to roughly prefs.hex_text_max_lines */ static void scrollback_shrink (session *sess) { char *file; char *buf; int fh; int lines; int line; gsize len; char *p; scrollback_close (sess); sess->scrollwritten = 0; lines = 0; if ((file = scrollback_get_filename (sess)) == NULL) { g_free (file); return; } if (!g_file_get_contents (file, &buf, &len, NULL)) { g_free (file); return; } /* count all lines */ p = buf; while (p != buf + len) { if (*p == '\n') lines++; p++; } fh = g_open (file, O_CREAT | O_TRUNC | O_APPEND | O_WRONLY, 0644); g_free (file); if (fh == -1) { free (buf); return; } line = 0; p = buf; while (p != buf + len) { if (*p == '\n') { line++; if (line >= lines - prefs.hex_text_max_lines && p + 1 != buf + len) { p++; write (fh, p, len - (p - buf)); break; } } p++; } close (fh); free (buf); } static void scrollback_save (session *sess, char *text) { char *buf; time_t stamp; int len; if (sess->type == SESS_SERVER) return; if (sess->text_scrollback == SET_DEFAULT) { if (!prefs.hex_text_replay) return; } else { if (sess->text_scrollback != SET_ON) return; } if (sess->scrollfd == -1) { if ((buf = scrollback_get_filename (sess)) == NULL) return; sess->scrollfd = g_open (buf, O_CREAT | O_APPEND | O_WRONLY, 0644); g_free (buf); if (sess->scrollfd == -1) return; } stamp = time (0); if (sizeof (stamp) == 4) /* gcc will optimize one of these out */ buf = g_strdup_printf ("T %d ", (int) stamp); else buf = g_strdup_printf ("T %" G_GINT64_FORMAT " ", (gint64)stamp); write (sess->scrollfd, buf, strlen (buf)); g_free (buf); len = strlen (text); write (sess->scrollfd, text, len); if (len && text[len - 1] != '\n') write (sess->scrollfd, "\n", 1); sess->scrollwritten++; if ((sess->scrollwritten * 2 > prefs.hex_text_max_lines && prefs.hex_text_max_lines > 0) || sess->scrollwritten > 32000) scrollback_shrink (sess); } void scrollback_load (session *sess) { char *buf; char *text; time_t stamp; int lines; GIOChannel *io; GError *file_error = NULL; GError *io_err = NULL; if (sess->text_scrollback == SET_DEFAULT) { if (!prefs.hex_text_replay) return; } else { if (sess->text_scrollback != SET_ON) return; } if ((buf = scrollback_get_filename (sess)) == NULL) return; io = g_io_channel_new_file (buf, "r", &file_error); g_free (buf); if (!io) return; lines = 0; while (1) { gsize n_bytes; GIOStatus io_status; io_status = g_io_channel_read_line (io, &buf, &n_bytes, NULL, &io_err); if (io_status == G_IO_STATUS_NORMAL) { char *buf_tmp; n_bytes--; buf_tmp = buf; buf = g_strndup (buf_tmp, n_bytes); g_free (buf_tmp); if (buf[0] == 'T') { if (sizeof (time_t) == 4) stamp = strtoul (buf + 2, NULL, 10); else stamp = strtoull (buf + 2, NULL, 10); /* in case time_t is 64 bits */ text = strchr (buf + 3, ' '); if (text) { if (prefs.hex_text_stripcolor_replay) { text = strip_color (text + 1, -1, STRIP_COLOR); } fe_print_text (sess, text, stamp); if (prefs.hex_text_stripcolor_replay) { g_free (text); } } lines++; } g_free (buf); } else break; } g_io_channel_unref (io); sess->scrollwritten = lines; if (lines) { text = ctime (&stamp); text[24] = 0; /* get rid of the \n */ buf = g_strdup_printf ("\n*\t%s %s\n\n", _("Loaded log from"), text); fe_print_text (sess, buf, 0); g_free (buf); /*EMIT_SIGNAL (XP_TE_GENMSG, sess, "*", buf, NULL, NULL, NULL, 0);*/ } } void log_close (session *sess) { char obuf[512]; time_t currenttime; if (sess->logfd != -1) { currenttime = time (NULL); write (sess->logfd, obuf, snprintf (obuf, sizeof (obuf) - 1, _("**** ENDING LOGGING AT %s\n"), ctime (¤ttime))); close (sess->logfd); sess->logfd = -1; } } static void mkdir_p (char *filename) { char *dirname; dirname = g_path_get_dirname (filename); g_mkdir_with_parents (dirname, 0700); g_free (dirname); } static char * log_create_filename (char *channame) { char *tmp, *ret; int mbl; ret = tmp = strdup (channame); while (*tmp) { mbl = g_utf8_skip[((unsigned char *)tmp)[0]]; if (mbl == 1) { #ifndef WIN32 *tmp = rfc_tolower (*tmp); if (*tmp == '/') #else /* win32 can't handle filenames with \|/><:"*? characters */ if (*tmp == '\\' || *tmp == '|' || *tmp == '/' || *tmp == '>' || *tmp == '<' || *tmp == ':' || *tmp == '\"' || *tmp == '*' || *tmp == '?') #endif *tmp = '_'; } tmp += mbl; } return ret; } /* like strcpy, but % turns into %% */ static char * log_escape_strcpy (char *dest, char *src, char *end) { while (*src) { *dest = *src; if (dest + 1 == end) break; dest++; src++; if (*src == '%') { if (dest + 1 == end) break; dest[0] = '%'; dest++; } } dest[0] = 0; return dest - 1; } /* substitutes %c %n %s into buffer */ static void log_insert_vars (char *buf, int bufsize, char *fmt, char *c, char *n, char *s) { char *end = buf + bufsize; while (1) { switch (fmt[0]) { case 0: buf[0] = 0; return; case '%': fmt++; switch (fmt[0]) { case 'c': buf = log_escape_strcpy (buf, c, end); break; case 'n': buf = log_escape_strcpy (buf, n, end); break; case 's': buf = log_escape_strcpy (buf, s, end); break; default: buf[0] = '%'; buf++; buf[0] = fmt[0]; break; } break; default: buf[0] = fmt[0]; } fmt++; buf++; /* doesn't fit? */ if (buf == end) { buf[-1] = 0; return; } } } static int logmask_is_fullpath () { /* Check if final path/filename is absolute or relative. * If one uses log mask variables, such as "%c/...", %c will be empty upon * connecting since there's no channel name yet, so we have to make sure * we won't try to write to the FS root. On Windows we can be sure it's * full path if the 2nd character is a colon since Windows doesn't allow * colons in filenames. */ #ifdef WIN32 /* Treat it as full path if it * - starts with '\' which denotes the root directory of the current drive letter * - starts with a drive letter and followed by ':' */ if (prefs.hex_irc_logmask[0] == '\\' || (((prefs.hex_irc_logmask[0] >= 'A' && prefs.hex_irc_logmask[0] <= 'Z') || (prefs.hex_irc_logmask[0] >= 'a' && prefs.hex_irc_logmask[0] <= 'z')) && prefs.hex_irc_logmask[1] == ':')) #else if (prefs.hex_irc_logmask[0] == '/') #endif { return 1; } else { return 0; } } static char * log_create_pathname (char *servname, char *channame, char *netname) { char fname[384]; char fnametime[384]; struct tm *tm; time_t now; if (!netname) { netname = "NETWORK"; } /* first, everything is in UTF-8 */ if (!rfc_casecmp (channame, servname)) { channame = strdup ("server"); } else { channame = log_create_filename (channame); } log_insert_vars (fname, sizeof (fname), prefs.hex_irc_logmask, channame, netname, servname); free (channame); /* insert time/date */ now = time (NULL); tm = localtime (&now); strftime (fnametime, sizeof (fnametime), fname, tm); /* create final path/filename */ if (logmask_is_fullpath ()) { snprintf (fname, sizeof (fname), "%s", fnametime); } else /* relative path */ { snprintf (fname, sizeof (fname), "%s" G_DIR_SEPARATOR_S "logs" G_DIR_SEPARATOR_S "%s", get_xdir (), fnametime); } /* create all the subdirectories */ mkdir_p (fname); return g_strdup(fname); } static int log_open_file (char *servname, char *channame, char *netname) { char buf[512]; int fd; char *file; time_t currenttime; file = log_create_pathname (servname, channame, netname); if (!file) return -1; #ifdef WIN32 fd = g_open (file, O_CREAT | O_APPEND | O_WRONLY, S_IREAD|S_IWRITE); #else fd = g_open (file, O_CREAT | O_APPEND | O_WRONLY, 0644); #endif g_free (file); if (fd == -1) return -1; currenttime = time (NULL); write (fd, buf, snprintf (buf, sizeof (buf), _("**** BEGIN LOGGING AT %s\n"), ctime (¤ttime))); return fd; } static void log_open (session *sess) { static gboolean log_error = FALSE; log_close (sess); sess->logfd = log_open_file (sess->server->servername, sess->channel, server_get_network (sess->server, FALSE)); if (!log_error && sess->logfd == -1) { char *message; message = g_strdup_printf (_("* Can't open log file(s) for writing. Check the\npermissions on %s"), log_create_pathname (sess->server->servername, sess->channel, server_get_network (sess->server, FALSE))); fe_message (message, FE_MSG_WAIT | FE_MSG_ERROR); g_free (message); log_error = TRUE; } } void log_open_or_close (session *sess) { if (sess->text_logging == SET_DEFAULT) { if (prefs.hex_irc_logging) log_open (sess); else log_close (sess); } else { if (sess->text_logging) log_open (sess); else log_close (sess); } } int get_stamp_str (char *fmt, time_t tim, char **ret) { char *loc = NULL; char dest[128]; gsize len; /* strftime wants the format string in LOCALE! */ if (!prefs.utf8_locale) { const gchar *charset; g_get_charset (&charset); loc = g_convert_with_fallback (fmt, -1, charset, "UTF-8", "?", 0, 0, 0); if (loc) fmt = loc; } len = strftime (dest, sizeof (dest), fmt, localtime (&tim)); #ifdef WIN32 if (!len) { /* use failsafe format until a correct one is specified */ len = strftime (dest, sizeof (dest), "[%H:%M:%S]", localtime (&tim)); } #endif if (len) { if (prefs.utf8_locale) *ret = g_strdup (dest); else *ret = g_locale_to_utf8 (dest, len, 0, &len, 0); } if (loc) g_free (loc); return len; } static void log_write (session *sess, char *text) { char *temp; char *stamp; char *file; int len; if (sess->text_logging == SET_DEFAULT) { if (!prefs.hex_irc_logging) return; } else { if (sess->text_logging != SET_ON) return; } if (sess->logfd == -1) log_open (sess); /* change to a different log file? */ file = log_create_pathname (sess->server->servername, sess->channel, server_get_network (sess->server, FALSE)); if (file) { if (g_access (file, F_OK) != 0) { close (sess->logfd); sess->logfd = log_open_file (sess->server->servername, sess->channel, server_get_network (sess->server, FALSE)); } g_free (file); } if (prefs.hex_stamp_log) { len = get_stamp_str (prefs.hex_stamp_log_format, time (0), &stamp); if (len) { write (sess->logfd, stamp, len); g_free (stamp); } } temp = strip_color (text, -1, STRIP_ALL); len = strlen (temp); write (sess->logfd, temp, len); /* lots of scripts/plugins print without a \n at the end */ if (temp[len - 1] != '\n') write (sess->logfd, "\n", 1); /* emulate what xtext would display */ g_free (temp); } /* converts a CP1252/ISO-8859-1(5) hybrid to UTF-8 */ /* Features: 1. It never fails, all 00-FF chars are converted to valid UTF-8 */ /* 2. Uses CP1252 in the range 80-9f because ISO doesn't have any- */ /* thing useful in this range and it helps us receive from mIRC */ /* 3. The five undefined chars in CP1252 80-9f are replaced with */ /* ISO-8859-15 control codes. */ /* 4. Handles 0xa4 as a Euro symbol ala ISO-8859-15. */ /* 5. Uses ISO-8859-1 (which matches CP1252) for everything else. */ /* 6. This routine measured 3x faster than g_convert :) */ static unsigned char * iso_8859_1_to_utf8 (unsigned char *text, int len, gsize *bytes_written) { unsigned int idx; unsigned char *res, *output; static const unsigned short lowtable[] = /* 74 byte table for 80-a4 */ { /* compressed utf-8 table: if the first byte's 0x20 bit is set, it indicates a 2-byte utf-8 sequence, otherwise prepend a 0xe2. */ 0x82ac, /* 80 Euro. CP1252 from here on... */ 0xe281, /* 81 NA */ 0x809a, /* 82 */ 0xe692, /* 83 */ 0x809e, /* 84 */ 0x80a6, /* 85 */ 0x80a0, /* 86 */ 0x80a1, /* 87 */ 0xeb86, /* 88 */ 0x80b0, /* 89 */ 0xe5a0, /* 8a */ 0x80b9, /* 8b */ 0xe592, /* 8c */ 0xe28d, /* 8d NA */ 0xe5bd, /* 8e */ 0xe28f, /* 8f NA */ 0xe290, /* 90 NA */ 0x8098, /* 91 */ 0x8099, /* 92 */ 0x809c, /* 93 */ 0x809d, /* 94 */ 0x80a2, /* 95 */ 0x8093, /* 96 */ 0x8094, /* 97 */ 0xeb9c, /* 98 */ 0x84a2, /* 99 */ 0xe5a1, /* 9a */ 0x80ba, /* 9b */ 0xe593, /* 9c */ 0xe29d, /* 9d NA */ 0xe5be, /* 9e */ 0xe5b8, /* 9f */ 0xe2a0, /* a0 */ 0xe2a1, /* a1 */ 0xe2a2, /* a2 */ 0xe2a3, /* a3 */ 0x82ac /* a4 ISO-8859-15 Euro. */ }; if (len == -1) len = strlen (text); /* worst case scenario: every byte turns into 3 bytes */ res = output = g_malloc ((len * 3) + 1); if (!output) return NULL; while (len) { if (G_LIKELY (*text < 0x80)) { *output = *text; /* ascii maps directly */ } else if (*text <= 0xa4) /* 80-a4 use a lookup table */ { idx = *text - 0x80; if (lowtable[idx] & 0x2000) { *output++ = (lowtable[idx] >> 8) & 0xdf; /* 2 byte utf-8 */ *output = lowtable[idx] & 0xff; } else { *output++ = 0xe2; /* 3 byte utf-8 */ *output++ = (lowtable[idx] >> 8) & 0xff; *output = lowtable[idx] & 0xff; } } else if (*text < 0xc0) { *output++ = 0xc2; *output = *text; } else { *output++ = 0xc3; *output = *text - 0x40; } output++; text++; len--; } *output = 0; /* terminate */ *bytes_written = output - res; return res; } char * text_validate (char **text, int *len) { char *utf; gsize utf_len; /* valid utf8? */ if (g_utf8_validate (*text, *len, 0)) return NULL; #ifdef WIN32 if (GetACP () == 1252) /* our routine is better than iconv's 1252 */ #else if (prefs.utf8_locale) #endif /* fallback to iso-8859-1 */ utf = iso_8859_1_to_utf8 (*text, *len, &utf_len); else { /* fallback to locale */ utf = g_locale_to_utf8 (*text, *len, 0, &utf_len, NULL); if (!utf) utf = iso_8859_1_to_utf8 (*text, *len, &utf_len); } if (!utf) { *text = g_strdup ("%INVALID%"); *len = 9; } else { *text = utf; *len = utf_len; } return utf; } void PrintText (session *sess, char *text) { char *conv; if (!sess) { if (!sess_list) return; sess = (session *) sess_list->data; } /* make sure it's valid utf8 */ if (text[0] == 0) { text = "\n"; conv = NULL; } else { int len = -1; conv = text_validate ((char **)&text, &len); } log_write (sess, text); scrollback_save (sess, text); fe_print_text (sess, text, 0); if (conv) g_free (conv); } void PrintTextf (session *sess, char *format, ...) { va_list args; char *buf; va_start (args, format); buf = g_strdup_vprintf (format, args); va_end (args); PrintText (sess, buf); g_free (buf); } /* Print Events stuff here --AGL */ /* Consider the following a NOTES file: The main upshot of this is: * Plugins and Perl scripts (when I get round to signaling perl.c) can intercept text events and do what they like * The default text engine can be config'ed By default it should appear *exactly* the same (I'm working hard not to change the default style) but if you go into Settings->Edit Event Texts you can change the text's. The format is thus: The normal %Cx (color) and %B (bold) etc work $x is replaced with the data in var x (e.g. $1 is often the nick) $axxx is replace with a single byte of value xxx (in base 10) AGL (990507) */ /* These lists are thus: pntevts_text[] are the strings the user sees (WITH %x etc) pntevts[] are the data strings with \000 etc */ /* To add a new event: Think up a name (like "Join") Make up a pevt_name_help struct Add an entry to textevents.in Type: make textevents */ /* Internals: On startup ~/.xchat/printevents.conf is loaded if it doesn't exist the defaults are loaded. Any missing events are filled from defaults. Each event is parsed by pevt_build_string and a binary output is produced which looks like: (byte) value: 0 = { (int) numbers of bytes (char []) that number of byte to be memcpy'ed into the buffer } 1 = (byte) number of varable to insert 2 = end of buffer Each XP_TE_* signal is hard coded to call text_emit which calls display_event which decodes the data This means that this system *should be faster* than snprintf because it always 'knows' that format of the string (basically is preparses much of the work) --AGL */ char *pntevts_text[NUM_XP]; char *pntevts[NUM_XP]; #define pevt_generic_none_help NULL static char * const pevt_genmsg_help[] = { N_("Left message"), N_("Right message"), }; #if 0 static char * const pevt_identd_help[] = { N_("IP address"), N_("Username") }; #endif static char * const pevt_join_help[] = { N_("The nick of the joining person"), N_("The channel being joined"), N_("The host of the person"), }; static char * const pevt_chanaction_help[] = { N_("Nickname"), N_("The action"), N_("Mode char"), N_("Identified text"), }; static char * const pevt_chanmsg_help[] = { N_("Nickname"), N_("The text"), N_("Mode char"), N_("Identified text"), }; static char * const pevt_privmsg_help[] = { N_("Nickname"), N_("The message"), N_("Identified text") }; static char * const pevt_capack_help[] = { N_("Server Name"), N_("Acknowledged Capabilities") }; static char * const pevt_caplist_help[] = { N_("Server Name"), N_("Server Capabilities") }; static char * const pevt_capreq_help[] = { N_("Requested Capabilities") }; static char * const pevt_changenick_help[] = { N_("Old nickname"), N_("New nickname"), }; static char * const pevt_newtopic_help[] = { N_("Nick of person who changed the topic"), N_("Topic"), N_("Channel"), }; static char * const pevt_topic_help[] = { N_("Channel"), N_("Topic"), }; static char * const pevt_kick_help[] = { N_("The nickname of the kicker"), N_("The person being kicked"), N_("The channel"), N_("The reason"), }; static char * const pevt_part_help[] = { N_("The nick of the person leaving"), N_("The host of the person"), N_("The channel"), }; static char * const pevt_chandate_help[] = { N_("The channel"), N_("The time"), }; static char * const pevt_topicdate_help[] = { N_("The channel"), N_("The creator"), N_("The time"), }; static char * const pevt_quit_help[] = { N_("Nick"), N_("Reason"), N_("Host"), }; static char * const pevt_pingrep_help[] = { N_("Who it's from"), N_("The time in x.x format (see below)"), }; static char * const pevt_notice_help[] = { N_("Who it's from"), N_("The message"), }; static char * const pevt_channotice_help[] = { N_("Who it's from"), N_("The Channel it's going to"), N_("The message"), }; static char * const pevt_uchangenick_help[] = { N_("Old nickname"), N_("New nickname"), }; static char * const pevt_ukick_help[] = { N_("The person being kicked"), N_("The channel"), N_("The nickname of the kicker"), N_("The reason"), }; static char * const pevt_partreason_help[] = { N_("The nick of the person leaving"), N_("The host of the person"), N_("The channel"), N_("The reason"), }; static char * const pevt_ctcpsnd_help[] = { N_("The sound"), N_("The nick of the person"), N_("The channel"), }; static char * const pevt_ctcpgen_help[] = { N_("The CTCP event"), N_("The nick of the person"), }; static char * const pevt_ctcpgenc_help[] = { N_("The CTCP event"), N_("The nick of the person"), N_("The Channel it's going to"), }; static char * const pevt_chansetkey_help[] = { N_("The nick of the person who set the key"), N_("The key"), }; static char * const pevt_chansetlimit_help[] = { N_("The nick of the person who set the limit"), N_("The limit"), }; static char * const pevt_chanop_help[] = { N_("The nick of the person who did the op'ing"), N_("The nick of the person who has been op'ed"), }; static char * const pevt_chanhop_help[] = { N_("The nick of the person who has been halfop'ed"), N_("The nick of the person who did the halfop'ing"), }; static char * const pevt_chanvoice_help[] = { N_("The nick of the person who did the voice'ing"), N_("The nick of the person who has been voice'ed"), }; static char * const pevt_chanban_help[] = { N_("The nick of the person who did the banning"), N_("The ban mask"), }; static char * const pevt_chanquiet_help[] = { N_("The nick of the person who did the quieting"), N_("The quiet mask"), }; static char * const pevt_chanrmkey_help[] = { N_("The nick who removed the key"), }; static char * const pevt_chanrmlimit_help[] = { N_("The nick who removed the limit"), }; static char * const pevt_chandeop_help[] = { N_("The nick of the person of did the deop'ing"), N_("The nick of the person who has been deop'ed"), }; static char * const pevt_chandehop_help[] = { N_("The nick of the person of did the dehalfop'ing"), N_("The nick of the person who has been dehalfop'ed"), }; static char * const pevt_chandevoice_help[] = { N_("The nick of the person of did the devoice'ing"), N_("The nick of the person who has been devoice'ed"), }; static char * const pevt_chanunban_help[] = { N_("The nick of the person of did the unban'ing"), N_("The ban mask"), }; static char * const pevt_chanunquiet_help[] = { N_("The nick of the person of did the unquiet'ing"), N_("The quiet mask"), }; static char * const pevt_chanexempt_help[] = { N_("The nick of the person who did the exempt"), N_("The exempt mask"), }; static char * const pevt_chanrmexempt_help[] = { N_("The nick of the person removed the exempt"), N_("The exempt mask"), }; static char * const pevt_chaninvite_help[] = { N_("The nick of the person who did the invite"), N_("The invite mask"), }; static char * const pevt_chanrminvite_help[] = { N_("The nick of the person removed the invite"), N_("The invite mask"), }; static char * const pevt_chanmodegen_help[] = { N_("The nick of the person setting the mode"), N_("The mode's sign (+/-)"), N_("The mode letter"), N_("The channel it's being set on"), }; static char * const pevt_whois1_help[] = { N_("Nickname"), N_("Username"), N_("Host"), N_("Full name"), }; static char * const pevt_whois2_help[] = { N_("Nickname"), N_("Channel Membership/\"is an IRC operator\""), }; static char * const pevt_whois3_help[] = { N_("Nickname"), N_("Server Information"), }; static char * const pevt_whois4_help[] = { N_("Nickname"), N_("Idle time"), }; static char * const pevt_whois4t_help[] = { N_("Nickname"), N_("Idle time"), N_("Signon time"), }; static char * const pevt_whois5_help[] = { N_("Nickname"), N_("Away reason"), }; static char * const pevt_whois6_help[] = { N_("Nickname"), }; static char * const pevt_whoisid_help[] = { N_("Nickname"), N_("Message"), "Numeric" }; static char * const pevt_whoisauth_help[] = { N_("Nickname"), N_("Message"), N_("Account"), }; static char * const pevt_whoisrealhost_help[] = { N_("Nickname"), N_("Real user@host"), N_("Real IP"), N_("Message"), }; static char * const pevt_generic_channel_help[] = { N_("Channel Name"), }; static char * const pevt_saslauth_help[] = { N_("Username") }; static char * const pevt_saslresponse_help[] = { N_("Server Name"), N_("Raw Numeric or Identifier"), N_("Username"), N_("Message") }; static char * const pevt_servertext_help[] = { N_("Text"), N_("Server Name"), N_("Raw Numeric or Identifier") }; static char * const pevt_sslmessage_help[] = { N_("Text"), N_("Server Name") }; static char * const pevt_invited_help[] = { N_("Channel Name"), N_("Nick of person who invited you"), N_("Server Name"), }; static char * const pevt_usersonchan_help[] = { N_("Channel Name"), N_("Users"), }; static char * const pevt_nickclash_help[] = { N_("Nickname in use"), N_("Nick being tried"), }; static char * const pevt_connfail_help[] = { N_("Error"), }; static char * const pevt_connect_help[] = { N_("Host"), N_("IP"), N_("Port"), }; static char * const pevt_sconnect_help[] = { "PID" }; static char * const pevt_generic_nick_help[] = { N_("Nickname"), N_("Server Name"), N_("Network") }; static char * const pevt_chanmodes_help[] = { N_("Channel Name"), N_("Modes string"), }; static char * const pevt_rawmodes_help[] = { N_("Nickname"), N_("Modes string"), }; static char * const pevt_kill_help[] = { N_("Nickname"), N_("Reason"), }; static char * const pevt_dccchaterr_help[] = { N_("Nickname"), N_("IP address"), N_("Port"), N_("Error"), }; static char * const pevt_dccstall_help[] = { N_("DCC Type"), N_("Filename"), N_("Nickname"), }; static char * const pevt_generic_file_help[] = { N_("Filename"), N_("Error"), }; static char * const pevt_dccrecverr_help[] = { N_("Filename"), N_("Destination filename"), N_("Nickname"), N_("Error"), }; static char * const pevt_dccrecvcomp_help[] = { N_("Filename"), N_("Destination filename"), N_("Nickname"), N_("CPS"), }; static char * const pevt_dccconfail_help[] = { N_("DCC Type"), N_("Nickname"), N_("Error"), }; static char * const pevt_dccchatcon_help[] = { N_("Nickname"), N_("IP address"), }; static char * const pevt_dcccon_help[] = { N_("Nickname"), N_("IP address"), N_("Filename"), }; static char * const pevt_dccsendfail_help[] = { N_("Filename"), N_("Nickname"), N_("Error"), }; static char * const pevt_dccsendcomp_help[] = { N_("Filename"), N_("Nickname"), N_("CPS"), }; static char * const pevt_dccoffer_help[] = { N_("Filename"), N_("Nickname"), N_("Pathname"), }; static char * const pevt_dccfileabort_help[] = { N_("Nickname"), N_("Filename") }; static char * const pevt_dccchatabort_help[] = { N_("Nickname"), }; static char * const pevt_dccresumeoffer_help[] = { N_("Nickname"), N_("Filename"), N_("Position"), }; static char * const pevt_dccsendoffer_help[] = { N_("Nickname"), N_("Filename"), N_("Size"), N_("IP address"), }; static char * const pevt_dccgenericoffer_help[] = { N_("DCC String"), N_("Nickname"), }; static char * const pevt_notifyaway_help[] = { N_("Nickname"), N_("Away Reason"), }; static char * const pevt_notifynumber_help[] = { N_("Number of notify items"), }; static char * const pevt_serverlookup_help[] = { N_("Server Name"), }; static char * const pevt_servererror_help[] = { N_("Text"), }; static char * const pevt_foundip_help[] = { N_("IP"), }; static char * const pevt_dccrename_help[] = { N_("Old Filename"), N_("New Filename"), }; static char * const pevt_ctcpsend_help[] = { N_("Receiver"), N_("Message"), }; static char * const pevt_ignoreaddremove_help[] = { N_("Hostmask"), }; static char * const pevt_resolvinguser_help[] = { N_("Nickname"), N_("Hostname"), }; static char * const pevt_malformed_help[] = { N_("Nickname"), N_("The Packet"), }; static char * const pevt_pingtimeout_help[] = { N_("Seconds"), }; static char * const pevt_uinvite_help[] = { N_("Nick of person who have been invited"), N_("Channel Name"), N_("Server Name"), }; static char * const pevt_banlist_help[] = { N_("Channel"), N_("Banmask"), N_("Who set the ban"), N
/* Copyright (C) 2006-2007 Peter Zelezny. */

#include <string.h>
#include <unistd.h>
#include "../common/xchat-plugin.h"
#include "../common/xchat.h"
#include "../common/xchatc.h"
#include "../common/inbound.h"
#include "../common/server.h"
#include "../common/fe.h"
#include "../common/util.h"
#include "fe-gtk.h"
#include "pixmaps.h"
#include "maingui.h"
#include "menu.h"
#include <gtk/gtk.h>

#ifdef USE_LIBNOTIFY
#include <libnotify/notify.h>
#ifndef NOTIFY_CHECK_VERSION
#define NOTIFY_CHECK_VERSION(x,y,z) 0
#endif
#if NOTIFY_CHECK_VERSION(0,7,0)
#define XC_NOTIFY_NEW(a,b,c,d) notify_notification_new(a,b,c)
#else
#define XC_NOTIFY_NEW(a,b,c,d) notify_notification_new(a,b,c,d)
#endif
#endif

typedef enum	/* current icon status */
{
	TS_NONE,
	TS_MESSAGE,
	TS_HIGHLIGHT,
	TS_FILEOFFER,
	TS_CUSTOM /* plugin */
} TrayStatus;

typedef enum
{
	WS_FOCUSED,
	WS_NORMAL,
	WS_HIDDEN
} WinStatus;

typedef GdkPixbuf* TrayIcon;
#define tray_icon_from_file(f) gdk_pixbuf_new_from_file(f,NULL)
#define tray_icon_free(i) g_object_unref(i)

#define ICON_NORMAL pix_xchat
#define ICON_MSG pix_tray_msg
#define ICON_HILIGHT pix_tray_hilight
#define ICON_FILE pix_tray_file
#define TIMEOUT 500

static GtkStatusIcon *sticon;
static gint flash_tag;
static TrayStatus tray_status;
static xchat_plugin *ph;

static TrayIcon custom_icon1;
static TrayIcon custom_icon2;

static int tray_priv_count = 0;
static int tray_pub_count = 0;
static int tray_hilight_count = 0;
static int tray_file_count = 0;


void tray_apply_setup (void);


static WinStatus
tray_get_window_status (void)
{
	const char *st;

	st = xchat_get_info (ph, "win_status");

	if (!st)
		return WS_HIDDEN;

	if (!strcmp (st, "active"))
		return WS_FOCUSED;

	if (!strcmp (st, "hidden"))
		return WS_HIDDEN;

	return WS_NORMAL;
}

static int
tray_count_channels (void)
{
	int cons = 0;
	GSList *list;
	session *sess;

	for (list = sess_list; list; list = list->next)
	{
		sess = list->data;
		if (sess->server->connected && sess->channel[0] &&
			 sess->type == SESS_CHANNEL)
			cons++;
	}
	return cons;
}

static int
tray_count_networks (void)
{
	int cons = 0;
	GSList *list;

	for (list = serv_list; list; list = list->next)
	{
		if (((server *)list->data)->connected)
			cons++;
	}
	return cons;