purplepipe.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <stdio.h>
00021 #include <stdlib.h>
00022 #include <errno.h>
00023 #include <string.h>
00024 #include <unistd.h>
00025
00026 #include "../../mem/shm_mem.h"
00027
00028 #include "purplepipe.h"
00029 #include "purple.h"
00030
00031 extern int pipefds[2];
00032
00033 static char *shm_strdup(str *src) {
00034 char *res;
00035
00036 if (!src || !src->s)
00037 return NULL;
00038 if (!(res = (char *) shm_malloc(src->len + 1)))
00039 return NULL;
00040 strncpy(res, src->s, src->len);
00041 res[src->len] = 0;
00042 return res;
00043 }
00044
00045 static struct purple_cmd* purple_new_cmd(enum purple_cmd_type type) {
00046 struct purple_cmd *cmd;
00047
00048 LM_DBG("allocating cmd\n");
00049
00050 cmd = (struct purple_cmd *) shm_malloc(sizeof(struct purple_cmd));
00051
00052 if (cmd == NULL) {
00053 LM_ERR("error allocating memory for cmd\n");
00054 return NULL;
00055 }
00056
00057 memset(cmd, 0, sizeof(struct purple_cmd));
00058 cmd->type = type;
00059
00060 return cmd;
00061 }
00062
00063 void purple_free_cmd(struct purple_cmd *cmd) {
00064 LM_DBG("freeing cmd\n");
00065 switch (cmd->type) {
00066 case PURPLE_MESSAGE_CMD:
00067 if (cmd->message.from)
00068 shm_free(cmd->message.from);
00069 if (cmd->message.to)
00070 shm_free(cmd->message.to);
00071 if (cmd->message.body)
00072 shm_free(cmd->message.body);
00073 if (cmd->message.id)
00074 shm_free(cmd->message.id);
00075 break;
00076 case PURPLE_PUBLISH_CMD:
00077 if (cmd->publish.from)
00078 shm_free(cmd->publish.from);
00079 if (cmd->publish.id)
00080 shm_free(cmd->publish.id);
00081 if (cmd->publish.note)
00082 shm_free(cmd->publish.note);
00083 break;
00084 case PURPLE_SUBSCRIBE_CMD:
00085 if (cmd->subscribe.from)
00086 shm_free(cmd->subscribe.from);
00087 if (cmd->subscribe.to)
00088 shm_free(cmd->subscribe.to);
00089 break;
00090 }
00091 shm_free(cmd);
00092 }
00093
00094
00095 static int purple_send_cmd(struct purple_cmd **cmd) {
00096 LM_DBG("writing cmd to pipe\n");
00097 if (write(pipefds[1], cmd, sizeof(cmd)) != sizeof(cmd)) {
00098 LM_ERR("failed to write to command pipe: %s\n", strerror(errno));
00099 purple_free_cmd(*cmd);
00100 return -1;
00101 }
00102 LM_DBG("cmd has been wrote to pipe\n");
00103 return 0;
00104 }
00105
00106
00107 int purple_send_message_cmd(str *from, str *to, str *body, str *id) {
00108 LM_DBG("building MESSAGE cmd\n");
00109 struct purple_cmd *cmd = purple_new_cmd(PURPLE_MESSAGE_CMD);
00110 if (cmd == NULL)
00111 return -1;
00112
00113 cmd->message.from = shm_strdup(from);
00114 cmd->message.to = shm_strdup(to);
00115 cmd->message.body = shm_strdup(body);
00116 cmd->message.id = shm_strdup(id);
00117
00118 return purple_send_cmd(&cmd);
00119 }
00120
00121 int purple_send_publish_cmd(enum purple_publish_basic basic, PurpleStatusPrimitive primitive, str *from, str *id, str *note) {
00122 LM_DBG("building PUBLISH cmd... %.*s,%.*s,%.*s\n", from->len, from->s, id->len, id->s, note->len, note->s);
00123 struct purple_cmd *cmd = purple_new_cmd(PURPLE_PUBLISH_CMD);
00124 if (cmd == NULL)
00125 return -1;
00126
00127 cmd->publish.from = shm_strdup(from);
00128 cmd->publish.id = shm_strdup(id);
00129 cmd->publish.note = shm_strdup(note);
00130 cmd->publish.primitive = primitive;
00131 cmd->publish.basic = basic;
00132
00133 return purple_send_cmd(&cmd);
00134 }
00135
00136 int purple_send_subscribe_cmd(str *from, str *to, int expires) {
00137 LM_DBG("building SUBSCRIBE cmd\n");
00138 struct purple_cmd *cmd = purple_new_cmd(PURPLE_SUBSCRIBE_CMD);
00139 if (cmd == NULL)
00140 return -1;
00141
00142 cmd->subscribe.from = shm_strdup(from);
00143 cmd->subscribe.to = shm_strdup(to);
00144 cmd->subscribe.expires = expires;
00145
00146 return purple_send_cmd(&cmd);
00147 }
00148
00149