Implement BLOWFISh, AES, and EXTERNAL SASL mechanisms
[rofl0r-ixchat.git] / src / common / text.c
blob559fc38a3dbd99f2e9f577052f2df8433112c6b3
1 /* X-Chat
2 * Copyright (C) 1998 Peter Zelezny.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
19 #include <stdlib.h>
20 #include <stdio.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <ctype.h>
24 #include <time.h>
25 #include <sys/types.h>
26 #include <fcntl.h>
27 #include <sys/stat.h>
28 #include <sys/mman.h>
30 #include "xchat.h"
31 #include <glib.h>
32 #include "cfgfiles.h"
33 #include "chanopt.h"
34 #include "plugin.h"
35 #include "fe.h"
36 #include "server.h"
37 #include "util.h"
38 #include "outbound.h"
39 #include "xchatc.h"
40 #include "text.h"
42 struct pevt_stage1
44 int len;
45 char *data;
46 struct pevt_stage1 *next;
50 static void mkdir_p (char *dir);
51 static char *log_create_filename (char *channame);
54 static char *
55 scrollback_get_filename (session *sess, char *buf, int max)
57 char *net, *chan;
59 net = server_get_network (sess->server, FALSE);
60 if (!net)
61 return NULL;
63 snprintf (buf, max, "%s/scrollback/%s/%s.txt", get_xdir_fs (), net, "");
64 mkdir_p (buf);
66 chan = log_create_filename (sess->channel);
67 snprintf (buf, max, "%s/scrollback/%s/%s.txt", get_xdir_fs (), net, chan);
68 free (chan);
70 return buf;
73 #if 0
75 static void
76 scrollback_unlock (session *sess)
78 char buf[1024];
80 if (scrollback_get_filename (sess, buf, sizeof (buf) - 6) == NULL)
81 return;
83 strcat (buf, ".lock");
84 unlink (buf);
87 static gboolean
88 scrollback_lock (session *sess)
90 char buf[1024];
91 int fh;
93 if (scrollback_get_filename (sess, buf, sizeof (buf) - 6) == NULL)
94 return FALSE;
96 strcat (buf, ".lock");
98 if (access (buf, F_OK) == 0)
99 return FALSE; /* can't get lock */
101 fh = open (buf, O_CREAT | O_TRUNC | O_APPEND | O_WRONLY, 0644);
102 if (fh == -1)
103 return FALSE;
105 return TRUE;
108 #endif
110 void
111 scrollback_close (session *sess)
113 if (sess->scrollfd != -1)
115 close (sess->scrollfd);
116 sess->scrollfd = -1;
120 static char *
121 file_to_buffer (char *file, int *len)
123 int fh;
124 char *buf;
125 struct stat st;
127 fh = open (file, O_RDONLY | OFLAGS);
128 if (fh == -1)
129 return NULL;
131 fstat (fh, &st);
133 buf = malloc (st.st_size);
134 if (!buf)
136 close (fh);
137 return NULL;
140 if (read (fh, buf, st.st_size) != st.st_size)
142 free (buf);
143 close (fh);
144 return NULL;
147 *len = st.st_size;
148 close (fh);
149 return buf;
152 /* shrink the file to roughly prefs.max_lines */
154 static void
155 scrollback_shrink (session *sess)
157 char file[1024];
158 char *buf;
159 int fh;
160 int lines;
161 int line;
162 int len;
163 char *p;
165 scrollback_close (sess);
166 sess->scrollwritten = 0;
167 lines = 0;
169 if (scrollback_get_filename (sess, file, sizeof (file)) == NULL)
170 return;
172 buf = file_to_buffer (file, &len);
173 if (!buf)
174 return;
176 /* count all lines */
177 p = buf;
178 while (p != buf + len)
180 if (*p == '\n')
181 lines++;
182 p++;
185 fh = open (file, O_CREAT | O_TRUNC | O_APPEND | O_WRONLY, 0644);
186 if (fh == -1)
188 free (buf);
189 return;
192 line = 0;
193 p = buf;
194 while (p != buf + len)
196 if (*p == '\n')
198 line++;
199 if (line >= lines - prefs.max_lines &&
200 p + 1 != buf + len)
202 p++;
203 write (fh, p, len - (p - buf));
204 break;
207 p++;
210 close (fh);
211 free (buf);
214 static void
215 scrollback_save (session *sess, char *text)
217 char buf[512 * 4];
218 time_t stamp;
219 int len;
221 if (sess->type == SESS_SERVER)
222 return;
224 if (sess->text_scrollback == SET_DEFAULT)
226 if (!prefs.text_replay)
227 return;
229 else
231 if (sess->text_scrollback != SET_ON)
232 return;
235 if (sess->scrollfd == -1)
237 if (scrollback_get_filename (sess, buf, sizeof (buf)) == NULL)
238 return;
240 sess->scrollfd = open (buf, O_CREAT | O_APPEND | O_WRONLY, 0644);
241 if (sess->scrollfd == -1)
242 return;
245 stamp = time (0);
246 if (sizeof (stamp) == 4) /* gcc will optimize one of these out */
247 write (sess->scrollfd, buf, snprintf (buf, sizeof (buf), "T %d ", (int)stamp));
248 else
249 write (sess->scrollfd, buf, snprintf (buf, sizeof (buf), "T %"G_GINT64_FORMAT" ", (gint64)stamp));
251 len = strlen (text);
252 write (sess->scrollfd, text, len);
253 if (len && text[len - 1] != '\n')
254 write (sess->scrollfd, "\n", 1);
256 sess->scrollwritten++;
258 if ((sess->scrollwritten * 2 > prefs.max_lines && prefs.max_lines > 0) ||
259 sess->scrollwritten > 32000)
260 scrollback_shrink (sess);
263 void
264 scrollback_load (session *sess)
266 int fh;
267 char buf[512 * 4];
268 char *text;
269 time_t stamp;
270 int lines;
271 char *map, *end_map;
272 struct stat statbuf;
273 const char *begin, *eol;
275 if (sess->text_scrollback == SET_DEFAULT)
277 if (!prefs.text_replay)
278 return;
280 else
282 if (sess->text_scrollback != SET_ON)
283 return;
286 if (scrollback_get_filename (sess, buf, sizeof (buf)) == NULL)
287 return;
289 fh = open (buf, O_RDONLY | OFLAGS);
290 if (fh == -1)
291 return;
293 if (fstat (fh, &statbuf) < 0)
294 return;
296 map = mmap (NULL, statbuf.st_size, PROT_READ, MAP_PRIVATE, fh, 0);
297 if (map == MAP_FAILED)
298 return;
300 end_map = map + statbuf.st_size;
302 lines = 0;
303 begin = map;
304 while (begin < end_map)
306 int n_bytes;
308 eol = memchr (begin, '\n', end_map - begin);
310 if (!eol)
311 eol = end_map;
313 n_bytes = MIN (eol - begin, sizeof (buf) - 1);
315 strncpy (buf, begin, n_bytes);
317 buf[n_bytes] = 0;
319 if (buf[0] == 'T')
321 if (sizeof (time_t) == 4)
322 stamp = strtoul (buf + 2, NULL, 10);
323 else
324 stamp = strtoull (buf + 2, NULL, 10); /* just incase time_t is 64 bits */
325 text = strchr (buf + 3, ' ');
326 if (text)
328 text = strip_color (text + 1, -1, STRIP_COLOR);
329 fe_print_text (sess, text, stamp);
330 g_free (text);
332 lines++;
335 begin = eol + 1;
338 sess->scrollwritten = lines;
340 if (lines)
342 text = ctime (&stamp);
343 text[24] = 0; /* get rid of the \n */
344 snprintf (buf, sizeof (buf), "\n*\t%s %s\n\n", _("Loaded log from"), text);
345 fe_print_text (sess, buf, 0);
346 /*EMIT_SIGNAL (XP_TE_GENMSG, sess, "*", buf, NULL, NULL, NULL, 0);*/
349 munmap (map, statbuf.st_size);
350 close (fh);
353 void
354 log_close (session *sess)
356 char obuf[512];
357 time_t currenttime;
359 if (sess->logfd != -1)
361 currenttime = time (NULL);
362 write (sess->logfd, obuf,
363 snprintf (obuf, sizeof (obuf) - 1, _("**** ENDING LOGGING AT %s\n"),
364 ctime (&currenttime)));
365 close (sess->logfd);
366 sess->logfd = -1;
370 static void
371 mkdir_p (char *dir) /* like "mkdir -p" from a shell, FS encoding */
373 char *start = dir;
375 /* the whole thing already exists? */
376 if (access (dir, F_OK) == 0)
377 return;
379 while (*dir)
381 if (dir != start && *dir == '/')
383 *dir = 0;
384 mkdir (start, S_IRUSR | S_IWUSR | S_IXUSR);
385 *dir = '/';
387 dir++;
391 static char *
392 log_create_filename (char *channame)
394 char *tmp, *ret;
395 int mbl;
397 ret = tmp = strdup (channame);
398 while (*tmp)
400 mbl = g_utf8_skip[((unsigned char *)tmp)[0]];
401 if (mbl == 1)
403 *tmp = rfc_tolower (*tmp);
404 if (*tmp == '/')
405 *tmp = '_';
407 tmp += mbl;
410 return ret;
413 /* like strcpy, but % turns into %% */
415 static char *
416 log_escape_strcpy (char *dest, char *src, char *end)
418 while (*src)
420 *dest = *src;
421 if (dest + 1 == end)
422 break;
423 dest++;
424 src++;
426 if (*src == '%')
428 if (dest + 1 == end)
429 break;
430 dest[0] = '%';
431 dest++;
435 dest[0] = 0;
436 return dest - 1;
439 /* substitutes %c %n %s into buffer */
441 static void
442 log_insert_vars (char *buf, int bufsize, char *fmt, char *c, char *n, char *s)
444 char *end = buf + bufsize;
446 while (1)
448 switch (fmt[0])
450 case 0:
451 buf[0] = 0;
452 return;
454 case '%':
455 fmt++;
456 switch (fmt[0])
458 case 'c':
459 buf = log_escape_strcpy (buf, c, end);
460 break;
461 case 'n':
462 buf = log_escape_strcpy (buf, n, end);
463 break;
464 case 's':
465 buf = log_escape_strcpy (buf, s, end);
466 break;
467 default:
468 buf[0] = '%';
469 buf++;
470 buf[0] = fmt[0];
471 break;
473 break;
475 default:
476 buf[0] = fmt[0];
478 fmt++;
479 buf++;
480 /* doesn't fit? */
481 if (buf == end)
483 buf[-1] = 0;
484 return;
489 static char *
490 log_create_pathname (char *servname, char *channame, char *netname)
492 char fname[384];
493 char fnametime[384];
494 char *fs;
495 struct tm *tm;
496 time_t now;
498 if (!netname)
499 netname = "NETWORK";
501 /* first, everything is in UTF-8 */
502 if (!rfc_casecmp (channame, servname))
503 channame = strdup ("server");
504 else
505 channame = log_create_filename (channame);
506 log_insert_vars (fname, sizeof (fname), prefs.logmask, channame, netname, servname);
507 free (channame);
509 /* insert time/date */
510 now = time (NULL);
511 tm = localtime (&now);
512 strftime (fnametime, sizeof (fnametime), fname, tm);
514 /* create final path/filename */
515 if (fnametime[0] == '/') /* is it fullpath already? */
516 snprintf (fname, sizeof (fname), "%s", fnametime);
517 else
518 snprintf (fname, sizeof (fname), "%s/xchatlogs/%s", get_xdir_utf8 (), fnametime);
520 /* now we need it in FileSystem encoding */
521 fs = xchat_filename_from_utf8 (fname, -1, 0, 0, 0);
523 /* create all the subdirectories */
524 if (fs)
525 mkdir_p (fs);
527 return fs;
530 static int
531 log_open_file (char *servname, char *channame, char *netname)
533 char buf[512];
534 int fd;
535 char *file;
536 time_t currenttime;
538 file = log_create_pathname (servname, channame, netname);
539 if (!file)
540 return -1;
542 fd = open (file, O_CREAT | O_APPEND | O_WRONLY, 0644);
543 g_free (file);
545 if (fd == -1)
546 return -1;
547 currenttime = time (NULL);
548 write (fd, buf,
549 snprintf (buf, sizeof (buf), _("**** BEGIN LOGGING AT %s\n"),
550 ctime (&currenttime)));
552 return fd;
555 static void
556 log_open (session *sess)
558 static gboolean log_error = FALSE;
560 log_close (sess);
561 sess->logfd = log_open_file (sess->server->servername, sess->channel,
562 server_get_network (sess->server, FALSE));
564 if (!log_error && sess->logfd == -1)
566 char message[512];
567 snprintf (message, sizeof (message),
568 _("* Can't open log file(s) for writing. Check the\n" \
569 " permissions on %s/xchatlogs"), get_xdir_utf8 ());
570 fe_message (message, FE_MSG_WAIT | FE_MSG_ERROR);
572 log_error = TRUE;
576 void
577 log_open_or_close (session *sess)
579 if (sess->text_logging == SET_DEFAULT)
581 if (prefs.logging)
582 log_open (sess);
583 else
584 log_close (sess);
586 else
588 if (sess->text_logging)
589 log_open (sess);
590 else
591 log_close (sess);
596 get_stamp_str (char *fmt, time_t tim, char **ret)
598 char *loc = NULL;
599 char dest[128];
600 gsize len;
602 /* strftime wants the format string in LOCALE! */
603 if (!prefs.utf8_locale)
605 const gchar *charset;
607 g_get_charset (&charset);
608 loc = g_convert_with_fallback (fmt, -1, charset, "UTF-8", "?", 0, 0, 0);
609 if (loc)
610 fmt = loc;
613 len = strftime (dest, sizeof (dest), fmt, localtime (&tim));
614 if (len)
616 if (prefs.utf8_locale)
617 *ret = g_strdup (dest);
618 else
619 *ret = g_locale_to_utf8 (dest, len, 0, &len, 0);
622 if (loc)
623 g_free (loc);
625 return len;
628 static void
629 log_write (session *sess, char *text)
631 char *temp;
632 char *stamp;
633 char *file;
634 int len;
636 if (sess->text_logging == SET_DEFAULT)
638 if (!prefs.logging)
639 return;
641 else
643 if (sess->text_logging != SET_ON)
644 return;
647 if (sess->logfd == -1)
648 log_open (sess);
650 /* change to a different log file? */
651 file = log_create_pathname (sess->server->servername, sess->channel,
652 server_get_network (sess->server, FALSE));
653 if (file)
655 if (access (file, F_OK) != 0)
657 close (sess->logfd);
658 sess->logfd = log_open_file (sess->server->servername, sess->channel,
659 server_get_network (sess->server, FALSE));
661 g_free (file);
664 if (prefs.timestamp_logs)
666 len = get_stamp_str (prefs.timestamp_log_format, time (0), &stamp);
667 if (len)
669 write (sess->logfd, stamp, len);
670 g_free (stamp);
673 temp = strip_color (text, -1, STRIP_ALL);
674 len = strlen (temp);
675 write (sess->logfd, temp, len);
676 /* lots of scripts/plugins print without a \n at the end */
677 if (temp[len - 1] != '\n')
678 write (sess->logfd, "\n", 1); /* emulate what xtext would display */
679 g_free (temp);
682 /* converts a CP1252/ISO-8859-1(5) hybrid to UTF-8 */
683 /* Features: 1. It never fails, all 00-FF chars are converted to valid UTF-8 */
684 /* 2. Uses CP1252 in the range 80-9f because ISO doesn't have any- */
685 /* thing useful in this range and it helps us receive from mIRC */
686 /* 3. The five undefined chars in CP1252 80-9f are replaced with */
687 /* ISO-8859-15 control codes. */
688 /* 4. Handles 0xa4 as a Euro symbol ala ISO-8859-15. */
689 /* 5. Uses ISO-8859-1 (which matches CP1252) for everything else. */
690 /* 6. This routine measured 3x faster than g_convert :) */
692 static unsigned char *
693 iso_8859_1_to_utf8 (unsigned char *text, int len, gsize *bytes_written)
695 unsigned int idx;
696 unsigned char *res, *output;
697 static const unsigned short lowtable[] = /* 74 byte table for 80-a4 */
699 /* compressed utf-8 table: if the first byte's 0x20 bit is set, it
700 indicates a 2-byte utf-8 sequence, otherwise prepend a 0xe2. */
701 0x82ac, /* 80 Euro. CP1252 from here on... */
702 0xe281, /* 81 NA */
703 0x809a, /* 82 */
704 0xe692, /* 83 */
705 0x809e, /* 84 */
706 0x80a6, /* 85 */
707 0x80a0, /* 86 */
708 0x80a1, /* 87 */
709 0xeb86, /* 88 */
710 0x80b0, /* 89 */
711 0xe5a0, /* 8a */
712 0x80b9, /* 8b */
713 0xe592, /* 8c */
714 0xe28d, /* 8d NA */
715 0xe5bd, /* 8e */
716 0xe28f, /* 8f NA */
717 0xe290, /* 90 NA */
718 0x8098, /* 91 */
719 0x8099, /* 92 */
720 0x809c, /* 93 */
721 0x809d, /* 94 */
722 0x80a2, /* 95 */
723 0x8093, /* 96 */
724 0x8094, /* 97 */
725 0xeb9c, /* 98 */
726 0x84a2, /* 99 */
727 0xe5a1, /* 9a */
728 0x80ba, /* 9b */
729 0xe593, /* 9c */
730 0xe29d, /* 9d NA */
731 0xe5be, /* 9e */
732 0xe5b8, /* 9f */
733 0xe2a0, /* a0 */
734 0xe2a1, /* a1 */
735 0xe2a2, /* a2 */
736 0xe2a3, /* a3 */
737 0x82ac /* a4 ISO-8859-15 Euro. */
740 if (len == -1)
741 len = strlen (text);
743 /* worst case scenario: every byte turns into 3 bytes */
744 res = output = g_malloc ((len * 3) + 1);
745 if (!output)
746 return NULL;
748 while (len)
750 if (G_LIKELY (*text < 0x80))
752 *output = *text; /* ascii maps directly */
754 else if (*text <= 0xa4) /* 80-a4 use a lookup table */
756 idx = *text - 0x80;
757 if (lowtable[idx] & 0x2000)
759 *output++ = (lowtable[idx] >> 8) & 0xdf; /* 2 byte utf-8 */
760 *output = lowtable[idx] & 0xff;
762 else
764 *output++ = 0xe2; /* 3 byte utf-8 */
765 *output++ = (lowtable[idx] >> 8) & 0xff;
766 *output = lowtable[idx] & 0xff;
769 else if (*text < 0xc0)
771 *output++ = 0xc2;
772 *output = *text;
774 else
776 *output++ = 0xc3;
777 *output = *text - 0x40;
779 output++;
780 text++;
781 len--;
783 *output = 0; /* terminate */
784 *bytes_written = output - res;
786 return res;
789 char *
790 text_validate (char **text, int *len)
792 char *utf;
793 gsize utf_len;
795 /* valid utf8? */
796 if (g_utf8_validate (*text, *len, 0))
797 return NULL;
799 if (prefs.utf8_locale)
800 /* fallback to iso-8859-1 */
801 utf = iso_8859_1_to_utf8 (*text, *len, &utf_len);
802 else
804 /* fallback to locale */
805 utf = g_locale_to_utf8 (*text, *len, 0, &utf_len, NULL);
806 if (!utf)
807 utf = iso_8859_1_to_utf8 (*text, *len, &utf_len);
810 if (!utf)
812 *text = g_strdup ("%INVALID%");
813 *len = 9;
814 } else
816 *text = utf;
817 *len = utf_len;
820 return utf;
823 void
824 PrintText (session *sess, char *text)
826 char *conv;
828 if (!sess)
830 if (!sess_list)
831 return;
832 sess = (session *) sess_list->data;
835 /* make sure it's valid utf8 */
836 if (text[0] == 0)
838 text = "\n";
839 conv = NULL;
840 } else
842 int len = -1;
843 conv = text_validate ((char **)&text, &len);
846 log_write (sess, text);
847 scrollback_save (sess, text);
848 fe_print_text (sess, text, 0);
850 if (conv)
851 g_free (conv);
854 void
855 PrintTextf (session *sess, char *format, ...)
857 va_list args;
858 char *buf;
860 va_start (args, format);
861 buf = g_strdup_vprintf (format, args);
862 va_end (args);
864 PrintText (sess, buf);
865 g_free (buf);
868 /* Print Events stuff here --AGL */
870 /* Consider the following a NOTES file:
872 The main upshot of this is:
873 * Plugins and Perl scripts (when I get round to signaling perl.c) can intercept text events and do what they like
874 * The default text engine can be config'ed
876 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:
878 The normal %Cx (color) and %B (bold) etc work
880 $x is replaced with the data in var x (e.g. $1 is often the nick)
882 $axxx is replace with a single byte of value xxx (in base 10)
884 AGL (990507)
887 /* These lists are thus:
888 pntevts_text[] are the strings the user sees (WITH %x etc)
889 pntevts[] are the data strings with \000 etc
892 /* To add a new event:
894 Think up a name (like "Join")
895 Make up a pevt_name_help struct
896 Add an entry to textevents.in
897 Type: make textevents
900 /* Internals:
902 On startup ~/.xchat/printevents.conf is loaded if it doesn't exist the
903 defaults are loaded. Any missing events are filled from defaults.
904 Each event is parsed by pevt_build_string and a binary output is produced
905 which looks like:
907 (byte) value: 0 = {
908 (int) numbers of bytes
909 (char []) that number of byte to be memcpy'ed into the buffer
912 (byte) number of varable to insert
913 2 = end of buffer
915 Each XP_TE_* signal is hard coded to call text_emit which calls
916 display_event which decodes the data
918 This means that this system *should be faster* than snprintf because
919 it always 'knows' that format of the string (basically is preparses much
920 of the work)
922 --AGL
925 char *pntevts_text[NUM_XP];
926 char *pntevts[NUM_XP];
928 #define pevt_generic_none_help NULL
930 static char * const pevt_genmsg_help[] = {
931 N_("Left message"),
932 N_("Right message"),
935 static char * const pevt_join_help[] = {
936 N_("The nick of the joining person"),
937 N_("The channel being joined"),
938 N_("The host of the person"),
941 static char * const pevt_chanaction_help[] = {
942 N_("Nickname"),
943 N_("The action"),
944 N_("Mode char"),
945 N_("Identified text"),
948 static char * const pevt_chanmsg_help[] = {
949 N_("Nickname"),
950 N_("The text"),
951 N_("Mode char"),
952 N_("Identified text"),
955 static char * const pevt_privmsg_help[] = {
956 N_("Nickname"),
957 N_("The message"),
958 N_("Identified text")
961 static char * const pevt_capack_help[] = {
962 N_("Server Name"),
963 N_("Acknowledged Capabilities")
966 static char * const pevt_caplist_help[] = {
967 N_("Server Name"),
968 N_("Server Capabilities")
971 static char * const pevt_capreq_help[] = {
972 N_("Requested Capabilities")
975 static char * const pevt_changenick_help[] = {
976 N_("Old nickname"),
977 N_("New nickname"),
980 static char * const pevt_newtopic_help[] = {
981 N_("Nick of person who changed the topic"),
982 N_("Topic"),
983 N_("Channel"),
986 static char * const pevt_topic_help[] = {
987 N_("Channel"),
988 N_("Topic"),
991 static char * const pevt_kick_help[] = {
992 N_("The nickname of the kicker"),
993 N_("The person being kicked"),
994 N_("The channel"),
995 N_("The reason"),
998 static char * const pevt_part_help[] = {
999 N_("The nick of the person leaving"),
1000 N_("The host of the person"),
1001 N_("The channel"),
1004 static char * const pevt_chandate_help[] = {
1005 N_("The channel"),
1006 N_("The time"),
1009 static char * const pevt_topicdate_help[] = {
1010 N_("The channel"),
1011 N_("The creator"),
1012 N_("The time"),
1015 static char * const pevt_quit_help[] = {
1016 N_("Nick"),
1017 N_("Reason"),
1018 N_("Host"),
1021 static char * const pevt_pingrep_help[] = {
1022 N_("Who it's from"),
1023 N_("The time in x.x format (see below)"),
1026 static char * const pevt_notice_help[] = {
1027 N_("Who it's from"),
1028 N_("The message"),
1031 static char * const pevt_channotice_help[] = {
1032 N_("Who it's from"),
1033 N_("The Channel it's going to"),
1034 N_("The message"),
1037 static char * const pevt_uchangenick_help[] = {
1038 N_("Old nickname"),
1039 N_("New nickname"),
1042 static char * const pevt_ukick_help[] = {
1043 N_("The person being kicked"),
1044 N_("The channel"),
1045 N_("The nickname of the kicker"),
1046 N_("The reason"),
1049 static char * const pevt_partreason_help[] = {
1050 N_("The nick of the person leaving"),
1051 N_("The host of the person"),
1052 N_("The channel"),
1053 N_("The reason"),
1056 static char * const pevt_ctcpsnd_help[] = {
1057 N_("The sound"),
1058 N_("The nick of the person"),
1059 N_("The channel"),
1062 static char * const pevt_ctcpgen_help[] = {
1063 N_("The CTCP event"),
1064 N_("The nick of the person"),
1067 static char * const pevt_ctcpgenc_help[] = {
1068 N_("The CTCP event"),
1069 N_("The nick of the person"),
1070 N_("The Channel it's going to"),
1073 static char * const pevt_chansetkey_help[] = {
1074 N_("The nick of the person who set the key"),
1075 N_("The key"),
1078 static char * const pevt_chansetlimit_help[] = {
1079 N_("The nick of the person who set the limit"),
1080 N_("The limit"),
1083 static char * const pevt_chanop_help[] = {
1084 N_("The nick of the person who did the op'ing"),
1085 N_("The nick of the person who has been op'ed"),
1088 static char * const pevt_chanhop_help[] = {
1089 N_("The nick of the person who has been halfop'ed"),
1090 N_("The nick of the person who did the halfop'ing"),
1093 static char * const pevt_chanvoice_help[] = {
1094 N_("The nick of the person who did the voice'ing"),
1095 N_("The nick of the person who has been voice'ed"),
1098 static char * const pevt_chanban_help[] = {
1099 N_("The nick of the person who did the banning"),
1100 N_("The ban mask"),
1103 static char * const pevt_chanrmkey_help[] = {
1104 N_("The nick who removed the key"),
1107 static char * const pevt_chanrmlimit_help[] = {
1108 N_("The nick who removed the limit"),
1111 static char * const pevt_chandeop_help[] = {
1112 N_("The nick of the person of did the deop'ing"),
1113 N_("The nick of the person who has been deop'ed"),
1115 static char * const pevt_chandehop_help[] = {
1116 N_("The nick of the person of did the dehalfop'ing"),
1117 N_("The nick of the person who has been dehalfop'ed"),
1120 static char * const pevt_chandevoice_help[] = {
1121 N_("The nick of the person of did the devoice'ing"),
1122 N_("The nick of the person who has been devoice'ed"),
1125 static char * const pevt_chanunban_help[] = {
1126 N_("The nick of the person of did the unban'ing"),
1127 N_("The ban mask"),
1130 static char * const pevt_chanexempt_help[] = {
1131 N_("The nick of the person who did the exempt"),
1132 N_("The exempt mask"),
1135 static char * const pevt_chanrmexempt_help[] = {
1136 N_("The nick of the person removed the exempt"),
1137 N_("The exempt mask"),
1140 static char * const pevt_chaninvite_help[] = {
1141 N_("The nick of the person who did the invite"),
1142 N_("The invite mask"),
1145 static char * const pevt_chanrminvite_help[] = {
1146 N_("The nick of the person removed the invite"),
1147 N_("The invite mask"),
1150 static char * const pevt_chanmodegen_help[] = {
1151 N_("The nick of the person setting the mode"),
1152 N_("The mode's sign (+/-)"),
1153 N_("The mode letter"),
1154 N_("The channel it's being set on"),
1157 static char * const pevt_whois1_help[] = {
1158 N_("Nickname"),
1159 N_("Username"),
1160 N_("Host"),
1161 N_("Full name"),
1164 static char * const pevt_whois2_help[] = {
1165 N_("Nickname"),
1166 N_("Channel Membership/\"is an IRC operator\""),
1169 static char * const pevt_whois3_help[] = {
1170 N_("Nickname"),
1171 N_("Server Information"),
1174 static char * const pevt_whois4_help[] = {
1175 N_("Nickname"),
1176 N_("Idle time"),
1179 static char * const pevt_whois4t_help[] = {
1180 N_("Nickname"),
1181 N_("Idle time"),
1182 N_("Signon time"),
1185 static char * const pevt_whois5_help[] = {
1186 N_("Nickname"),
1187 N_("Away reason"),
1190 static char * const pevt_whois6_help[] = {
1191 N_("Nickname"),
1194 static char * const pevt_whoisid_help[] = {
1195 N_("Nickname"),
1196 N_("Message"),
1197 "Numeric"
1200 static char * const pevt_whoisauth_help[] = {
1201 N_("Nickname"),
1202 N_("Message"),
1203 N_("Account"),
1206 static char * const pevt_whoisrealhost_help[] = {
1207 N_("Nickname"),
1208 N_("Real user@host"),
1209 N_("Real IP"),
1210 N_("Message"),
1213 static char * const pevt_generic_channel_help[] = {
1214 N_("Channel Name"),
1217 static char * const pevt_saslauth_help[] = {
1218 N_("Username"),
1219 N_("Mechanism")
1222 static char * const pevt_saslresponse_help[] = {
1223 N_("Server Name"),
1224 N_("Raw Numeric or Identifier"),
1225 N_("Username"),
1226 N_("Message")
1229 static char * const pevt_servertext_help[] = {
1230 N_("Text"),
1231 N_("Server Name"),
1232 N_("Raw Numeric or Identifier")
1235 static char * const pevt_sslmessage_help[] = {
1236 N_("Text"),
1237 N_("Server Name")
1240 static char * const pevt_invited_help[] = {
1241 N_("Channel Name"),
1242 N_("Nick of person who invited you"),
1243 N_("Server Name"),
1246 static char * const pevt_usersonchan_help[] = {
1247 N_("Channel Name"),
1248 N_("Users"),
1251 static char * const pevt_nickclash_help[] = {
1252 N_("Nickname in use"),
1253 N_("Nick being tried"),
1256 static char * const pevt_connfail_help[] = {
1257 N_("Error"),
1260 static char * const pevt_connect_help[] = {
1261 N_("Host"),
1262 N_("IP"),
1263 N_("Port"),
1266 static char * const pevt_sconnect_help[] = {
1267 "PID"
1270 static char * const pevt_generic_nick_help[] = {
1271 N_("Nickname"),
1272 N_("Server Name"),
1273 N_("Network")
1276 static char * const pevt_chanmodes_help[] = {
1277 N_("Channel Name"),
1278 N_("Modes string"),
1281 static char * const pevt_rawmodes_help[] = {
1282 N_("Nickname"),
1283 N_("Modes string"),
1286 static char * const pevt_kill_help[] = {
1287 N_("Nickname"),
1288 N_("Reason"),
1291 static char * const pevt_dccchaterr_help[] = {
1292 N_("Nickname"),
1293 N_("IP address"),
1294 N_("Port"),
1295 N_("Error"),
1298 static char * const pevt_dccstall_help[] = {
1299 N_("DCC Type"),
1300 N_("Filename"),
1301 N_("Nickname"),
1304 static char * const pevt_generic_file_help[] = {
1305 N_("Filename"),
1306 N_("Error"),
1309 static char * const pevt_dccrecverr_help[] = {
1310 N_("Filename"),
1311 N_("Destination filename"),
1312 N_("Nickname"),
1313 N_("Error"),
1316 static char * const pevt_dccrecvcomp_help[] = {
1317 N_("Filename"),
1318 N_("Destination filename"),
1319 N_("Nickname"),
1320 N_("CPS"),
1323 static char * const pevt_dccconfail_help[] = {
1324 N_("DCC Type"),
1325 N_("Nickname"),
1326 N_("Error"),
1329 static char * const pevt_dccchatcon_help[] = {
1330 N_("Nickname"),
1331 N_("IP address"),
1334 static char * const pevt_dcccon_help[] = {
1335 N_("Nickname"),
1336 N_("IP address"),
1337 N_("Filename"),
1340 static char * const pevt_dccsendfail_help[] = {
1341 N_("Filename"),
1342 N_("Nickname"),
1343 N_("Error"),
1346 static char * const pevt_dccsendcomp_help[] = {
1347 N_("Filename"),
1348 N_("Nickname"),
1349 N_("CPS"),
1352 static char * const pevt_dccoffer_help[] = {
1353 N_("Filename"),
1354 N_("Nickname"),
1355 N_("Pathname"),
1358 static char * const pevt_dccfileabort_help[] = {
1359 N_("Nickname"),
1360 N_("Filename")
1363 static char * const pevt_dccchatabort_help[] = {
1364 N_("Nickname"),
1367 static char * const pevt_dccresumeoffer_help[] = {
1368 N_("Nickname"),
1369 N_("Filename"),
1370 N_("Position"),
1373 static char * const pevt_dccsendoffer_help[] = {
1374 N_("Nickname"),
1375 N_("Filename"),
1376 N_("Size"),
1377 N_("IP address"),
1380 static char * const pevt_dccgenericoffer_help[] = {
1381 N_("DCC String"),
1382 N_("Nickname"),
1385 static char * const pevt_notifynumber_help[] = {
1386 N_("Number of notify items"),
1389 static char * const pevt_serverlookup_help[] = {
1390 N_("Server Name"),
1393 static char * const pevt_servererror_help[] = {
1394 N_("Text"),
1397 static char * const pevt_foundip_help[] = {
1398 N_("IP"),
1401 static char * const pevt_dccrename_help[] = {
1402 N_("Old Filename"),
1403 N_("New Filename"),
1406 static char * const pevt_ctcpsend_help[] = {
1407 N_("Receiver"),
1408 N_("Message"),
1411 static char * const pevt_ignoreaddremove_help[] = {
1412 N_("Hostmask"),
1415 static char * const pevt_resolvinguser_help[] = {
1416 N_("Nickname"),
1417 N_("Hostname"),
1420 static char * const pevt_malformed_help[] = {
1421 N_("Nickname"),
1422 N_("The Packet"),
1425 static char * const pevt_pingtimeout_help[] = {
1426 N_("Seconds"),
1429 static char * const pevt_uinvite_help[] = {
1430 N_("Nick of person who have been invited"),
1431 N_("Channel Name"),
1432 N_("Server Name"),
1435 static char * const pevt_banlist_help[] = {
1436 N_("Channel"),
1437 N_("Banmask"),
1438 N_("Who set the ban"),
1439 N_("Ban time"),
1442 static char * const pevt_discon_help[] = {
1443 N_("Error"),
1446 #include "textevents.h"
1448 static void
1449 pevent_load_defaults ()
1451 int i;
1453 for (i = 0; i < NUM_XP; i++)
1455 if (pntevts_text[i])
1456 free (pntevts_text[i]);
1458 /* make-te.c sets this 128 flag (DON'T call gettext() flag) */
1459 if (te[i].num_args & 128)
1460 pntevts_text[i] = strdup (te[i].def);
1461 else
1462 pntevts_text[i] = strdup (_(te[i].def));
1466 void
1467 pevent_make_pntevts ()
1469 int i, m;
1470 char out[1024];
1472 for (i = 0; i < NUM_XP; i++)
1474 if (pntevts[i] != NULL)
1475 free (pntevts[i]);
1476 if (pevt_build_string (pntevts_text[i], &(pntevts[i]), &m) != 0)
1478 snprintf (out, sizeof (out),
1479 _("Error parsing event %s.\nLoading default."), te[i].name);
1480 fe_message (out, FE_MSG_WARN);
1481 free (pntevts_text[i]);
1482 /* make-te.c sets this 128 flag (DON'T call gettext() flag) */
1483 if (te[i].num_args & 128)
1484 pntevts_text[i] = strdup (te[i].def);
1485 else
1486 pntevts_text[i] = strdup (_(te[i].def));
1487 if (pevt_build_string (pntevts_text[i], &(pntevts[i]), &m) != 0)
1489 fprintf (stderr,
1490 "XChat CRITICAL *** default event text failed to build!\n");
1491 abort ();
1497 /* Loading happens at 2 levels:
1498 1) File is read into blocks
1499 2) Pe block is parsed and loaded
1501 --AGL */
1503 /* Better hope you pass good args.. --AGL */
1505 static void
1506 pevent_trigger_load (int *i_penum, char **i_text, char **i_snd)
1508 int penum = *i_penum, len;
1509 char *text = *i_text, *snd = *i_snd;
1511 if (penum != -1 && text != NULL)
1513 len = strlen (text) + 1;
1514 if (pntevts_text[penum])
1515 free (pntevts_text[penum]);
1516 pntevts_text[penum] = malloc (len);
1517 memcpy (pntevts_text[penum], text, len);
1520 if (text)
1521 free (text);
1522 if (snd)
1523 free (snd);
1524 *i_text = NULL;
1525 *i_snd = NULL;
1526 *i_penum = 0;
1529 static int
1530 pevent_find (char *name, int *i_i)
1532 int i = *i_i, j;
1534 j = i + 1;
1535 while (1)
1537 if (j == NUM_XP)
1538 j = 0;
1539 if (strcmp (te[j].name, name) == 0)
1541 *i_i = j;
1542 return j;
1544 if (j == i)
1545 return -1;
1546 j++;
1551 pevent_load (char *filename)
1553 /* AGL, I've changed this file and pevent_save, could you please take a look at
1554 * the changes and possibly modify them to suit you
1555 * //David H
1557 char *buf, *ibuf;
1558 int fd, i = 0, pnt = 0;
1559 struct stat st;
1560 char *text = NULL, *snd = NULL;
1561 int penum = 0;
1562 char *ofs;
1564 if (filename == NULL)
1565 fd = xchat_open_file ("pevents.conf", O_RDONLY, 0, 0);
1566 else
1567 fd = xchat_open_file (filename, O_RDONLY, 0, XOF_FULLPATH);
1569 if (fd == -1)
1570 return 1;
1571 if (fstat (fd, &st) != 0)
1572 return 1;
1573 ibuf = malloc (st.st_size);
1574 read (fd, ibuf, st.st_size);
1575 close (fd);
1577 while (buf_get_line (ibuf, &buf, &pnt, st.st_size))
1579 if (buf[0] == '#')
1580 continue;
1581 if (strlen (buf) == 0)
1582 continue;
1584 ofs = strchr (buf, '=');
1585 if (!ofs)
1586 continue;
1587 *ofs = 0;
1588 ofs++;
1589 /*if (*ofs == 0)
1590 continue;*/
1592 if (strcmp (buf, "event_name") == 0)
1594 if (penum >= 0)
1595 pevent_trigger_load (&penum, &text, &snd);
1596 penum = pevent_find (ofs, &i);
1597 continue;
1598 } else if (strcmp (buf, "event_text") == 0)
1600 if (text)
1601 free (text);
1603 #if 0
1604 /* This allows updating of old strings. We don't use new defaults
1605 if the user has customized the strings (.e.g a text theme).
1606 Hash of the old default is enough to identify and replace it.
1607 This only works in English. */
1609 switch (g_str_hash (ofs))
1611 case 0x526743a4:
1612 /* %C08,02 Hostmask PRIV NOTI CHAN CTCP INVI UNIG %O */
1613 text = strdup (te[XP_TE_IGNOREHEADER].def);
1614 break;
1616 case 0xe91bc9c2:
1617 /* %C08,02 %O */
1618 text = strdup (te[XP_TE_IGNOREFOOTER].def);
1619 break;
1621 case 0x1fbfdf22:
1622 /* -%C10-%C11-%O$tDCC RECV: Cannot open $1 for writing - aborting. */
1623 text = strdup (te[XP_TE_DCCFILEERR].def);
1624 break;
1626 default:
1627 text = strdup (ofs);
1629 #else
1630 text = strdup (ofs);
1631 #endif
1633 continue;
1634 }/* else if (strcmp (buf, "event_sound") == 0)
1636 if (snd)
1637 free (snd);
1638 snd = strdup (ofs);
1639 continue;
1642 continue;
1645 pevent_trigger_load (&penum, &text, &snd);
1646 free (ibuf);
1647 return 0;
1650 static void
1651 pevent_check_all_loaded ()
1653 int i;
1655 for (i = 0; i < NUM_XP; i++)
1657 if (pntevts_text[i] == NULL)
1659 /*printf ("%s\n", te[i].name);
1660 snprintf(out, sizeof(out), "The data for event %s failed to load. Reverting to defaults.\nThis may be because a new version of XChat is loading an old config file.\n\nCheck all print event texts are correct", evtnames[i]);
1661 gtkutil_simpledialog(out); */
1662 /* make-te.c sets this 128 flag (DON'T call gettext() flag) */
1663 if (te[i].num_args & 128)
1664 pntevts_text[i] = strdup (te[i].def);
1665 else
1666 pntevts_text[i] = strdup (_(te[i].def));
1671 void
1672 load_text_events ()
1674 memset (&pntevts_text, 0, sizeof (char *) * (NUM_XP));
1675 memset (&pntevts, 0, sizeof (char *) * (NUM_XP));
1677 if (pevent_load (NULL))
1678 pevent_load_defaults ();
1679 pevent_check_all_loaded ();
1680 pevent_make_pntevts ();
1684 CL: format_event now handles filtering of arguments:
1685 1) if prefs.stripcolor is set, filter all style control codes from arguments
1686 2) always strip \010 (ATTR_HIDDEN) from arguments: it is only for use in the format string itself
1688 #define ARG_FLAG(argn) (1 << (argn))
1690 void
1691 format_event (session *sess, int index, char **args, char *o, int sizeofo, unsigned int stripcolor_args)
1693 int len, oi, ii, numargs;
1694 char *i, *ar, d, a, done_all = FALSE;
1696 i = pntevts[index];
1697 numargs = te[index].num_args & 0x7f;
1699 oi = ii = len = d = a = 0;
1700 o[0] = 0;
1702 if (i == NULL)
1703 return;
1705 while (done_all == FALSE)
1707 d = i[ii++];
1708 switch (d)
1710 case 0:
1711 memcpy (&len, &(i[ii]), sizeof (int));
1712 ii += sizeof (int);
1713 if (oi + len > sizeofo)
1715 printf ("Overflow in display_event (%s)\n", i);
1716 o[0] = 0;
1717 return;
1719 memcpy (&(o[oi]), &(i[ii]), len);
1720 oi += len;
1721 ii += len;
1722 break;
1723 case 1:
1724 a = i[ii++];
1725 if (a > numargs)
1727 fprintf (stderr,
1728 "XChat DEBUG: display_event: arg > numargs (%d %d %s)\n",
1729 a, numargs, i);
1730 break;
1732 ar = args[(int) a + 1];
1733 if (ar == NULL)
1735 printf ("arg[%d] is NULL in print event\n", a + 1);
1736 } else
1738 if (stripcolor_args & ARG_FLAG(a + 1)) len = strip_color2 (ar, -1, &o[oi], STRIP_ALL);
1739 else len = strip_hidden_attribute (ar, &o[oi]);
1740 oi += len;
1742 break;
1743 case 2:
1744 o[oi++] = '\n';
1745 o[oi++] = 0;
1746 done_all = TRUE;
1747 continue;
1748 case 3:
1749 /* if (sess->type == SESS_DIALOG)
1751 if (prefs.dialog_indent_nicks)
1752 o[oi++] = '\t';
1753 else
1754 o[oi++] = ' ';
1755 } else
1757 if (prefs.indent_nicks)
1758 o[oi++] = '\t';
1759 else
1760 o[oi++] = ' ';
1761 /*}*/
1762 break;
1765 o[oi] = 0;
1766 if (*o == '\n')
1767 o[0] = 0;
1770 static void
1771 display_event (session *sess, int event, char **args, unsigned int stripcolor_args)
1773 char o[4096];
1774 format_event (sess, event, args, o, sizeof (o), stripcolor_args);
1775 if (o[0])
1776 PrintText (sess, o);
1780 pevt_build_string (const char *input, char **output, int *max_arg)
1782 struct pevt_stage1 *s = NULL, *base = NULL, *last = NULL, *next;
1783 int clen;
1784 char o[4096], d, *obuf, *i;
1785 int oi, ii, max = -1, len, x;
1787 len = strlen (input);
1788 i = malloc (len + 1);
1789 memcpy (i, input, len + 1);
1790 check_special_chars (i, TRUE);
1792 len = strlen (i);
1794 clen = oi = ii = 0;
1796 for (;;)
1798 if (ii == len)
1799 break;
1800 d = i[ii++];
1801 if (d != '$')
1803 o[oi++] = d;
1804 continue;
1806 if (i[ii] == '$')
1808 o[oi++] = '$';
1809 continue;
1811 if (oi > 0)
1813 s = (struct pevt_stage1 *) malloc (sizeof (struct pevt_stage1));
1814 if (base == NULL)
1815 base = s;
1816 if (last != NULL)
1817 last->next = s;
1818 last = s;
1819 s->next = NULL;
1820 s->data = malloc (oi + sizeof (int) + 1);
1821 s->len = oi + sizeof (int) + 1;
1822 clen += oi + sizeof (int) + 1;
1823 s->data[0] = 0;
1824 memcpy (&(s->data[1]), &oi, sizeof (int));
1825 memcpy (&(s->data[1 + sizeof (int)]), o, oi);
1826 oi = 0;
1828 if (ii == len)
1830 fe_message ("String ends with a $", FE_MSG_WARN);
1831 return 1;
1833 d = i[ii++];
1834 if (d == 'a')
1835 { /* Hex value */
1836 x = 0;
1837 if (ii == len)
1838 goto a_len_error;
1839 d = i[ii++];
1840 d -= '0';
1841 x = d * 100;
1842 if (ii == len)
1843 goto a_len_error;
1844 d = i[ii++];
1845 d -= '0';
1846 x += d * 10;
1847 if (ii == len)
1848 goto a_len_error;
1849 d = i[ii++];
1850 d -= '0';
1851 x += d;
1852 if (x > 255)
1853 goto a_range_error;
1854 o[oi++] = x;
1855 continue;
1857 a_len_error:
1858 fe_message ("String ends in $a", FE_MSG_WARN);
1859 return 1;
1860 a_range_error:
1861 fe_message ("$a value is greater than 255", FE_MSG_WARN);
1862 return 1;
1864 if (d == 't')
1866 /* Tab - if tabnicks is set then write '\t' else ' ' */
1867 s = (struct pevt_stage1 *) malloc (sizeof (struct pevt_stage1));
1868 if (base == NULL)
1869 base = s;
1870 if (last != NULL)
1871 last->next = s;
1872 last = s;
1873 s->next = NULL;
1874 s->data = malloc (1);
1875 s->len = 1;
1876 clen += 1;
1877 s->data[0] = 3;
1879 continue;
1881 if (d < '1' || d > '9')
1883 snprintf (o, sizeof (o), "Error, invalid argument $%c\n", d);
1884 fe_message (o, FE_MSG_WARN);
1885 return 1;
1887 d -= '0';
1888 if (max < d)
1889 max = d;
1890 s = (struct pevt_stage1 *) malloc (sizeof (struct pevt_stage1));
1891 if (base == NULL)
1892 base = s;
1893 if (last != NULL)
1894 last->next = s;
1895 last = s;
1896 s->next = NULL;
1897 s->data = malloc (2);
1898 s->len = 2;
1899 clen += 2;
1900 s->data[0] = 1;
1901 s->data[1] = d - 1;
1903 if (oi > 0)
1905 s = (struct pevt_stage1 *) malloc (sizeof (struct pevt_stage1));
1906 if (base == NULL)
1907 base = s;
1908 if (last != NULL)
1909 last->next = s;
1910 last = s;
1911 s->next = NULL;
1912 s->data = malloc (oi + sizeof (int) + 1);
1913 s->len = oi + sizeof (int) + 1;
1914 clen += oi + sizeof (int) + 1;
1915 s->data[0] = 0;
1916 memcpy (&(s->data[1]), &oi, sizeof (int));
1917 memcpy (&(s->data[1 + sizeof (int)]), o, oi);
1918 oi = 0;
1920 s = (struct pevt_stage1 *) malloc (sizeof (struct pevt_stage1));
1921 if (base == NULL)
1922 base = s;
1923 if (last != NULL)
1924 last->next = s;
1925 last = s;
1926 s->next = NULL;
1927 s->data = malloc (1);
1928 s->len = 1;
1929 clen += 1;
1930 s->data[0] = 2;
1932 oi = 0;
1933 s = base;
1934 obuf = malloc (clen);
1935 while (s)
1937 next = s->next;
1938 memcpy (&obuf[oi], s->data, s->len);
1939 oi += s->len;
1940 free (s->data);
1941 free (s);
1942 s = next;
1945 free (i);
1947 if (max_arg)
1948 *max_arg = max;
1949 if (output)
1950 *output = obuf;
1952 return 0;
1956 /* black n white(0/1) are bad colors for nicks, and we'll use color 2 for us */
1957 /* also light/dark gray (14/15) */
1958 /* 5,7,8 are all shades of yellow which happen to look dman near the same */
1960 static char rcolors[] = { 19, 20, 22, 24, 25, 26, 27, 28, 29 };
1962 static int
1963 color_of (char *name)
1965 int i = 0, sum = 0;
1967 while (name[i])
1968 sum += name[i++];
1969 sum %= sizeof (rcolors) / sizeof (char);
1970 return rcolors[sum];
1974 /* called by EMIT_SIGNAL macro */
1975 #include <stdio.h>
1976 void
1977 text_emit (int index, session *sess, char *a, char *b, char *c, char *d, char* file, int lineno)
1979 char *word[PDIWORDS];
1980 int i;
1981 unsigned int stripcolor_args = (prefs.stripcolor ? 0xFFFFFFFF : 0);
1982 char tbuf[NICKLEN + 4];
1984 if(getenv("XCHATDEBUG"))
1985 dprintf(2, "%s:%d %s %s %s %s\n", file, lineno, a ? a : "", b ? b : "", c ? c : "", d ? d : "");
1987 if (prefs.colorednicks && (index == XP_TE_CHANACTION || index == XP_TE_CHANMSG))
1989 snprintf (tbuf, sizeof (tbuf), "\003%d%s", color_of (a), a);
1990 a = tbuf;
1991 stripcolor_args &= ~ARG_FLAG(1); /* don't strip color from this argument */
1994 word[0] = te[index].name;
1995 word[1] = (a ? a : "\000");
1996 word[2] = (b ? b : "\000");
1997 word[3] = (c ? c : "\000");
1998 word[4] = (d ? d : "\000");
1999 for (i = 5; i < PDIWORDS; i++)
2000 word[i] = "\000";
2002 if (plugin_emit_print (sess, word))
2003 return;
2005 /* If a plugin's callback executes "/close", 'sess' may be invalid */
2006 if (!is_session (sess))
2007 return;
2009 switch (index)
2011 case XP_TE_JOIN:
2012 case XP_TE_PART:
2013 case XP_TE_PARTREASON:
2014 case XP_TE_QUIT:
2015 /* implement ConfMode / Hide Join and Part Messages */
2016 if (chanopt_is_set (prefs.confmode, sess->text_hidejoinpart))
2017 return;
2018 break;
2020 /* ===Private message=== */
2021 case XP_TE_PRIVMSG:
2022 case XP_TE_DPRIVMSG:
2023 case XP_TE_PRIVACTION:
2024 case XP_TE_DPRIVACTION:
2025 if (chanopt_is_set_a (prefs.input_beep_priv, sess->alert_beep))
2026 sound_beep (sess);
2027 if (chanopt_is_set_a (prefs.input_flash_priv, sess->alert_taskbar))
2028 fe_flash_window (sess);
2029 /* why is this one different? because of plugin-tray.c's hooks! ugly */
2030 if (sess->alert_tray == SET_ON)
2031 fe_tray_set_icon (FE_ICON_MESSAGE);
2032 break;
2034 /* ===Highlighted message=== */
2035 case XP_TE_HCHANACTION:
2036 case XP_TE_HCHANMSG:
2037 if (chanopt_is_set_a (prefs.input_beep_hilight, sess->alert_beep))
2038 sound_beep (sess);
2039 if (chanopt_is_set_a (prefs.input_flash_hilight, sess->alert_taskbar))
2040 fe_flash_window (sess);
2041 if (sess->alert_tray == SET_ON)
2042 fe_tray_set_icon (FE_ICON_MESSAGE);
2043 break;
2045 /* ===Channel message=== */
2046 case XP_TE_CHANACTION:
2047 case XP_TE_CHANMSG:
2048 if (chanopt_is_set_a (prefs.input_beep_chans, sess->alert_beep))
2049 sound_beep (sess);
2050 if (chanopt_is_set_a (prefs.input_flash_chans, sess->alert_taskbar))
2051 fe_flash_window (sess);
2052 if (sess->alert_tray == SET_ON)
2053 fe_tray_set_icon (FE_ICON_MESSAGE);
2054 break;
2055 case XP_TE_DISCON:
2056 /* do not spam log */
2057 if(!sess->total) return;
2060 sound_play_event (index);
2061 display_event (sess, index, word, stripcolor_args);
2064 char *
2065 text_find_format_string (char *name)
2067 int i = 0;
2069 i = pevent_find (name, &i);
2070 if (i >= 0)
2071 return pntevts_text[i];
2073 return NULL;
2077 text_emit_by_name (char *name, session *sess, char *a, char *b, char *c, char *d)
2079 int i = 0;
2081 i = pevent_find (name, &i);
2082 if (i >= 0)
2084 text_emit (i, sess, a, b, c, d, "", 0);
2085 return 1;
2088 return 0;
2091 void
2092 pevent_save (char *fn)
2094 int fd, i;
2095 char buf[1024];
2097 if (!fn)
2098 fd = xchat_open_file ("pevents.conf", O_CREAT | O_TRUNC | O_WRONLY,
2099 0x180, XOF_DOMODE);
2100 else
2101 fd = xchat_open_file (fn, O_CREAT | O_TRUNC | O_WRONLY, 0x180,
2102 XOF_FULLPATH | XOF_DOMODE);
2103 if (fd == -1)
2106 fe_message ("Error opening config file\n", FALSE);
2107 If we get here when X-Chat is closing the fe-message causes a nice & hard crash
2108 so we have to use perror which doesn't rely on GTK
2111 perror ("Error opening config file\n");
2112 return;
2115 for (i = 0; i < NUM_XP; i++)
2117 write (fd, buf, snprintf (buf, sizeof (buf),
2118 "event_name=%s\n", te[i].name));
2119 write (fd, buf, snprintf (buf, sizeof (buf),
2120 "event_text=%s\n\n", pntevts_text[i]));
2123 close (fd);
2126 /* =========================== */
2127 /* ========== SOUND ========== */
2128 /* =========================== */
2130 char *sound_files[NUM_XP];
2132 void
2133 sound_beep (session *sess)
2135 if (sound_files[XP_TE_BEEP] && sound_files[XP_TE_BEEP][0])
2136 /* user defined beep _file_ */
2137 sound_play_event (XP_TE_BEEP);
2138 else
2139 /* system beep */
2140 fe_beep ();
2143 static char *
2144 sound_find_command (void)
2146 /* some sensible unix players. You're bound to have one of them */
2147 static const char * const progs[] = {"aplay", "esdplay", "soxplay", "artsplay", NULL};
2148 char *cmd;
2149 int i = 0;
2151 if (prefs.soundcmd[0])
2152 return g_strdup (prefs.soundcmd);
2154 while (progs[i])
2156 cmd = g_find_program_in_path (progs[i]);
2157 if (cmd)
2158 return cmd;
2159 i++;
2162 return NULL;
2165 void
2166 sound_play (const char *file, gboolean quiet)
2168 char buf[512];
2169 char wavfile[512];
2170 char *file_fs;
2171 char *cmd;
2173 /* the pevents GUI editor triggers this after removing a soundfile */
2174 if (!file[0])
2175 return;
2177 if (file[0] != '/')
2179 snprintf (wavfile, sizeof (wavfile), "%s/%s", prefs.sounddir, file);
2180 } else
2182 strncpy (wavfile, file, sizeof (wavfile));
2184 wavfile[sizeof (wavfile) - 1] = 0; /* ensure termination */
2186 file_fs = xchat_filename_from_utf8 (wavfile, -1, 0, 0, 0);
2187 if (!file_fs)
2188 return;
2190 if (access (file_fs, R_OK) == 0)
2192 cmd = sound_find_command ();
2194 if (cmd)
2196 if (strchr (file_fs, ' '))
2197 snprintf (buf, sizeof (buf), "%s \"%s\"", cmd, file_fs);
2198 else
2199 snprintf (buf, sizeof (buf), "%s %s", cmd, file_fs);
2200 buf[sizeof (buf) - 1] = '\0';
2201 xchat_exec (buf);
2204 if (cmd)
2205 g_free (cmd);
2207 } else
2209 if (!quiet)
2211 snprintf (buf, sizeof (buf), _("Cannot read sound file:\n%s"), wavfile);
2212 fe_message (buf, FE_MSG_ERROR);
2216 g_free (file_fs);
2219 void
2220 sound_play_event (int i)
2222 if (sound_files[i])
2223 sound_play (sound_files[i], FALSE);
2226 static void
2227 sound_load_event (char *evt, char *file)
2229 int i = 0;
2231 if (file[0] && pevent_find (evt, &i) != -1)
2233 if (sound_files[i])
2234 free (sound_files[i]);
2235 sound_files[i] = strdup (file);
2239 void
2240 sound_load ()
2242 int fd;
2243 char buf[512];
2244 char evt[128];
2246 memset (&sound_files, 0, sizeof (char *) * (NUM_XP));
2248 fd = xchat_open_file ("sound.conf", O_RDONLY, 0, 0);
2249 if (fd == -1)
2250 return;
2252 evt[0] = 0;
2253 while (waitline (fd, buf, sizeof buf, FALSE) != -1)
2255 if (strncmp (buf, "event=", 6) == 0)
2257 safe_strcpy (evt, buf + 6, sizeof (evt));
2259 else if (strncmp (buf, "sound=", 6) == 0)
2261 if (evt[0] != 0)
2263 sound_load_event (evt, buf + 6);
2264 evt[0] = 0;
2269 close (fd);
2272 void
2273 sound_save ()
2275 int fd, i;
2276 char buf[512];
2278 fd = xchat_open_file ("sound.conf", O_CREAT | O_TRUNC | O_WRONLY, 0x180,
2279 XOF_DOMODE);
2280 if (fd == -1)
2281 return;
2283 for (i = 0; i < NUM_XP; i++)
2285 if (sound_files[i] && sound_files[i][0])
2287 write (fd, buf, snprintf (buf, sizeof (buf),
2288 "event=%s\n", te[i].name));
2289 write (fd, buf, snprintf (buf, sizeof (buf),
2290 "sound=%s\n\n", sound_files[i]));
2294 close (fd);