restructure configure so pkg-config derived SSL flags get used
[rofl0r-ixchat.git] / src / fe-text / fe-text.c
blob30054e04467bdd5f0c17e4136ea14857460c1993
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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #ifdef HAVE_STRINGS_H
23 #include <strings.h>
24 #endif
25 #include <sys/time.h>
26 #include <sys/types.h>
27 #include <unistd.h>
28 #include <ctype.h>
29 #include "../common/xchat.h"
30 #include "../common/xchatc.h"
31 #include "../common/outbound.h"
32 #include "../common/util.h"
33 #include "../common/fe.h"
34 #include "fe-text.h"
37 static GSList *tmr_list; /* timer list */
38 static int tmr_list_count;
39 static GSList *se_list; /* socket event list */
40 static int se_list_count;
41 static int done = FALSE; /* finished ? */
44 static void
45 send_command (char *cmd)
47 handle_multiline (sess_list->data, cmd, TRUE, FALSE);
50 static void
51 read_stdin (void)
53 int len, i = 0;
54 static int pos = 0;
55 static char inbuf[1024];
56 char tmpbuf[512];
58 len = read (STDIN_FILENO, tmpbuf, sizeof tmpbuf - 1);
60 while (i < len)
62 switch (tmpbuf[i])
64 case '\r':
65 break;
67 case '\n':
68 inbuf[pos] = 0;
69 pos = 0;
70 send_command (inbuf);
71 break;
73 default:
74 inbuf[pos] = tmpbuf[i];
75 if (pos < (sizeof inbuf - 2))
76 pos++;
78 i++;
82 static int done_intro = 0;
84 void
85 fe_new_window (struct session *sess, int focus)
87 char buf[512];
89 sess->gui = malloc (4);
91 if (!sess->server->front_session)
92 sess->server->front_session = sess;
93 if (!sess->server->server_session)
94 sess->server->server_session = sess;
95 if (!current_tab)
96 current_tab = sess;
98 if (done_intro)
99 return;
100 done_intro = 1;
102 snprintf (buf, sizeof (buf),
103 "\n"
104 " \017xchat \00310"PACKAGE_VERSION"\n"
105 " \017Running on \00310%s \017glib \00310%d.%d.%d\n"
106 get_cpu_str(),
107 glib_major_version, glib_minor_version, glib_micro_version);
108 fe_print_text (sess, buf, 0);
110 fe_print_text (sess, "\n\nCompiled in Features\0032:\017 "
111 #ifdef USE_PLUGIN
112 "Plugin "
113 #endif
114 #ifdef ENABLE_NLS
115 "NLS "
116 #endif
117 #ifdef USE_OPENSSL
118 "OpenSSL "
119 #endif
120 #ifdef USE_IPV6
121 "IPv6"
122 #endif
123 "\n\n", 0);
124 fflush (stdout);
125 fflush (stdin);
128 static int
129 get_stamp_str (time_t tim, char *dest, int size)
131 return strftime (dest, size, prefs.stamp_format, localtime (&tim));
134 static int
135 timecat (char *buf)
137 char stampbuf[64];
139 get_stamp_str (time (0), stampbuf, sizeof (stampbuf));
140 strcat (buf, stampbuf);
141 return strlen (stampbuf);
144 /* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 */
145 static const short colconv[] = { 0, 7, 4, 2, 1, 3, 5, 11, 13, 12, 6, 16, 14, 15, 10, 7 };
147 void
148 fe_print_text (struct session *sess, char *text, time_t stamp)
150 int dotime = FALSE;
151 char num[8];
152 int reverse = 0, under = 0, bold = 0,
153 comma, k, i = 0, j = 0, len = strlen (text);
154 unsigned char *newtext = malloc (len + 1024);
156 if (prefs.timestamp)
158 newtext[0] = 0;
159 j += timecat (newtext);
161 while (i < len)
163 if (dotime && text[i] != 0)
165 dotime = FALSE;
166 newtext[j] = 0;
167 j += timecat (newtext);
169 switch (text[i])
171 case 3:
172 i++;
173 if (!isdigit (text[i]))
175 newtext[j] = 27;
176 j++;
177 newtext[j] = '[';
178 j++;
179 newtext[j] = 'm';
180 j++;
181 i--;
182 goto jump2;
184 k = 0;
185 comma = FALSE;
186 while (i < len)
188 if (text[i] >= '0' && text[i] <= '9' && k < 2)
190 num[k] = text[i];
191 k++;
192 } else
194 int col, mirc;
195 num[k] = 0;
196 newtext[j] = 27;
197 j++;
198 newtext[j] = '[';
199 j++;
200 if (k == 0)
202 newtext[j] = 'm';
203 j++;
204 } else
206 if (comma)
207 col = 40;
208 else
209 col = 30;
210 mirc = atoi (num);
211 mirc = colconv[mirc];
212 if (mirc > 9)
214 mirc += 50;
215 sprintf ((char *) &newtext[j], "%dm", mirc + col);
216 } else
218 sprintf ((char *) &newtext[j], "%dm", mirc + col);
220 j = strlen (newtext);
222 switch (text[i])
224 case ',':
225 comma = TRUE;
226 break;
227 default:
228 goto jump;
230 k = 0;
232 i++;
234 break;
235 case '\026': /* REVERSE */
236 if (reverse)
238 reverse = FALSE;
239 strcpy (&newtext[j], "\033[27m");
240 } else
242 reverse = TRUE;
243 strcpy (&newtext[j], "\033[7m");
245 j = strlen (newtext);
246 break;
247 case '\037': /* underline */
248 if (under)
250 under = FALSE;
251 strcpy (&newtext[j], "\033[24m");
252 } else
254 under = TRUE;
255 strcpy (&newtext[j], "\033[4m");
257 j = strlen (newtext);
258 break;
259 case '\002': /* bold */
260 if (bold)
262 bold = FALSE;
263 strcpy (&newtext[j], "\033[22m");
264 } else
266 bold = TRUE;
267 strcpy (&newtext[j], "\033[1m");
269 j = strlen (newtext);
270 break;
271 case '\007':
272 if (!prefs.filterbeep)
274 newtext[j] = text[i];
275 j++;
277 break;
278 case '\017': /* reset all */
279 strcpy (&newtext[j], "\033[m");
280 j += 3;
281 reverse = FALSE;
282 bold = FALSE;
283 under = FALSE;
284 break;
285 case '\t':
286 newtext[j] = ' ';
287 j++;
288 break;
289 case '\n':
290 newtext[j] = '\r';
291 j++;
292 if (prefs.timestamp)
293 dotime = TRUE;
294 default:
295 newtext[j] = text[i];
296 j++;
298 jump2:
299 i++;
300 jump:
301 i += 0;
303 newtext[j] = 0;
304 write (STDOUT_FILENO, newtext, j);
305 free (newtext);
308 void
309 fe_timeout_remove (int tag)
311 timerevent *te;
312 GSList *list;
314 list = tmr_list;
315 while (list)
317 te = (timerevent *) list->data;
318 if (te->tag == tag)
320 tmr_list = g_slist_remove (tmr_list, te);
321 free (te);
322 return;
324 list = list->next;
329 fe_timeout_add (int interval, void *callback, void *userdata)
331 struct timeval now;
332 timerevent *te = malloc (sizeof (timerevent));
334 tmr_list_count++; /* this overflows at 2.2Billion, who cares!! */
336 te->tag = tmr_list_count;
337 te->interval = interval;
338 te->callback = callback;
339 te->userdata = userdata;
341 gettimeofday (&now, NULL);
342 te->next_call = now.tv_sec * 1000 + (now.tv_usec / 1000) + te->interval;
344 tmr_list = g_slist_prepend (tmr_list, te);
346 return te->tag;
349 void
350 fe_input_remove (int tag)
352 socketevent *se;
353 GSList *list;
355 list = se_list;
356 while (list)
358 se = (socketevent *) list->data;
359 if (se->tag == tag)
361 se_list = g_slist_remove (se_list, se);
362 free (se);
363 return;
365 list = list->next;
370 fe_input_add (int sok, int flags, void *func, void *data)
372 socketevent *se = malloc (sizeof (socketevent));
374 se_list_count++; /* this overflows at 2.2Billion, who cares!! */
376 se->tag = se_list_count;
377 se->sok = sok;
378 se->rread = flags & FIA_READ;
379 se->wwrite = flags & FIA_WRITE;
380 se->eexcept = flags & FIA_EX;
381 se->callback = func;
382 se->userdata = data;
383 se_list = g_slist_prepend (se_list, se);
385 return se->tag;
389 fe_args (int argc, char *argv[])
391 if (argc > 1)
393 if (!strcasecmp (argv[1], "--version") || !strcasecmp (argv[1], "-v"))
395 puts (PACKAGE_VERSION);
396 return 0;
399 return -1;
402 void
403 fe_init (void)
405 se_list = 0;
406 se_list_count = 0;
407 tmr_list = 0;
408 tmr_list_count = 0;
409 prefs.autosave = 0;
410 prefs.use_server_tab = 0;
411 prefs.autodialog = 0;
412 prefs.lagometer = 0;
413 prefs.slist_skip = 1;
416 void
417 fe_main (void)
419 struct timeval timeout, now;
420 socketevent *se;
421 timerevent *te;
422 fd_set rd, wd, ex;
423 GSList *list;
424 guint64 shortest, delay;
426 if (!sess_list)
427 new_ircwindow (NULL, NULL, SESS_SERVER, 0);
429 while (!done)
431 FD_ZERO (&rd);
432 FD_ZERO (&wd);
433 FD_ZERO (&ex);
435 list = se_list;
436 while (list)
438 se = (socketevent *) list->data;
439 if (se->rread)
440 FD_SET (se->sok, &rd);
441 if (se->wwrite)
442 FD_SET (se->sok, &wd);
443 if (se->eexcept)
444 FD_SET (se->sok, &ex);
445 list = list->next;
448 FD_SET (STDIN_FILENO, &rd); /* for reading keyboard */
450 /* find the shortest timeout event */
451 shortest = 0;
452 list = tmr_list;
453 while (list)
455 te = (timerevent *) list->data;
456 if (te->next_call < shortest || shortest == 0)
457 shortest = te->next_call;
458 list = list->next;
460 gettimeofday (&now, NULL);
461 delay = shortest - ((now.tv_sec * 1000) + (now.tv_usec / 1000));
462 timeout.tv_sec = delay / 1000;
463 timeout.tv_usec = (delay % 1000) * 1000;
465 select (FD_SETSIZE, &rd, &wd, &ex, &timeout);
467 if (FD_ISSET (STDIN_FILENO, &rd))
468 read_stdin ();
470 /* set all checked flags to false */
471 list = se_list;
472 while (list)
474 se = (socketevent *) list->data;
475 se->checked = 0;
476 list = list->next;
479 /* check all the socket callbacks */
480 list = se_list;
481 while (list)
483 se = (socketevent *) list->data;
484 se->checked = 1;
485 if (se->rread && FD_ISSET (se->sok, &rd))
487 se->callback (NULL, 1, se->userdata);
488 } else if (se->wwrite && FD_ISSET (se->sok, &wd))
490 se->callback (NULL, 2, se->userdata);
491 } else if (se->eexcept && FD_ISSET (se->sok, &ex))
493 se->callback (NULL, 4, se->userdata);
495 list = se_list;
496 if (list)
498 se = (socketevent *) list->data;
499 while (se->checked)
501 list = list->next;
502 if (!list)
503 break;
504 se = (socketevent *) list->data;
509 /* now check our list of timeout events, some might need to be called! */
510 gettimeofday (&now, NULL);
511 list = tmr_list;
512 while (list)
514 te = (timerevent *) list->data;
515 list = list->next;
516 if (now.tv_sec * 1000 + (now.tv_usec / 1000) >= te->next_call)
518 /* if the callback returns 0, it must be removed */
519 if (te->callback (te->userdata) == 0)
521 fe_timeout_remove (te->tag);
522 } else
524 te->next_call = now.tv_sec * 1000 + (now.tv_usec / 1000) + te->interval;
532 void
533 fe_exit (void)
535 done = TRUE;
538 void
539 fe_new_server (struct server *serv)
541 serv->gui = malloc (4);
544 void
545 fe_message (char *msg, int flags)
547 puts (msg);
550 void
551 fe_close_window (struct session *sess)
553 session_free (sess);
554 done = TRUE;
557 void
558 fe_beep (void)
560 putchar (7);
563 void
564 fe_add_rawlog (struct server *serv, char *text, int len, int outbound)
567 void
568 fe_set_topic (struct session *sess, char *topic, char *stripped_topic)
571 void
572 fe_cleanup (void)
575 void
576 fe_set_hilight (struct session *sess)
579 void
580 fe_set_tab_color (struct session *sess, int col)
583 void
584 fe_update_mode_buttons (struct session *sess, char mode, char sign)
587 void
588 fe_update_channel_key (struct session *sess)
591 void
592 fe_update_channel_limit (struct session *sess)
596 fe_is_chanwindow (struct server *serv)
598 return 0;
601 void
602 fe_add_chan_list (struct server *serv, char *chan, char *users, char *topic)
605 void
606 fe_chan_list_end (struct server *serv)
610 fe_is_banwindow (struct session *sess)
612 return 0;
614 void
615 fe_add_ban_list (struct session *sess, char *mask, char *who, char *when, int is_exemption)
618 void
619 fe_ban_list_end (struct session *sess, int is_exemption)
622 void
623 fe_notify_update (char *name)
626 void
627 fe_notify_ask (char *name, char *networks)
630 void
631 fe_text_clear (struct session *sess, int lines)
634 void
635 fe_progressbar_start (struct session *sess)
638 void
639 fe_progressbar_end (struct server *serv)
642 void
643 fe_userlist_insert (struct session *sess, struct User *newuser, int row, int sel)
647 fe_userlist_remove (struct session *sess, struct User *user)
649 return 0;
651 void
652 fe_userlist_rehash (struct session *sess, struct User *user)
655 void
656 fe_userlist_move (struct session *sess, struct User *user, int new_row)
659 void
660 fe_userlist_numbers (struct session *sess)
663 void
664 fe_userlist_clear (struct session *sess)
667 void
668 fe_userlist_set_selected (struct session *sess)
671 void
672 fe_dcc_add (struct DCC *dcc)
675 void
676 fe_dcc_update (struct DCC *dcc)
679 void
680 fe_dcc_remove (struct DCC *dcc)
683 void
684 fe_clear_channel (struct session *sess)
687 void
688 fe_session_callback (struct session *sess)
691 void
692 fe_server_callback (struct server *serv)
695 void
696 fe_url_add (const char *text)
699 void
700 fe_pluginlist_update (void)
703 void
704 fe_buttons_update (struct session *sess)
707 void
708 fe_dlgbuttons_update (struct session *sess)
711 void
712 fe_dcc_send_filereq (struct session *sess, char *nick, int maxcps, int passive)
715 void
716 fe_set_channel (struct session *sess)
719 void
720 fe_set_title (struct session *sess)
723 void
724 fe_set_nonchannel (struct session *sess, int state)
727 void
728 fe_set_nick (struct server *serv, char *newnick)
731 void
732 fe_change_nick (struct server *serv, char *nick, char *newnick)
735 void
736 fe_ignore_update (int level)
740 fe_dcc_open_recv_win (int passive)
742 return FALSE;
745 fe_dcc_open_send_win (int passive)
747 return FALSE;
750 fe_dcc_open_chat_win (int passive)
752 return FALSE;
754 void
755 fe_userlist_hide (session * sess)
758 void
759 fe_lastlog (session * sess, session * lastlog_sess, char *sstr, gboolean regexp)
762 void
763 fe_set_lag (server * serv, int lag)
766 void
767 fe_set_throttle (server * serv)
770 void
771 fe_set_away (server *serv)
774 void
775 fe_serverlist_open (session *sess)
778 void
779 fe_get_str (char *prompt, char *def, void *callback, void *ud)
782 void
783 fe_get_int (char *prompt, int def, void *callback, void *ud)
786 void
787 fe_idle_add (void *func, void *data)
790 void
791 fe_ctrl_gui (session *sess, fe_gui_action action, int arg)
795 fe_gui_info (session *sess, int info_type)
797 return -1;
799 void *
800 fe_gui_info_ptr (session *sess, int info_type)
802 return NULL;
804 void fe_confirm (const char *message, void (*yesproc)(void *), void (*noproc)(void *), void *ud)
807 char *fe_get_inputbox_contents (struct session *sess)
809 return NULL;
811 void fe_set_inputbox_contents (struct session *sess, char *text)
814 int fe_get_inputbox_cursor (struct session *sess)
816 return 0;
818 void fe_set_inputbox_cursor (struct session *sess, int delta, int pos)
821 void fe_open_url (const char *url)
824 void fe_menu_del (menu_entry *me)
827 char *fe_menu_add (menu_entry *me)
829 return NULL;
831 void fe_menu_update (menu_entry *me)
834 void fe_uselect (struct session *sess, char *word[], int do_clear, int scroll_to)
837 void
838 fe_server_event (server *serv, int type, int arg)
841 void
842 fe_flash_window (struct session *sess)
845 void fe_get_file (const char *title, char *initial,
846 void (*callback) (void *userdata, char *file), void *userdata,
847 int flags)
850 void fe_tray_set_flash (const char *filename1, const char *filename2, int timeout){}
851 void fe_tray_set_file (const char *filename){}
852 void fe_tray_set_icon (feicon icon){}
853 void fe_tray_set_tooltip (const char *text){}
854 void fe_tray_set_balloon (const char *title, const char *text){}
855 void fe_userlist_update (session *sess, struct User *user){}