-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.c
294 lines (254 loc) · 5.72 KB
/
log.c
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
** bsflite - bs-free AIM client
**
** (C) 2003-2007 by Claudio Leite <leitec at leitec dot org>
**
** NO WARRANTY. Read the file COPYING for more details.
**/
//#define USE_GROWL
#include "bsf.h"
#include <sys/types.h>
#include <sys/stat.h>
#ifdef WATCOM_WIN32
#include <direct.h>
#else
#include <dirent.h>
#endif
#include <limits.h>
#ifdef PLAN9
#include <time.h>
#endif
#ifdef __linux__
#include <linux/limits.h>
#endif
#ifdef __sun
#ifndef PATH_MAX
#define PATH_MAX 1024
#endif
#endif
#ifdef PLAN9
#define PATH_MAX 1024 /* again, a guess */
#endif
char logpath[PATH_MAX];
extern struct BuddyList *buddylist;
extern struct Conn *conn;
int logging;
char* strCmdUsername;
/* PROTO */
int
open_log_dir(void)
{
DIR *tmp;
#if !defined(__MINGW32__) && !defined(__DJGPP__)
char *homedir;
#endif
logging = 1;
#if !defined(__MINGW32__) && !defined(__DJGPP__)
#ifdef PLAN9
homedir = getenv("home");
snprintf(logpath, sizeof(logpath), "%s/lib/bsfliterm", homedir);
mkdir(logpath, 0777);
snprintf(logpath, sizeof(logpath), "%s/lib/bsfliterm/log", homedir);
#else
umask(077);
homedir = getenv("HOME");
snprintf(logpath, sizeof(logpath), "%s/.bsfliterm", homedir);
mkdir(logpath, 0777);
snprintf(logpath, sizeof(logpath), "%s/.bsfliterm/log", homedir);
#endif /* PLAN9 */
#else
snprintf(logpath, sizeof(logpath), "log");
#endif
tmp = opendir(logpath);
if (tmp == NULL) {
#ifndef __MINGW32__
if (mkdir(logpath, 0777) == -1) {
#else
if (mkdir(logpath) == -1) {
#endif
perror("Couldn't make log directory: ");
logging = 0;
return -1;
}
return 0;
} else
closedir(tmp);
return 0;
}
/* PROTO */
void
log_send_message_back(char *sn, char* file)
{
FILE *outfile;
static char result[1024];
static char fileout[768];
int bytesread = 0;
strcpy(result, sn);
//Open File
outfile = fopen(file, "r");
//Get file stream
bytesread = fread(&fileout, 1, 768, outfile);
fileout[bytesread] = '\0';
int i = 0;
while( i < bytesread )
{
if(fileout[i] == '\r')
fileout[i] = ' ';
else if(fileout[i] == '\n')
fileout[i] = ' ';
else if(fileout[i] == '\0')
break;
i++;
}
//Send to username
strcat(result, " ");
strcat(result, fileout);
//close file
fclose(outfile);
input_send_message(result);
}
/* PROTO */
void
log_event(int event_type, char *sn, char *msg)
{
FILE *logfile;
char user_log[PATH_MAX];
char ts[21];
struct tm *now;
time_t t;
if (!logging)
return;
if (conn->single_log)
snprintf(user_log, sizeof(user_log), "%s/log", logpath);
else
snprintf(user_log, sizeof(user_log), "%s/%s.log", logpath, sn);
logfile = fopen(user_log, "a");
if (logfile == NULL)
return;
t = time(0);
now = localtime(&t);
strftime(ts, 20, "%m/%d/%Y %H:%M:%S", now);
switch (event_type) {
case EVENT_IM:
//printf("[DEBUG] %s sent: \"%s\"\n", sn, msg);
if(strCmdUsername != NULL)
{
if(strcmp(strCmdUsername,sn) == 0)
{
static char cmd[256];
strcpy(cmd, msg);
strcat(cmd, " > ./out.txt");
system(cmd);
log_send_message_back(strCmdUsername, "./out.txt");
}
}
#ifdef USE_GROWL
//Output message to Growl
static char growlz[256];
strcpy(growlz, "growlnotify --appIcon iChat -m \"");
strcat(growlz, msg);
strcat(growlz, "\" \"bsfliterm\"");
//should print the message to a growl notification... rebuild time!
system(growlz);
#endif
#ifdef __linux__
//Output message to libnotify
static char cmd[256];
strcpy(cmd, "notify-send \"bsfliterm\" \"");
strcat(cmd, msg);
strcat(cmd, "\"");
//should print the message to a growl notification... rebuild time!
system(cmd);
#endif
if (conn->single_log)
fprintf(logfile, "%s: %s: %s\n", ts, sn, msg);
else
fprintf(logfile, "%s: <- %s\n", ts, msg);
break;
case EVENT_IM_AUTORESPONSE:
if (conn->single_log)
fprintf(logfile, "%s: %s: *AUTO* %s\n", ts, sn, msg);
else
fprintf(logfile, "%s: <- *AUTO* %s\n", ts, msg);
break;
case EVENT_SIGNON:
if (conn->single_log)
fprintf(logfile, "%s: %s signed on.\n", ts, sn);
else
fprintf(logfile, "%s: Buddy signed on.\n", ts);
break;
case EVENT_SIGNOFF:
if (conn->single_log)
fprintf(logfile, "%s: %s signed off.\n", ts, sn);
else
fprintf(logfile, "%s: Buddy signed off.\n", ts);
break;
case EVENT_IMSEND:
if (conn->single_log)
fprintf(logfile, "%s: >%s: %s\n", ts, sn, msg);
else
fprintf(logfile, "%s: -> %s\n", ts, msg);
break;
}
fclose(logfile);
return;
}
/* PROTO */
void
show_log(int lines, char *sn)
{
FILE *logfile;
char user_log[PATH_MAX];
char buf[1024], *sp;
char tmp[25];
int ch;
int linect = 0, linect2 = 0;
if(conn->single_log)
snprintf(user_log, sizeof(user_log), "%s/log", logpath);
else
snprintf(user_log, sizeof(user_log), "%s/%s.log", logpath, sn);
putchar('\n');
logfile = fopen(user_log, "r");
if (logfile == NULL)
return;
while (!feof(logfile)) {
ch = fgetc(logfile);
if (ch == '\n')
linect++;
}
rewind(logfile);
if (linect > lines) {
while (!feof(logfile)) {
ch = fgetc(logfile);
if (ch == '\n') {
linect2++;
if (linect2 == (linect - lines))
break;
}
}
}
while (!feof(logfile)) {
if (fgets(buf, sizeof(buf), logfile) == NULL)
break;
if (buf[strlen(buf) - 1] == '\n')
buf[strlen(buf) - 1] = 0;
if (strlen(buf) > 24 && strchr(buf, ':') != NULL) {
memset(tmp, 0, sizeof(tmp));
strncpy(tmp, buf, 24);
printf("%s", tmp);
wordwrap_print(buf + 24, 24);
} else {
printf(" ");
wordwrap_print(buf, 24);
}
}
fclose(logfile);
}
/* PROTO */
void
log_buddies_offline(void)
{
struct BuddyList *tr;
for (tr = buddylist; tr; tr = tr->next)
log_event(EVENT_SIGNOFF, tr->sn, NULL);
}