home *** CD-ROM | disk | FTP | other *** search
/ Dream 52 / Amiga_Dream_52.iso / Linux / Divers / yagirc-0.51.tar.gz / yagirc-0.51.tar / yagirc-0.51 / no_ui.c < prev    next >
C/C++ Source or Header  |  1998-05-06  |  9KB  |  374 lines

  1. #ifndef USE_SCRIPT
  2. #error You need to have USE_SCRIPT enabled from Makefile to make BOT-version of yagIRC...
  3. #endif
  4.  
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <stdlib.h>
  8.  
  9. #include <pwd.h>
  10. #include <unistd.h>
  11.  
  12. #include <glib.h>
  13.  
  14. #include "irc.h"
  15. #include "commands.h"
  16. #include "script.h"
  17.  
  18. typedef struct
  19. {
  20.     int handle;
  21.     int mode;
  22.     int tag;
  23.     GUI_INPUT_FUNC func;
  24.     void *data;
  25. }
  26. INPUT_REC;
  27.  
  28. typedef struct
  29. {
  30.     int ms;
  31.     int gone;
  32.     int tag;
  33.     GUI_TIMEOUT_FUNC func;
  34.     void *data;
  35. }
  36. TIMEOUT_REC;
  37.  
  38. static GSList *inputlist, *timelist;
  39. static int maxhandle, mintimeout, incount, timecount;
  40.  
  41. static int quit_program;
  42.  
  43. char *default_server;
  44. int default_server_port;
  45. char *default_nick;
  46. char *real_name;
  47. char *user_name;
  48.  
  49. /* Create DCC transfer window */
  50. void gui_dcc_init(DCC_REC *dcc) {}
  51. /* Update DCC transfer window */
  52. void gui_dcc_update(DCC_REC *dcc) {}
  53. /* Close DCC transfer window */
  54. void gui_dcc_close(DCC_REC *dcc) { g_free(dcc); }
  55. /* Destroy DCC transfer window */
  56. void gui_dcc_force_close(DCC_REC *dcc) {}
  57.  
  58. /* DCC chat connection established - do something.. */
  59. void gui_dcc_chat_init(DCC_REC *dcc) {}
  60. /* Text received from DCC chat - write to screen */
  61. void gui_dcc_chat_write(DCC_REC *dcc, char *str) {}
  62. /* Hilight channel button */
  63. void gui_channel_hilight(CHAN_REC *chan) {}
  64. /* Dehilight channel button */
  65. void gui_channel_dehilight(CHAN_REC *chan) {}
  66. /* Select channel in specific window, if channel is in some other window,
  67.    move it from there */
  68. void gui_select_channel(WINDOW_REC *win, CHAN_REC *chan) {}
  69. /* Redraw nick list */
  70. void gui_nicklist_redraw(WINDOW_REC *window) {}
  71.  
  72. /* Change nick name */
  73. void gui_nick_change(SERVER_REC *server, char *from, char *to) {}
  74. /* Change nick's mode (@, +) */
  75. void gui_change_nick_mode(CHAN_REC *chan, char *nick, char type) {}
  76. /* Someone joined some channel.. If nick==NULL, you joined */
  77. void gui_channel_join(CHAN_REC *chan, char *nick) {}
  78. /* Someone left some channel.. If nick==NULL, you left, if chan==NULL someone
  79.    quit from IRC */
  80. void gui_channel_part(CHAN_REC *chan, char *nick) {}
  81.  
  82. /* Change topic of channel */
  83. void gui_change_topic(CHAN_REC *chan) {}
  84.  
  85. void printtext(char *str, WINDOW_REC *win) {}
  86.  
  87. /* Initialize IRC window */
  88. void gui_window_init(WINDOW_REC *win, WINDOW_REC *parent)
  89. {
  90.     win->drawfunc = (DRAW_FUNC) printtext;
  91.     win->drawfuncdata = win;
  92. }
  93.  
  94. /* Deinitialize IRC window */
  95. void gui_window_deinit(WINDOW_REC *win) {}
  96.  
  97. /* IRC channel/window changed, update everything you can think of.. */
  98. void gui_window_update(WINDOW_REC *win) {}
  99.  
  100. /* Change focus to specified window */
  101. void gui_window_select(WINDOW_REC *win) {}
  102.  
  103. /* Quit program requested */
  104. void gui_exit(void)
  105. {
  106.     quit_program = 1;
  107. }
  108.  
  109. /* Inform GUI what connection state we are in - 0 = disconnected, 1 = socket
  110.    connected, 2 = IRC server ready */
  111. void gui_connected(SERVER_REC *serv, int state) {}
  112.  
  113. /* Someone in notify list joined IRC */
  114. void gui_notify_join(char *nick) {}
  115. /* Someone in notify list left IRC */
  116. void gui_notify_part(char *nick) {}
  117.  
  118. /* Update status bar */
  119. void gui_update_statusbar(WINDOW_REC *win) {}
  120.  
  121. int gui_input_add(int fh, int mode, GUI_INPUT_FUNC func, void *data)
  122. {
  123.     INPUT_REC *inp;
  124.  
  125.     inp = (INPUT_REC *) g_malloc(sizeof(INPUT_REC));
  126.     inp->handle = fh;
  127.     inp->mode = mode;
  128.     inp->func = func;
  129.     inp->data = data;
  130.     inp->tag = ++incount;
  131.     if (fh > maxhandle) maxhandle = fh;
  132.  
  133.     inputlist = g_slist_append(inputlist, inp);
  134.     return incount;
  135. }
  136.  
  137. void gui_input_remove(int tag)
  138. {
  139.     GSList *tmp, *rec;
  140.     int maxhandle;
  141.  
  142.     rec = NULL; maxhandle = -1;
  143.     for (tmp = inputlist; tmp != NULL; tmp = tmp->next)
  144.     {
  145.         INPUT_REC *inp;
  146.  
  147.         inp = (INPUT_REC *) tmp->data;
  148.         if (inp->tag == tag)
  149.         {
  150.             g_free(tmp->data);
  151.             rec = tmp;
  152.         }
  153.         else
  154.         {
  155.             if (inp->handle > maxhandle) maxhandle = inp->handle;
  156.         }
  157.     }
  158.  
  159.     if (rec != NULL)
  160.     {
  161.         incount--;
  162.         g_slist_remove_link(inputlist, rec);
  163.     }
  164. }
  165.  
  166. int gui_timeout_new(int ms, GUI_TIMEOUT_FUNC func, void *data)
  167. {
  168.     TIMEOUT_REC *t;
  169.  
  170.     t = (TIMEOUT_REC *) g_malloc(sizeof(TIMEOUT_REC));
  171.     t->ms = ms;
  172.     t->gone = 0;
  173.     t->func = func;
  174.     t->data = data;
  175.     t->tag = ++timecount;
  176.     if (ms < mintimeout) mintimeout = ms;
  177.     timelist = g_slist_append(timelist, t);
  178.     return timecount;
  179. }
  180.  
  181. void gui_timeout_remove(int tag)
  182. {
  183.     GSList *tmp, *rec;
  184.  
  185.     rec = NULL; mintimeout = 1000;
  186.     for (tmp = timelist; tmp != NULL; tmp = tmp->next)
  187.     {
  188.         TIMEOUT_REC *t;
  189.  
  190.         t = (TIMEOUT_REC *) tmp->data;
  191.         if (t->tag == tag)
  192.         {
  193.             g_free(tmp->data);
  194.             rec = tmp;
  195.         }
  196.         else
  197.         {
  198.             if (t->ms < mintimeout) mintimeout = t->ms;
  199.         }
  200.     }
  201.     if (rec != NULL)
  202.     {
  203.         timecount--;
  204.         g_slist_remove_link(timelist, rec);
  205.     }
  206. }
  207.  
  208. void gui_init(void)
  209. {
  210.     inputlist = timelist = NULL;
  211.     maxhandle = -1; mintimeout = 1000; incount = 0; timecount = 0;
  212.     quit_program = 0;
  213. }
  214.  
  215. void gui_deinit(void)
  216. {
  217.     GSList *tmp;
  218.  
  219.     for (tmp = inputlist; tmp != NULL; tmp = tmp->next)
  220.         g_free(tmp->data);
  221.     g_slist_free(inputlist);
  222.  
  223.     for (tmp = timelist; tmp != NULL; tmp = tmp->next)
  224.         g_free(tmp->data);
  225.     g_slist_free(timelist);
  226.  
  227.     g_free(user_name);
  228.     g_free(real_name);
  229.     g_free(default_nick);
  230.     if (default_server != NULL) g_free(default_server);
  231. }
  232.  
  233. void main_loop(void)
  234. {
  235.     GSList *tmp;
  236.     static fd_set infd, outfd;
  237.  
  238.     while (!quit_program)
  239.     {
  240.         struct timeval tv;
  241.  
  242.         FD_ZERO(&infd); FD_ZERO(&outfd);
  243.         for (tmp = inputlist; tmp != NULL; tmp = tmp->next)
  244.         {
  245.             INPUT_REC *inp;
  246.  
  247.             inp = (INPUT_REC *) tmp->data;
  248.  
  249.             if (inp->mode == GUI_INPUT_READ)
  250.                 FD_SET(inp->handle, &infd);
  251.             else
  252.                 FD_SET(inp->handle, &outfd);
  253.         }
  254.  
  255.         tv.tv_sec = mintimeout/1000;
  256.         tv.tv_usec = (mintimeout%1000)*1000;
  257.         if (select(maxhandle+1, &infd, &outfd, NULL, &tv) > 0)
  258.         {
  259.             if (quit_program) break;
  260.             for (tmp = inputlist; tmp != NULL; tmp = tmp->next)
  261.             {
  262.                 INPUT_REC *inp;
  263.  
  264.                 inp = (INPUT_REC *) tmp->data;
  265.                 if ((inp->mode == GUI_INPUT_READ && FD_ISSET(inp->handle, &infd)) ||
  266.                     (inp->mode == GUI_INPUT_WRITE && FD_ISSET(inp->handle, &outfd)))
  267.                 {
  268.                     inp->func(inp->data);
  269.                     if (quit_program) break;
  270.                 }
  271.             }
  272.         }
  273.         if (quit_program) break;
  274.  
  275.         for (tmp = timelist; tmp != NULL; tmp = tmp->next)
  276.         {
  277.             TIMEOUT_REC *t;
  278.  
  279.             t = (TIMEOUT_REC *) tmp->data;
  280.             t->gone += mintimeout;
  281.             if (t->ms >= t->gone)
  282.             {
  283.                 t->gone = 0;
  284.                 t->func(t->data);
  285.                 if (quit_program) break;
  286.             }
  287.         }
  288.     }
  289. }
  290.  
  291. void gui_setup(void)
  292. {
  293.     struct passwd *entry;
  294.     char *ptr;
  295.  
  296.     /* get user information */
  297.     user_name = getenv("IRCUSER");
  298.     real_name = getenv("IRCNAME");
  299.     default_server = getenv("IRCSERVER");
  300.  
  301.     default_server_port = 6667;
  302.     if (default_server != NULL)
  303.     {
  304.         /* get server port */
  305.         ptr = strchr(default_server, ':');
  306.         if (ptr != NULL)
  307.         {
  308.             *ptr = '\0';
  309.             if (sscanf(ptr, "%d", &default_server_port) != 1)
  310.                 default_server_port = 6667;
  311.         }
  312.     }
  313.  
  314.     /* get passwd-entry */
  315.     entry = getpwuid(getuid());
  316.     if (entry != NULL)
  317.     {
  318.         user_name = entry->pw_name;
  319.         if (real_name == NULL) real_name = entry->pw_gecos;
  320.     }
  321.  
  322.     ptr = getenv("IRCNICK");
  323.     if (ptr == NULL)
  324.     {
  325.         if (user_name != NULL)
  326.             ptr = user_name;
  327.         else
  328.             ptr = "someone";
  329.     }
  330.  
  331.     default_nick = ptr;
  332.     if (user_name == NULL || *user_name == '\0') user_name = default_nick;
  333.     if (real_name == NULL || *real_name == '\0') real_name = user_name;
  334.  
  335.     user_name = g_strdup(user_name);
  336.     real_name = g_strdup(real_name);
  337.     default_nick = g_strdup(default_nick);
  338.     if (default_server != NULL) default_server = g_strdup(default_server);
  339. }
  340.  
  341. int main (int argc, char *argv[])
  342. {
  343.     WINDOW_REC *win;
  344.     int pid;
  345.  
  346.     pid = fork();
  347.     if (pid == -1)
  348.     {
  349.         g_error("fork() failed.\n");
  350.         return 1;
  351.     }
  352.  
  353.     if (pid != 0)
  354.         g_print("forking to background process %d\n", pid);
  355.     else
  356.     {
  357.         gui_setup();
  358.         gui_init();
  359.         irc_init();
  360.  
  361.         win = irc_window_new(NULL, NULL);
  362.  
  363.         script_load("irc.pl");
  364.  
  365.         main_loop();
  366.         irc_window_close(win);
  367.  
  368.         irc_deinit();
  369.         gui_deinit();
  370.     }
  371.  
  372.     return 0;
  373. }
  374.