1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
|
#define _POSIX_C_SOURCE 200809L
#include <netdb.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/socket.h>
#include <unistd.h>
#include <libsx.h>
#define BUFSIZE 513 /* IRC max line length is 510 + \r\n + \0 */
struct textpair {
Widget in, out;
};
static int sfd;
int sock_write(int sfd, char *msg, ...){
va_list ap;
va_start(ap, msg);
if (vdprintf(sfd, msg, ap) < 0) {
perror("sock_write");
va_end(ap);
return 1;
}
va_end(ap);
return 0;
}
void
add_text_to_display_newline(char *s, Widget w) {
size_t len;
char *old, *new;
old = GetTextWidgetText(w);
if (old == NULL) {
fprintf(stderr, "error getting old text\n");
exit(1);
}
len = strlen(old);
new = calloc(strlen(s) + len + 2, 1);
if (new == NULL) {
perror("malloc");
exit(1);
}
strcpy(new, old);
if (len > 0 && old[len - 1] != '\n')
strcat(new, "\n");
strcat(new, s);
SetTextWidgetText(w, new, 0);
free(new);
}
void
submit_cb(Widget w, void *d) {
struct textpair *tp = (struct textpair *)d;
char *new;
new = GetTextWidgetText(tp->in);
if (new == NULL) {
fprintf(stderr, "error getting text\n");
exit(1);
}
sock_write(sfd, "%s\r\n", /* TODO: conditionally add \r\n */ new);
add_text_to_display_newline(new, tp->out);
SetStringEntry(tp->in, "");
}
void
string_cb(Widget w, char *s, void *data) {
sock_write(sfd, "%s\r\n", /* TODO: conditionally add \r\n */ s);
add_text_to_display_newline(s, (Widget)data);
SetStringEntry(w, "");
}
void
quit(Widget w, void *d) {
sock_write(sfd, "QUIT :nähdään\r\n");
close(sfd);
exit(0);
}
void
pollin_cb(XtPointer client_data, int *source, XtInputId *id) {
char c[BUFSIZE] = {'\0'};
if (read(*source, c, BUFSIZE) == 0)
XtRemoveInput(*id);
else
add_text_to_display_newline(c, (Widget)client_data);
}
int
setup_sx(int argc, char **argv, XtAppContext *app, Widget *output) {
static struct textpair tp;
static Widget w[4];
if (OpenDisplay(argc, argv) == 0) {
fprintf(stderr, "couldn't open display\n");
return 1;
}
w[0] = MakeTextWidget("", 0, 0, 300, 200);
w[1] = MakeStringEntry(NULL, 200, string_cb, w[0]);
tp.in = w[1];
tp.out = w[0];
*output = w[0];
w[2] = MakeButton("Submit", submit_cb, &tp);
w[3] = MakeButton("Quit", quit, NULL);
SetWidgetPos(w[1], PLACE_UNDER, w[0], NO_CARE, NULL);
SetWidgetPos(w[2], PLACE_RIGHT, w[1], PLACE_UNDER, w[0]);
SetWidgetPos(w[3], PLACE_RIGHT, w[2], PLACE_UNDER, w[0]);
if (!w[0] || !w[1])
return 1;
*app = XtWidgetToApplicationContext(w[0]);
if (*app == NULL)
return 1;
return 0;
}
int
server_connect(char *nodename, char *servname) {
int s, sfd;
struct addrinfo hints = {0}, *result, *rp;
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
s = getaddrinfo(nodename, servname, &hints, &result);
if (s != 0) {
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(s));
return -1;
}
for (rp = result; rp != NULL; rp = rp->ai_next) {
sfd = socket(rp->ai_family, rp->ai_socktype, rp->ai_protocol);
if (sfd == -1)
continue;
if (connect(sfd, rp->ai_addr, rp->ai_addrlen) == 0)
break;
if (close(sfd) == -1) {
perror("close");
return -1;
}
}
freeaddrinfo(result);
if (rp == NULL)
return -1;
return sfd;
}
int
setup_irc(int *sockfd) {
/* initially hardcode the user details; TODO make a popup to prompt for them */
char nodename[] = "irc.libera.chat";
char servname[] = "6667";
char nick[] = "sxirc";
int s;
s = server_connect(nodename, servname);
if (s == -1)
return 1;
/* identify */
sock_write(s, "NICK %s\r\nUSER %s 8 x :%s\r\n", nick, nick, nick);
*sockfd = s;
return 0;
}
int
main(int argc, char **argv) {
XtAppContext app;
Widget output;
if (setup_sx(argc, argv, &app, &output) != 0)
return 1;
if (setup_irc(&sfd) != 0)
return 1;
XtAppAddInput(app, sfd, XtInputReadMask, pollin_cb, output);
ShowDisplay();
GetStandardColors();
MainLoop();
return 0;
}
|