-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathquery.c
201 lines (171 loc) · 4.26 KB
/
query.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
/*-------------------------------------------------------*/
/* util/query.c ( NTHU CS MapleBBS Ver 2.36.sob ) */
/*-------------------------------------------------------*/
/* target : 站外 query 使用者 */
/* create : 96/11/27 */
/* update : 隨時更新 */
/*-------------------------------------------------------*/
/* syntax : query <userid> */
/*-------------------------------------------------------*/
#include <stdio.h>
#include "bbs.h"
#include <sys/ipc.h>
#include <sys/shm.h>
#define _MODES_C_
char buf[256];
char *ptr;
userec uec;
fileheader fh;
struct UTMPFILE *utmpshm;
user_info *ushm_head, *ushm_tail;
static void
attach_err(shmkey, name)
int shmkey;
char *name;
{
char buf[80];
sprintf(buf, "error, key = %x", shmkey);
/* logit(name, buf);*/
exit(1);
}
static void *
attach_shm(shmkey, shmsize)
int shmkey, shmsize;
{
void *shmptr;
int shmid;
shmid = shmget(shmkey, shmsize, 0);
if (shmid < 0)
{
shmid = shmget(shmkey, shmsize, IPC_CREAT | 0600);
if (shmid < 0)
attach_err(shmkey, "shmget");
shmptr = (void *) shmat(shmid, NULL, 0);
if (shmptr == (void *) -1)
attach_err(shmkey, "shmat");
memset(shmptr, 0, shmsize);
}
else
{
shmptr = (void *) shmat(shmid, NULL, 0);
if (shmptr == (void *) -1)
attach_err(shmkey, "shmat");
}
return shmptr;
}
resolve_utmp()
{
if (utmpshm == NULL)
{
register struct UTMPFILE *ushm;
register user_info *uentp;
utmpshm = ushm = attach_shm(UTMPSHM_KEY, sizeof(*utmpshm));
ushm_head = uentp = ushm->uinfo;
ushm_tail = uentp + USHM_SIZE;
}
}
char *
Cdate(clock)
time_t *clock;
{
static char foo[22];
struct tm *mytm = localtime(clock);
strftime(foo, 22, "%D %T %a", mytm);
return (foo);
}
int
chkmail(userid)
char *userid;
{
FILE *mail;
sprintf(buf, BBSHOME"/home/%s/.DIR", userid);
if( access(buf,F_OK) ) /* .DIR 不存在 */
return 0;
else
if( (mail=fopen(buf,"r")) == NULL){
perror("fopen:.DIR");
exit(1);
}
else
while( fread(&fh,sizeof(fileheader),1,mail) > 0)
;
if( fh.filemode & FILE_READ )
return 0;
else
return 1;
}
void
showuser(uec)
userec *uec;
{
register int i=0 , user_num;
FILE *plans;
char *modestr;
user_info *uentp, *utail;
printf("[1;33;44m 批踢踢實業坊 [m\n");
printf("%s %s(%s) 共上站 %d 次,發表過 %d 篇文章\n",
(uec->userlevel & PERM_LOGINOK) ? "㊣" : "?",
uec->userid, uec->username, uec->numlogins, uec->numposts);
ptr=uec->lasthost;
if( strchr(ptr,'@') ){
ptr=index(uec->lasthost,'@')+1;
}
printf("最近(%s)從[%s]上站\n", Cdate(&(uec->lastlogin)),ptr );
modestr = "不在站上";
uentp = ushm_head;
utail = ushm_tail;
do
{
if (!strcmp(uec->userid, uentp->userid))
{
if (!PERM_HIDE(uentp))
// modestr = ModeTypeTable[uentp->mode];
break;
}
} while (++uentp <= ushm_tail);
printf("[1;33m[目前動態:%s][m %s\n",
modestr, chkmail(uec->userid) ? "有新進信件還沒看":"所有信件都看過了");
sprintf(buf,BBSHOME"/home/%s/plans", uec->userid);
if( !(i=access(buf, F_OK)) ){
printf("%s 的名片:\n", uec->userid);
if( (plans=fopen(buf,"r")) == NULL ){
perror("fopen:plans");
exit(1);
}
while (i++ < MAXQUERYLINES && fgets(buf,256,plans))
printf("%s",buf);
printf("",buf);
}
else
printf("%s 目前沒有名片\n", uec->userid);
}
void
main(argc,argv)
int argc;
char **argv;
{
char *c;
FILE *pwd;
int len;
if (argc != 2){
fprintf(stderr,"Wrong argument!\n");
exit(1);
}
if( (pwd=fopen(BBSHOME"/.PASSWDS","r")) == NULL){
perror("fopen");
exit(1);
}
resolve_utmp();
while( fread( &uec, sizeof(userec), 1, pwd) > 0 )
{
if( strcmp(uec.userid,argv[1]))
{
continue;
}
else
showuser(&uec);
exit(0);
}
fprintf(stderr,"Can not find user %s\n\n",argv[1]);
exit(1);
}