From e339aed5e0127c2aeac31e44425901192d10fb08 Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Wed, 17 Jul 2013 18:14:18 -0300 Subject: [PATCH] The very start of a small framework for social. Please share! --- AUTHORS | 0 ChangeLog | 0 Makefile.am | 1 + NEWS | 0 README | 0 configure.ac | 6 +++++ include/sgp/channel.h | 28 ++++++++++++++++++++ include/sgp/ctx.h | 24 +++++++++++++++++ include/sgp/friend.h | 30 ++++++++++++++++++++++ include/sgp/group.h | 38 +++++++++++++++++++++++++++ include/sgp/msg.h | 27 +++++++++++++++++++ include/sgp/send.h | 27 +++++++++++++++++++ include/sgp/share.h | 27 +++++++++++++++++++ src/Makefile.am | 3 +++ src/channel.c | 33 ++++++++++++++++++++++++ src/friend.c | 46 +++++++++++++++++++++++++++++++++ src/group.c | 60 +++++++++++++++++++++++++++++++++++++++++++ src/main.c | 33 ++++++++++++++++++++++++ src/msg.c | 39 ++++++++++++++++++++++++++++ src/send.c | 40 +++++++++++++++++++++++++++++ src/share.c | 40 +++++++++++++++++++++++++++++ 21 files changed, 502 insertions(+) create mode 100644 AUTHORS create mode 100644 ChangeLog create mode 100644 Makefile.am create mode 100644 NEWS create mode 100644 README create mode 100644 configure.ac create mode 100644 include/sgp/channel.h create mode 100644 include/sgp/ctx.h create mode 100644 include/sgp/friend.h create mode 100644 include/sgp/group.h create mode 100644 include/sgp/msg.h create mode 100644 include/sgp/send.h create mode 100644 include/sgp/share.h create mode 100644 src/Makefile.am create mode 100644 src/channel.c create mode 100644 src/friend.c create mode 100644 src/group.c create mode 100644 src/main.c create mode 100644 src/msg.c create mode 100644 src/send.c create mode 100644 src/share.c diff --git a/AUTHORS b/AUTHORS new file mode 100644 index 0000000..e69de29 diff --git a/ChangeLog b/ChangeLog new file mode 100644 index 0000000..e69de29 diff --git a/Makefile.am b/Makefile.am new file mode 100644 index 0000000..af437a6 --- /dev/null +++ b/Makefile.am @@ -0,0 +1 @@ +SUBDIRS = src diff --git a/NEWS b/NEWS new file mode 100644 index 0000000..e69de29 diff --git a/README b/README new file mode 100644 index 0000000..e69de29 diff --git a/configure.ac b/configure.ac new file mode 100644 index 0000000..c649e14 --- /dev/null +++ b/configure.ac @@ -0,0 +1,6 @@ +AC_INIT(sgp,0.1,cascardo@cascardo.info) +AM_INIT_AUTOMAKE(AC_PACKAGE_NAME,AC_PACKAGE_VERSION) +AC_PROG_CC +AC_PROG_INSTALL +AC_OUTPUT(Makefile) +AC_OUTPUT(src/Makefile) diff --git a/include/sgp/channel.h b/include/sgp/channel.h new file mode 100644 index 0000000..8fe64c8 --- /dev/null +++ b/include/sgp/channel.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _SGP_CHANNEL_H +#define _SGP_CHANNEL_H + +#include + +struct sgp_channel; + +int sgp_channel_send(struct sgp_channel *, struct sgp_msg *); + +#endif diff --git a/include/sgp/ctx.h b/include/sgp/ctx.h new file mode 100644 index 0000000..2bb7f2c --- /dev/null +++ b/include/sgp/ctx.h @@ -0,0 +1,24 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _SGP_CTX_H +#define _SGP_CTX_H + +struct sgp_ctx; + +#endif diff --git a/include/sgp/friend.h b/include/sgp/friend.h new file mode 100644 index 0000000..7087f48 --- /dev/null +++ b/include/sgp/friend.h @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _SGP_FRIEND_H +#define _SGP_FRIEND_H + +#include + +struct sgp_friend; + +struct sgp_friend * sgp_friend_new(char *); + +struct sgp_channel * sgp_friend_get_channel(struct sgp_friend *); + +#endif diff --git a/include/sgp/group.h b/include/sgp/group.h new file mode 100644 index 0000000..26f940a --- /dev/null +++ b/include/sgp/group.h @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _SGP_GROUP_H +#define _SGP_GROUP_H + +#include /* for NULL */ +#include + +struct sgp_group; + +struct sgp_group * sgp_group_new(); +int sgp_group_add_friend(struct sgp_group *, struct sgp_friend *); + +#define sgp_foreach_friend(group, friend, pfriend) \ + for (pfriend = sgp_group_first(group), friend = pfriend ? *pfriend : NULL; \ + pfriend != NULL; \ + pfriend = sgp_group_next(group, pfriend), friend = pfriend ? *pfriend : NULL) + +struct sgp_friend ** sgp_group_first(struct sgp_group *); +struct sgp_friend ** sgp_group_next(struct sgp_group *, struct sgp_friend **); + +#endif diff --git a/include/sgp/msg.h b/include/sgp/msg.h new file mode 100644 index 0000000..1071ee0 --- /dev/null +++ b/include/sgp/msg.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _SGP_MSG_H +#define _SGP_MSG_H + +struct sgp_msg; + +struct sgp_msg * sgp_msg_new(char *); +void sgp_msg_destroy(struct sgp_msg *); + +#endif diff --git a/include/sgp/send.h b/include/sgp/send.h new file mode 100644 index 0000000..27520f2 --- /dev/null +++ b/include/sgp/send.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _SGP_SEND_H +#define _SGP_SEND_H + +#include +#include + +int sgp_send(struct sgp_friend *, struct sgp_msg *); + +#endif diff --git a/include/sgp/share.h b/include/sgp/share.h new file mode 100644 index 0000000..33c4c2a --- /dev/null +++ b/include/sgp/share.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#ifndef _SGP_SHARE_H +#define _SGP_SHARE_H + +#include +#include + +int sgp_share(struct sgp_group *, struct sgp_msg *); + +#endif diff --git a/src/Makefile.am b/src/Makefile.am new file mode 100644 index 0000000..f41ffa1 --- /dev/null +++ b/src/Makefile.am @@ -0,0 +1,3 @@ +bin_PROGRAMS = sgp +sgp_SOURCES = main.c channel.c friend.c group.c send.c share.c msg.c +sgp_CFLAGS = -I../include/ diff --git a/src/channel.c b/src/channel.c new file mode 100644 index 0000000..3aec1bf --- /dev/null +++ b/src/channel.c @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include + +struct sgp_channel { + int (*send) (void *, struct sgp_msg *); + void * data; +}; + +/* + * Send a message through a channel. + */ +int sgp_channel_send(struct sgp_channel *channel, struct sgp_msg *msg) +{ + return channel->send(channel->data, msg); +} diff --git a/src/friend.c b/src/friend.c new file mode 100644 index 0000000..db5c457 --- /dev/null +++ b/src/friend.c @@ -0,0 +1,46 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include + +struct sgp_friend { + struct sgp_channel *channel; +}; + +struct sgp_friend * sgp_friend_new(char *alias) +{ + struct sgp_friend *friend; + friend = malloc(sizeof(*friend)); + friend->channel = NULL; + return friend; +} + +/* + * Get the channel used for communication with a friend. + */ +struct sgp_channel * sgp_friend_get_channel(struct sgp_friend *friend) +{ + /* + * TODO: A friend may have multiple channels for contact. We + * should either implement this at the core (at sgp_send, for + * example), or create a channel that encapsulates that. + */ + return friend->channel; +} diff --git a/src/group.c b/src/group.c new file mode 100644 index 0000000..3566557 --- /dev/null +++ b/src/group.c @@ -0,0 +1,60 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include + +struct sgp_group { + struct sgp_friend **friends; + int nr_friends; +}; + +struct sgp_group * sgp_group_new() +{ + struct sgp_group *group; + group = malloc(sizeof(*group)); + group->nr_friends = 0; + group->friends = NULL; + return group; +} + +int sgp_group_add_friend(struct sgp_group *group, struct sgp_friend *friend) +{ + return 0; +} + +/* + * Get the channel used for communication with a friend. + */ +struct sgp_friend ** sgp_group_first(struct sgp_group *group) +{ + if (group->nr_friends > 0) + return &group->friends[0]; + return NULL; +} + +struct sgp_friend ** sgp_group_next(struct sgp_group *group, + struct sgp_friend **friend) +{ + int n; + n = (friend - group->friends) / sizeof(*friend); + if (group->nr_friends > n + 1) + return &group->friends[n+1]; + return NULL; +} diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..9ce1fb7 --- /dev/null +++ b/src/main.c @@ -0,0 +1,33 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include + +int main(int argc, char **argv) +{ + struct sgp_group *group; + struct sgp_msg *msg; + struct sgp_friend *friend; + /* TODO: access a database here? */ + group = sgp_group_new(); + friend = sgp_friend_new("Thadeu Cascardo"); + msg = sgp_msg_new("New message for you"); + sgp_group_add_friend(group, friend); + sgp_share(group, msg); + return 0; +} diff --git a/src/msg.c b/src/msg.c new file mode 100644 index 0000000..9a04735 --- /dev/null +++ b/src/msg.c @@ -0,0 +1,39 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include + +struct sgp_msg { + char *subject; +}; + +struct sgp_msg * sgp_msg_new(char *subject) +{ + struct sgp_msg *msg; + msg = malloc(sizeof(*msg)); + msg->subject = strdup(subject); + return msg; +} + +void sgp_msg_destroy(struct sgp_msg *msg) +{ + free(msg->subject); + free(msg); +} diff --git a/src/send.c b/src/send.c new file mode 100644 index 0000000..59ad6e1 --- /dev/null +++ b/src/send.c @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include +#include + +/* + * Send a message to a friend. + */ +int sgp_send(struct sgp_friend *friend, struct sgp_msg *msg) +{ + struct sgp_channel *channel; + /* + * TODO: if we are sharing with a whole group, should we tell + * our friend? Cc vs Bcc. + */ + channel = sgp_friend_get_channel(friend); + if (!channel) + /* FIXME: find a better error structure */ + return -ENODEV; + return sgp_channel_send(channel, msg); +} diff --git a/src/share.c b/src/share.c new file mode 100644 index 0000000..4fd08cc --- /dev/null +++ b/src/share.c @@ -0,0 +1,40 @@ +/* + * Copyright (C) 2013 Thadeu Lima de Souza Cascardo + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License along + * with this program; if not, write to the Free Software Foundation, Inc., + * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + */ + +#include +#include +#include +#include +#include + +/* + * Share message with a group of friends. + */ +int sgp_share(struct sgp_group *group, struct sgp_msg *msg) +{ + struct sgp_friend *friend; + struct sgp_friend **pfriend; + sgp_foreach_friend(group, friend, pfriend) { + /* + * TODO: mark message as sent if successfully sent and + * queue for another try if it was not possible. + */ + sgp_send(friend, msg); + } + return 0; +} -- 2.20.1