aboutsummaryrefslogtreecommitdiff
path: root/gsocial-cli.sh
blob: 372a348b2df0a8117fb1bbcc02cd54a78e323346 (plain)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
#!/bin/sh
# -*- ENCODING: UTF-8 -*-
# gsocial-cli - GnuSocial in your Command Line Interface
# An actually real simple, minimalistic, full featured
# command-line client for gnu-social (ostatus)
# Author: ziggys
# License: The Drunken BEER License v 1.1 (https://git.p4g.club/git/beer/about)

# scriptinfo
SCRIPTNAME="${0##*/}"
SCRIPTNAME="${SCRIPTNAME%%.sh}"
VERSION="0.5.1"               # Tags count. Remote users timelines
AUTHOR="ziggys"
LICENSE="The Drunken BEER License v 1.1 (https://git.p4g.club/git/beer/about)"

# usage
usage () {
test $# = 0 || printf "%s %s\\n" "${SCRIPTNAME}" "$@"
cat << EOF | fold -s
${SCRIPTNAME} version ${VERSION}
  by ${AUTHOR}
  License: ${LICENSE}

${SCRIPTNAME} is an actually simple, minimalistic and full-featured
  command-line client for gnu-social that iteracts with 'ostatus' api
  usign curl and core '*nix' utilitiies.
 
Usage:   ${SCRIPTNAME} -c <account>
   or:   ${SCRIPTNAME} -a <account> [option] <args>
         ${SCRIPTNAME} -a <account> [-t|-T|-m] <number>
   or:   ${SCRIPTNAME} -a <account> [-r|-f|-d|-q|-R] <status_id>
   or:   ${SCRIPTNAME} -a <account> [-p|-s]
   or:   ${SCRIPTNAME} -h

Options
   -c   <account>         Create a configuration file for 'account'
   -t   <N>               Retrieve last 'N' statuses from 'home timeline'
   -T   <N>               Retrieve last 'N' statuses from 'public timeline'
   -m   <N>               Retrieve last 'N' 'mentions' and 'interactions'
   -s   SENT              Retrieve your own 20 most recent statuses
   -e   <TAG>             Retrieve a tag's timeline
   -U   <USERRNAME> <N>   Retrieve last 'N' statuses from 'USERNAME''timeline'
   -r   <ID>              Repeat status with 'id' code number
   -d   <ID>              Delete status with 'id' code number
   -f   <ID>              Favorite status with 'id' code number
   -q   <ID>              Quote status with 'id' code number
   -R   <ID>              Reply status with 'id' code number
   -p   POST              Post a status message. Option don't uses arguments
   -h   HELP              Print this help and exit

Set general configuration parametters
Usage:  ${SCRIPTNAME} set option=value

General Settings
  d_account                 Sets default account 
  g_theme                   Sets global theme for color output
  d_editor                  Sets default editor
  history_keep              Sets number of maximun lines to keep in history
  informative   yes | no    Sets wether output is or not informative
  ...                       More settings to come

  To print actual settings run:
  ${SCRIPTNAME} show settings

For more details about this script go to
Official Repository: https://git.p4g.club/git/gsocial-cli
Mirror Repository: https://gitgud.io/ziggys/gsocial-cli
EOF
exit 0
}

# configure general settings
create_gsf () {
  printf "#\\n# %s -- %s_config\\n#\\n# Warning: it is not recommended \
    \\n# to edit this file by hand.\\n#\\n# Use '%s set option=value' \
    \\n# to change this settings.\\n#\\n" \
    "${SCRIPTNAME}" "${SCRIPTNAME}" "${SCRIPTNAME}" > "${GSF}"
  printf "d_account=\\ng_theme=default\\ng_language=en\\n \
    d_editor=vim\\ndata_storage=plain\\nhistory_keep=1000\\n \
    informative=yes\\n" >> "${GSF}"
  sed -i 's/^ *//g' "${GSF}"
  chmod 600 "${GSF}"
}

set_gsf () {
  OPTION="$(echo "${SETTING}" | awk 'BEGIN{FS="="} {printf $1}')"
  VALUE="$(echo "${SETTING}" | awk 'BEGIN{FS="="} {printf $2}')"
  ISOPT="$(sed '/^'"${OPTION}"'=/ !d' "${GSF}")"

  if [ -z "${ISOPT}" ]; then
    printf "%s\\n" "${SETTING}" >> "${GSF}"
  else
    sed -i 's/^'"${OPTION}"'=.*$/'"${OPTION}"'='"${VALUE}"'/' "${GSF}"
  fi

  # print some header
  printf "\\e%s%s\\e[0m v%s\\n" "${good_c}" "${SCRIPTNAME}" "${VERSION}"
  printf "\\e%ssetting:\\e%s %s=%s\\e[0m\\n\\n" \
    "${info_c}" "${good_c}" "${OPTION}" "${VALUE}"
}

# create theme file
create_thf () {
  printf "\\e%s...updating data\\n" "${warn_c}"
  printf "\\e%sCreating \\e%s%s.theme\\e[0m file for \\e%s%s\\n" \
    "${info_c}" "${good_c}" "${g_theme}" "${good_c}" "${SCRIPTNAME}"
  printf "\\e%sYou can customize this theme editing\\n\\e%s%s\\n\\n\\e[0m" \
    "${info_c}" "${good_c}" "${THF}"
    
  printf "#\\n# %s -- %s.theme\\n#\\n \
    # Colors uses 'ANSI escape codes'\\n#\\n \
    # Black 0;30m - DarkGrey 1;30m\\n \
    # Red 0;31m - LightRed 1;31m\\n \
    # Green 0;32m - LightGreen 1;32m\\n \
    # Brown/Orange 0;33m - Yellow 1;33m\\n \
    # Blue 0;34m - LightBlue 1;34m\\n \
    # Purple 0;35m - LightPurple 1;35m\\n \
    # Cyan 0;36m - LightCyan 1;36m\\n \
    # LightGray 0;37m - White 1;37m\\n#\\n" \
    "${SCRIPTNAME}" "${g_theme}" > "${THF}"
  printf "sname_c=1;32m\\nuname_c=0;37m\\nsid_c=0;33m\\n \
    text_c=0;37m\\nmeta_c=0;36m\\nmyself_c=0;35m\\n" >> "${THF}"
  sed -i 's/^ *//g' "${THF}"
  chmod 600 "${THF}"
}

# avoid history to grow ad infinitum
history_keep () {
  test ! -f "${HISTORY}" && echo "" > "${HISTORY}"
  CURRLINES="$(awk 'END{print NR}' "${HISTORY}")"

  if [ "${CURRLINES}" -gt "${history_keep}" ]; then
    printf "\\e%sCleaning history for \\e%s%s\\e[0m\\n\\n" \
      "${info_c}" "${warn_c}" "${ACCNAME}"
    sed -i ''"${history_keep}"',$ d' "${HISTORY}"
    HISTORYT="$(mktemp -p "${TMPDIR}")"
    uniq < "${HISTORY}" > "${HISTORYT}"
    mv "${HISTORYT}" "${HISTORY}"
  else
    printf "\\e%sKeeping history clean\\e[0m\\n\\n" \
      "${info_c}"
  fi
}

# account configuration
acc_conf () {
  trap exit INT
  test -f "${ACCRC}" \
    && printf "\\e%swarning: \\e%s%s.config\\e[0m already exists\\n" \
      "${warn_c}" "${good_c}" "${ACCNAME}" \
    && printf "Do you want to overwrite? (\\e%ssyes/\\e[0mno): " "${warn_c}" \
    && read -r T_ACCRC

  case "${T_ACCRC}" in
    y | yes | Yes | YES ) 
      printf "\\e%soverwriting existing \\e%s%s.config\\e[0m\\n\\n" \
        "${info_c}" "${good_c}" "${ACCNAME}" 
       ;; 
    n | no | No | NO ) 
      printf "\\e%susing existing \\e%s%s.config\\n" \
        "${info_c}" "${good_c}" "${ACCNAME}" ;
      exit 0 ;;
  esac

  printf "Configuring %s with your gnu-social account info\\n" "${SCRIPTNAME}"
  printf "\\e%sPlease enter your gnu-social account information\\n" "${info_c}"
  printf "\\e%s'username'\\e[0m (only nickname - the part before '@'): " \
    "${good_c}" && read -r C_USERNAME
  printf "\\e%s'password'\\e[0m (your gnu-social password): " \
    "${good_c}" && read -r C_PASSWORD
  printf "\\e%s'gnu-social node'\\e[0m (e.g: https://myno.de): " \
    "${good_c}" && read -r C_NODE

  C_USERNAME="$(echo "${C_USERNAME}" | base64 -)"
  C_PASSWORD="$(echo "${C_PASSWORD}" | base64 -)"
  C_NODE="$(echo "${C_NODE}" | base64 -)"

  printf "\\nPopulating \\e%s%s.config\\e[0m with account information\\n" \
    "${good_c}" "${ACCNAME}"
  printf "#\\n# %s -- %s.config\\n#\\n# Warning: it is not recommended \
    \\n# to edit this file by hand.\\n#\\n# Use '%s -c %s'\\n#\\n" \
    "${SCRIPTNAME}" "${ACCNAME}" "${SCRIPTNAME}" "${ACCNAME}" > "${ACCRC}"
  printf "USER=%s;\\nPASSWORD=%s;\\nNODE=%s;\\n" \
    "${C_USERNAME}" "${C_PASSWORD}" "${C_NODE}" >> "${ACCRC}"
  sed -i 's/^ *//g' "${ACCRC}"
  chmod 600 "${ACCRC}"

  printf "Done!"
  exit 0
}

# obtain user info
user_info () {
  UINFOF="$(mktemp -p "${TMPDIR}")"
  iget_api="${INODE}/api/users/show.xml?screen_name=${IUSER}"

  curl -s "${iget_api}" > "${UINFOF}"

  USERID="$(sed '/^ <id>/ !d; s/ *<[^>]*> *//g' "${UINFOF}")"
  DESCRIPTION="$(sed '/^ <description>/ !d; s/ *<[^>]*> *//g' "${UINFOF}")"
  STATUSESQ="$(sed '/^ <statuses_count>/ !d; s/ *<[^>]*> *//g' "${UINFOF}")"
  FOLLOWSQ="$(sed '/^ <friends_count>/ !d; s/ *<[^>]*> *//g' "${UINFOF}")"
  FOLLOWERSQ="$(sed '/^ <followers_count>/ !d; s/ *<[^>]*> *//g' "${UINFOF}")"
  GROUPSQ="$(sed '/^ <groups_count>/ !d; s/ *<[^>]*> *//g' "${UINFOF}")"

  printf "\\e%s%s@%s [%s]\\n" "${warn_c}" "${IUSER}" "${NONODE}" "${USERID}"
  printf "\\e%s%s\\e[0m\\n" "${info_c}" "${DESCRIPTION}"
  printf "Statuses: \\e%s%s\\e[0m\\t" "${warn_c}" "${STATUSESQ}"
  printf "Followers: \\e%s%s\\e[0m\\t" "${warn_c}" "${STATUSESQ}"
  printf "Follows: \\e%s%s\\e[0m\\t" "${warn_c}" "${FOLLOWSQ}"
  printf "Groups: \\e%s%s\\e[0m\\n" "${warn_c}" "${GROUPSQ}"
}

# verify credentials
verify_credentials () {
  ACCVERIFY="$(curl -s -u "${USER}:${PASSWORD}" \
    "${NODE}/api/account/verify_credentials.xml" | sed '/^ <error>/ !d')"

  test ! -z "${ACCVERIFY}" \
    && printf "\\e%sfatal: \\e%s%s authentication failed %s\\e[0m\\n\\n" \
      "${diee_c}" "${info_c}" "${SCRIPTNAME}" "${NONODE}" \
    && exit 0
}

# get timeline, public, mentions
tget () {
  test "${ACCNAME}" = "notprovided" \
    && printf "\\e%sfatal:\\e%s account name not provided\\n\\e[0m" \
      "${diee_c}" "${info_c}"

  printf "...retrieving last \\e%s%s\\e[0m statuses from \\e%s%s\\e[0m\\n\\n" \
    "${good_c}" "${RETR}" "${good_c}" "${TGET}"

  TIMELINEF="$(mktemp -p "${TMPDIR}")"
  OUTPUT0="$(mktemp -p "${TMPDIR}")"
  OUTPUT1="$(mktemp -p "${TMPDIR}")"
  OUTPUT2="$(mktemp -p "${TMPDIR}")"
  OUTPUT3="$(mktemp -p "${TMPDIR}")"
  OUTPUT4="$(mktemp -p "${TMPDIR}")"
  SIDSF="$(mktemp -p "${TMPDIR}")"
  SNAMESF="$(mktemp -p "${TMPDIR}")"
  UNAMESF="$(mktemp -p "${TMPDIR}")"
  TEXTSF="$(mktemp -p "${TMPDIR}")"
  SOURCESF="$(mktemp -p "${TMPDIR}")"
  CREATEDSF="$(mktemp -p "${TMPDIR}")"
  OIFS="$IFS"
  IFS="$(printf '\n')"
  
  case "${TGET}" in
    "timeline")
      data_api="-d count=${RETR}"
      get_api="${NODE}/api/statuses/home_timeline.xml"
      ;;
    "public")
      data_api="-d count=${RETR}"
      get_api="${NODE}/api/statuses/public_timeline.xml"
      ;;
    "mentions")
      data_api="-d count=${RETR}"
      get_api="${NODE}/api/statuses/mentions_timeline.xml"
      ;;
    "sent")
      get_api="${NODE}/api/statuses/user_timeline.xml"
      ;;
    "tag")
      get_uri="${GETATAG}.xml&count=${RETR:=20}"
      get_api="${NODE}/api/statusnet/tags/timeline/${get_uri}"
      TGET="${TGET} #${GETATAG}"
      warn_c="[0;36m"
      printf "\\e%sTag: \\e%s%s\\e[0m\\n" "${info_c}" "${warn_c}" "${GETATAG}"
      ;;
    "user")
      RNODE="https://${RUSER##*@}"
      GETUSER="${RUSER%%@*}"
      get_uri="include_rst=true&screen_name=${GETUSER}&count=${RETR:=20}"
      get_api="${RNODE}/api/statuses/user_timeline.xml?${get_uri}"
      TGET="${TGET} ${RUSER}"
      IUSER="${GETUSER}"
      INODE="${RNODE}"
      warn_c="[0;36m"
      printf "\\e%sUser Info:\\e[0m\\n" "${info_c}"
      test "${informative}" = "yes" && user_info
      printf "\\n"
      ;;
  esac

  verify_credentials
  curl -s -u "$USER:$PASSWORD" "${data_api}" "${get_api}" \
    > "${TIMELINEF}" 

  SID="$(sed '/^[ \t] <id>/ !d; s/^[ \t]*<id>//; s/<\/id>$//' \
    "${TIMELINEF}")"
  ST="screen_name>"
  SNAME="$(sed '/^[ \t] .<'${ST}'/ !d; s/^[ \t]*<'${ST}'//; s/<\/'${ST}'$//' \
    "${TIMELINEF}")"
  UNAME="$(sed '/^[ \t] .<name>/ !d; s/^[ \t]*<name>//; s/<\/name>$//' \
    "${TIMELINEF}")"
  TEXT="$(sed '/^[ \t] <text>/ !d; s/^[ \t]*<text>//; s/<\/text>$//' \
    "${TIMELINEF}")"
  SSOURCE="$(sed '/^[ \t].<source>/ !d; s/^[ \t]*<source>//; s/<\/source>$//' \
    "${TIMELINEF}")"
  CT="created_at>"
  CREATED="$(sed '/^[ \t].<'${CT}'/ !d; s/^[ \t]*<'${CT}'//; s/<\/'${CT}'//;' \
    "${TIMELINEF}")"
  
  test -z "${SID}" \
    && printf "\\e%swarning:\\e%s command returned an empty output\\e[0m\\n" \
      "${warn_c}" "${info_c}" \
    && printf "exiting now..." \
    && exit 0

  # many shells do not handle arrays, let's make lists
  echo "${SID}" > "${SIDSF}"
  echo "${SNAME}" > "${SNAMESF}"
  echo "${UNAME}" > "${UNAMESF}"
  echo "${TEXT}" > "${TEXTSF}"
  echo "${SSOURCE}" > "${SOURCESF}"
  echo "${CREATED}" > "${CREATEDSF}"
  sed -i 's/&quot;/"/g' "${TEXTSF}"
 
  # let's use awk to concatenate lists
  awk '
    BEGIN{IFS="\n"; OFS=":;:"} 
    FNR==NR { a[(FNR"")] = $0; next} 
    {print a[(FNR"")],$0}' \
      "${SIDSF}" "${SNAMESF}" > "${OUTPUT0}"
  awk '
    BEGIN{IFS="\n"; OFS=":;:"} 
    FNR==NR { a[(FNR"")] = $0; next} 
    {print a[(FNR"")],$0}' \
      "${OUTPUT0}" "${UNAMESF}" > "${OUTPUT1}"
  awk '
    BEGIN{IFS="\n"; OFS=":;:"} 
    FNR==NR { a[(FNR"")] = $0; next} 
    {print a[(FNR"")],$0}' \
      "${OUTPUT1}" "${TEXTSF}" > "${OUTPUT2}"
  awk '
    BEGIN{IFS="\n"; OFS=":;:"} 
    FNR==NR { a[(FNR"")] = $0; next} 
    {print a[(FNR"")],$0}' \
      "${OUTPUT2}" "${SOURCESF}" > "${OUTPUT3}"
  awk '
    BEGIN{IFS="\n"; OFS=":;:"} 
    FNR==NR { a[(FNR"")] = $0; next} 
    {print a[(FNR"")],$0}' \
      "${OUTPUT3}" "${CREATEDSF}" > "${OUTPUT4}"

  # print concatenated file
  awk -v user="${USER}" '
    BEGIN{FS=":;:"; OFS="\n"} 
    {
      if ($2==user)
        printf \
        "\033['"$myself_c"'" $3 \
        "\033['"$uname_c"'" " @" $2 \
        "\033['"$myself_c"'" " [" NR " - id " $1 "]" OFS \
        "\033[1;30m" $4 OFS \
        "\033[1;30m" " - " \
        "\033['"$text_c"'" $6 \
        "\033[1;30m" " from " \
        "\033['"$text_c"'" $5 OFS \
        "\033[0m" 
      else
        printf \
        "\033['"$sname_c"'" $3 \
        "\033['"$uname_c"'" " @" $2 \
        "\033['"$sid_c"'" " [" NR " - id " $1 "]" OFS \
        "\033['"$text_c"'" $4 OFS \
        "\033['"$text_c"'" " - " \
        "\033['"$meta_c"'" $6 \
        "\033['"$text_c"'" " from " \
        "\033['"$meta_c"'" $5 OFS \
        "\033[0m" }' \
    "${OUTPUT4}" | more
  
  history_keep

  cat "$OUTPUT4" >> "${HISTORY}" && chmod 600 "${HISTORY}"
  IFS="${OIFS}"
}

# repeat, favorite, destroy
rfd () {
  ACTION="$(echo "${action_api}" | awk 'BEGIN{FS="/"}{printf $2}')"

  if [ ! -f "${HISTORY}" ]; then
    printf "\\e%swarning: no history file was found.\\n" "${warn_c}"
    printf "\\e%s%s is still available, but expect missbehavior.\\e[0m\\n" \
      "${info_c}" "${METHOD}"
    printf "Do you wish to continue? (y/n): "
    read -r NOHISTORY

    test "${NOHISTORY}" = "n" \
      && printf "Run \\e%s'%s -a account -T <N>'\\e[0m\\n" \
        "${good_c}" "${SCRIPTNAME}" \
      && printf "to create history, and try too %s again" "${METHOD}" \
      && exit 0
  else
    TUSER="$(sed '/^'"${TID}"'/ !d' "${HISTORY}" | sort | uniq \
      | awk 'BEGIN{FS=":;:"} {printf $2}')"
    TNAME="$(sed '/^'"${TID}"'/ !d' "${HISTORY}" | sort | uniq \
      | awk 'BEGIN{FS=":;:"} {printf $3}')"
    TSTS="$(sed '/^'"${TID}"'/ !d' "${HISTORY}" | sort | uniq \
      | awk 'BEGIN{FS=":;:"} {printf $4}')"
  fi

  case "${ACTION}" in
    "create")
      printf "...favoriting status by \\e%s%s\\e[0m in \\e%s%s:\\e[0m\\n\\n" \
        "${good_c}" "${TUSER}" "${good_c}" "${NONODE}"
      printf "Status details:\\n"
      printf "\\e%s[%s] %s\\n\\n" "${info_c}" "${TID}" "${TSTS}"
      ;;
    "retweet")
      printf "...repeating a status by \\e%s%s\\e[0m in \\e%s%s:\\e[0m\\n\\n" \
        "${good_c}" "${TUSER}" "${good_c}" "${NONODE}"
      printf "Status details:\\n"
      printf "\\e%s[%s] %s\\n\\n" "${info_c}" "${TID}" "${TSTS}"
      ;;
    "destroy")
      printf "...deleting a status by \\e%syou\\e[0m in \\e%s%s:\\e[0m\\n\\n" \
        "${good_c}" "${good_c}" "${NONODE}"
      printf "Status details:\\n"
      printf "\\e%s[%s] %s\\n\\n" "${info_c}" "${TID}" "${TSTS}"
      ;;
  esac
  
  verify_credentials
  curl -s -u "$USER:$PASSWORD" -d id="${TID}" -d source="${SCRIPTNAME}" \
    "${NODE}/api/${action_api}.xml" > "${CURLOUT}" 2 > "${DN}"

  CURLOUT_E="$(sed '/error/ !d' "${CURLOUT}")"
  test ! -z "${CURLOUT_E}" \
    && printf "\\e%serr:\\e%s error ocurred when trying to %s in %s\\e[0m\\n" \
      "${diee_c}" "${info_c}" "${ACTION}" "${NONODE}" \
    || printf "\\e%sDone!\\n" "${good_c}"
}

# post
post () {
  POSTF="$(mktemp -p "${TMPDIR}")" 
 
  if [ ! -f "${HISTORY}" ]; then
    printf "\\e%swarning: no history file was found.\\n" "${warn_c}"
    printf "\\e%s%s is still available, but expect missbehavior.\\e[0m\\n" \
      "${info_c}" "${METHOD}"
    printf "Do you wish to continue? (y/n): "
    read -r NOHISTORY

    test "${NOHISTORY}" = "n" \
      && printf "Run \\e%s'%s -a account -T <N>'\\e[0m\\n" \
        "${good_c}" "${SCRIPTNAME}" \
      && printf "to create history, and try too %s again" "${METHOD}" \
      && exit 0

  else
    TUSER="$(sed '/^'"${TID}"'/ !d' "${HISTORY}" | sort | uniq \
      | awk 'BEGIN{FS=":;:"} {printf $2}')"
    TNAME="$(sed '/^'"${TID}"'/ !d' "${HISTORY}" | sort | uniq \
      | awk 'BEGIN{FS=":;:"} {printf $3}')"
    TSTS="$(sed '/^'"${TID}"'/ !d' "${HISTORY}" | sort | uniq \
      | awk 'BEGIN{FS=":;:"} {printf $4}')"
   fi

  case "${METHOD}" in
    "post")
      printf "\\n/* Write a status message, save the file and close %s\\n \
        /* Your message will be automatically posted\\n \
        /* and showed in standard output.\\n \
        /* Lines starting with '/*' will be ignored\\n \
        /* Leave firs line 'blank' to cancel post\\n \
        /* or simply write 'kill!' at any line beginning" \
        "${EDITOR:=${d_editor}}" > "${POSTF}"
      post_api="${NODE}/api/statuses/update.xml"
      ;;        
    "reply") 
      printf "\\n/to @%s\\n \
        /* Write a response message for %s, save the file and close %s\\n \
        /* Your respones will be automatically posted\\n \
        /* and showed in standard output.\\n \
        /* Lines starting with '/*' will be ignored\\n \
        /* Leave firs line 'blank' to cancel post\\n \
        /* or simply write 'kill!' at any line beginning" \
        "${TUSER}" "${TUSER}" "${EDITOR:=${d_editor}}" > "${POSTF}"
      data_api="-d in_reply_to_status_id=${TID}"
      post_api="${NODE}/api/statuses/update.xml"
      ;;
   "quote")
      printf "\\n[QT] @%s (%s): %s\\n \
        /* Write a status message, save the file and close %s\\n \
        /* Your quoted status will be automatically posted\\n \
        /* and showed in standard output.\\n \
        /* Lines starting with '/*' will be ignored\\n \
        /* Leave first line 'blank' to cancel post\\n \
        /* or simply write 'kill!' at any line beginning" \
         "${TUSER}" "${TNAME}" "${TSTS}" "${EDITOR:=${d_editor}}" > "${POSTF}"
      post_api="${NODE}/api/statuses/update.xml"
  esac

  sed -i 's/^ *//g' "${POSTF}"
  ${EDITOR} "${POSTF}"
  STATUS="$(sed '/^\/\*/ d' "${POSTF}")"
  BLANK="$(sed '1 !d' "${POSTF}")"
  CANCEL="$(sed '/^kill!$/ !d' "${POSTF}")"

  test ! -z "${CANCEL}" \
    && printf "\\e%scancel post action: \\e%s%s\\e[0m\\n\\n" \
      "${info_c}" "${warn_c}" "${METHOD}" \
    && exit 0
  test -z "${BLANK}" \
    && printf "\\e%scancel post action: \\e%s%s\\e[0m\\n\\n" \
      "${info_c}" "${warn_c}" "${METHOD}" \
    && exit 0
 
  printf "...sending a \\e%s%s\\e[0m status to \\e%s%s:\\e[0m\\n\\n" \
    "${good_c}" "${METHOD}" "${good_c}" "${NONODE}"
  printf "Message content:\\n"
  printf "\\e%s%s\\n\\n\\e[0m" "${info_c}" "${STATUS}"

  verify_credentials
  curl -s -u "$USER:$PASSWORD" --data-binary status="${STATUS}" \
    -d source="${SCRIPTNAME}" "${data_api}" "${post_api}" > "${DN}"

  test "$?" -ne 0 \
    && printf "\\e%serr:\\e%s error ocurred when trying to %s in %s\\e[0m\\n" \
      "${diee_c}" "${info_c}" "${METHOD}" "${NONODE}" \
    || printf "\\e%sDone!\\n" "${good_c}"
}

# stdout colors
good_c="[0;32m"
warn_c="[0;33m"
diee_c="[0;31m"
info_c="[1;30m"

# print some header
printf "\\e%s%s\\e[0m v%s\\n" "${good_c}" "${SCRIPTNAME}" "${VERSION}"

TMPDIR="$(mktemp -d)"
GSFT="$(mktemp -p "${TMPDIR}")"
ACCT="$(mktemp -p "${TMPDIR}")"
THFT="$(mktemp -p "${TMPDIR}")"
CURLOUT="$(mktemp -p "${TMPDIR}")"
DN="/dev/null"
XDGCONFIG="${XDG_HOME_CONFIG:=${HOME}/.config}"
CONFIGD="${XDGCONFIG}/${SCRIPTNAME}"
GSF="${CONFIGD}/${SCRIPTNAME}_config"

test ! -d "${CONFIGD}" && mkdir -p "${CONFIGD}"
test ! -f "${GSF}" && create_gsf
test "$#" -lt 1 && usage "$@"
if [ "$1" = "set" ]; then
  SETTING="$2"
  set_gsf
elif [ "$1" = "show" ]; then
  SHOW="$2"
  sed '/^#/ d; s/[ \t]*$//g' "${GSF}"
fi

# obtain general settings
sed '/^#/ d; s/[ \t]*$//g' "${GSF}" > "${GSFT}"
d_account="$(sed '/d_account=/ !d; s/d_account=//' "${GSFT}")"
g_theme="$(sed '/g_theme=/ !d; s/g_theme=//' "${GSFT}")"
d_editor="$(sed '/d_editor=/ !d; s/d_editor=//' "${GSFT}")"
history_keep="$(sed '/history_keep=/ !d; s/history_keep=//' "${GSFT}")"
informative="$(sed '/informative=/ !d; s/informative=//' "${GSFT}")"

# theme settings
THF="${CONFIGD}/${g_theme}.theme"
test ! -f "${THF}" && create_thf
sed '/^#/ d; s/[ \t]*$//g' "${THF}" > "${THFT}"
sname_c="$(sed '/sname_c=/ !d; s/sname_c=//' "${THFT}")"
uname_c="$(sed '/uname_c=/ !d; s/uname_c=//' "${THFT}")"
sid_c="$(sed '/sid_c=/ !d; s/sid_c=//' "${THFT}")"
text_c="$(sed '/text_c=/ !d; s/text_c=//' "${THFT}")"
meta_c="$(sed '/meta_c=/ !d; s/meta_c=//' "${THFT}")"
myself_c="$(sed '/myself_c=/ !d; s/myself_c=//' "${THFT}")"

# provide account or use default
if [ "$1" = "-a" ];then
  ACCNAME="$2" ;
  ACCRC="${CONFIGD}/${ACCNAME}.config" ;
  test ! -f "${ACCRC}" \
    && acc_conf
  shift 2 
elif [ ! -z "${d_account}" ]; then
  ACCNAME="${d_account}" ;
  ACCRC="${CONFIGD}/${ACCNAME}.config" ;
  test ! -f "${ACCRC}" \
    && acc_conf
else
  ACCNAME="notprovided"
fi  

# obtain account variables
sed '/^#/ d; s/;[ \t].*//g; s/;$//g; s/[ \t]*$//g' "${ACCRC}" > "${ACCT}" 2>$DN
USER="$(sed '/USER=/ !d; s/USER=//' "${ACCT}")"
PASSWORD="$(sed '/PASSWORD=/ !d; s/PASSWORD=//' "${ACCT}")"
NODE="$(sed '/NODE=/ !d; s/NODE=//' "${ACCT}")"
USER="$(echo "${USER}" | base64 -d -)"
PASSWORD="$(echo "${PASSWORD}" | base64 -d -)"
NODE="$(echo "${NODE}" | base64 -d -)"
NONODE="${NODE##*//}"
HISTORY="${CONFIGD}/${ACCNAME}.history" ;
IUSER="${USER}"
INODE="${NODE}"
test "${informative}" = "yes" && user_info
   
# read options from commandline
test "$#" -lt 1 && usage "$@"
while getopts ":c:t:T:m:e:U:r:f:d:R:q:psh" opt; do
  case "${opt}" in
    c) ACCNAME="${OPTARG}" ; ACCRC="${CONFIGD}/${ACCNAME}.config" ; acc_conf ;;
    t) RETR="${OPTARG}" ; TGET="timeline" ; tget ;;
    T) RETR="${OPTARG}" ; TGET="public" ; tget ;;
    m) RETR="${OPTARG}" ; TGET="mentions" ; tget ;;
    e) GETATAG="${OPTARG}" ; RETR="$3" ; TGET="tag" ; tget ;;
    U) RUSER="${OPTARG}" ; RETR="$3" ; TGET="user" ; tget ;;
    r) TID="${OPTARG}" ; action_api="statuses/retweet/${TID}" ; rfd ;;
    f) TID="${OPTARG}" ; action_api="favorites/create" ; rfd ;;
    d) TID="${OPTARG}" ; action_api="statuses/destroy/${TID}" ; rfd ;;
    s) RETR="${OPTARG}" ; TGET="sent" ; tget ;;
    p) METHOD="post" ; post ;;
    R) TID="${OPTARG}" ; METHOD="reply" ; post ;;
    q) METHOD="quote" ; post ;;
    \?) printf "Invalid: -%s" "${OPTARG}" 1>&2 ; exit 3 ;;
    :) printf "Invalid: -%s requires an argument" "${OPTARG}" 1>&2 ; exit 3 ;;
    h) usage "$@" ;;
  esac
done
shift $((OPTIND -1))

# clean and exit
rm -rf "${TMPDIR}"
exit 0