/[cvs]/eggdrop1.9/src/terminal.c
ViewVC logotype

Contents of /eggdrop1.9/src/terminal.c

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.11 - (show annotations) (download) (as text)
Thu Sep 13 22:20:58 2007 UTC (11 years, 9 months ago) by sven
Branch: MAIN
CVS Tags: HEAD
Changes since 1.10: +2 -3 lines
File MIME type: text/x-chdr
 * Added a owner to sockbufs. Ugly but necessary.
 * Some more botnet stuff. Bots can actually link now.

1 /* terminal.c: terminal mode support
2 *
3 * Copyright (C) 2003, 2004 Eggheads Development Team
4 *
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 */
19
20 #ifndef lint
21 static const char rcsid[] = "$Id: terminal.c,v 1.10 2007-06-03 23:43:46 sven Exp $";
22 #endif
23
24 #include <eggdrop/eggdrop.h> /* partyline_* */
25 #include <unistd.h> /* dup */
26 #include "terminal.h" /* protoypes */
27
28 extern int terminal_enabled; /* from logfile.c */
29
30 static int on_privmsg(void *client_data, partymember_t *dest, botnet_entity_t *src, const char *text, int len);
31 static int on_nick(void *client_data, partymember_t *src, const char *oldnick, const char *newnick);
32 static int on_quit(void *client_data, partymember_t *src, const botnet_bot_t *lostbot, const char *text, int len);
33 static int on_chanmsg(void *client_data, partychan_t *chan, botnet_entity_t *src, const char *text, int len);
34 static int on_join(void *client_data, partychan_t *chan, partymember_t *src, int linking);
35 static int on_part(void *client_data, partychan_t *chan, partymember_t *src, const char *text, int len);
36 static int on_read(void *client_data, int idx, char *data, int len);
37
38 static sockbuf_handler_t terminal_sockbuf_handler = {
39 "terminal",
40 NULL,
41 NULL,
42 NULL,
43 on_read,
44 NULL
45 };
46
47 static partyline_event_t terminal_party_handler = {
48 on_privmsg,
49 on_nick,
50 on_quit,
51 on_chanmsg,
52 on_join,
53 on_part
54 };
55
56 struct {
57 int in_idx, out_idx;
58 partymember_t *party;
59 } terminal_session = {-1, -1, NULL};
60
61 static user_t *terminal_user = NULL;
62
63 int terminal_init(void)
64 {
65 if (terminal_user == NULL) {
66 terminal_user = user_lookup_by_handle(TERMINAL_HANDLE);
67 if (terminal_user == NULL) {
68 terminal_user = user_new(TERMINAL_HANDLE);
69 user_set_flags_str(terminal_user, NULL, "n");
70 }
71 }
72
73 /* Connect an idx to stdin. */
74 terminal_session.in_idx = sockbuf_new();
75 sockbuf_set_sock(terminal_session.in_idx, dup(fileno(stdin)), SOCKBUF_CLIENT);
76 sockbuf_set_handler(terminal_session.in_idx, &terminal_sockbuf_handler, NULL, NULL);
77 linemode_on(terminal_session.in_idx);
78
79 /* And one for stdout. */
80 terminal_session.out_idx = sockbuf_new();
81 sockbuf_set_sock(terminal_session.out_idx, dup(fileno(stdout)), SOCKBUF_NOREAD);
82
83 putlog(LOG_MISC, "*", _("Entering terminal mode."));
84
85 terminal_session.party = partymember_new(-1,
86 terminal_user, NULL, TERMINAL_NICK, TERMINAL_USER,
87 TERMINAL_HOST, &terminal_party_handler, NULL, NULL);
88
89 /* Put them on the main channel. */
90 partychan_join_name("*", terminal_session.party, 0);
91
92 return (0);
93 }
94
95 #define RUNMODE_RESTART 3
96
97 int terminal_shutdown(int runmode)
98 {
99 if (runmode == RUNMODE_RESTART) {
100 terminal_user = NULL;
101 return(0);
102 }
103
104 if (terminal_session.party) {
105 partymember_delete(terminal_session.party, NULL, _("Shutdown"));
106 terminal_session.party = NULL;
107 }
108
109 if (terminal_session.in_idx != -1) {
110 sockbuf_set_sock(terminal_session.in_idx, -1, 0);
111 sockbuf_delete(terminal_session.in_idx);
112 terminal_session.in_idx = -1;
113 }
114
115 if (terminal_session.out_idx != -1) {
116 sockbuf_set_sock(terminal_session.out_idx, -1, 0);
117 sockbuf_delete(terminal_session.out_idx);
118 terminal_session.out_idx = -1;
119 }
120
121 if (terminal_user) {
122 user_delete(terminal_user);
123 terminal_user = NULL;
124 }
125
126 /* let logfile.c know that there's no longer the faked
127 * stdout sockbuf of our HQ partymember */
128 terminal_enabled = 0;
129
130 putlog(LOG_MISC, "*", _("Leaving terminal mode."));
131
132 return (0);
133 }
134
135 static int on_read(void *client_data, int idx, char *data, int len)
136 {
137 partyline_on_input(NULL, terminal_session.party, data, len);
138 return 0;
139 }
140
141 static int on_privmsg(void *client_data, partymember_t *dest, botnet_entity_t *src, const char *text, int len)
142 {
143 return partyline_idx_privmsg(terminal_session.out_idx, dest, src, text, len);
144 }
145
146 static int on_nick(void *client_data, partymember_t *src, const char *oldnick, const char *newnick)
147 {
148 return partyline_idx_nick (terminal_session.out_idx, src, oldnick, newnick);
149 }
150
151 static int on_quit(void *client_data, partymember_t *src, const botnet_bot_t *lostbot, const char *text, int len)
152 {
153 if (lostbot) return 0;
154 partyline_idx_quit(terminal_session.out_idx, src, text, len);
155
156 /* if this quit are we delete our sockbuf. */
157 if (src == terminal_session.party)
158 sockbuf_delete(terminal_session.out_idx);
159
160 return(0);
161 }
162
163 static int on_chanmsg(void *client_data, partychan_t *chan, botnet_entity_t *src, const char *text, int len)
164 {
165 return partyline_idx_chanmsg(terminal_session.out_idx, chan, src, text, len);
166 }
167
168 static int on_join(void *client_data, partychan_t *chan, partymember_t *src, int linking)
169 {
170 if (linking) return 0;
171 return partyline_idx_join(terminal_session.out_idx, chan, src);
172 }
173
174 static int on_part(void *client_data, partychan_t *chan, partymember_t *src, const char *text, int len)
175 {
176 return partyline_idx_part(terminal_session.out_idx, chan, src, text, len);
177 }

webmaster@eggheads.org
ViewVC Help
Powered by ViewVC 1.1.23