[mw-devel] [Git][arthur/mw][master] Goodbye who.bb, gone forever, and good riddance
Justin Mitchell
arthur at sucs.org
Thu Oct 8 16:01:19 BST 2015
Justin Mitchell pushed to branch master at Justin Mitchell / mw
Commits:
d725e0a5 by Justin Mitchell at 2015-10-08T16:00:18Z
Goodbye who.bb, gone forever, and good riddance
- - - - -
19 changed files:
- INSTALL
- src/client/js.c
- src/client/main.c
- src/client/script_inst.c
- src/client/who.c
- src/client/who.h
- src/ipc.c
- src/ipc.h
- src/server/PROTOCOL
- src/server/mwserv.c
- src/server/replay.c
- src/server/servsock.c
- src/server/servsock.h
- src/socket.h
- src/webclient/comms.c
- src/webclient/comms.h
- − src/who.c
- − src/who.h
- web/bb.h
Changes:
=====================================
INSTALL
=====================================
--- a/INSTALL
+++ b/INSTALL
@@ -14,8 +14,8 @@ Milliways III installation notes
the libdir and bindir variables in Makefile as appropriate. `make install'
will create a subdirectory of libdir called `mw' under which it will place
all the files needed by Milliways to run (apart from users.bb which is
- created when mw is first run, and who.bb which you must create yourself, as
- below). It will also put a copy of the mw executable under bindir.
+ created when mw is first run. It will also put a copy of the mw executable
+ under bindir.
To build, cd to the src subdirectory and type `make'.
=====================================
src/client/js.c
=====================================
--- a/src/client/js.c
+++ b/src/client/js.c
@@ -21,7 +21,6 @@
#pragma GCC diagnostic warning "-Winvalid-offsetof"
#pragma GCC diagnostic warning "-Wstrict-prototypes"
-#include <who.h>
#include <sqlite.h>
#include <iconv.h>
#include "bb.h"
=====================================
src/client/main.c
=====================================
--- a/src/client/main.c
+++ b/src/client/main.c
@@ -470,8 +470,6 @@ int main(int argc, char **argv)
printf(_(" -new Summary of new messages and quit\n"));
printf(_(" -server <a> Connect to server <a>\n"));
printf(_(" -since Lists people logged on between now and when you last logged on\n"));
- printf(_(" -who Show a list of users logged on and quit\n"));
- printf(_(" -what Show a list of what users are doing\n"));
/*
printf(" -quiet Don't announce your login to board or talker\n");
printf(" -X <user> Display messages for that user\n");
=====================================
src/client/script_inst.c
=====================================
--- a/src/client/script_inst.c
+++ b/src/client/script_inst.c
@@ -419,7 +419,7 @@ void scr_roomnum( struct code *pc, int fargc, char **fargv )
const char *name = json_getstring(entry, "name");
const char *chatmode = NULL;
if (perms!=NULL) chatmode=json_getstring(perms, "chatmode");
- int room = json_getint(entry, "room");
+ int room = json_getint(entry, "channel");
/* wrong person */
if (strcasecmp(name, uname)!=0) continue;
=====================================
src/client/who.c
=====================================
--- a/src/client/who.c
+++ b/src/client/who.c
@@ -134,8 +134,6 @@ void update_wholist(ipc_message_t *msg)
whoinfo = json_init(msg);
whowhen = time(NULL);
- printf("Received whodata = %s\n", msg->body);
-
/* we were waiting for one, show it */
if (whotype >= 0) {
display_wholist(whotype);
@@ -252,3 +250,22 @@ json_t * grab_wholist(void)
if (whoinfo != NULL) json_incref(whoinfo);
return whoinfo;
}
+
+int32_t who_find(const char *username)
+{
+ json_t *wlist = grab_wholist();
+ if (wlist == NULL) return -1;
+
+ size_t wi;
+ json_t *entry;
+ json_array_foreach(wlist, wi, entry) {
+ const char *name = json_getstring(entry, "name");
+ int32_t uid = json_getint(entry, "id");
+ if (strcasecmp(username, name)==0) {
+ json_decref(wlist);
+ return uid;
+ }
+ }
+ json_decref(wlist);
+ return -1;
+}
=====================================
src/client/who.h
=====================================
--- a/src/client/who.h
+++ b/src/client/who.h
@@ -12,12 +12,7 @@ void what_list(void);
char *part_who_talk(const char *text, int status);
char *part_who(const char *text, int status);
json_t * grab_wholist(void);
+int32_t who_find(const char *username);
-#ifndef json_array_foreach
-#define json_array_foreach(array, index, value) \
- for(index = 0; \
- index < json_array_size(array) && (value = json_array_get(array, index)); \
- index++)
-#endif
#endif /* CLIENT_WHO_H */
=====================================
src/ipc.c
=====================================
--- a/src/ipc.c
+++ b/src/ipc.c
@@ -105,9 +105,16 @@ int ipc_getfd()
return ipcsock->fd;
}
+extern uint32_t who_find(const char *username);
+
/*
* Send a message to a remote process addressed by username.
* Returns the number of processes successfuly sent messages
+ *
+ * DEPRECATED ! - move away from this function
+ * server should decide who messages go to not you
+ *
+ * Any program including this must provide its own who_find()
*/
unsigned int ipc_send_to_username(const char * dest, enum ipc_types msgtype, const char * data) {
int ret = 0;
=====================================
src/ipc.h
=====================================
--- a/src/ipc.h
+++ b/src/ipc.h
@@ -2,7 +2,6 @@
#define IPC_H
#include <unistd.h>
-#include "who.h"
#include "user.h"
#include "socket.h"
@@ -59,8 +58,6 @@ enum ipc_types {
#endif
-typedef int (send_filter)(const struct person * usr, const struct who * who, const void * info);
-
void ipc_connect(const char *target);
int ipc_connected(void);
void ipc_check(void);
=====================================
src/server/PROTOCOL
=====================================
--- a/src/server/PROTOCOL
+++ b/src/server/PROTOCOL
@@ -107,6 +107,9 @@ IPC_ACTION - user requests an action
gag - Apply gagging filter to user
gag - name of gag to Apply
ungag - remove gagging filters
+ channel - enter/leave a channel
+ channel - channel to move to
+ target - (opt) if this is a summons/force
Response:
type - command request
target - victim
=====================================
src/server/mwserv.c
=====================================
--- a/src/server/mwserv.c
+++ b/src/server/mwserv.c
@@ -9,7 +9,6 @@
#include <string.h>
#include <time.h>
#include <socket.h>
-#include <who.h>
#include "servsock.h"
/* unused, but necessary to link other util functions */
@@ -117,12 +116,6 @@ int main(int argc, char **argv)
}
}
- /* at server start nobody is logged in, wipe who list */
- int fd = who_open(O_TRUNC|O_WRONLY);
- if (fd < 0)
- return 1;
- close(fd);
-
if (!opts.foreground) daemon(0,0);
uptime = time(0);
=====================================
src/server/replay.c
=====================================
--- a/src/server/replay.c
+++ b/src/server/replay.c
@@ -134,13 +134,9 @@ void replay(ipc_connection_t *conn, ipc_message_t *msg)
json_decref(cmd);
/* who are we doing this for */
- struct who who;
- who.posn = conn->user;
- who.pid = conn->addr;
-
struct person user;
int users_fd = userdb_open(O_RDONLY);
- lseek(users_fd, who.posn, SEEK_SET);
+ lseek(users_fd, conn->user, SEEK_SET);
if (read(users_fd, &user, sizeof(user)) <= 0) {
close(users_fd);
send_error(conn, msg, "Error cannot find your user record");
=====================================
src/server/servsock.c
=====================================
--- a/src/server/servsock.c
+++ b/src/server/servsock.c
@@ -112,7 +112,6 @@ void accept_connection(int mainsock)
void drop_connection(ipc_connection_t * conn)
{
struct epoll_event ev;
- int wfd;
printf("Drop connection fd=%d\n", conn->fd);
bzero(&ev, sizeof(ev));
@@ -120,9 +119,6 @@ void drop_connection(ipc_connection_t * conn)
list_del_init(&conn->list);
if (conn->fd != -1) close(conn->fd);
conn->fd = -1;
- wfd = who_open(O_RDWR);
- who_delete(wfd, conn->addr);
- close(wfd);
conn->state = IPCSTATE_DELETED;
if (!list_empty(&conn->outq)) {
struct list_head *pos, *q;
@@ -270,7 +266,6 @@ void process_msg(ipc_connection_t *conn, ipc_message_t *msg)
/* client just told us who they are */
if (msg->head.type == IPC_HELLO) {
- int wfd;
if (msg->bodylen < 4) {
printf("Invalid HELO from fd=%d. dropping.\n", conn->fd);
ipcmsg_destroy(msg);
@@ -290,9 +285,6 @@ void process_msg(ipc_connection_t *conn, ipc_message_t *msg)
return;
}
printf("WHO Add: pid=%d posn=%d\n", conn->addr, conn->user);
- wfd = who_open(O_RDWR);
- who_add(wfd, conn->addr, conn->user);
- close(wfd);
conn->state = IPCSTATE_VALID;
ipcmsg_destroy(msg);
@@ -435,20 +427,19 @@ void process_msg(ipc_connection_t *conn, ipc_message_t *msg)
* except username==exclude (notsayto command) */
void msg_attach_to_channel(ipc_message_t *msg, int channel, const char * exclude)
{
- int who_fd = who_open(O_RDONLY);
int users_fd = userdb_open(O_RDONLY);
- struct who who;
struct person user;
struct room room;
RoomInit(&room);
LoadRoom(&room, channel);
- while (read(who_fd, &who, sizeof(who)) > 0) {
- if (who.posn < 0) continue;
- if (who.pid <= 0) continue;
+ struct list_head *pos;
+ list_for_each(pos, &connection_list) {
+ ipc_connection_t *c = list_entry(pos, ipc_connection_t, list);
+ if (c->state != IPCSTATE_VALID) continue;
- lseek(users_fd, who.posn, SEEK_SET);
+ lseek(users_fd, c->user, SEEK_SET);
if (read(users_fd, &user, sizeof(user)) <= 0) continue;
/* have we been told to exclude someone in this room */
@@ -459,41 +450,38 @@ void msg_attach_to_channel(ipc_message_t *msg, int channel, const char * exclude
/* room matches, send them a copy */
if (user.room == msg->head.dst) {
- msg_attach_to_pid(msg, who.pid);
+ msg_attach(msg, c);
} else
/* room not soundproof, and user has global on */
if (room.sproof < 1 && (user.chatmode & CM_GLOBAL)) {
- msg_attach_to_pid(msg, who.pid);
+ msg_attach(msg, c);
}
}
RoomDestroy(&room);
- close(who_fd);
close(users_fd);
}
/* send copy to all sessions belonging to username */
int msg_attach_to_username(ipc_message_t *msg, const char * username)
{
- int who_fd = who_open(O_RDONLY);
int users_fd = userdb_open(O_RDONLY);
- struct who who;
struct person user;
int found = 0;
- while (read(who_fd, &who, sizeof(who)) > 0) {
- if (who.posn < 0) continue;
- if (who.pid <= 0) continue;
+ struct list_head *pos;
+ list_for_each(pos, &connection_list) {
+ ipc_connection_t *c = list_entry(pos, ipc_connection_t, list);
+ if (c->state != IPCSTATE_VALID) continue;
- lseek(users_fd, who.posn, SEEK_SET);
+ lseek(users_fd, c->user, SEEK_SET);
if (read(users_fd, &user, sizeof(user)) <= 0) continue;
/* is this the username we are looking for */
if (strcasecmp(username, user.name)==0) {
- msg_attach_to_pid(msg, who.pid);
+ msg_attach(msg, c);
found++;
}
}
- close(who_fd);
close(users_fd);
return found;
}
@@ -501,25 +489,23 @@ int msg_attach_to_username(ipc_message_t *msg, const char * username)
/* send copy of message to every session */
void msg_attach_to_all(ipc_message_t *msg)
{
- int who_fd = who_open(O_RDONLY);
int users_fd = userdb_open(O_RDONLY);
- struct who who;
struct person user;
- while (read(who_fd, &who, sizeof(who)) > 0) {
- if (who.posn < 0) continue;
- if (who.pid <= 0) continue;
+ struct list_head *pos;
+ list_for_each(pos, &connection_list) {
+ ipc_connection_t *c = list_entry(pos, ipc_connection_t, list);
+ if (c->state != IPCSTATE_VALID) continue;
- lseek(users_fd, who.posn, SEEK_SET);
+ lseek(users_fd, c->user, SEEK_SET);
if (read(users_fd, &user, sizeof(user)) <= 0) continue;
/* person not on talker, just skip them */
if (!cm_flags(user.chatmode, CM_ONCHAT, CM_MODE_ALL)) continue;
- msg_attach_to_pid(msg, who.pid);
+ msg_attach(msg, c);
printf("Broadcast to %s in %d\n", user.name, user.room);
}
- close(who_fd);
close(users_fd);
}
@@ -616,23 +602,22 @@ ipc_message_t * msg_wholist(void)
{
ipc_message_t * msg = ipcmsg_create(IPC_WHOLIST, SYSTEM_USER);
- int who_fd = who_open(O_RDONLY);
int users_fd = userdb_open(O_RDONLY);
- struct who who;
struct person user;
json_t * list = json_array();
- while (read(who_fd, &who, sizeof(who)) > 0) {
- if (who.posn < 0) continue;
- if (who.pid <= 0) continue;
+ struct list_head *pos;
+ list_for_each(pos, &connection_list) {
+ ipc_connection_t *c = list_entry(pos, ipc_connection_t, list);
+ if (c->state != IPCSTATE_VALID) continue;
- lseek(users_fd, who.posn, SEEK_SET);
+ lseek(users_fd, c->user, SEEK_SET);
if (read(users_fd, &user, sizeof(user)) <= 0) continue;
json_t * j = json_init(NULL);
- json_addint(j, "pid", who.pid);
- json_addint(j, "id", who.posn);
+ json_addint(j, "pid", c->addr);
+ json_addint(j, "id", c->user);
json_addstring(j, "name", user.name);
json_addstring(j, "realname", user.realname);
json_addstring(j, "doing", user.doing);
@@ -659,3 +644,29 @@ ipc_message_t * msg_wholist(void)
json_decref(list);
return msg;
}
+
+/* whats the uid/userposn for this username
+ * searchs only online users
+ */
+int32_t who_find(const char * username)
+{
+ int users_fd = userdb_open(O_RDONLY);
+ struct person user;
+
+ struct list_head *pos;
+ list_for_each(pos, &connection_list) {
+ ipc_connection_t *c = list_entry(pos, ipc_connection_t, list);
+ if (c->state != IPCSTATE_VALID) continue;
+
+ lseek(users_fd, c->user, SEEK_SET);
+ if (read(users_fd, &user, sizeof(user)) <= 0) continue;
+ /* is this the username we are looking for */
+ if (strcasecmp(username, user.name)==0) {
+ close(users_fd);
+ return c->user;
+ }
+ }
+
+ close(users_fd);
+ return -1;
+}
=====================================
src/server/servsock.h
=====================================
--- a/src/server/servsock.h
+++ b/src/server/servsock.h
@@ -20,5 +20,6 @@ void init_server(void);
void send_error(ipc_connection_t *conn, ipc_message_t *orig, const char *format, ...);
void msg_apply_gag(struct person * from, ipc_message_t * msg, const char * field);
ipc_message_t * msg_wholist(void);
+int32_t who_find(const char * username);
#endif /* SERVSOCK_H */
=====================================
src/socket.h
=====================================
--- a/src/socket.h
+++ b/src/socket.h
@@ -85,5 +85,11 @@ const char *json_getstring(json_t *js, const char *key);
int json_getint(json_t *js, const char *key);
void ipcmsg_summary(const char *intro, ipc_message_t *msg);
+#ifndef json_array_foreach
+#define json_array_foreach(array, index, value) \
+ for(index = 0; \
+ index < json_array_size(array) && (value = json_array_get(array, index)); \
+ index++)
+#endif
#endif
=====================================
src/webclient/comms.c
=====================================
--- a/src/webclient/comms.c
+++ b/src/webclient/comms.c
@@ -632,3 +632,24 @@ void mwlog(const char *fmt, ...)
va_end(ap);
}
+int32_t who_find(const char *username)
+{
+ if (whoinfo == NULL) return -1;
+ json_error_t err;
+ json_t *wlist = json_loads(whoinfo, 0, &err);
+ if (wlist == NULL) return -1;
+
+ size_t wi;
+ json_t *entry;
+ json_array_foreach(wlist, wi, entry) {
+ const char *name = json_getstring(entry, "name");
+ int32_t uid = json_getint(entry, "id");
+ if (strcasecmp(username, name)==0) {
+ json_decref(wlist);
+ return uid;
+ }
+ }
+ json_decref(wlist);
+ return -1;
+}
+
=====================================
src/webclient/comms.h
=====================================
--- a/src/webclient/comms.h
+++ b/src/webclient/comms.h
@@ -8,4 +8,4 @@ void close_cmd(void);
void talk_rawbcast(const char *fmt, ...) __attribute__((format(printf,1,2)));
void create_user(struct person *me, int *userposn, const char *username, const char *password);
const char * fetch_who(int immediate);
-
+int32_t who_find(const char *username);
=====================================
src/who.c deleted
=====================================
--- a/src/who.c
+++ /dev/null
@@ -1,86 +0,0 @@
-#include <unistd.h>
-#include <fcntl.h>
-#include <stdio.h>
-#include <strings.h>
-
-#include "who.h"
-#include "files.h"
-#include "user.h"
-
-int who_open(int flags)
-{
- int wfd = open(WHOFILE, flags|O_CREAT, 0644);
- if (wfd < 0)
- perror(WHOFILE);
- return wfd;
-}
-
-void who_add(int wfd, int pid, int32_t posn)
-{
- struct who rec;
- Lock_File(wfd);
- while(read(wfd,&rec,sizeof(rec))==sizeof(rec))
- {
- if(rec.pid<0)
- {
- lseek(wfd,-sizeof(rec), SEEK_CUR);
- break;
- }
- }
- rec.pid=pid;
- rec.posn=posn;
- write(wfd,&rec,sizeof(rec));
- Unlock_File(wfd);
-}
-
-void who_delete(int wfd, int pid)
-{
- struct who temp;
- Lock_File(wfd);
- while(read(wfd,&temp,sizeof(temp))==sizeof(temp))
- {
- if (temp.pid==pid) {
- lseek(wfd,(long)-sizeof(temp),1);
- temp.pid=-1;
- temp.posn=-1L;
- write(wfd,&temp,sizeof(temp));
- }
- }
- Unlock_File(wfd);
-}
-
-/* find userid of logged in users */
-long who_find(const char * username)
-{
- int who_fd;
- int users_fd = -1;
- struct who who;
- struct person user;
- long where = -1;
-
- who_fd = who_open(O_RDONLY);
- if (who_fd < 0) return -1;
-
- users_fd = userdb_open(O_RDONLY);
- if (users_fd < 0) {
- close(who_fd);
- return -1;
- }
-
- while (read(who_fd, &who, sizeof(who)) > 0) {
- if (who.posn < 0) continue;
- if (who.pid <= 0) continue;
-
- lseek(users_fd, who.posn, SEEK_SET);
- if (read(users_fd, &user, sizeof(user)) <= 0) continue;
- if (strcasecmp(user.name, username)==0) {
- where = who.posn;
- break;
- }
- }
- close(who_fd);
- close(users_fd);
-
- return where;
-}
-
=====================================
src/who.h deleted
=====================================
--- a/src/who.h
+++ /dev/null
@@ -1,20 +0,0 @@
-#ifndef WHO_H
-#define WHO_H
-
-#include <stdint.h>
-
-#define WHOFILE MSGDIR"/who.bb"
-
-struct who
-{
- int32_t pid;
- int32_t posn;
-};
-
-extern int who_open(int mode);
-extern void who_add(int wfd, int pid, int32_t posn);
-extern void who_delete(int wfd, int pid);
-extern long who_find(const char * username);
-
-
-#endif /* WHO_H */
=====================================
web/bb.h
=====================================
--- a/web/bb.h
+++ b/web/bb.h
@@ -2,7 +2,6 @@
#define USERFILE "users.bb"
#define LOGFILE "log.bb"
#define LOGIN_BANNER "login.banner"
-#define WHOFILE "who.bb"
#define LOCKFILE "/tmp/bb.locked"
#define EDITOR "/usr/bin/vi"
#define HOMEPATH "/usr/local/lib/mw"
View it on GitLab: https://projects.sucs.org/arthur/mw/commit/d725e0a5003b9f59ab3481a1bdde17cfcd787470
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.sucs.org/pipermail/mw-devel/attachments/20151008/3a5f08d5/attachment-0001.html>
More information about the mw-devel
mailing list