cmd2twitter
コマンドラインで打ったものをTwitterにpostする。 cURLが必要。 chatty を改造しています。
diff
diff -uw chatty-0.1/chatty.c cmd2twitter.c :
-
.c
old new 41 41 * - modify `script' to create `chatty'. 42 42 */ 43 43 44 /* 2007-11-23 Motohiro Takayama <mootoh@gmail.com> 45 * - modify `chatty' to create `cmd2twitter'. 46 */ 47 44 48 /* 45 49 * script 46 50 */ … … 63 67 #ifdef __linux__ 64 68 #include <unistd.h> 65 69 #include <string.h> 70 #else 71 #ifdef __APPLE__ 72 #include <unistd.h> 73 #include <string.h> 74 #endif 66 75 #endif 67 76 68 77 #include <sys/time.h> 69 #include "dict.h"70 78 71 79 #define HAVE_inet_aton 72 80 #define HAVE_scsi_h … … 106 114 int subchild; 107 115 char *fname; 108 116 117 char user[BUFSIZ]; 118 char pass[BUFSIZ]; 119 char post[BUFSIZ]; 120 109 121 struct termios tt; 110 122 struct winsize win; 111 123 int lb; … … 124 136 char *tail; 125 137 } RevBuf; 126 138 127 Dict* read_dict (const char *file_name);128 139 RevBuf* revbuf_new (void); 129 140 void revbuf_add (RevBuf *revbuf, int ch); 130 141 void revbuf_clear (RevBuf *revbuf); 131 142 char* revbuf_top (RevBuf *revbuf); 132 143 int revbuf_full_p (RevBuf *revbuf); 133 void watch_input ( Dict *dict,RevBuf *revbuf,144 void watch_input (RevBuf *revbuf, 134 145 const char *str, int len); 135 146 136 147 int … … 143 154 void finish(); 144 155 char *getenv(); 145 156 char *command = NULL; 157 char *usage = "usage: cmd2twitter <username> <password>\n"; 146 158 147 159 while ((ch = getopt(argc, argv, "h?")) != EOF) 148 160 switch((char)ch) { 149 161 case 'h': 150 162 case '?': 151 163 default: 152 fprintf(stderr, _("usage: chatty <dict>\n"));164 fprintf(stderr, _(usage)); 153 165 exit(1); 154 166 } 155 167 argc -= optind; 156 168 argv += optind; 157 169 158 if (argc != 1) {159 fprintf(stderr, _("usage: chatty <dict>\n"));170 if (argc != 2) { 171 fprintf(stderr, _(usage)); 160 172 exit(1); 161 173 } 162 174 … … 184 196 else 185 197 doshell(command); 186 198 } 199 200 strncpy(user, argv[0], BUFSIZ); 201 strncpy(pass, argv[1], BUFSIZ); 202 187 203 doinput(argv[0]); 188 204 189 205 return 0; … … 203 219 return str; 204 220 } 205 221 206 Dict *207 read_dict (const char *file_name)208 {209 char buf[BUFSIZ];210 Dict *dict = dict_new();211 FILE *fp = fopen(file_name, "r");212 213 if (fp == NULL) {214 fprintf(stderr, "chatty: %s: %s\n", file_name, strerror(errno));215 exit(1);216 }217 218 while (fgets(buf, BUFSIZ, fp)) {219 char *tabpos;220 buf[strlen(buf)-1] = '\0';221 tabpos = strchr(buf, '\t');222 if (tabpos) {223 char *key, value[BUFSIZ];224 *tabpos = '\0';225 key = buf;226 if (strlen(key) > 0) {227 char *rkey = reverse(key);228 sprintf(value, "%s: %s", key, tabpos + 1);229 dict_add(dict, rkey, value);230 free(rkey);231 }232 }233 }234 fclose(fp);235 return dict;236 }237 238 222 RevBuf * 239 223 revbuf_new (void) 240 224 { … … 272 256 revbuf->top = revbuf->tail; 273 257 } 274 258 259 void reverse_to_dst(char *dst, const char *src) { 260 int i; 261 int len = strlen(src)-1; 262 for (i=0; i<len; i++) { 263 dst[i] = src[len-i]; 264 } 265 } 266 275 267 void 276 watch_input ( Dict *dict,RevBuf *revbuf, const char *str, int len)268 watch_input (RevBuf *revbuf, const char *str, int len) 277 269 { 278 270 int i; 279 271 280 272 for (i = 0; i < len; i++) { 281 273 int ch = str[i]; 282 char *message;283 274 if (revbuf_full_p(revbuf)) 284 275 revbuf_clear(revbuf); 285 276 revbuf_add(revbuf, ch); 286 message = dict_search_longest(dict, revbuf_top(revbuf)); 287 if (message) 288 printf("\033]0;%s\a", message); 289 if (ch == '\n' || ch == '\r') 277 278 if (ch == '\n' || ch == '\r') { 279 char status[BUFSIZ]; 280 memset(status, 0, BUFSIZ); 281 memset(post, 0, BUFSIZ); 282 reverse_to_dst(status, revbuf_top(revbuf)); 283 snprintf(post, BUFSIZ, "curl -s " 284 "--basic --user \"%s:%s\" --data-ascii " 285 "\"status=%s\" " 286 "\"http://twitter.com/statuses/update.json\"", 287 user, pass, status); 288 system(post); 289 290 290 revbuf_clear(revbuf); 291 291 } 292 292 } 293 } 293 294 294 295 void 295 296 doinput(const char *file_name) … … 297 298 register int cc; 298 299 char ibuf[BUFSIZ]; 299 300 300 Dict* dict = read_dict(file_name);301 301 RevBuf* revbuf = revbuf_new(); 302 302 303 303 setbuf(stdout, NULL); 304 304 #ifdef HAVE_openpty 305 305 (void) close(slave); 306 306 #endif 307 307 308 while ((cc = read(0, ibuf, BUFSIZ)) > 0) { 308 watch_input(dict,revbuf, ibuf, cc);309 watch_input(revbuf, ibuf, cc); 309 310 (void) write(master, ibuf, cc); 310 311 } 311 312 done();
![(please configure the [header_logo] section in trac.ini)](/share/chrome/site/your_project_logo.png)