diff --git a/epicyon.py b/epicyon.py index d995b21c..950e2000 100644 --- a/epicyon.py +++ b/epicyon.py @@ -169,6 +169,8 @@ parser.add_argument("--allowdeletion", type=str2bool, nargs='?', \ help="Do not allow deletions") parser.add_argument('--repeat','--announce', dest='announce', type=str,default=None, \ help='Announce/repeat a url') +parser.add_argument('--favorite','--like', dest='like', type=str,default=None, \ + help='Like a url') parser.add_argument('--sendto', nargs='+',dest='sendto', \ help='List of post recipients') parser.add_argument('--attach', dest='attach', type=str,default=None, \ @@ -385,6 +387,30 @@ if args.announce: time.sleep(1) sys.exit() +if args.like: + if not nickname: + print('Specify a nickname with the --nickname option') + sys.exit() + + if not args.password: + print('Specify a password with the --password option') + sys.exit() + + session = createSession(domain,port,useTor) + personCache={} + cachedWebfingers={} + print('Sending like of '+args.like) + + sendLikeViaServer(session,nickname,args.password, + domain,port, \ + httpPrefix,args.like, \ + cachedWebfingers,personCache, \ + True) + for i in range(10): + # TODO detect send success/fail + time.sleep(1) + sys.exit() + if args.delete: if not nickname: print('Specify a nickname with the --nickname option') diff --git a/like.py b/like.py index d8d6a2e9..c333453c 100644 --- a/like.py +++ b/like.py @@ -268,3 +268,70 @@ def undoLikePost(session,baseDir: str,federationList: [], \ return undoLike(session,baseDir,federationList,nickname,domain,port, \ ccList,httpPrefix,objectUrl,clientToServer, \ sendThreads,postLog,personCache,cachedWebfingers,debug) + +def sendLikeViaServer(session,fromNickname: str,password: str, + fromDomain: str,fromPort: int, \ + httpPrefix: str,likeUrl: str, \ + cachedWebfingers: {},personCache: {}, \ + debug: bool) -> {}: + """Creates a like via c2s + """ + if not session: + print('WARN: No session for sendLikeViaServer') + return 6 + + fromDomainFull=fromDomain + if fromPort!=80 and fromPort!=443: + fromDomainFull=fromDomain+':'+str(fromPort) + + toUrl = 'https://www.w3.org/ns/activitystreams#Public' + ccUrl = httpPrefix + '://'+fromDomainFull+'/users/'+fromNickname+'/followers' + + newLikeJson = { + 'type': 'Like', + 'actor': httpPrefix+'://'+fullDomain+'/users/'+nickname, + 'object': likeUrl, + 'to': [toUrl], + 'cc': [ccUrl] + } + + handle=httpPrefix+'://'+fromDomainFull+'/@'+fromNickname + + # lookup the inbox for the To handle + wfRequest = webfingerHandle(session,handle,httpPrefix,cachedWebfingers) + if not wfRequest: + if debug: + print('DEBUG: announce webfinger failed for '+handle) + return 1 + + postToBox='outbox' + + # get the actor inbox for the To handle + inboxUrl,pubKeyId,pubKey,fromPersonId,sharedInbox,capabilityAcquisition = \ + getPersonBox(session,wfRequest,personCache,postToBox) + + if not inboxUrl: + if debug: + print('DEBUG: No '+postToBox+' was found for '+handle) + return 3 + if not fromPersonId: + if debug: + print('DEBUG: No actor was found for '+handle) + return 4 + + authHeader=createBasicAuthHeader(fromNickname,password) + + headers = {'host': fromDomain, \ + 'Content-type': 'application/json', \ + 'Authorization': authHeader} + postResult = \ + postJson(session,newLikeJson,[],inboxUrl,headers,"inbox:write") + #if not postResult: + # if debug: + # print('DEBUG: POST announce failed for c2s to '+inboxUrl) + # return 5 + + if debug: + print('DEBUG: c2s POST like success') + + return newLikeJson